Skip to content

Commit

Permalink
tctl: add a --with-secrets flag to tctl tokens ls (#47545)
Browse files Browse the repository at this point in the history
Show the "safe name" for tokens by default, which is the name of
the token for non-sensitive join tokens, and a redacted version
of the name for shared secret tokens.

Note: for --format=json or --format=yaml we currently maintain
the original behavior (always show the raw token contents).
The tctl get tokens command has also not been touched - it
continues to return the raw token resource from the backend.

Updates #47254
  • Loading branch information
zmb3 authored Oct 14, 2024
1 parent f638ff4 commit 5d1304b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tool/tctl/common/token_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Use this token to add an MDM service to Teleport.
type TokensCommand struct {
config *servicecfg.Config

withSecrets bool

// format is the output format, e.g. text or json
format string

Expand Down Expand Up @@ -136,6 +138,7 @@ func (c *TokensCommand) Initialize(app *kingpin.Application, config *servicecfg.
// "tctl tokens ls"
c.tokenList = tokens.Command("ls", "List node and user invitation tokens.")
c.tokenList.Flag("format", "Output format, 'text', 'json' or 'yaml'").EnumVar(&c.format, formats...)
c.tokenList.Flag("with-secrets", "Do not redact join tokens").BoolVar(&c.withSecrets)

if c.stdout == nil {
c.stdout = os.Stdout
Expand Down Expand Up @@ -384,6 +387,11 @@ func (c *TokensCommand) List(ctx context.Context, client *authclient.Client) err
// Sort by expire time.
sort.Slice(tokens, func(i, j int) bool { return tokens[i].Expiry().Unix() < tokens[j].Expiry().Unix() })

nameFunc := (types.ProvisionToken).GetSafeName
if c.withSecrets {
nameFunc = (types.ProvisionToken).GetName
}

switch c.format {
case teleport.JSON:
err := utils.WriteJSONArray(c.stdout, tokens)
Expand All @@ -397,20 +405,20 @@ func (c *TokensCommand) List(ctx context.Context, client *authclient.Client) err
}
case teleport.Text:
for _, token := range tokens {
fmt.Fprintln(c.stdout, token.GetName())
fmt.Fprintln(c.stdout, nameFunc(token))
}
default:
tokensView := func() string {
table := asciitable.MakeTable([]string{"Token", "Type", "Labels", "Expiry Time (UTC)"})
now := time.Now()
for _, t := range tokens {
expiry := "never"
if !t.Expiry().IsZero() {
if !t.Expiry().IsZero() && t.Expiry().Unix() != 0 {
exptime := t.Expiry().Format(time.RFC822)
expdur := t.Expiry().Sub(now).Round(time.Second)
expiry = fmt.Sprintf("%s (%s)", exptime, expdur.String())
}
table.AddRow([]string{t.GetName(), t.GetRoles().String(), printMetadataLabels(t.GetMetadata().Labels), expiry})
table.AddRow([]string{nameFunc(t), t.GetRoles().String(), printMetadataLabels(t.GetMetadata().Labels), expiry})
}
return table.AsBuffer().String()
}
Expand Down

0 comments on commit 5d1304b

Please sign in to comment.