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