Skip to content
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

[v16] fix: prevent tctl edit overwriting static file config #48392

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions tool/tctl/common/edit_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ import (
// EditCommand implements the `tctl edit` command for modifying
// Teleport resources.
type EditCommand struct {
app *kingpin.Application
cmd *kingpin.CmdClause
config *servicecfg.Config
ref services.Ref
app *kingpin.Application
cmd *kingpin.CmdClause
config *servicecfg.Config
ref services.Ref
confirm bool

// Editor is used by tests to inject the editing mechanism
// so that different scenarios can be asserted.
Expand All @@ -61,9 +62,10 @@ func (e *EditCommand) Initialize(app *kingpin.Application, config *servicecfg.Co
e.cmd.Arg("resource type/resource name", `Resource to update
<resource type> Type of a resource [for example: rc]
<resource name> Resource name to update

Example:
$ tctl edit rc/remote`).SetValue(&e.ref)
e.cmd.Flag("confirm", "Confirm an unsafe or temporary resource update").Hidden().BoolVar(&e.confirm)
}

func (e *EditCommand) TryRun(ctx context.Context, cmd string, client *authclient.Client) (bool, error) {
Expand Down Expand Up @@ -115,6 +117,7 @@ func (e *EditCommand) editResource(ctx context.Context, client *authclient.Clien
filename: f.Name(),
force: true,
withSecrets: true,
confirm: e.confirm,
}
rc.Initialize(e.app, e.config)

Expand Down
33 changes: 31 additions & 2 deletions tool/tctl/common/resource_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,14 @@ func (rc *ResourceCommand) updateAuthPreference(ctx context.Context, client *aut
return trace.Wrap(err)
}

storedAuthPref, err := client.GetAuthPreference(ctx)
if err != nil {
return trace.Wrap(err)
}
if err := checkUpdateResourceWithOrigin(storedAuthPref, "cluster auth preference", rc.confirm); err != nil {
return trace.Wrap(err)
}

if _, err := client.UpdateAuthPreference(ctx, newAuthPref); err != nil {
return trace.Wrap(err)
}
Expand Down Expand Up @@ -751,6 +759,14 @@ func (rc *ResourceCommand) updateClusterNetworkingConfig(ctx context.Context, cl
return trace.Wrap(err)
}

storedNetConfig, err := client.GetClusterNetworkingConfig(ctx)
if err != nil {
return trace.Wrap(err)
}
if err := checkUpdateResourceWithOrigin(storedNetConfig, "cluster networking configuration", rc.confirm); err != nil {
return trace.Wrap(err)
}

if _, err := client.UpdateClusterNetworkingConfig(ctx, newNetConfig); err != nil {
return trace.Wrap(err)
}
Expand Down Expand Up @@ -809,6 +825,14 @@ func (rc *ResourceCommand) updateSessionRecordingConfig(ctx context.Context, cli
return trace.Wrap(err)
}

storedRecConfig, err := client.GetSessionRecordingConfig(ctx)
if err != nil {
return trace.Wrap(err)
}
if err := checkUpdateResourceWithOrigin(storedRecConfig, "session recording configuration", rc.confirm); err != nil {
return trace.Wrap(err)
}

if _, err := client.UpdateSessionRecordingConfig(ctx, newRecConfig); err != nil {
return trace.Wrap(err)
}
Expand Down Expand Up @@ -3165,10 +3189,15 @@ func checkCreateResourceWithOrigin(storedRes types.ResourceWithOrigin, resDesc s
if exists := (storedRes.Origin() != types.OriginDefaults); exists && !force {
return trace.AlreadyExists("non-default %s already exists", resDesc)
}
if managedByStatic := (storedRes.Origin() == types.OriginConfigFile); managedByStatic && !confirm {
return checkUpdateResourceWithOrigin(storedRes, resDesc, confirm)
}

func checkUpdateResourceWithOrigin(storedRes types.ResourceWithOrigin, resDesc string, confirm bool) error {
managedByStatic := storedRes.Origin() == types.OriginConfigFile
if managedByStatic && !confirm {
return trace.BadParameter(`The %s resource is managed by static configuration. We recommend removing configuration from teleport.yaml, restarting the servers and trying this command again.

If you would still like to proceed, re-run the command with both --force and --confirm flags.`, resDesc)
If you would still like to proceed, re-run the command with the --confirm flag.`, resDesc)
}
return nil
}
Expand Down
Loading