Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func Load() {
CustomerID = cfg.CustomerID
}
if cfg.APIEndpoint != "" && isPlaceholder(APIEndpoint) {
APIEndpoint = cfg.APIEndpoint
APIEndpoint = NormalizeAPIEndpoint(cfg.APIEndpoint)
}
if cfg.APIKey != "" && isPlaceholder(APIKey) {
APIKey = cfg.APIKey
Expand Down Expand Up @@ -397,7 +397,18 @@ func loadExisting() *ConfigFile {
return &cfg
}

// NormalizeAPIEndpoint strips trailing slashes and surrounding whitespace
// from an API endpoint URL. Every callsite in the agent composes URLs as
// fmt.Sprintf("%s/v1/...", APIEndpoint, ...) — a trailing slash on the
// configured value would compose to "//v1/..." and some gateways respond
// with 403/500 instead of normalizing. Normalising once at the config
// boundary keeps every consumer simple.
func NormalizeAPIEndpoint(s string) string {
return strings.TrimRight(strings.TrimSpace(s), "/")
}

func save(cfg *ConfigFile) error {
cfg.APIEndpoint = NormalizeAPIEndpoint(cfg.APIEndpoint)
dir := writeConfigDir()

// File-mode bits below ARE meaningful on POSIX (per-user community installs
Expand Down
21 changes: 21 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ func TestIsEnterpriseMode_Valid(t *testing.T) {
}
}

func TestNormalizeAPIEndpoint(t *testing.T) {
tests := []struct {
in, want string
}{
{"https://api.example.com", "https://api.example.com"},
{"https://api.example.com/", "https://api.example.com"},
{"https://api.example.com//", "https://api.example.com"},
{" https://api.example.com/ ", "https://api.example.com"},
{"https://api.example.com/v1", "https://api.example.com/v1"},
{"https://api.example.com/v1/", "https://api.example.com/v1"},
{"", ""},
{" ", ""},
{"/", ""},
}
for _, tt := range tests {
if got := NormalizeAPIEndpoint(tt.in); got != tt.want {
t.Errorf("NormalizeAPIEndpoint(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}

func TestIsPlaceholder(t *testing.T) {
tests := []struct {
input string
Expand Down
Loading