Skip to content

Commit 1c25985

Browse files
authored
[ASCII-2587] Migrating TraceAgent to use IPC cert (#31847)
1 parent 7992c12 commit 1c25985

File tree

13 files changed

+80
-29
lines changed

13 files changed

+80
-29
lines changed

cmd/agent/subcommands/flare/command.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,18 @@ func readProfileData(seconds int) (flare.ProfileData, error) {
175175

176176
type pprofGetter func(path string) ([]byte, error)
177177

178-
tcpGet := func(portConfig string) pprofGetter {
179-
pprofURL := fmt.Sprintf("http://127.0.0.1:%d/debug/pprof", pkgconfigsetup.Datadog().GetInt(portConfig))
178+
tcpGet := func(portConfig string, onHTTPS bool) pprofGetter {
179+
endpoint := url.URL{
180+
Scheme: "http",
181+
Host: net.JoinHostPort("127.0.0.1", strconv.Itoa(pkgconfigsetup.Datadog().GetInt(portConfig))),
182+
Path: "/debug/pprof",
183+
}
184+
if onHTTPS {
185+
endpoint.Scheme = "https"
186+
}
187+
180188
return func(path string) ([]byte, error) {
181-
return util.DoGet(c, pprofURL+path, util.LeaveConnectionOpen)
189+
return util.DoGet(c, endpoint.String()+path, util.LeaveConnectionOpen)
182190
}
183191
}
184192

@@ -228,15 +236,15 @@ func readProfileData(seconds int) (flare.ProfileData, error) {
228236
}
229237

230238
agentCollectors := map[string]agentProfileCollector{
231-
"core": serviceProfileCollector(tcpGet("expvar_port"), seconds),
232-
"security-agent": serviceProfileCollector(tcpGet("security_agent.expvar_port"), seconds),
239+
"core": serviceProfileCollector(tcpGet("expvar_port", false), seconds),
240+
"security-agent": serviceProfileCollector(tcpGet("security_agent.expvar_port", false), seconds),
233241
}
234242

235243
if pkgconfigsetup.Datadog().GetBool("process_config.enabled") ||
236244
pkgconfigsetup.Datadog().GetBool("process_config.container_collection.enabled") ||
237245
pkgconfigsetup.Datadog().GetBool("process_config.process_collection.enabled") {
238246

239-
agentCollectors["process"] = serviceProfileCollector(tcpGet("process_config.expvar_port"), seconds)
247+
agentCollectors["process"] = serviceProfileCollector(tcpGet("process_config.expvar_port", false), seconds)
240248
}
241249

242250
if pkgconfigsetup.Datadog().GetBool("apm_config.enabled") {
@@ -249,7 +257,7 @@ func readProfileData(seconds int) (flare.ProfileData, error) {
249257
traceCpusec = 4
250258
}
251259

252-
agentCollectors["trace"] = serviceProfileCollector(tcpGet("apm_config.debug.port"), traceCpusec)
260+
agentCollectors["trace"] = serviceProfileCollector(tcpGet("apm_config.debug.port", true), traceCpusec)
253261
}
254262

255263
if pkgconfigsetup.SystemProbe().GetBool("system_probe_config.enabled") {

cmd/agent/subcommands/flare/command_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type commandTestSuite struct {
2929
suite.Suite
3030
sysprobeSocketPath string
3131
tcpServer *httptest.Server
32+
tcpTLSServer *httptest.Server
3233
unixServer *httptest.Server
3334
systemProbeServer *httptest.Server
3435
}
@@ -42,13 +43,17 @@ func (c *commandTestSuite) SetupSuite() {
4243
// This should be called by each test that requires them.
4344
func (c *commandTestSuite) startTestServers() {
4445
t := c.T()
45-
c.tcpServer, c.unixServer, c.systemProbeServer = c.getPprofTestServer()
46+
c.tcpServer, c.tcpTLSServer, c.unixServer, c.systemProbeServer = c.getPprofTestServer()
4647

4748
t.Cleanup(func() {
4849
if c.tcpServer != nil {
4950
c.tcpServer.Close()
5051
c.tcpServer = nil
5152
}
53+
if c.tcpTLSServer != nil {
54+
c.tcpTLSServer.Close()
55+
c.tcpTLSServer = nil
56+
}
5257
if c.unixServer != nil {
5358
c.unixServer.Close()
5459
c.unixServer = nil
@@ -82,12 +87,13 @@ func newMockHandler() http.HandlerFunc {
8287
})
8388
}
8489

85-
func (c *commandTestSuite) getPprofTestServer() (tcpServer *httptest.Server, unixServer *httptest.Server, sysProbeServer *httptest.Server) {
90+
func (c *commandTestSuite) getPprofTestServer() (tcpServer *httptest.Server, tcpTLSServer *httptest.Server, unixServer *httptest.Server, sysProbeServer *httptest.Server) {
8691
var err error
8792
t := c.T()
8893

8994
handler := newMockHandler()
9095
tcpServer = httptest.NewServer(handler)
96+
tcpTLSServer = httptest.NewTLSServer(handler)
9197
if runtime.GOOS == "linux" {
9298
unixServer = httptest.NewUnstartedServer(handler)
9399
unixServer.Listener, err = net.Listen("unix", c.sysprobeSocketPath)
@@ -101,7 +107,7 @@ func (c *commandTestSuite) getPprofTestServer() (tcpServer *httptest.Server, uni
101107
sysProbeServer.Start()
102108
}
103109

104-
return tcpServer, unixServer, sysProbeServer
110+
return tcpServer, tcpTLSServer, unixServer, sysProbeServer
105111
}
106112

107113
func TestCommandTestSuite(t *testing.T) {
@@ -116,10 +122,14 @@ func (c *commandTestSuite) TestReadProfileData() {
116122
require.NoError(t, err)
117123
port := u.Port()
118124

125+
u, err = url.Parse(c.tcpTLSServer.URL)
126+
require.NoError(t, err)
127+
httpsPort := u.Port()
128+
119129
mockConfig := configmock.New(t)
120130
mockConfig.SetWithoutSource("expvar_port", port)
121131
mockConfig.SetWithoutSource("apm_config.enabled", true)
122-
mockConfig.SetWithoutSource("apm_config.debug.port", port)
132+
mockConfig.SetWithoutSource("apm_config.debug.port", httpsPort)
123133
mockConfig.SetWithoutSource("apm_config.receiver_timeout", "10")
124134
mockConfig.SetWithoutSource("process_config.expvar_port", port)
125135
mockConfig.SetWithoutSource("security_agent.expvar_port", port)

cmd/agent/subcommands/secret/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func traceAgentSecretRefresh(conf config.Component) ([]byte, error) {
101101
c := apiutil.GetClient(false)
102102
c.Timeout = conf.GetDuration("server_timeout") * time.Second
103103

104-
url := fmt.Sprintf("http://127.0.0.1:%d/secret/refresh", port)
104+
url := fmt.Sprintf("https://127.0.0.1:%d/secret/refresh", port)
105105
res, err := apiutil.DoGet(c, url, apiutil.CloseConnection)
106106
if err != nil {
107107
return nil, fmt.Errorf("could not contact trace-agent: %s", err)

comp/trace/agent/impl/agent.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"go.opentelemetry.io/collector/pdata/ptrace"
2525
"go.uber.org/fx"
2626

27+
"github.com/DataDog/datadog-agent/comp/api/authtoken"
2728
"github.com/DataDog/datadog-agent/comp/core/secrets"
2829
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
2930
"github.com/DataDog/datadog-agent/comp/dogstatsd/statsd"
@@ -68,6 +69,7 @@ type dependencies struct {
6869
Statsd statsd.Component
6970
Tagger tagger.Component
7071
Compressor compression.Component
72+
At authtoken.Component
7173
}
7274

7375
var _ traceagent.Component = (*component)(nil)
@@ -93,6 +95,7 @@ type component struct {
9395
params *Params
9496
tagger tagger.Component
9597
telemetryCollector telemetry.TelemetryCollector
98+
at authtoken.Component
9699
wg *sync.WaitGroup
97100
}
98101

@@ -115,6 +118,7 @@ func NewAgent(deps dependencies) (traceagent.Component, error) {
115118
params: deps.Params,
116119
telemetryCollector: deps.TelemetryCollector,
117120
tagger: deps.Tagger,
121+
at: deps.At,
118122
wg: &sync.WaitGroup{},
119123
}
120124
statsdCl, err := setupMetrics(deps.Statsd, c.config, c.telemetryCollector)

comp/trace/agent/impl/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func runAgentSidekicks(ag component) error {
9898
}))
9999
}
100100

101+
// Configure the Trace Agent Debug server to use the IPC certificate
102+
ag.Agent.DebugServer.SetTLSConfig(ag.at.GetTLSServerConfig())
103+
101104
log.Infof("Trace agent running on host %s", tracecfg.Hostname)
102105
if pcfg := profilingConfig(tracecfg); pcfg != nil {
103106
if err := profiling.Start(*pcfg); err != nil {

comp/trace/bundle_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/stretchr/testify/require"
1414
"go.uber.org/fx"
1515

16+
"github.com/DataDog/datadog-agent/comp/api/authtoken/createandfetchimpl"
17+
"github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
1618
"github.com/DataDog/datadog-agent/comp/core"
1719
coreconfig "github.com/DataDog/datadog-agent/comp/core/config"
1820
log "github.com/DataDog/datadog-agent/comp/core/log/def"
@@ -45,6 +47,7 @@ func TestBundleDependencies(t *testing.T) {
4547
zstdfx.Module(),
4648
taggerfx.Module(tagger.Params{}),
4749
fx.Supply(&traceagentimpl.Params{}),
50+
createandfetchimpl.Module(),
4851
)
4952
}
5053

@@ -75,6 +78,7 @@ func TestMockBundleDependencies(t *testing.T) {
7578
fx.Invoke(func(_ traceagent.Component) {}),
7679
MockBundle(),
7780
taggerfx.Module(tagger.Params{}),
81+
fetchonlyimpl.MockModule(),
7882
))
7983

8084
require.NotNil(t, cfg.Object())

comp/trace/status/statusimpl/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (s statusProvider) populateStatus() map[string]interface{} {
9595
port := s.Config.GetInt("apm_config.debug.port")
9696

9797
c := client()
98-
url := fmt.Sprintf("http://localhost:%d/debug/vars", port)
98+
url := fmt.Sprintf("https://localhost:%d/debug/vars", port)
9999
resp, err := apiutil.DoGet(c, url, apiutil.CloseConnection)
100100
if err != nil {
101101
return map[string]interface{}{

pkg/config/fetcher/from_processes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TraceAgentConfig(config config.Reader) (string, error) {
7171
c := util.GetClient(false)
7272
c.Timeout = config.GetDuration("server_timeout") * time.Second
7373

74-
ipcAddressWithPort := fmt.Sprintf("http://127.0.0.1:%d/config", port)
74+
ipcAddressWithPort := fmt.Sprintf("https://127.0.0.1:%d/config", port)
7575

7676
client := settingshttp.NewClient(c, ipcAddressWithPort, "trace-agent", settingshttp.NewHTTPClientOptions(util.CloseConnection))
7777
return client.FullConfig()

pkg/flare/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func getExpVar(fb flaretypes.FlareBuilder) error {
214214

215215
apmDebugPort := pkgconfigsetup.Datadog().GetInt("apm_config.debug.port")
216216
f := filepath.Join("expvar", "trace-agent")
217-
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/debug/vars", apmDebugPort))
217+
resp, err := http.Get(fmt.Sprintf("https://127.0.0.1:%d/debug/vars", apmDebugPort))
218218
if err != nil {
219219
return fb.AddFile(f, []byte(fmt.Sprintf("Error retrieving vars: %v", err)))
220220
}

pkg/trace/api/api_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,14 +1039,26 @@ func TestExpvar(t *testing.T) {
10391039
}
10401040

10411041
c := newTestReceiverConfig()
1042-
c.DebugServerPort = 5012
1042+
c.DebugServerPort = 6789
10431043
info.InitInfo(c)
1044+
1045+
// Starting a TLS httptest server to retrieve tlsCert
1046+
ts := httptest.NewTLSServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
1047+
tlsConfig := ts.TLS.Clone()
1048+
// Setting a client with the proper TLS configuration
1049+
client := ts.Client()
1050+
ts.Close()
1051+
1052+
// Starting Debug Server
10441053
s := NewDebugServer(c)
1054+
s.SetTLSConfig(tlsConfig)
1055+
1056+
// Starting the Debug server
10451057
s.Start()
10461058
defer s.Stop()
10471059

1048-
resp, err := http.Get("http://127.0.0.1:5012/debug/vars")
1049-
assert.NoError(t, err)
1060+
resp, err := client.Get(fmt.Sprintf("https://127.0.0.1:%d/debug/vars", c.DebugServerPort))
1061+
require.NoError(t, err)
10501062
defer resp.Body.Close()
10511063

10521064
t.Run("read-expvars", func(t *testing.T) {

0 commit comments

Comments
 (0)