-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[ACTP] Add PAR identity rotation commands #52031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dd-gplassard
wants to merge
3
commits into
main
Choose a base branch
from
gabriel.plassard/rotate-identity-command-stacked
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
cmd/cluster-agent/subcommands/rotateparidentity/command.go
This file contains hidden or 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,108 @@ | ||
| // 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 2025-present Datadog, Inc. | ||
|
|
||
| //go:build !windows && kubeapiserver | ||
|
|
||
| // Package rotateparidentity implements 'cluster-agent rotate-par-identity'. | ||
| package rotateparidentity | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "go.uber.org/fx" | ||
|
|
||
| "github.com/DataDog/datadog-agent/cmd/cluster-agent/command" | ||
| "github.com/DataDog/datadog-agent/comp/core" | ||
| "github.com/DataDog/datadog-agent/comp/core/config" | ||
| "github.com/DataDog/datadog-agent/comp/core/hostname" | ||
| "github.com/DataDog/datadog-agent/comp/core/hostname/hostnameimpl" | ||
| log "github.com/DataDog/datadog-agent/comp/core/log/def" | ||
| pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" | ||
| parconfig "github.com/DataDog/datadog-agent/pkg/privateactionrunner/adapters/config" | ||
| "github.com/DataDog/datadog-agent/pkg/privateactionrunner/autoconnections" | ||
| "github.com/DataDog/datadog-agent/pkg/privateactionrunner/enrollment" | ||
| parutil "github.com/DataDog/datadog-agent/pkg/privateactionrunner/util" | ||
| "github.com/DataDog/datadog-agent/pkg/util/fxutil" | ||
| "github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver" | ||
| "github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver/common" | ||
| ) | ||
|
|
||
| // Commands returns a slice of subcommands for the 'cluster-agent' command. | ||
| func Commands(globalParams *command.GlobalParams) []*cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "rotate-par-identity", | ||
| Short: "Rotate the Private Action Runner identity for this cluster", | ||
| Long: `Generates fresh credentials and registers a new Private Action Runner identity. | ||
| The new identity is written to the shared Kubernetes secret. Running cluster agent | ||
| replicas will detect the change and reload their PAR connection automatically.`, | ||
| RunE: func(_ *cobra.Command, _ []string) error { | ||
| return fxutil.OneShot(run, | ||
| fx.Supply(core.BundleParams{ | ||
| ConfigParams: config.NewClusterAgentParams(globalParams.ConfFilePath, config.WithExtraConfFiles(globalParams.ExtraConfFilePath)), | ||
| LogParams: log.ForOneShot(command.LoggerName, command.DefaultLogLevel, true), | ||
| }), | ||
| core.Bundle(core.WithSecrets()), | ||
| hostnameimpl.Module(), | ||
| ) | ||
| }, | ||
| } | ||
| return []*cobra.Command{cmd} | ||
| } | ||
|
|
||
| func run(_ log.Component, cfg config.Component, hostnameComp hostname.Component) error { | ||
| ctx := context.Background() | ||
|
|
||
| if !cfg.GetBool(pkgconfigsetup.PAREnabled) { | ||
| return errors.New("private_action_runner.enabled is false - set it to true before rotating the identity") | ||
| } | ||
|
|
||
| // Match the running agent's hostname so ShouldReenroll keeps the rotated identity. | ||
| hostnameVal, err := hostnameComp.Get(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get hostname: %w", err) | ||
| } | ||
|
|
||
| // clustername.GetClusterID would call the DCA HTTP client (no cross-node TLS in one-shot). | ||
| apiClient, err := apiserver.GetAPIClient() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get Kubernetes client: %w", err) | ||
| } | ||
| orchClusterID, err := common.GetOrCreateClusterID(apiClient.Cl.CoreV1()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get cluster ID: %w", err) | ||
| } | ||
|
|
||
| agentIdentifier := &enrollment.AgentIdentifier{Hostname: hostnameVal, OrchClusterID: orchClusterID} | ||
|
|
||
| result, err := enrollment.Enroll(ctx, cfg, agentIdentifier) | ||
| if err != nil { | ||
| return fmt.Errorf("enrollment failed: %w", err) | ||
| } | ||
|
|
||
| if err := enrollment.RotateIdentity(ctx, cfg, result); err != nil { | ||
| return fmt.Errorf("failed to persist new identity: %w", err) | ||
| } | ||
|
|
||
| parCfg, err := parconfig.FromDDConfig(cfg) | ||
| if err != nil { | ||
| fmt.Printf("Identity rotated, but failed to load runner config for auto-connection: %v\n", err) | ||
| } else if urnParts, err := parutil.ParseRunnerURN(result.URN); err != nil { | ||
| fmt.Printf("Identity rotated, but failed to parse URN for auto-connection: %v\n", err) | ||
| } else { | ||
| autoconnections.CreateConnectionsIfEnabled( | ||
| ctx, cfg, parCfg, | ||
| cfg.GetString("api_key"), cfg.GetString("app_key"), urnParts.RunnerID, | ||
| result, autoconnections.NewBasicTagsProvider(), | ||
| ) | ||
| } | ||
|
|
||
| fmt.Printf("Identity successfully rotated. New URN: %s\n", result.URN) | ||
| fmt.Println("The new identity has been written to the Kubernetes secret.") | ||
| fmt.Println("Running cluster agent replicas will detect the change and reload automatically.") | ||
| return nil | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
cmd/cluster-agent/subcommands/rotateparidentity/command_test.go
This file contains hidden or 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,41 @@ | ||
| // 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 2025-present Datadog, Inc. | ||
|
|
||
| //go:build !windows && kubeapiserver && test | ||
|
|
||
| package rotateparidentity | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/DataDog/datadog-agent/cmd/cluster-agent/command" | ||
| coreconfig "github.com/DataDog/datadog-agent/comp/core/config" | ||
| hostnamemock "github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface/mock" | ||
| logmock "github.com/DataDog/datadog-agent/comp/core/log/mock" | ||
| "github.com/DataDog/datadog-agent/pkg/util/fxutil" | ||
| ) | ||
|
|
||
| func TestRotatePARIdentityCommand(t *testing.T) { | ||
| fxutil.TestOneShotSubcommand(t, | ||
| Commands(&command.GlobalParams{}), | ||
| []string{"rotate-par-identity"}, | ||
| run, | ||
| func() {}) | ||
| } | ||
|
|
||
| func TestRun_DisabledPAR(t *testing.T) { | ||
| cfg := coreconfig.NewMockWithOverrides(t, map[string]interface{}{ | ||
| "private_action_runner.enabled": false, | ||
| }) | ||
| hostnameComp, _ := hostnamemock.NewMock("test-host") | ||
|
|
||
| err := run(logmock.New(t), cfg, hostnameComp) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "private_action_runner.enabled is false") | ||
| } |
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
cmd/privateactionrunner/subcommands/rotateidentity/BUILD.bazel
This file contains hidden or 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,40 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "rotateidentity", | ||
| srcs = ["command.go"], | ||
| importpath = "github.com/DataDog/datadog-agent/cmd/privateactionrunner/subcommands/rotateidentity", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//cmd/privateactionrunner/command", | ||
| "//comp/core", | ||
| "//comp/core/config", | ||
| "//comp/core/hostname", | ||
| "//comp/core/hostname/hostnameimpl", | ||
| "//comp/core/log/def", | ||
| "//pkg/config/setup", | ||
| "//pkg/privateactionrunner/adapters/config", | ||
| "//pkg/privateactionrunner/enrollment", | ||
| "//pkg/privateactionrunner/util", | ||
| "//pkg/util/fxutil", | ||
| "@//pkg/privateactionrunner/autoconnections", | ||
| "@com_github_spf13_cobra//:cobra", | ||
| "@org_uber_go_fx//:fx", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "rotateidentity_test", | ||
| srcs = ["command_test.go"], | ||
| embed = [":rotateidentity"], | ||
| gotags = ["test"], | ||
| deps = [ | ||
| "//cmd/privateactionrunner/command", | ||
| "//comp/core/config", | ||
| "//comp/core/hostname/hostnameinterface/mock", | ||
| "//comp/core/log/mock", | ||
| "//pkg/util/fxutil", | ||
| "@com_github_stretchr_testify//assert", | ||
| "@com_github_stretchr_testify//require", | ||
| ], | ||
| ) |
91 changes: 91 additions & 0 deletions
91
cmd/privateactionrunner/subcommands/rotateidentity/command.go
This file contains hidden or 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,91 @@ | ||
| // 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 2025-present Datadog, Inc. | ||
|
|
||
| // Package rotateidentity implements the 'rotate-identity' subcommand for the private-action-runner. | ||
| package rotateidentity | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "go.uber.org/fx" | ||
|
|
||
| "github.com/DataDog/datadog-agent/cmd/privateactionrunner/command" | ||
| "github.com/DataDog/datadog-agent/comp/core" | ||
| "github.com/DataDog/datadog-agent/comp/core/config" | ||
| "github.com/DataDog/datadog-agent/comp/core/hostname" | ||
| "github.com/DataDog/datadog-agent/comp/core/hostname/hostnameimpl" | ||
| log "github.com/DataDog/datadog-agent/comp/core/log/def" | ||
| pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" | ||
| parconfig "github.com/DataDog/datadog-agent/pkg/privateactionrunner/adapters/config" | ||
| "github.com/DataDog/datadog-agent/pkg/privateactionrunner/autoconnections" | ||
| "github.com/DataDog/datadog-agent/pkg/privateactionrunner/enrollment" | ||
| parutil "github.com/DataDog/datadog-agent/pkg/privateactionrunner/util" | ||
| "github.com/DataDog/datadog-agent/pkg/util/fxutil" | ||
| ) | ||
|
|
||
| // Commands returns a slice of subcommands for the 'private-action-runner' command. | ||
| func Commands(globalParams *command.GlobalParams) []*cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "rotate-identity", | ||
| Short: "Rotate the Private Action Runner identity by performing a new enrollment", | ||
| Long: `Generates fresh credentials and registers a new Private Action Runner identity. | ||
| The new identity is persisted to the configured storage (file or Kubernetes secret). | ||
| Restart the Private Action Runner process to apply the new identity.`, | ||
| RunE: func(_ *cobra.Command, _ []string) error { | ||
| return fxutil.OneShot(run, | ||
| fx.Supply(core.BundleParams{ | ||
| ConfigParams: config.NewAgentParams(globalParams.ConfFilePath, config.WithExtraConfFiles(globalParams.ExtraConfFilePath)), | ||
| LogParams: log.ForOneShot(command.LoggerName, "info", true), | ||
| }), | ||
| core.Bundle(core.WithSecrets()), | ||
| hostnameimpl.Module(), | ||
| ) | ||
| }, | ||
| } | ||
| return []*cobra.Command{cmd} | ||
| } | ||
|
|
||
| func run(_ log.Component, cfg config.Component, hostnameComp hostname.Component) error { | ||
| ctx := context.Background() | ||
|
|
||
| if !cfg.GetBool(pkgconfigsetup.PAREnabled) { | ||
| return errors.New("private_action_runner.enabled is false - set it to true before rotating the identity") | ||
| } | ||
|
|
||
| // Match the running agent's hostname so ShouldReenroll keeps the rotated identity. | ||
| agentIdentifier, err := enrollment.GetAgentIdentifier(ctx, hostnameComp) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get agent identifier: %w", err) | ||
| } | ||
|
|
||
| result, err := enrollment.Enroll(ctx, cfg, agentIdentifier) | ||
| if err != nil { | ||
| return fmt.Errorf("enrollment failed: %w", err) | ||
| } | ||
|
|
||
| if err := enrollment.RotateIdentity(ctx, cfg, result); err != nil { | ||
| return fmt.Errorf("failed to persist new identity: %w", err) | ||
| } | ||
|
|
||
| parCfg, err := parconfig.FromDDConfig(cfg) | ||
| if err != nil { | ||
| fmt.Printf("Identity rotated, but failed to load runner config for auto-connection: %v\n", err) | ||
| } else if urnParts, err := parutil.ParseRunnerURN(result.URN); err != nil { | ||
| fmt.Printf("Identity rotated, but failed to parse URN for auto-connection: %v\n", err) | ||
| } else { | ||
| autoconnections.CreateConnectionsIfEnabled( | ||
| ctx, cfg, parCfg, | ||
| cfg.GetString("api_key"), cfg.GetString("app_key"), urnParts.RunnerID, | ||
| result, autoconnections.NewBasicTagsProvider(), | ||
| ) | ||
| } | ||
|
|
||
| fmt.Printf("Identity successfully rotated. New URN: %s\n", result.URN) | ||
| fmt.Println("Restart the Private Action Runner to apply the new identity.") | ||
| return nil | ||
| } |
41 changes: 41 additions & 0 deletions
41
cmd/privateactionrunner/subcommands/rotateidentity/command_test.go
This file contains hidden or 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,41 @@ | ||
| // 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 2025-present Datadog, Inc. | ||
|
|
||
| //go:build test | ||
|
|
||
| package rotateidentity | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/DataDog/datadog-agent/cmd/privateactionrunner/command" | ||
| coreconfig "github.com/DataDog/datadog-agent/comp/core/config" | ||
| hostnamemock "github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface/mock" | ||
| logmock "github.com/DataDog/datadog-agent/comp/core/log/mock" | ||
| "github.com/DataDog/datadog-agent/pkg/util/fxutil" | ||
| ) | ||
|
|
||
| func TestRotateIdentityCommand(t *testing.T) { | ||
| fxutil.TestOneShotSubcommand(t, | ||
| Commands(&command.GlobalParams{}), | ||
| []string{"rotate-identity"}, | ||
| run, | ||
| func() {}) | ||
| } | ||
|
|
||
| func TestRun_DisabledPAR(t *testing.T) { | ||
| cfg := coreconfig.NewMockWithOverrides(t, map[string]interface{}{ | ||
| "private_action_runner.enabled": false, | ||
| }) | ||
| hostnameComp, _ := hostnamemock.NewMock("test-host") | ||
|
|
||
| err := run(logmock.New(t), cfg, hostnameComp) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "private_action_runner.enabled is false") | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
private_action_runner.enabledis true butprivate_action_runner.identity_use_k8s_secretis false, this command still enrolls and callsenrollment.RotateIdentity, which falls back to writing the identity file instead of the shared Secret. The command then reports that the identity was written to Kubernetes and replicas will reload, but in a multi-replica cluster-agent deployment only the local filesystem is updated and the other replicas keep using the old credentials.Useful? React with 👍 / 👎.