Skip to content

Commit

Permalink
Creating an authtoken comp (#22968)
Browse files Browse the repository at this point in the history
Creating an authtoken comp
  • Loading branch information
hush-hush authored Feb 21, 2024
1 parent ebd420a commit 2c40962
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/agent/subcommands/flare/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/DataDog/datadog-agent/cmd/agent/common"
commonpath "github.com/DataDog/datadog-agent/cmd/agent/common/path"
"github.com/DataDog/datadog-agent/comp/aggregator/diagnosesendermanager/diagnosesendermanagerimpl"
authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/collector/collector"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
Expand Down Expand Up @@ -120,6 +121,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
hostimpl.Module(),
inventoryhostimpl.Module(),
resourcesimpl.Module(),
authtokenimpl.Module(),
// inventoryagent require a serializer. Since we're not actually sending the payload to
// the backend a nil will work.
fx.Provide(func() serializer.MetricSerializer {
Expand Down
2 changes: 2 additions & 0 deletions cmd/agent/subcommands/jmx/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/DataDog/datadog-agent/comp/aggregator/diagnosesendermanager"
"github.com/DataDog/datadog-agent/comp/aggregator/diagnosesendermanager/diagnosesendermanagerimpl"
internalAPI "github.com/DataDog/datadog-agent/comp/api/api"
authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/createandfetchimpl"
"github.com/DataDog/datadog-agent/comp/collector/collector"

"github.com/DataDog/datadog-agent/comp/api/api/apiimpl"
Expand Down Expand Up @@ -130,6 +131,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
}),
workloadmeta.Module(),
apiimpl.Module(),
authtokenimpl.Module(),
// TODO(components): this is a temporary hack as the StartServer() method of the API package was previously called with nil arguments
// This highlights the fact that the API Server created by JMX (through ExecJmx... function) should be different from the ones created
// in others commands such as run.
Expand Down
2 changes: 2 additions & 0 deletions cmd/agent/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/DataDog/datadog-agent/comp/aggregator/demultiplexer/demultiplexerimpl"
internalAPI "github.com/DataDog/datadog-agent/comp/api/api"
"github.com/DataDog/datadog-agent/comp/api/api/apiimpl"
authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/createandfetchimpl"
"github.com/DataDog/datadog-agent/comp/collector/collector"
"github.com/DataDog/datadog-agent/comp/collector/collector/collectorimpl"
"github.com/DataDog/datadog-agent/comp/core"
Expand Down Expand Up @@ -329,6 +330,7 @@ func getSharedFxOption() fx.Option {
processagentStatusImpl.Module(),
dogstatsdStatusimpl.Module(),
statusimpl.Module(),
authtokenimpl.Module(),
apiimpl.Module(),

dogstatsd.Bundle(),
Expand Down
2 changes: 2 additions & 0 deletions cmd/dogstatsd/subcommands/start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/DataDog/datadog-agent/cmd/agent/common"
"github.com/DataDog/datadog-agent/comp/aggregator/demultiplexer"
"github.com/DataDog/datadog-agent/comp/aggregator/demultiplexer/demultiplexerimpl"
authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameimpl"
"github.com/DataDog/datadog-agent/comp/core/log"
Expand Down Expand Up @@ -162,6 +163,7 @@ func RunDogstatsdFct(cliParams *CLIParams, defaultConfPath string, defaultLogFil
resourcesimpl.Module(),
hostimpl.Module(),
inventoryagentimpl.Module(),
authtokenimpl.Module(),
// sysprobeconfig is optionally required by inventoryagent
sysprobeconfig.NoneModule(),
inventoryhostimpl.Module(),
Expand Down
2 changes: 2 additions & 0 deletions cmd/systray/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/DataDog/datadog-agent/cmd/agent/common/path"
"github.com/DataDog/datadog-agent/comp/aggregator/diagnosesendermanager/diagnosesendermanagerimpl"
authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/collector/collector"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
Expand Down Expand Up @@ -104,6 +105,7 @@ func MakeCommand() *cobra.Command {
flare.Module(),
fx.Supply(optional.NewNoneOption[collector.Component]()),
diagnosesendermanagerimpl.Module(),
authtokenimpl.Module(),
// We need inventoryagent to fill the status page generated by the flare.
inventoryagentimpl.Module(),
// inventoryagent require a serializer. Since we're not actually sending the payload to
Expand Down
6 changes: 6 additions & 0 deletions comp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Package api implements the "api" bundle,

Package api implements the internal Agent API which exposes endpoints such as config, flare or status

### [comp/api/authtoken](https://pkg.go.dev/github.com/DataDog/datadog-agent/comp/api/authtoken)

Package authtoken implements the creation and access to the auth_token used to communicate between Agent processes.
This component offers two implementations: one to create and fetch the auth_token and another that doesn't create the
auth_token file but can fetch it it's available.

## [comp/apm/etwtracer](https://pkg.go.dev/github.com/DataDog/datadog-agent/comp/apm/etwtracer) (Component Bundle)

*Datadog Team*: windows-agent
Expand Down
4 changes: 4 additions & 0 deletions comp/api/api/apiimpl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/DataDog/datadog-agent/comp/aggregator/demultiplexer"
"github.com/DataDog/datadog-agent/comp/api/api"
"github.com/DataDog/datadog-agent/comp/api/authtoken"
"github.com/DataDog/datadog-agent/comp/collector/collector"
"github.com/DataDog/datadog-agent/comp/core/flare"
"github.com/DataDog/datadog-agent/comp/core/secrets"
Expand Down Expand Up @@ -58,6 +59,7 @@ type apiServer struct {
eventPlatformReceiver eventplatformreceiver.Component
rcService optional.Option[rcservice.Component]
rcServiceHA optional.Option[rcserviceha.Component]
authToken authtoken.Component
}

type dependencies struct {
Expand All @@ -78,6 +80,7 @@ type dependencies struct {
EventPlatformReceiver eventplatformreceiver.Component
RcService optional.Option[rcservice.Component]
RcServiceHA optional.Option[rcserviceha.Component]
AuthToken authtoken.Component
}

var _ api.Component = (*apiServer)(nil)
Expand All @@ -99,6 +102,7 @@ func newAPIServer(deps dependencies) api.Component {
eventPlatformReceiver: deps.EventPlatformReceiver,
rcService: deps.RcService,
rcServiceHA: deps.RcServiceHA,
authToken: deps.AuthToken,
}
}

Expand Down
8 changes: 1 addition & 7 deletions comp/api/api/apiimpl/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"net"
"net/http"

"github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver"

"github.com/cihub/seelog"

"github.com/DataDog/datadog-agent/comp/aggregator/demultiplexer"
Expand All @@ -26,6 +24,7 @@ import (
"github.com/DataDog/datadog-agent/comp/dogstatsd/replay"
dogstatsdServer "github.com/DataDog/datadog-agent/comp/dogstatsd/server"
dogstatsddebug "github.com/DataDog/datadog-agent/comp/dogstatsd/serverDebug"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver"
logsAgent "github.com/DataDog/datadog-agent/comp/logs/agent"
"github.com/DataDog/datadog-agent/comp/metadata/host"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent"
Expand All @@ -35,7 +34,6 @@ import (
"github.com/DataDog/datadog-agent/comp/remote-config/rcservice"
"github.com/DataDog/datadog-agent/comp/remote-config/rcserviceha"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
Expand Down Expand Up @@ -110,10 +108,6 @@ func StartServers(
MinVersion: tls.VersionTLS12,
}

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

// start the CMD server
if err := startCMDServer(
apiAddr,
Expand Down
32 changes: 32 additions & 0 deletions comp/api/authtoken/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

// Package authtoken implements the creation and access to the auth_token used to communicate between Agent processes.
// This component offers two implementations: one to create and fetch the auth_token and another that doesn't create the
// auth_token file but can fetch it it's available.
package authtoken

import (
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/optional"
"go.uber.org/fx"
)

// team: agent-shared-components

// Component is the component type.
type Component interface {
Get() string
}

// NoneModule return a None optional type for authtoken.Component.
//
// This helper allows code that needs a disabled Optional type for authtoken to get it. The helper is split from
// the implementation to avoid linking with the dependencies from sysprobeconfig.
func NoneModule() fxutil.Module {
return fxutil.Component(fx.Provide(func() optional.Option[Component] {
return optional.NewNoneOption[Component]()
}))
}
54 changes: 54 additions & 0 deletions comp/api/authtoken/createandfetchimpl/authtoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

// Package createandfetchimpl implements the creation and access to the auth_token used to communicate between Agent
// processes.
package createandfetchimpl

import (
"go.uber.org/fx"

"github.com/DataDog/datadog-agent/comp/api/authtoken"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)

// Module defines the fx options for this component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(newAuthToken),
fx.Provide(func(authToken authtoken.Component) optional.Option[authtoken.Component] {
return optional.NewOption[authtoken.Component](authToken)
}),
)
}

type authToken struct{}

var _ authtoken.Component = (*authToken)(nil)

type dependencies struct {
fx.In

Conf config.Component
Log log.Component
}

func newAuthToken(deps dependencies) (authtoken.Component, error) {
if err := util.CreateAndSetAuthToken(deps.Conf); err != nil {
deps.Log.Error("could not create auth_token: %s", err)
return nil, err
}

return &authToken{}, nil
}

// Get returns the session token
func (at *authToken) Get() string {
return util.GetAuthToken()
}
45 changes: 45 additions & 0 deletions comp/api/authtoken/createandfetchimpl/authtoken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package createandfetchimpl

import (
"os"
"path/filepath"
"testing"

"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
)

func TestGet(t *testing.T) {
dir := t.TempDir()
authPath := filepath.Join(dir, "auth_token")
overrides := map[string]any{
"auth_token_file_path": authPath,
}

comp, err := newAuthToken(
fxutil.Test[dependencies](
t,
logimpl.MockModule(),
config.MockModule(),
fx.Replace(config.MockParams{Overrides: overrides}),
),
)
require.NoError(t, err)

data, err := os.ReadFile(authPath)
require.NoError(t, err)

assert.Equal(t, string(data), comp.Get())
assert.Equal(t, util.GetAuthToken(), comp.Get())
}
67 changes: 67 additions & 0 deletions comp/api/authtoken/fetchonlyimpl/authtoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

// Package fetchonlyimpl implements the access to the auth_token used to communicate between Agent
// processes but does not create it.
package fetchonlyimpl

import (
"go.uber.org/fx"

"github.com/DataDog/datadog-agent/comp/api/authtoken"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)

// Module defines the fx options for this component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(newAuthToken),
fx.Provide(func(authToken authtoken.Component) optional.Option[authtoken.Component] {
return optional.NewOption[authtoken.Component](authToken)
}),
)
}

type authToken struct {
log log.Component
conf config.Component

tokenLoaded bool
}

var _ authtoken.Component = (*authToken)(nil)

type dependencies struct {
fx.In

Log log.Component
Conf config.Component
}

func newAuthToken(deps dependencies) authtoken.Component {
return &authToken{
log: deps.Log,
conf: deps.Conf,
}
}

// Get returns the session token
func (at *authToken) Get() string {
if !at.tokenLoaded {
// We try to load the auth_token until we succeed since it might be created at some point by another
// process.
if err := util.SetAuthToken(at.conf); err != nil {
at.log.Debugf("could not load auth_token: %s", err)
return ""
}
at.tokenLoaded = true
}

return util.GetAuthToken()
}
2 changes: 2 additions & 0 deletions comp/metadata/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"go.uber.org/fx"

authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/collector/collector/collectorimpl"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/logs/agent"
Expand All @@ -27,6 +28,7 @@ func TestBundleDependencies(t *testing.T) {
fx.Provide(func() optional.Option[agent.Component] {
return optional.NewNoneOption[agent.Component]()
}),
authtokenimpl.Module(),
)
}

Expand Down
Loading

0 comments on commit 2c40962

Please sign in to comment.