-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating an authtoken comp
- Loading branch information
Showing
17 changed files
with
239 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]() | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.