-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Adding session recording encryption key rotation to tctl
#57780
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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
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
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
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,207 @@ | ||
| // Teleport | ||
| // Copyright (C) 2025 Gravitational, Inc. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package common | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/alecthomas/kingpin/v2" | ||
| "github.com/gravitational/trace" | ||
|
|
||
| "github.com/gravitational/teleport" | ||
| recordingencryptionv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/recordingencryption/v1" | ||
| "github.com/gravitational/teleport/lib/asciitable" | ||
| "github.com/gravitational/teleport/lib/auth/authclient" | ||
| "github.com/gravitational/teleport/lib/defaults" | ||
| "github.com/gravitational/teleport/lib/utils" | ||
| commonclient "github.com/gravitational/teleport/tool/tctl/common/client" | ||
| ) | ||
|
|
||
| type recordingsEncryptionCommand struct { | ||
| // cmd implements the "tctl recordings encryptino" parent command | ||
| cmd *kingpin.CmdClause | ||
|
|
||
| // rotateCmd implements the "tctl recordings encryption rotate" subcommand. | ||
| rotateCmd *kingpin.CmdClause | ||
|
|
||
| // statusCmd implements the "tctl recordings encryption status" subcommand. | ||
| statusCmd *kingpin.CmdClause | ||
|
|
||
| // completeCmd implements the "tctl recordings encryption complete" subcommand. | ||
| completeCmd *kingpin.CmdClause | ||
|
|
||
| // rollbackCmd implements the "tctl recordings encryption rollback" subcommand. | ||
| rollbackCmd *kingpin.CmdClause | ||
|
|
||
| // format is the output format of statusCmd (text, json, or yaml) | ||
| format string | ||
|
|
||
| // stdout allows for redirecting command output. Useful for tests. | ||
| stdout io.Writer | ||
| } | ||
|
|
||
| // Initialize allows recordingsEncryptionCommand to plug itself into the CLI parser. | ||
| func (c *recordingsEncryptionCommand) Initialize(recordingsCmd *kingpin.CmdClause, stdout io.Writer) { | ||
| c.cmd = recordingsCmd.Command("encryption", "Manage encryption properties of session recordings.") | ||
|
|
||
| c.rotateCmd = c.cmd.Command("rotate", "Rotate encryption keys used for encrypting session recordings.") | ||
| c.statusCmd = c.cmd.Command("status", "Show current rotation status.") | ||
| c.statusCmd.Flag("format", defaults.FormatFlagDescription(defaults.DefaultFormats...)+". Defaults to 'text'.").Default(teleport.Text).StringVar(&c.format) | ||
| c.completeCmd = c.cmd.Command("complete-rotation", "Completes an in-progress encryption key rotation.") | ||
| c.rollbackCmd = c.cmd.Command("rollback-rotation", "Rolls back an in-progress encryption key rotation.") | ||
| if stdout == nil { | ||
| c.stdout = os.Stdout | ||
| } | ||
| } | ||
|
|
||
| // TryRun attempts to run subcommands like "recordings encryption rotate". | ||
| func (c *recordingsEncryptionCommand) TryRun(ctx context.Context, cmd string, clientFunc commonclient.InitFunc) (match bool, err error) { | ||
| var commandFunc func(ctx context.Context, client *authclient.Client) error | ||
| switch cmd { | ||
| case c.rotateCmd.FullCommand(): | ||
| commandFunc = c.Rotate | ||
| case c.statusCmd.FullCommand(): | ||
| commandFunc = c.Status | ||
| case c.completeCmd.FullCommand(): | ||
| commandFunc = c.Complete | ||
| case c.rollbackCmd.FullCommand(): | ||
| commandFunc = c.Rollback | ||
| default: | ||
| return false, nil | ||
| } | ||
| client, closeFn, err := clientFunc(ctx) | ||
| if err != nil { | ||
| return false, trace.Wrap(err) | ||
| } | ||
| err = commandFunc(ctx, client) | ||
| closeFn(ctx) | ||
|
|
||
| return true, trace.Wrap(err) | ||
| } | ||
|
|
||
| // Rotate initiates a key rotation. It should fail if a key rotation is already | ||
| // in progress. | ||
| func (c *recordingsEncryptionCommand) Rotate(ctx context.Context, tc *authclient.Client) error { | ||
| client := tc.RecordingEncryptionServiceClient() | ||
| if _, err := client.RotateKey(ctx, &recordingencryptionv1.RotateKeyRequest{}); err != nil { | ||
| return trace.Errorf("rotating key encryption keys: %v", err) | ||
| } | ||
| fmt.Fprintln(c.stdout, "Rotation started") | ||
| return nil | ||
| } | ||
|
|
||
| // Complete an in progress key rotation. It should fail if any key is marked | ||
| // 'inaccessible'. | ||
| func (c *recordingsEncryptionCommand) Complete(ctx context.Context, tc *authclient.Client) error { | ||
| client := tc.RecordingEncryptionServiceClient() | ||
| if _, err := client.CompleteRotation(ctx, &recordingencryptionv1.CompleteRotationRequest{}); err != nil { | ||
| return trace.Errorf("completing encryption key rotation: %v", err) | ||
| } | ||
|
|
||
| fmt.Fprintln(c.stdout, "Rotation completed") | ||
| return nil | ||
| } | ||
|
|
||
| // Rollback an in progress key rotation. | ||
| func (c *recordingsEncryptionCommand) Rollback(ctx context.Context, tc *authclient.Client) error { | ||
| client := tc.RecordingEncryptionServiceClient() | ||
| if _, err := client.RollbackRotation(ctx, &recordingencryptionv1.RollbackRotationRequest{}); err != nil { | ||
| return trace.Errorf("rolling back encryption key rotation: %v", err) | ||
| } | ||
|
|
||
| fmt.Fprintln(c.stdout, "Rotation rollback successful") | ||
| return nil | ||
| } | ||
|
|
||
| // Status displays the current rotation status of the active encryption keys. | ||
| func (c *recordingsEncryptionCommand) Status(ctx context.Context, tc *authclient.Client) error { | ||
| client := tc.RecordingEncryptionServiceClient() | ||
| res, err := client.GetRotationState(ctx, &recordingencryptionv1.GetRotationStateRequest{}) | ||
| if err != nil { | ||
| return trace.Errorf("fetching encryption key status: %v", err) | ||
| } | ||
|
|
||
| switch c.format { | ||
| case teleport.Text, "": | ||
| return trace.Wrap(c.writeStatusText(c.stdout, res.GetKeyPairStates())) | ||
| case teleport.YAML: | ||
| return trace.Wrap(c.writeStatusYAML(c.stdout, res.GetKeyPairStates())) | ||
| case teleport.JSON: | ||
| return trace.Wrap(c.writeStatusJSON(c.stdout, res.GetKeyPairStates())) | ||
| } | ||
|
|
||
| return trace.Wrap(err, "writing encryption key status") | ||
| } | ||
|
|
||
| func (c *recordingsEncryptionCommand) writeStatusJSON(w io.Writer, keyStates []*recordingencryptionv1.FingerprintWithState) error { | ||
| data, err := json.MarshalIndent(keyStates, "", " ") | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| _, err = w.Write(data) | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| func (c *recordingsEncryptionCommand) writeStatusYAML(w io.Writer, keyStates []*recordingencryptionv1.FingerprintWithState) error { | ||
| return trace.Wrap(utils.WriteYAML(w, keyStates)) | ||
| } | ||
|
|
||
| func (c *recordingsEncryptionCommand) writeStatusText(w io.Writer, keyStates []*recordingencryptionv1.FingerprintWithState) error { | ||
| rotationState := recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_UNSPECIFIED | ||
| t := asciitable.MakeTable([]string{"Key Pair Fingerprint", "State"}) | ||
| for _, pair := range keyStates { | ||
| if pair.State == recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_INACCESSIBLE { | ||
| rotationState = pair.State | ||
| } | ||
|
|
||
| if pair.State == recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_ROTATING { | ||
| if rotationState != recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_INACCESSIBLE { | ||
| rotationState = pair.State | ||
| } | ||
| } | ||
|
|
||
| t.AddRow([]string{pair.Fingerprint, c.getFriendlyStatusString(pair.State)}) | ||
| } | ||
|
|
||
| switch rotationState { | ||
| case recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_INACCESSIBLE: | ||
| fmt.Fprintln(w, "Rotation failed due to inaccessible key") | ||
| case recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_ROTATING: | ||
| fmt.Fprintln(w, "Rotation in progress") | ||
| } | ||
|
|
||
| _, err := t.AsBuffer().WriteTo(w) | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| func (c *recordingsEncryptionCommand) getFriendlyStatusString(state recordingencryptionv1.KeyPairState) string { | ||
| switch state { | ||
| case recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_ROTATING: | ||
| return "rotating" | ||
| case recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_ACTIVE: | ||
| return "active" | ||
| case recordingencryptionv1.KeyPairState_KEY_PAIR_STATE_INACCESSIBLE: | ||
| return "inaccessible" | ||
| default: | ||
| return "unknown" | ||
| } | ||
| } |
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.
This seems like it could cause problems if we forget to update this in response to any changes made to the types.SessionRecordingEncryptionConfig resource. Why is this needed?
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.
Fields with underscores were broken before I added this, so
manual_key_managementandproxy_checks_host_keys. If there's a way to get the yaml parser to use thejsontags during unmarshaling, that would definitely be betterThere 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.
@atburke any thoughts here regarding the yaml parser and json tags with
_?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.
The yaml parser isn't seeing the json tags at all and is assuming camel case instead of snake case. I don't think any of our current yaml packages can handle mixed yaml and json tags within the same object; we'll have to either do what you're doing here or get goccy just for ReadConfig.