Skip to content

Commit

Permalink
Trim forward slashes from config urls (DataDog#4843)
Browse files Browse the repository at this point in the history
* update branch for merge

* fix issue where unset site or dd_url parameters broke the run, update test to reflect

* add logic to check if domain is empty

* replace trimSuffix with cleaner trimRight function

* move trim slash function to be called after overrides, add method name to description
  • Loading branch information
darthnater007 authored Feb 14, 2020
1 parent 4ebaf03 commit c0a8973
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ func load(config Config, origin string, loadSecret bool) error {
loadProxyFromEnv(config)
sanitizeAPIKey(config)
applyOverrides(config)
trimTrailingSlashFromURLS(config)
// setTracemallocEnabled *must* be called before setNumWorkers
setTracemallocEnabled(config)
setNumWorkers(config)
Expand Down Expand Up @@ -816,6 +817,48 @@ func sanitizeAPIKey(config Config) {
config.Set("api_key", strings.TrimSpace(config.GetString("api_key")))
}

//trimTrailingSlashFromURLS trims any forward slashes from the end of various config URL's (site, dd_url, and various additional endpoints)
func trimTrailingSlashFromURLS(config Config) error {
var urls = []string{
"site",
"dd_url",
}
var additionalEndpointSelectors = []string{
"additional_endpoints",
"apm_config.additional_endpoints",
"logs_config.additional_endpoints",
"process_config.additional_endpoints",
}

for _, domain := range urls {
key := config.GetString(domain)
if key == "" {
continue
}
config.Set(domain, strings.TrimRight(key, "/"))
}

for _, es := range additionalEndpointSelectors {
additionalEndpoints := make(map[string][]string)
sanitizedAdditionalEndpoints := make(map[string][]string)

err := config.UnmarshalKey(es, &additionalEndpoints)

if err != nil {
return err
}
for domain, keys := range additionalEndpoints {
if domain == "" {
continue
}
domain = strings.TrimRight(domain, "/")
sanitizedAdditionalEndpoints[domain] = keys
}
config.Set(es, sanitizedAdditionalEndpoints)
}
return nil
}

// GetMainInfraEndpoint returns the main DD Infra URL defined in the config, based on the value of `site` and `dd_url`
func GetMainInfraEndpoint() string {
return getMainInfraEndpointWithConfig(Datadog)
Expand Down
66 changes: 66 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,72 @@ func TestSanitizeAPIKey(t *testing.T) {
assert.Equal(t, "foo", config.GetString("api_key"))
}

func TestTrimTrailingSlashFromURLS(t *testing.T) {
var urls = []string{
"site",
"dd_url",
}
var additionalEndpointSelectors = []string{
"additional_endpoints",
"apm_config.additional_endpoints",
"logs_config.additional_endpoints",
"process_config.additional_endpoints",
}
datadogYaml := `
api_key: fakeapikey
site: datadoghq.com/////
dd_url:
additional_endpoints:
testing.com///:
- fakekey
test2.com/:
- fakekey
apm_config:
additional_endpoints:
testingapm.com//:
- fakekey
test2apm.com/:
- fakekey
logs_config:
additional_endpoints:
testinglogs.com///////:
- fakekey
test2logs.com/:
- fakekey
process_config:
additional_endpoints:
testingproc.com/////:
- fakekey
test2proc.com/:
- fakekey
`
testConfig := setupConfFromYAML(datadogYaml)
trimTrailingSlashFromURLS(testConfig)

for _, u := range urls {
testString := testConfig.GetString(u)
if len(testString) == 0 {
continue
}
if testString[len(testString)-1:] == "/" {
t.Errorf("Error: The key %v: has a vlue of %v -- The trailing forward slash was not properly trimmed", u, testConfig.GetString(u))
}
}
for _, es := range additionalEndpointSelectors {
additionalEndpoints := make(map[string][]string)
err := testConfig.UnmarshalKey(es, &additionalEndpoints)
if err != nil {
t.Errorf("Error: %v", err)
}

for domain := range additionalEndpoints {
if domain[len(domain)-1:] == "/" {
t.Errorf("Error: The key %v: has a vlue of %v -- The trailing forward slash was not properly trimmed", es, domain)
}
}
}
}

// TestSecretBackendWithMultipleEndpoints tests an edge case of `viper.AllSettings()` when a config
// key includes the key delimiter. Affects the config package when both secrets and multiple
// endpoints are configured.
Expand Down

0 comments on commit c0a8973

Please sign in to comment.