-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Adding tctl scoped tokens family of subcommands
#60035
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * 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 token | ||
|
|
||
| import ( | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/gravitational/trace" | ||
|
|
||
| joiningv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/scopes/joining/v1" | ||
| "github.com/gravitational/teleport/api/types" | ||
| ) | ||
|
|
||
| // Scoped wraps a [joiningv1.ScopedToken] such that it can be used to provision | ||
| // resources. | ||
| type Scoped struct { | ||
| token *joiningv1.ScopedToken | ||
| joinMethod types.JoinMethod | ||
| roles types.SystemRoles | ||
| } | ||
|
|
||
| // NewScoped returns the wrapped version of the given [joiningv1.ScopedToken]. | ||
| // It will return an error if the configured join method is not a valid | ||
| // [types.JoinMethod] or if any of the configured roles are not a valid | ||
| // [types.SystemRole]. The validated join method and roles are cached on the | ||
| // [Scoped] wrapper itself so they can be read without repeating validation. | ||
| func NewScoped(token *joiningv1.ScopedToken) (*Scoped, error) { | ||
| joinMethod := types.JoinMethod(token.GetSpec().GetJoinMethod()) | ||
| if err := types.ValidateJoinMethod(joinMethod); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| roles, err := types.NewTeleportRoles(token.GetSpec().GetRoles()) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| return &Scoped{token: token, joinMethod: joinMethod, roles: roles}, nil | ||
| } | ||
|
|
||
| // GetName returns the name of a [joiningv1.ScopedToken]. | ||
| func (s *Scoped) GetName() string { | ||
| return s.token.GetMetadata().GetName() | ||
| } | ||
|
|
||
| // GetJoinMethod returns the cached [types.JoinMethod] generated when the | ||
| // [joiningv1.ScopedToken] was wrapped. | ||
| func (s *Scoped) GetJoinMethod() types.JoinMethod { | ||
| return s.joinMethod | ||
| } | ||
|
|
||
| // GetRoles returns the cached [types.SystemRoles] generated when the | ||
| // [joiningv1.ScopedToken] was wrapped. | ||
| func (s *Scoped) GetRoles() types.SystemRoles { | ||
| return s.roles | ||
| } | ||
|
|
||
| // GetSafeName returns the name of the scoped token, sanitized appropriately | ||
| // for join methods where the name is secret. This should be used when logging | ||
| // the token name. | ||
| func (s *Scoped) GetSafeName() string { | ||
|
rosstimothy marked this conversation as resolved.
|
||
| return GetSafeScopedTokenName(s.token) | ||
| } | ||
|
|
||
| // Expiry returns the [time.Time] representing when the wrapped | ||
| // [joiningv1.ScopedToken] will expire. | ||
| func (s *Scoped) Expiry() time.Time { | ||
| return s.token.GetMetadata().GetExpires().AsTime() | ||
| } | ||
|
|
||
| // GetSafeScopedTokenName returns the name of the scoped token, sanitized | ||
| // appropriately for join methods where the name is secret. This should be used | ||
| // when logging the token name. | ||
| func GetSafeScopedTokenName(token *joiningv1.ScopedToken) string { | ||
| name := token.GetMetadata().GetName() | ||
| if types.JoinMethod(token.GetSpec().GetJoinMethod()) != types.JoinMethodToken { | ||
| return name | ||
| } | ||
|
|
||
| // If the token name is short, we just blank the whole thing. | ||
| if len(name) < 16 { | ||
| return strings.Repeat("*", len(name)) | ||
| } | ||
|
|
||
| // If the token name is longer, we can show the last 25% of it to help | ||
| // the operator identify it. | ||
| hiddenBefore := int(0.75 * float64(len(name))) | ||
| name = name[hiddenBefore:] | ||
| name = strings.Repeat("*", hiddenBefore) + name | ||
| return name | ||
| } | ||
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,60 @@ | ||
| /* | ||
| * 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" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/alecthomas/kingpin/v2" | ||
|
|
||
| "github.com/gravitational/teleport/lib/service/servicecfg" | ||
| commonclient "github.com/gravitational/teleport/tool/tctl/common/client" | ||
| tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config" | ||
| ) | ||
|
|
||
| // ScopedCommand implements scoped variants of tctl command groups, such as | ||
| // `tctl scoped tokens`. | ||
| type ScopedCommand struct { | ||
| config *servicecfg.Config | ||
| tokens *ScopedTokensCommand | ||
| Stdout io.Writer | ||
| } | ||
|
|
||
| // Initialize allows ScopedCommand to plug itself into the CLI parser | ||
| func (c *ScopedCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) { | ||
| c.config = config | ||
| scoped := app.Command("scoped", "Run a subcommand using scoped auth") | ||
|
|
||
| if c.Stdout == nil { | ||
| c.Stdout = os.Stdout | ||
| } | ||
|
|
||
| c.tokens = &ScopedTokensCommand{ | ||
| Stdout: c.Stdout, | ||
| } | ||
|
|
||
| c.tokens.Initialize(scoped, config) | ||
| } | ||
|
|
||
| // TryRun takes the CLI command as an argument (like "scoped tokens") and executes it. | ||
| func (c *ScopedCommand) TryRun(ctx context.Context, cmd string, clientFunc commonclient.InitFunc) (match bool, err error) { | ||
| return c.tokens.TryRun(ctx, cmd, clientFunc) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.