Files
threshold/internal/cmd/commands/authtokenscmd/funcs.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

158 lines
4.1 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: BUSL-1.1
2021-02-23 19:06:39 -05:00
package authtokenscmd
import (
"errors"
2021-02-23 19:06:39 -05:00
"fmt"
"time"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/api"
2021-02-23 19:06:39 -05:00
"github.com/hashicorp/boundary/api/authtokens"
"github.com/hashicorp/boundary/internal/cmd/base"
2021-07-14 17:37:40 -07:00
"github.com/hashicorp/go-secure-stdlib/strutil"
2021-02-23 19:06:39 -05:00
)
const selfFlag = "self"
func init() {
extraFlagsHandlingFunc = extraFlagsHandlingFuncImpl
}
func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, _ *[]authtokens.Option) bool {
if c.Func != "delete" && c.Func != "read" {
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return false
}
return true
}
if c.FlagId == "" {
fmt.Printf("No ID provided; %s the stored token? (Pass an ID of %q to suppress this question.) y/n: ", c.Func, selfFlag)
var yesNo string
fmt.Scanf("%s", &yesNo)
switch yesNo {
case "y", "Y":
c.FlagId = selfFlag
default:
c.PrintCliError(errors.New(`"Y" or "y" not provided, refusing to continue`))
return false
}
}
if c.FlagId == selfFlag {
// We should have already read this and have it cached
client, err := c.Client()
if err != nil {
c.PrintCliError(fmt.Errorf("Error reading cached API client: %w", err))
return false
}
c.FlagId, err = base.TokenIdFromToken(client.Token())
if err != nil {
c.PrintCliError(err)
return false
}
}
return true
}
2021-02-23 19:06:39 -05:00
func (c *Command) printListTable(items []*authtokens.AuthToken) string {
if len(items) == 0 {
return "No auth tokens found"
}
var output []string
output = []string{
"",
"Auth Token information:",
}
for i, t := range items {
if i > 0 {
output = append(output, "")
}
2021-05-04 19:57:35 -05:00
if t.Id != "" {
2021-02-23 19:06:39 -05:00
output = append(output,
fmt.Sprintf(" ID: %s", t.Id),
)
}
2021-05-04 19:57:35 -05:00
if c.FlagRecursive && t.ScopeId != "" {
2021-02-23 19:06:39 -05:00
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" Scope ID: %s", t.ScopeId),
2021-02-23 19:06:39 -05:00
)
}
2021-05-04 19:57:35 -05:00
if !t.ApproximateLastUsedTime.IsZero() {
2021-02-23 19:06:39 -05:00
output = append(output,
fmt.Sprintf(" Approximate Last Used Time: %s", t.ApproximateLastUsedTime.Local().Format(time.RFC1123)),
2021-05-04 19:57:35 -05:00
)
}
if t.AuthMethodId != "" {
output = append(output,
2021-02-23 19:06:39 -05:00
fmt.Sprintf(" Auth Method ID: %s", t.AuthMethodId),
2021-05-04 19:57:35 -05:00
)
}
if !t.CreatedTime.IsZero() {
output = append(output,
2021-02-23 19:06:39 -05:00
fmt.Sprintf(" Created Time: %s", t.CreatedTime.Local().Format(time.RFC1123)),
2021-05-04 19:57:35 -05:00
)
}
if !t.ExpirationTime.IsZero() {
output = append(output,
2021-02-23 19:06:39 -05:00
fmt.Sprintf(" Expiration Time: %s", t.ExpirationTime.Local().Format(time.RFC1123)),
2021-05-04 19:57:35 -05:00
)
}
if !t.UpdatedTime.IsZero() {
output = append(output,
2021-02-23 19:06:39 -05:00
fmt.Sprintf(" Updated Time: %s", t.UpdatedTime.Local().Format(time.RFC1123)),
2021-05-04 19:57:35 -05:00
)
}
if t.UserId != "" {
output = append(output,
2021-02-23 19:06:39 -05:00
fmt.Sprintf(" User ID: %s", t.UserId),
)
}
if len(t.AuthorizedActions) > 0 {
output = append(output,
" Authorized Actions:",
base.WrapSlice(6, t.AuthorizedActions),
)
}
}
return base.WrapForHelpText(output)
}
func printItemTable(item *authtokens.AuthToken, resp *api.Response) string {
2022-10-25 18:29:38 -05:00
nonAttributeMap := map[string]any{
2021-05-04 19:57:35 -05:00
"ID": item.Id,
"Auth Method ID": item.AuthMethodId,
"User ID": item.UserId,
"Created Time": item.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": item.UpdatedTime.Local().Format(time.RFC1123),
"Expiration Time": item.ExpirationTime.Local().Format(time.RFC1123),
"Approximate Last Used Time": item.ApproximateLastUsedTime.Local().Format(time.RFC1123),
2021-02-23 19:06:39 -05:00
}
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
ret := []string{
"",
"Auth Token information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
"",
" Scope:",
2021-05-04 19:57:35 -05:00
base.ScopeInfoForOutput(item.Scope, maxLength),
2021-02-23 19:06:39 -05:00
}
2021-05-04 19:57:35 -05:00
if len(item.AuthorizedActions) > 0 {
2021-02-23 19:06:39 -05:00
ret = append(ret,
"",
" Authorized Actions:",
2021-05-04 19:57:35 -05:00
base.WrapSlice(4, item.AuthorizedActions),
2021-02-23 19:06:39 -05:00
)
}
return base.WrapForHelpText(ret)
}