Skip to content

Commit

Permalink
Reapply "[ASCII-2586] Migrating SecurityAgent to use IPC cert" (#32313)
Browse files Browse the repository at this point in the history
  • Loading branch information
misteriaud authored Dec 17, 2024
1 parent 6fb76d5 commit 007c8aa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
48 changes: 10 additions & 38 deletions cmd/security-agent/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ package api

import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
stdLog "log"
"net"
"net/http"
Expand All @@ -23,10 +20,10 @@ import (
"github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/cmd/security-agent/api/agent"
"github.com/DataDog/datadog-agent/comp/api/authtoken"
"github.com/DataDog/datadog-agent/comp/core/settings"
"github.com/DataDog/datadog-agent/comp/core/status"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/api/security"
"github.com/DataDog/datadog-agent/pkg/api/util"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand All @@ -35,19 +32,21 @@ import (

// Server implements security agent API server
type Server struct {
listener net.Listener
agent *agent.Agent
listener net.Listener
agent *agent.Agent
tlsConfig *tls.Config
}

// NewServer creates a new Server instance
func NewServer(statusComponent status.Component, settings settings.Component, wmeta workloadmeta.Component) (*Server, error) {
func NewServer(statusComponent status.Component, settings settings.Component, wmeta workloadmeta.Component, at authtoken.Component) (*Server, error) {
listener, err := newListener()
if err != nil {
return nil, err
}
return &Server{
listener: listener,
agent: agent.NewAgent(statusComponent, settings, wmeta),
listener: listener,
agent: agent.NewAgent(statusComponent, settings, wmeta),
tlsConfig: at.GetTLSServerConfig(),
}, nil
}

Expand All @@ -62,43 +61,16 @@ func (s *Server) Start() error {
// Validate token for every request
r.Use(validateToken)

err := util.CreateAndSetAuthToken(pkgconfigsetup.Datadog())
if err != nil {
return err
}

hosts := []string{"127.0.0.1", "localhost"}
_, rootCertPEM, rootKey, err := security.GenerateRootCert(hosts, 2048)
if err != nil {
return fmt.Errorf("unable to start TLS server")
}

// PEM encode the private key
rootKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(rootKey),
})

// Create a TLS cert using the private key and certificate
rootTLSCert, err := tls.X509KeyPair(rootCertPEM, rootKeyPEM)
if err != nil {
return fmt.Errorf("invalid key pair: %v", err)
}

tlsConfig := tls.Config{
Certificates: []tls.Certificate{rootTLSCert},
MinVersion: tls.VersionTLS13,
}

// Use a stack depth of 4 on top of the default one to get a relevant filename in the stdlib
logWriter, _ := pkglogsetup.NewLogWriter(4, log.ErrorLvl)

srv := &http.Server{
Handler: r,
ErrorLog: stdLog.New(logWriter, "Error from the agent http API server: ", 0), // log errors to seelog,
TLSConfig: &tlsConfig,
TLSConfig: s.tlsConfig,
WriteTimeout: pkgconfigsetup.Datadog().GetDuration("server_timeout") * time.Second,
}
tlsListener := tls.NewListener(s.listener, &tlsConfig)
tlsListener := tls.NewListener(s.listener, s.tlsConfig)

go srv.Serve(tlsListener) //nolint:errcheck
return nil
Expand Down
6 changes: 4 additions & 2 deletions cmd/security-agent/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/DataDog/datadog-agent/cmd/security-agent/subcommands/start"
"github.com/DataDog/datadog-agent/comp/agent/autoexit"
"github.com/DataDog/datadog-agent/comp/agent/autoexit/autoexitimpl"
"github.com/DataDog/datadog-agent/comp/api/authtoken"
"github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
Expand Down Expand Up @@ -91,10 +92,11 @@ func (s *service) Run(svcctx context.Context) error {
params := &cliParams{}
err := fxutil.OneShot(
func(log log.Component, config config.Component, _ secrets.Component, _ statsd.Component, _ sysprobeconfig.Component,
telemetry telemetry.Component, _ workloadmeta.Component, _ *cliParams, statusComponent status.Component, _ autoexit.Component, settings settings.Component, wmeta workloadmeta.Component) error {
telemetry telemetry.Component, _ workloadmeta.Component, _ *cliParams, statusComponent status.Component, _ autoexit.Component,
settings settings.Component, wmeta workloadmeta.Component, at authtoken.Component) error {
defer start.StopAgent(log)

err := start.RunAgent(log, config, telemetry, statusComponent, settings, wmeta)
err := start.RunAgent(log, config, telemetry, statusComponent, settings, wmeta, at)
if err != nil {
if errors.Is(err, start.ErrAllComponentsDisabled) {
// If all components are disabled, we should exit cleanly
Expand Down
9 changes: 5 additions & 4 deletions cmd/security-agent/subcommands/start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/DataDog/datadog-agent/cmd/security-agent/subcommands/runtime"
"github.com/DataDog/datadog-agent/comp/agent/autoexit"
"github.com/DataDog/datadog-agent/comp/agent/autoexit/autoexitimpl"
"github.com/DataDog/datadog-agent/comp/api/authtoken"
"github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
Expand Down Expand Up @@ -201,10 +202,10 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
// TODO(components): note how workloadmeta is passed anonymously, it is still required as it is used
// as a global. This should eventually be fixed and all workloadmeta interactions should be via the
// injected instance.
func start(log log.Component, config config.Component, _ secrets.Component, _ statsd.Component, _ sysprobeconfig.Component, telemetry telemetry.Component, statusComponent status.Component, _ pid.Component, _ autoexit.Component, settings settings.Component, wmeta workloadmeta.Component) error {
func start(log log.Component, config config.Component, _ secrets.Component, _ statsd.Component, _ sysprobeconfig.Component, telemetry telemetry.Component, statusComponent status.Component, _ pid.Component, _ autoexit.Component, settings settings.Component, wmeta workloadmeta.Component, at authtoken.Component) error {
defer StopAgent(log)

err := RunAgent(log, config, telemetry, statusComponent, settings, wmeta)
err := RunAgent(log, config, telemetry, statusComponent, settings, wmeta, at)
if errors.Is(err, ErrAllComponentsDisabled) || errors.Is(err, errNoAPIKeyConfigured) {
return nil
}
Expand Down Expand Up @@ -256,7 +257,7 @@ var ErrAllComponentsDisabled = errors.New("all security-agent component are disa
var errNoAPIKeyConfigured = errors.New("no API key configured")

// RunAgent initialized resources and starts API server
func RunAgent(log log.Component, config config.Component, telemetry telemetry.Component, statusComponent status.Component, settings settings.Component, wmeta workloadmeta.Component) (err error) {
func RunAgent(log log.Component, config config.Component, telemetry telemetry.Component, statusComponent status.Component, settings settings.Component, wmeta workloadmeta.Component, at authtoken.Component) (err error) {
if err := coredump.Setup(config); err != nil {
log.Warnf("Can't setup core dumps: %v, core dumps might not be available after a crash", err)
}
Expand Down Expand Up @@ -299,7 +300,7 @@ func RunAgent(log log.Component, config config.Component, telemetry telemetry.Co
}
}()

srv, err = api.NewServer(statusComponent, settings, wmeta)
srv, err = api.NewServer(statusComponent, settings, wmeta, at)
if err != nil {
return log.Errorf("Error while creating api server, exiting: %v", err)
}
Expand Down

0 comments on commit 007c8aa

Please sign in to comment.