Skip to content

Commit

Permalink
[process-agent] Remove unused properties from AgentConfig (#14842)
Browse files Browse the repository at this point in the history
* [process-agent] Remove unused properties from AgentConfig

* Fix tests
  • Loading branch information
xornivore authored Dec 22, 2022
1 parent 0dbb959 commit 4ea1a91
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 183 deletions.
95 changes: 0 additions & 95 deletions pkg/process/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -35,10 +33,6 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// defaultProxyPort is the default port used for proxies.
// This mirrors the configuration for the infrastructure agent.
const defaultProxyPort = 3128

// Name for check performed by process-agent or system-probe
const (
ProcessCheckName = "process"
Expand All @@ -60,8 +54,6 @@ const (
ProcessDiscoveryCheckDefaultInterval = 4 * time.Hour
)

type proxyFunc func(*http.Request) (*url.URL, error)

type cmdFunc = func(name string, arg ...string) *exec.Cmd

// AgentConfig is the global config for the process-agent. This information
Expand All @@ -74,23 +66,18 @@ type AgentConfig struct {
Blacklist []*regexp.Regexp
Scrubber *DataScrubber
MaxConnsPerMessage int
Transport *http.Transport `json:"-"`

// host type of the agent, used to populate container payload with additional host information
ContainerHostType model.ContainerHostType

// System probe collection configuration
EnableSystemProbe bool
SystemProbeAddress string

// Orchestrator config
Orchestrator *oconfig.OrchestratorConfig

// Check config
CheckIntervals map[string]time.Duration

// Internal store of a proxy used for generating the Transport
proxy proxyFunc
}

// CheckInterval returns the interval for the given check name, defaulting to 10s if not found.
Expand Down Expand Up @@ -123,12 +110,10 @@ func NewDefaultAgentConfig() *AgentConfig {
ac := &AgentConfig{
MaxConnsPerMessage: 600,
HostName: "",
Transport: NewDefaultTransport(),

ContainerHostType: model.ContainerHostType_notSpecified,

// System probe collection configuration
EnableSystemProbe: false,
SystemProbeAddress: defaultSystemProbeAddress,

// Orchestrator config
Expand Down Expand Up @@ -189,8 +174,6 @@ func LoadConfigIfExists(path string) error {
// NewAgentConfig returns an AgentConfig using a configuration file. It can be nil
// if there is no file available. In this case we'll configure only via environment.
func NewAgentConfig(loggerName config.LoggerName, yamlPath string, syscfg *sysconfig.Config) (*AgentConfig, error) {
var err error

cfg := NewDefaultAgentConfig()
if err := cfg.LoadAgentConfig(yamlPath); err != nil {
return nil, err
Expand All @@ -208,17 +191,10 @@ func NewAgentConfig(loggerName config.LoggerName, yamlPath string, syscfg *sysco
}

if syscfg.Enabled {
cfg.EnableSystemProbe = true
cfg.MaxConnsPerMessage = syscfg.MaxConnsPerMessage
cfg.SystemProbeAddress = syscfg.SocketAddress
}

// TODO: Once proxies have been moved to common config util, remove this
if cfg.proxy, err = proxyFromEnv(cfg.proxy); err != nil {
log.Errorf("error parsing environment proxy settings, not using a proxy: %s", err)
cfg.proxy = nil
}

if err := validate.ValidHostname(cfg.HostName); err != nil {
// lookup hostname if there is no config override or if the override is invalid
agentBin := config.Datadog.GetString("process_config.dd_agent_bin")
Expand All @@ -232,10 +208,6 @@ func NewAgentConfig(loggerName config.LoggerName, yamlPath string, syscfg *sysco

cfg.ContainerHostType = getContainerHostType()

if cfg.proxy != nil {
cfg.Transport.Proxy = cfg.proxy
}

return cfg, nil
}

Expand Down Expand Up @@ -376,73 +348,6 @@ func getHostnameFromGRPC(ctx context.Context, grpcClientFn func(ctx context.Cont
return reply.Hostname, nil
}

// proxyFromEnv parses out the proxy configuration from the ENV variables in a
// similar way to getProxySettings and, if enough values are available, returns
// a new proxy URL value. If the environment is not set for this then the
// `defaultVal` is returned.
func proxyFromEnv(defaultVal proxyFunc) (proxyFunc, error) {
var host string
scheme := "http"
if v := os.Getenv("PROXY_HOST"); v != "" {
// accept either http://myproxy.com or myproxy.com
if i := strings.Index(v, "://"); i != -1 {
// when available, parse the scheme from the url
scheme = v[0:i]
host = v[i+3:]
} else {
host = v
}
}

if host == "" {
return defaultVal, nil
}

port := defaultProxyPort
if v := os.Getenv("PROXY_PORT"); v != "" {
port, _ = strconv.Atoi(v)
}
var user, password string
if v := os.Getenv("PROXY_USER"); v != "" {
user = v
}
if v := os.Getenv("PROXY_PASSWORD"); v != "" {
password = v
}

return constructProxy(host, scheme, port, user, password)
}

// constructProxy constructs a *url.Url for a proxy given the parts of a
// Note that we assume we have at least a non-empty host for this call but
// all other values can be their defaults (empty string or 0).
func constructProxy(host, scheme string, port int, user, password string) (proxyFunc, error) {
var userpass *url.Userinfo
if user != "" {
if password != "" {
userpass = url.UserPassword(user, password)
} else {
userpass = url.User(user)
}
}

var path string
if userpass != nil {
path = fmt.Sprintf("%s@%s:%v", userpass.String(), host, port)
} else {
path = fmt.Sprintf("%s:%v", host, port)
}
if scheme != "" {
path = fmt.Sprintf("%s://%s", scheme, path)
}

u, err := url.Parse(path)
if err != nil {
return nil, err
}
return http.ProxyURL(u), nil
}

func setupLogger(loggerName config.LoggerName, logFile string) error {
if config.Datadog.GetBool("disable_file_logging") {
logFile = ""
Expand Down
83 changes: 0 additions & 83 deletions pkg/process/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -317,53 +315,6 @@ func TestAgentConfigYamlAndSystemProbeConfig(t *testing.T) {
}
}

func TestProxyEnv(t *testing.T) {
assert := assert.New(t)
for i, tc := range []struct {
host string
port int
user string
pass string
expected string
}{
{
"example.com",
1234,
"",
"",
"http://example.com:1234",
},
{
"https://example.com",
4567,
"foo",
"bar",
"https://foo:[email protected]:4567",
},
{
"example.com",
0,
"foo",
"",
"http://[email protected]:3128",
},
} {
t.Setenv("PROXY_HOST", tc.host)
if tc.port > 0 {
t.Setenv("PROXY_PORT", strconv.Itoa(tc.port))
} else {
t.Setenv("PROXY_PORT", "")
}
t.Setenv("PROXY_USER", tc.user)
t.Setenv("PROXY_PASSWORD", tc.pass)
pf, err := proxyFromEnv(nil)
assert.NoError(err, "proxy case %d had error", i)
u, err := pf(&http.Request{})
assert.NoError(err)
assert.Equal(tc.expected, u.String())
}
}

func TestEnvOrchestratorAdditionalEndpoints(t *testing.T) {
newConfig()
defer restoreGlobalConfig()
Expand All @@ -385,40 +336,6 @@ func TestEnvOrchestratorAdditionalEndpoints(t *testing.T) {
}
}

func TestNetworkConfig(t *testing.T) {
t.Run("yaml", func(t *testing.T) {
newConfig()
defer restoreGlobalConfig()

agentConfig := loadAgentConfigForTest(t, "./testdata/TestDDAgentConfigYamlOnly.yaml", "./testdata/TestDDAgentConfig-NetConfig.yaml")

assert.True(t, agentConfig.EnableSystemProbe)
})

t.Run("env", func(t *testing.T) {
newConfig()
defer restoreGlobalConfig()

t.Setenv("DD_SYSTEM_PROBE_NETWORK_ENABLED", "true")

syscfg, err := sysconfig.Merge("")
require.NoError(t, err)
agentConfig, err := NewAgentConfig("test", "", syscfg)
require.NoError(t, err)

assert.True(t, agentConfig.EnableSystemProbe)
})
}

func TestSystemProbeNoNetwork(t *testing.T) {
newConfig()
defer restoreGlobalConfig()

agentConfig := loadAgentConfigForTest(t, "./testdata/TestDDAgentConfigYamlOnly.yaml", "./testdata/TestDDAgentConfig-OOMKillOnly.yaml")

assert.True(t, agentConfig.EnableSystemProbe)
}

func TestGetHostnameFromGRPC(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
Expand Down
5 changes: 0 additions & 5 deletions pkg/process/config/yaml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/DataDog/datadog-agent/pkg/config"
httputils "github.com/DataDog/datadog-agent/pkg/util/http"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

Expand Down Expand Up @@ -110,10 +109,6 @@ func (a *AgentConfig) LoadAgentConfig(path string) error {
log.Debug("Strip all process arguments enabled")
a.Scrubber.StripAllArguments = true
}

// Build transport (w/ proxy if needed)
a.Transport = httputils.CreateHTTPTransport()

return nil
}

Expand Down

0 comments on commit 4ea1a91

Please sign in to comment.