Centralize generating baseURL

This commit is contained in:
Grzegorz Zdunek
2025-07-11 17:52:33 +02:00
parent 223e4b58b2
commit a23bb34e58
3 changed files with 27 additions and 24 deletions
+2 -15
View File
@@ -27,7 +27,6 @@ import (
"github.com/gravitational/trace" "github.com/gravitational/trace"
"github.com/gravitational/teleport" "github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/autoupdate"
stacksignal "github.com/gravitational/teleport/lib/utils/signal" stacksignal "github.com/gravitational/teleport/lib/utils/signal"
) )
@@ -35,8 +34,6 @@ import (
var ( var (
// version is the current version of the Teleport. // version is the current version of the Teleport.
version = teleport.Version version = teleport.Version
// baseURL is CDN URL for downloading official Teleport packages.
baseURL = autoupdate.DefaultBaseURL
) )
// CheckAndUpdateLocal verifies if the TELEPORT_TOOLS_VERSION environment variable // CheckAndUpdateLocal verifies if the TELEPORT_TOOLS_VERSION environment variable
@@ -53,12 +50,7 @@ func CheckAndUpdateLocal(ctx context.Context, reExecArgs []string) error {
return nil return nil
} }
// Overrides default base URL for custom CDN for downloading updates. updater := NewUpdater(toolsDir, version)
if envBaseURL := os.Getenv(autoupdate.BaseURLEnvVar); envBaseURL != "" {
baseURL = envBaseURL
}
updater := NewUpdater(toolsDir, version, WithBaseURL(baseURL))
// At process startup, check if a version has already been downloaded to // At process startup, check if a version has already been downloaded to
// $TELEPORT_HOME/bin or if the user has set the TELEPORT_TOOLS_VERSION // $TELEPORT_HOME/bin or if the user has set the TELEPORT_TOOLS_VERSION
// environment variable. If so, re-exec that version of client tools. // environment variable. If so, re-exec that version of client tools.
@@ -87,12 +79,7 @@ func CheckAndUpdateRemote(ctx context.Context, proxy string, insecure bool, reEx
return nil return nil
} }
// Overrides default base URL for custom CDN for downloading updates. updater := NewUpdater(toolsDir, version)
if envBaseURL := os.Getenv(autoupdate.BaseURLEnvVar); envBaseURL != "" {
baseURL = envBaseURL
}
updater := NewUpdater(toolsDir, version, WithBaseURL(baseURL))
toolsVersion, reExec, err := updater.CheckRemote(ctx, proxy, insecure) toolsVersion, reExec, err := updater.CheckRemote(ctx, proxy, insecure)
if err != nil { if err != nil {
return trace.Wrap(err) return trace.Wrap(err)
+1 -2
View File
@@ -120,7 +120,6 @@ func NewUpdater(toolsDir, localVersion string, options ...Option) *Updater {
toolsDir: toolsDir, toolsDir: toolsDir,
localVersion: localVersion, localVersion: localVersion,
uriTemplate: autoupdate.DefaultCDNURITemplate, uriTemplate: autoupdate.DefaultCDNURITemplate,
baseURL: autoupdate.DefaultBaseURL,
client: http.DefaultClient, client: http.DefaultClient,
} }
for _, option := range options { for _, option := range options {
@@ -266,7 +265,7 @@ func (u *Updater) UpdateWithLock(ctx context.Context, updateToolsVersion string)
// with defined updater directory suffix. // with defined updater directory suffix.
func (u *Updater) Update(ctx context.Context, toolsVersion string) error { func (u *Updater) Update(ctx context.Context, toolsVersion string) error {
// Get platform specific download URLs. // Get platform specific download URLs.
packages, err := teleportPackageURLs(ctx, u.uriTemplate, u.baseURL, toolsVersion) packages, err := teleportPackageURLs(u.uriTemplate, u.baseURL, toolsVersion)
if err != nil { if err != nil {
return trace.Wrap(err) return trace.Wrap(err)
} }
+24 -7
View File
@@ -133,6 +133,22 @@ func GetReExecFromVersion(ctx context.Context) string {
return reExecFromVersion return reExecFromVersion
} }
// ResolveBaseURL calculates base URL.
func ResolveBaseURL() (string, error) {
envBaseURL := os.Getenv(autoupdate.BaseURLEnvVar)
if envBaseURL != "" {
return envBaseURL, nil
}
m := modules.GetModules()
if m.BuildType() == modules.BuildOSS {
slog.Warn("Client tools updates are disabled as they are licensed under AGPL. To use Community Edition builds or custom binaries, set the 'TELEPORT_CDN_BASE_URL' environment variable.")
return "", errNoBaseURL
}
return autoupdate.DefaultBaseURL, nil
}
// packageURL defines URLs to the archive and their archive sha256 hash file, and marks // packageURL defines URLs to the archive and their archive sha256 hash file, and marks
// if this package is optional, for such case download needs to be ignored if package // if this package is optional, for such case download needs to be ignored if package
// not found in CDN. // not found in CDN.
@@ -143,14 +159,15 @@ type packageURL struct {
} }
// teleportPackageURLs returns URLs for the Teleport archives to download. // teleportPackageURLs returns URLs for the Teleport archives to download.
func teleportPackageURLs(ctx context.Context, uriTmpl string, baseURL, version string) ([]packageURL, error) { func teleportPackageURLs(uriTmpl string, baseURL, version string) ([]packageURL, error) {
m := modules.GetModules() if baseURL == "" {
envBaseURL := os.Getenv(autoupdate.BaseURLEnvVar) url, err := ResolveBaseURL()
if m.BuildType() == modules.BuildOSS && envBaseURL == "" { if err != nil {
slog.WarnContext(ctx, "Client tools updates are disabled as they are licensed under AGPL. To use Community Edition builds or custom binaries, set the 'TELEPORT_CDN_BASE_URL' environment variable.") return nil, trace.Wrap(err)
return nil, errNoBaseURL }
baseURL = url
} }
m := modules.GetModules()
var flags autoupdate.InstallFlags var flags autoupdate.InstallFlags
if m.IsBoringBinary() { if m.IsBoringBinary() {
flags |= autoupdate.FlagFIPS flags |= autoupdate.FlagFIPS