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

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

224 lines
5.5 KiB
Go
Raw Normal View History

2021-02-23 19:06:39 -05:00
// 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 authmethodscmd
import (
"fmt"
"sort"
"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/authmethods"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/globals"
2021-02-23 19:06:39 -05:00
"github.com/hashicorp/boundary/internal/cmd/base"
)
func init() {
extraOidcSynopsisFunc = extraSynopsisFuncImpl
}
2021-02-23 19:06:39 -05:00
func (c *Command) extraHelpFunc(helpMap map[string]func() string) string {
var helpStr string
switch c.Func {
case "":
return base.WrapForHelpText([]string{
"Usage: boundary auth-methods [sub command] [options] [args]",
"",
" This command allows operations on Boundary auth method resources. Example:",
"",
" Read an auth method:",
"",
` $ boundary auth-methods read -id ampw_1234567890`,
"",
" Please see the auth-methods subcommand help for detailed usage information.",
})
case "create":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary auth-methods create [type] [sub command] [options] [args]",
"",
" This command allows create operations on Boundary auth method resources. Example:",
"",
" Create a password-type auth method:",
"",
` $ boundary auth-methods create password -name prodops -description "For ProdOps usage"`,
"",
" Please see the typed subcommand help for detailed usage information.",
})
case "update":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary auth-methods update [type] [sub command] [options] [args]",
"",
" This command allows update operations on Boundary auth method resources. Example:",
"",
" Update a password-type auth method:",
"",
` $ boundary auth-methods update password -id ampw_1234567890 -name devops -description "For DevOps usage"`,
"",
" Please see the typed subcommand help for detailed usage information.",
})
}
return helpStr + c.Flags().Help()
}
func extraSynopsisFuncImpl(c *OidcCommand) string {
switch c.Func {
case "change-state":
return "Change the active state of an auth method"
default:
return ""
}
}
2021-02-23 19:06:39 -05:00
func (c *Command) printListTable(items []*authmethods.AuthMethod) string {
if len(items) == 0 {
return "No auth methods found"
}
var output []string
output = []string{
"",
"Auth Method information:",
}
2021-05-04 19:57:35 -05:00
for i, item := range items {
2021-02-23 19:06:39 -05:00
if i > 0 {
output = append(output, "")
}
2021-05-04 19:57:35 -05:00
if item.Id != "" {
2021-02-23 19:06:39 -05:00
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" ID: %s", item.Id),
)
} else {
output = append(output,
fmt.Sprintf(" ID: %s", "(not available)"),
)
}
if c.FlagRecursive && item.ScopeId != "" {
output = append(output,
fmt.Sprintf(" Scope ID: %s", item.ScopeId),
2021-02-23 19:06:39 -05:00
)
}
2021-05-04 19:57:35 -05:00
if item.Version > 0 {
2021-02-23 19:06:39 -05:00
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" Version: %d", item.Version),
2021-02-23 19:06:39 -05:00
)
}
2021-05-04 19:57:35 -05:00
if item.Type != "" {
2021-02-23 19:06:39 -05:00
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" Type: %s", item.Type),
2021-02-23 19:06:39 -05:00
)
}
2021-05-04 19:57:35 -05:00
if item.Name != "" {
2021-02-23 19:06:39 -05:00
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" Name: %s", item.Name),
2021-02-23 19:06:39 -05:00
)
}
2021-05-04 19:57:35 -05:00
if item.Description != "" {
2021-02-23 19:06:39 -05:00
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" Description: %s", item.Description),
2021-02-23 19:06:39 -05:00
)
}
2021-05-04 19:57:35 -05:00
if item.IsPrimary {
output = append(output,
2021-05-04 19:57:35 -05:00
fmt.Sprintf(" Is Primary For Scope: %t", item.IsPrimary),
)
}
2021-05-04 19:57:35 -05:00
if len(item.AuthorizedActions) > 0 {
2021-02-23 19:06:39 -05:00
output = append(output,
" Authorized Actions:",
2021-05-04 19:57:35 -05:00
base.WrapSlice(6, item.AuthorizedActions),
2021-02-23 19:06:39 -05:00
)
}
}
return base.WrapForHelpText(output)
}
func printItemTable(item *authmethods.AuthMethod, resp *api.Response) string {
2022-10-25 18:29:38 -05:00
nonAttributeMap := map[string]any{}
2021-05-04 19:57:35 -05:00
if item.Id != "" {
nonAttributeMap["ID"] = item.Id
2021-02-23 19:06:39 -05:00
}
2021-05-04 19:57:35 -05:00
if item.Version != 0 {
nonAttributeMap["Version"] = item.Version
}
if item.Type != "" {
nonAttributeMap["Type"] = item.Type
}
if !item.CreatedTime.IsZero() {
nonAttributeMap["Created Time"] = item.CreatedTime.Local().Format(time.RFC1123)
}
if !item.UpdatedTime.IsZero() {
nonAttributeMap["Updated Time"] = item.UpdatedTime.Local().Format(time.RFC1123)
2021-02-23 19:06:39 -05:00
}
2021-05-04 19:57:35 -05:00
if item.Name != "" {
nonAttributeMap["Name"] = item.Name
}
if item.Description != "" {
nonAttributeMap["Description"] = item.Description
}
if resp != nil && resp.Map != nil {
if resp.Map[globals.IsPrimaryField] != nil {
2021-05-04 19:57:35 -05:00
nonAttributeMap["Is Primary For Scope"] = item.IsPrimary
}
2021-02-23 19:06:39 -05:00
}
2021-05-04 19:57:35 -05:00
maxLength := base.MaxAttributesLength(nonAttributeMap, item.Attributes, keySubstMap)
2021-02-23 19:06:39 -05:00
ret := []string{
"",
"Auth Method information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
}
2021-05-04 19:57:35 -05:00
if item.Scope != nil {
ret = append(ret,
"",
" Scope:",
base.ScopeInfoForOutput(item.Scope, maxLength),
)
}
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
)
}
2021-05-04 19:57:35 -05:00
if len(item.AuthorizedCollectionActions) > 0 {
keys := make([]string, 0, len(item.AuthorizedCollectionActions))
for k := range item.AuthorizedCollectionActions {
2021-02-23 19:06:39 -05:00
keys = append(keys, k)
}
sort.Strings(keys)
ret = append(ret,
"",
" Authorized Actions on Auth Method's Collections:",
)
for _, key := range keys {
ret = append(ret,
fmt.Sprintf(" %s:", key),
2021-05-04 19:57:35 -05:00
base.WrapSlice(6, item.AuthorizedCollectionActions[key]),
2021-02-23 19:06:39 -05:00
)
}
}
2021-05-04 19:57:35 -05:00
if len(item.Attributes) > 0 {
2021-02-23 19:06:39 -05:00
ret = append(ret,
"",
" Attributes:",
2021-05-04 19:57:35 -05:00
base.WrapMap(4, maxLength, item.Attributes),
2021-02-23 19:06:39 -05:00
)
}
return base.WrapForHelpText(ret)
}
var keySubstMap = map[string]string{
"min_login_name_length": "Minimum Login Name Length",
"min_password_length": "Minimum Password Length",
}