Skip to content

Commit

Permalink
[process-agent] [system-probe] Configure additional endpoints via env…
Browse files Browse the repository at this point in the history
…ironment variables (#5490)
  • Loading branch information
robertjli authored May 12, 2020
1 parent 6fe1009 commit 6cd9bb8
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/process/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -420,6 +421,24 @@ func loadEnvVariables() {
if v := os.Getenv("DD_CUSTOM_SENSITIVE_WORDS"); v != "" {
config.Datadog.Set("process_config.custom_sensitive_words", strings.Split(v, ","))
}

if v := os.Getenv("DD_PROCESS_ADDITIONAL_ENDPOINTS"); v != "" {
endpoints := make(map[string][]string)
if err := json.Unmarshal([]byte(v), &endpoints); err != nil {
log.Errorf(`Could not parse DD_PROCESS_ADDITIONAL_ENDPOINTS: %v. It must be of the form '{"https://process.agent.datadoghq.com": ["apikey1", ...], ...}'.`, err)
} else {
config.Datadog.Set("process_config.additional_endpoints", endpoints)
}
}

if v := os.Getenv("DD_ORCHESTRATOR_ADDITIONAL_ENDPOINTS"); v != "" {
endpoints := make(map[string][]string)
if err := json.Unmarshal([]byte(v), &endpoints); err != nil {
log.Errorf(`Could not parse DD_ORCHESTRATOR_ADDITIONAL_ENDPOINTS: %v. It must be of the form '{"https://process.agent.datadoghq.com": ["apikey1", ...], ...}'.`, err)
} else {
config.Datadog.Set("process_config.orchestrator_additional_endpoints", endpoints)
}
}
}

// IsBlacklisted returns a boolean indicating if the given command is blacklisted by our config.
Expand Down
78 changes: 78 additions & 0 deletions pkg/process/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,84 @@ func TestEnvSiteConfig(t *testing.T) {
os.Unsetenv("DD_PROCESS_AGENT_URL")
}

func TestEnvProcessAdditionalEndpoints(t *testing.T) {
config.Datadog = config.NewConfig("datadog", "DD", strings.NewReplacer(".", "_"))
defer restoreGlobalConfig()

assert := assert.New(t)

expected := make(map[string]string)
expected["key1"] = "url1.com"
expected["key2"] = "url2.com"
expected["key3"] = "url2.com"
expected["apikey_20"] = "my-process-app.datadoghq.com" // from config file

os.Setenv("DD_PROCESS_ADDITIONAL_ENDPOINTS", `{"https://url1.com": ["key1"], "https://url2.com": ["key2", "key3"]}`)
defer os.Unsetenv("DD_PROCESS_ADDITIONAL_ENDPOINTS")

agentConfig, err := NewAgentConfig(
"test",
"./testdata/TestDDAgentConfigYamlAndSystemProbeConfig.yaml",
"./testdata/TestDDAgentConfigYamlAndSystemProbeConfig-Net.yaml",
)
assert.NoError(err)

for _, actual := range agentConfig.APIEndpoints {
assert.Equal(expected[actual.APIKey], actual.Endpoint.Hostname(), actual)
}
}

func TestEnvOrchestratorAdditionalEndpoints(t *testing.T) {
config.Datadog = config.NewConfig("datadog", "DD", strings.NewReplacer(".", "_"))
defer restoreGlobalConfig()

assert := assert.New(t)

expected := make(map[string]string)
expected["key1"] = "url1.com"
expected["key2"] = "url2.com"
expected["key3"] = "url2.com"
expected["apikey_20"] = "orchestrator.datadoghq.com" // from config file

os.Setenv("DD_ORCHESTRATOR_ADDITIONAL_ENDPOINTS", `{"https://url1.com": ["key1"], "https://url2.com": ["key2", "key3"]}`)
defer os.Unsetenv("DD_ORCHESTRATOR_ADDITIONAL_ENDPOINTS")

agentConfig, err := NewAgentConfig(
"test",
"./testdata/TestDDAgentConfigYamlAndSystemProbeConfig.yaml",
"./testdata/TestDDAgentConfigYamlAndSystemProbeConfig-Net.yaml",
)
assert.NoError(err)

for _, actual := range agentConfig.OrchestratorEndpoints {
assert.Equal(expected[actual.APIKey], actual.Endpoint.Hostname(), actual)
}
}

func TestEnvAdditionalEndpointsMalformed(t *testing.T) {
config.Datadog = config.NewConfig("datadog", "DD", strings.NewReplacer(".", "_"))
defer restoreGlobalConfig()

assert := assert.New(t)

expected := make(map[string]string)
expected["apikey_20"] = "my-process-app.datadoghq.com" // from config file

os.Setenv("DD_PROCESS_ADDITIONAL_ENDPOINTS", `"https://url1.com","key1"`)
defer os.Unsetenv("DD_PROCESS_ADDITIONAL_ENDPOINTS")

agentConfig, err := NewAgentConfig(
"test",
"./testdata/TestDDAgentConfigYamlAndSystemProbeConfig.yaml",
"./testdata/TestDDAgentConfigYamlAndSystemProbeConfig-Net.yaml",
)
assert.NoError(err)

for _, actual := range agentConfig.APIEndpoints {
assert.Equal(expected[actual.APIKey], actual.Endpoint.Hostname(), actual)
}
}

func TestIsAffirmative(t *testing.T) {
value, err := isAffirmative("yes")
assert.Nil(t, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every releasenote are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
enhancements:
- |
Configure additional process and orchestrator endpoints by environment variable.

0 comments on commit 6cd9bb8

Please sign in to comment.