Skip to content

Commit

Permalink
Restore priority of env check over remote check
Browse files Browse the repository at this point in the history
In case of double re-execution case we should stop second one to prevent re-execution loop
  • Loading branch information
vapopov committed Jan 17, 2025
1 parent 5961a09 commit b8dc668
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
6 changes: 3 additions & 3 deletions integration/autoupdate/tools/updater_tsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestAliasLoginWithUpdater(t *testing.T) {
require.NoError(t, err)
configPath := filepath.Join(toolsDir, client.TSHConfigPath)
require.NoError(t, os.MkdirAll(filepath.Dir(configPath), 0700))
executable := filepath.Join(toolsDir, "tsh")
executable := filepath.Join(os.TempDir(), "tsh")
out, err := yaml.Marshal(client.TSHConfig{
Aliases: map[string]string{
"loginalice": fmt.Sprintf(
Expand All @@ -109,7 +109,7 @@ func TestAliasLoginWithUpdater(t *testing.T) {
require.NoError(t, os.WriteFile(configPath, out, 0600))

// Fetch compiled test binary and install to tools dir [v1.2.3].
err = tools.NewUpdater(toolsDir, testVersions[0], tools.WithBaseURL(baseURL)).Update(ctx, testVersions[0])
err = tools.NewUpdater(os.TempDir(), testVersions[0], tools.WithBaseURL(baseURL)).Update(ctx, testVersions[0])
require.NoError(t, err)

// Execute alias command which must be transformed to the login command.
Expand All @@ -122,7 +122,7 @@ func TestAliasLoginWithUpdater(t *testing.T) {
require.NoError(t, cmd.Run())

// Verify tctl status after login.
cmd = exec.CommandContext(ctx, filepath.Join(toolsDir, "tctl"), "status", "--insecure")
cmd = exec.CommandContext(ctx, filepath.Join(os.TempDir(), "tctl"), "status", "--insecure")
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
32 changes: 30 additions & 2 deletions lib/autoupdate/tools/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,31 @@ func (u *Updater) CheckLocal() (version string, reExec bool, err error) {
return toolsVersion, true, nil
}

// CheckRemote checks against the Proxy Service to determine if client tools need updating by requesting
// CheckRemote first checks the version set by the environment variable. If not set or disabled,
// it checks against the Proxy Service to determine if client tools need updating by requesting
// the `webapi/find` handler, which stores information about the required client tools version to
// operate with this cluster. It returns the semantic version that needs updating and whether
// re-execution is necessary, by re-execution flag we understand that update and re-execute is required.
func (u *Updater) CheckRemote(ctx context.Context, proxyAddr string, insecure bool) (version string, reExec bool, err error) {
// Check if the user has requested a specific version of client tools.
requestedVersion := os.Getenv(teleportToolsVersionEnv)
switch requestedVersion {
// The user has turned off any form of automatic updates.
case "off":
return "", false, nil
// Requested version already the same as client version.
case u.localVersion:
return u.localVersion, false, nil
// No requested version, we continue.
case "":
// Requested version that is not the local one.
default:
if _, err := semver.NewVersion(requestedVersion); err != nil {
return "", false, trace.Wrap(err, "checking that request version is semantic")
}
return requestedVersion, true, nil
}

certPool, err := x509.SystemCertPool()
if err != nil {
return "", false, trace.Wrap(err)
Expand Down Expand Up @@ -317,7 +337,15 @@ func (u *Updater) Exec(args []string) (int, error) {
if err := os.Unsetenv(teleportToolsVersionEnv); err != nil {
return 0, trace.Wrap(err)
}
env := append(os.Environ(), teleportToolsVersionEnv+"=off")

env := os.Environ()
executablePath, err := os.Executable()
if err != nil {
return 0, trace.Wrap(err)
}
if path == executablePath {
env = append(env, teleportToolsVersionEnv+"=off")
}

if runtime.GOOS == constants.WindowsOS {
cmd := exec.Command(path, args...)
Expand Down

0 comments on commit b8dc668

Please sign in to comment.