From 2258344e1f95fbf63133a446979ed3d939e26616 Mon Sep 17 00:00:00 2001 From: Stephanie Ahart Date: Fri, 6 Nov 2026 16:50:18 -0800 Subject: [PATCH] BRE2-1285: Warn when API endpoints are overridden --- pkg/cmd/cmd.go | 1 + pkg/config/config.go | 45 ++++++++++++++++++++++++++++++++++----- pkg/config/config_test.go | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 pkg/config/config_test.go diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 7411c830..4134de8f 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -150,6 +150,7 @@ func NewBrevCommand() *cobra.Command { //nolint:funlen,gocognit,gocyclo // defin return nil }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + config.WarnEndpointOverrides(cmd.ErrOrStderr()) analytics.RecordCommandStart(cmd) breverrors.GetDefaultErrorReporter().AddTag("command", cmd.Name()) if strings.TrimSpace(orgFlag) != "" && !allowsOrgOverrideWithExternalAuth(cmd) && auth.IsAPIKeyAuthStore(loginCmdStore) { diff --git a/pkg/config/config.go b/pkg/config/config.go index 37ad60c2..a1986bda 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,7 +1,10 @@ package config import ( + "fmt" + "io" "os" + "strings" ) type EnvVarName string // should be caps with underscore @@ -28,26 +31,26 @@ func NewConstants() *ConstantsConfig { } func (c ConstantsConfig) GetBrevAPIURl() string { - return getEnvOrDefault(brevAPIURL, "https://brevapi.us-west-2-prod.control-plane.brev.dev") + return getEnvOrDefault(brevAPIURL, defaultBrevAPIURL) } // GetBrevPublicAPIURL returns the base URL of dev-plane's public API server // (BREV_PUBLIC_API_URL). That server serves both the public HTTP routes (/v1/...) // and the ConnectRPC services the CLI calls (pkg/cmd/register, pkg/environment). func (c ConstantsConfig) GetBrevPublicAPIURL() string { - return getEnvOrDefault(brevPublicAPIURL, "https://api.brev.dev") + return getEnvOrDefault(brevPublicAPIURL, defaultBrevPublicAPIURL) } func (c ConstantsConfig) GetBrevAuthURL() string { - return getEnvOrDefault(brevAuthURL, "https://api.ngc.nvidia.com") + return getEnvOrDefault(brevAuthURL, defaultBrevAuthURL) } func (c ConstantsConfig) GetBrevAuthIssuerURL() string { - return getEnvOrDefault(brevAuthIssuerURL, "https://login.nvidia.com") + return getEnvOrDefault(brevAuthIssuerURL, defaultBrevAuthIssuerURL) } func (c ConstantsConfig) GetConsoleURL() string { - return getEnvOrDefault(brevConsoleURL, "https://brev.nvidia.com/cli-login") + return getEnvOrDefault(brevConsoleURL, defaultBrevConsoleURL) } func (c ConstantsConfig) GetOllamaAPIURL() string { @@ -97,3 +100,35 @@ type AllConfig interface { GetVersion() string GetDefaultClusterID() string } + +const ( + defaultBrevAPIURL = "https://brevapi.us-west-2-prod.control-plane.brev.dev" + defaultBrevPublicAPIURL = "https://api.brev.dev" + defaultBrevAuthURL = "https://api.ngc.nvidia.com" + defaultBrevAuthIssuerURL = "https://login.nvidia.com" + defaultBrevConsoleURL = "https://brev.nvidia.com/cli-login" +) + +// WarnEndpointOverrides reports custom authentication/API destinations without +// printing URLs, which may contain credentials or sensitive query parameters. +func WarnEndpointOverrides(w io.Writer) { + var overridden []string + for _, endpoint := range []struct { + name EnvVarName + defaultURL string + }{ + {brevAPIURL, defaultBrevAPIURL}, + {brevPublicAPIURL, defaultBrevPublicAPIURL}, + {brevAuthURL, defaultBrevAuthURL}, + {brevAuthIssuerURL, defaultBrevAuthIssuerURL}, + {brevConsoleURL, defaultBrevConsoleURL}, + } { + value := os.Getenv(string(endpoint.name)) + if value != "" && value != endpoint.defaultURL { + overridden = append(overridden, string(endpoint.name)) + } + } + if len(overridden) > 0 { + _, _ = fmt.Fprintf(w, "Warning: non-default Brev endpoints configured via %s. Authentication and API traffic may be sent to custom servers.\n", strings.Join(overridden, ", ")) + } +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 00000000..f329eaa3 --- /dev/null +++ b/pkg/config/config_test.go @@ -0,0 +1,39 @@ +package config + +import ( + "bytes" + "strings" + "testing" +) + +func TestWarnEndpointOverrides(t *testing.T) { + for _, name := range []EnvVarName{brevAPIURL, brevPublicAPIURL, brevAuthURL, brevAuthIssuerURL, brevConsoleURL} { + t.Setenv(string(name), "") + } + var output bytes.Buffer + WarnEndpointOverrides(&output) + if output.Len() != 0 { + t.Fatalf("default endpoints warned: %s", output.String()) + } + t.Setenv(string(brevAPIURL), defaultBrevAPIURL) + WarnEndpointOverrides(&output) + if output.Len() != 0 { + t.Fatalf("explicit default warned: %s", output.String()) + } + for _, name := range []EnvVarName{brevAPIURL, brevPublicAPIURL, brevAuthURL, brevAuthIssuerURL, brevConsoleURL} { + t.Setenv(string(name), "https://secret:password@custom.example/?token=private") + } + WarnEndpointOverrides(&output) + warning := output.String() + for _, name := range []EnvVarName{brevAPIURL, brevPublicAPIURL, brevAuthURL, brevAuthIssuerURL, brevConsoleURL} { + if strings.Count(warning, string(name)) != 1 { + t.Errorf("expected one warning for %s: %s", name, warning) + } + } + if strings.Contains(warning, "secret") || strings.Contains(warning, "custom.example") || strings.Contains(warning, "private") { + t.Fatalf("endpoint value leaked: %s", warning) + } + if got := (ConstantsConfig{}).GetBrevAPIURl(); got != "https://secret:password@custom.example/?token=private" { + t.Fatalf("override changed: %s", got) + } +}