Skip to content

Commit 7992c12

Browse files
authored
[ASCII-2584] Migrating CoreAgent to use IPC cert (#31843)
1 parent 04763f7 commit 7992c12

File tree

5 files changed

+13
-90
lines changed

5 files changed

+13
-90
lines changed

comp/api/api/apiimpl/api_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/DataDog/datadog-agent/comp/aggregator/demultiplexer/demultiplexerimpl"
2121
"github.com/DataDog/datadog-agent/comp/api/api/apiimpl/observability"
2222
api "github.com/DataDog/datadog-agent/comp/api/api/def"
23-
"github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
23+
"github.com/DataDog/datadog-agent/comp/api/authtoken/createandfetchimpl"
2424
"github.com/DataDog/datadog-agent/comp/collector/collector"
2525
"github.com/DataDog/datadog-agent/comp/core/autodiscovery"
2626
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/autodiscoveryimpl"
@@ -76,7 +76,7 @@ func getTestAPIServer(t *testing.T, params config.MockParams) testdeps {
7676
demultiplexerimpl.MockModule(),
7777
fx.Supply(optional.NewNoneOption[rcservice.Component]()),
7878
fx.Supply(optional.NewNoneOption[rcservicemrf.Component]()),
79-
fetchonlyimpl.MockModule(),
79+
createandfetchimpl.Module(),
8080
fx.Supply(context.Background()),
8181
taggermock.Module(),
8282
fx.Provide(func(mock taggermock.Mock) tagger.Component {
@@ -166,7 +166,6 @@ func TestStartBothServersWithObservability(t *testing.T) {
166166
req, err := http.NewRequest(http.MethodGet, url, nil)
167167
require.NoError(t, err)
168168

169-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authTokenValue))
170169
resp, err := util.GetClient(false).Do(req)
171170
require.NoError(t, err)
172171
defer resp.Body.Close()

comp/api/api/apiimpl/security.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@ package apiimpl
77

88
import (
99
"crypto/subtle"
10-
"crypto/tls"
11-
"crypto/x509"
12-
"encoding/pem"
1310
"errors"
14-
"fmt"
1511
"net/http"
16-
"runtime"
17-
"strings"
1812

19-
"github.com/DataDog/datadog-agent/pkg/api/security"
2013
"github.com/DataDog/datadog-agent/pkg/api/util"
2114
"github.com/DataDog/datadog-agent/pkg/util/log"
2215
)
@@ -44,44 +37,3 @@ func parseToken(token string) (interface{}, error) {
4437
// type.
4538
return struct{}{}, nil
4639
}
47-
48-
func buildSelfSignedKeyPair(additionalHostIdentities ...string) ([]byte, []byte) {
49-
hosts := []string{"127.0.0.1", "localhost", "::1"}
50-
hosts = append(hosts, additionalHostIdentities...)
51-
_, rootCertPEM, rootKey, err := security.GenerateRootCert(hosts, 2048)
52-
if err != nil {
53-
return nil, nil
54-
}
55-
56-
// PEM encode the private key
57-
rootKeyPEM := pem.EncodeToMemory(&pem.Block{
58-
Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(rootKey),
59-
})
60-
61-
// Create and return TLS private cert and key
62-
return rootCertPEM, rootKeyPEM
63-
}
64-
65-
func initializeTLS(additionalHostIdentities ...string) (*tls.Certificate, *x509.CertPool, error) {
66-
// print the caller to identify what is calling this function
67-
if _, file, line, ok := runtime.Caller(1); ok {
68-
log.Infof("[%s:%d] Initializing TLS certificates for hosts %v", file, line, strings.Join(additionalHostIdentities, ", "))
69-
}
70-
71-
cert, key := buildSelfSignedKeyPair(additionalHostIdentities...)
72-
if cert == nil {
73-
return nil, nil, errors.New("unable to generate certificate")
74-
}
75-
pair, err := tls.X509KeyPair(cert, key)
76-
if err != nil {
77-
return nil, nil, fmt.Errorf("unable to generate TLS key pair: %v", err)
78-
}
79-
80-
tlsCertPool := x509.NewCertPool()
81-
ok := tlsCertPool.AppendCertsFromPEM(cert)
82-
if !ok {
83-
return nil, nil, fmt.Errorf("unable to add new certificate to pool")
84-
}
85-
86-
return &pair, tlsCertPool, nil
87-
}

comp/api/api/apiimpl/server.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
stdLog "log"
1212
"net"
1313
"net/http"
14+
"strconv"
1415

1516
"github.com/DataDog/datadog-agent/comp/api/api/apiimpl/observability"
1617
"github.com/DataDog/datadog-agent/pkg/util/log"
@@ -47,43 +48,23 @@ func (server *apiServer) startServers() error {
4748
return fmt.Errorf("unable to get IPC address and port: %v", err)
4849
}
4950

50-
additionalHostIdentities := []string{apiAddr}
51-
52-
ipcServerHost, ipcServerHostPort, ipcServerEnabled := getIPCServerAddressPort()
53-
if ipcServerEnabled {
54-
additionalHostIdentities = append(additionalHostIdentities, ipcServerHost)
55-
}
56-
57-
tlsKeyPair, tlsCertPool, err := initializeTLS(additionalHostIdentities...)
58-
if err != nil {
59-
return fmt.Errorf("unable to initialize TLS: %v", err)
60-
}
61-
62-
// tls.Config is written to when serving, so it has to be cloned for each server
63-
tlsConfig := func() *tls.Config {
64-
return &tls.Config{
65-
Certificates: []tls.Certificate{*tlsKeyPair},
66-
NextProtos: []string{"h2"},
67-
MinVersion: tls.VersionTLS12,
68-
}
69-
}
70-
7151
tmf := observability.NewTelemetryMiddlewareFactory(server.telemetry)
7252

7353
// start the CMD server
7454
if err := server.startCMDServer(
7555
apiAddr,
76-
tlsConfig(),
77-
tlsCertPool,
7856
tmf,
7957
server.cfg,
8058
); err != nil {
8159
return fmt.Errorf("unable to start CMD API server: %v", err)
8260
}
8361

8462
// start the IPC server
85-
if ipcServerEnabled {
86-
if err := server.startIPCServer(ipcServerHostPort, tlsConfig(), tmf); err != nil {
63+
if ipcServerPort := server.cfg.GetInt("agent_ipc.port"); ipcServerPort > 0 {
64+
ipcServerHost := server.cfg.GetString("agent_ipc.host")
65+
ipcServerHostPort := net.JoinHostPort(ipcServerHost, strconv.Itoa(ipcServerPort))
66+
67+
if err := server.startIPCServer(ipcServerHostPort, tmf); err != nil {
8768
// if we fail to start the IPC server, we should stop the CMD server
8869
server.stopServers()
8970
return fmt.Errorf("unable to start IPC API server: %v", err)

comp/api/api/apiimpl/server_cmd.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package apiimpl
77

88
import (
99
"context"
10-
"crypto/tls"
11-
"crypto/x509"
1210
"fmt"
1311
"net/http"
1412
"time"
@@ -35,8 +33,6 @@ const cmdServerShortName string = "CMD"
3533

3634
func (server *apiServer) startCMDServer(
3735
cmdAddr string,
38-
tlsConfig *tls.Config,
39-
tlsCertPool *x509.CertPool,
4036
tmf observability.TelemetryMiddlewareFactory,
4137
cfg config.Component,
4238
) (err error) {
@@ -54,7 +50,7 @@ func (server *apiServer) startCMDServer(
5450
maxMessageSize := cfg.GetInt("cluster_agent.cluster_tagger.grpc_max_message_size")
5551

5652
opts := []grpc.ServerOption{
57-
grpc.Creds(credentials.NewClientTLSFromCert(tlsCertPool, cmdAddr)),
53+
grpc.Creds(credentials.NewTLS(server.authToken.GetTLSServerConfig())),
5854
grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(authInterceptor)),
5955
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(authInterceptor)),
6056
grpc.MaxRecvMsgSize(maxMessageSize),
@@ -79,11 +75,7 @@ func (server *apiServer) startCMDServer(
7975
autodiscovery: server.autoConfig,
8076
})
8177

82-
dcreds := credentials.NewTLS(&tls.Config{
83-
ServerName: cmdAddr,
84-
RootCAs: tlsCertPool,
85-
})
86-
dopts := []grpc.DialOption{grpc.WithTransportCredentials(dcreds)}
78+
dopts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(server.authToken.GetTLSClientConfig()))}
8779

8880
// starting grpc gateway
8981
ctx := context.Background()
@@ -133,7 +125,7 @@ func (server *apiServer) startCMDServer(
133125

134126
srv := grpcutil.NewMuxedGRPCServer(
135127
cmdAddr,
136-
tlsConfig,
128+
server.authToken.GetTLSServerConfig(),
137129
s,
138130
grpcutil.TimeoutHandlerFunc(cmdMuxHandler, time.Duration(pkgconfigsetup.Datadog().GetInt64("server_timeout"))*time.Second),
139131
)

comp/api/api/apiimpl/server_ipc.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package apiimpl
77

88
import (
9-
"crypto/tls"
109
"net/http"
1110
"time"
1211

@@ -18,7 +17,7 @@ import (
1817
const ipcServerName string = "IPC API Server"
1918
const ipcServerShortName string = "IPC"
2019

21-
func (server *apiServer) startIPCServer(ipcServerAddr string, tlsConfig *tls.Config, tmf observability.TelemetryMiddlewareFactory) (err error) {
20+
func (server *apiServer) startIPCServer(ipcServerAddr string, tmf observability.TelemetryMiddlewareFactory) (err error) {
2221
server.ipcListener, err = getListener(ipcServerAddr)
2322
if err != nil {
2423
return err
@@ -39,7 +38,7 @@ func (server *apiServer) startIPCServer(ipcServerAddr string, tlsConfig *tls.Con
3938
ipcServer := &http.Server{
4039
Addr: ipcServerAddr,
4140
Handler: http.TimeoutHandler(ipcMuxHandler, time.Duration(pkgconfigsetup.Datadog().GetInt64("server_timeout"))*time.Second, "timeout"),
42-
TLSConfig: tlsConfig,
41+
TLSConfig: server.authToken.GetTLSServerConfig(),
4342
}
4443

4544
startServer(server.ipcListener, ipcServer, ipcServerName)

0 commit comments

Comments
 (0)