From 166aee71f3390af6c43d096539b07fb417b4f41c Mon Sep 17 00:00:00 2001 From: James Rasell Date: Mon, 30 Jan 2023 11:44:26 +0100 Subject: [PATCH] cli: separate auth method config output for easier reading. (#15892) --- command/acl_auth_method.go | 27 ++++++++++--------- command/acl_auth_method_create.go | 2 +- command/acl_auth_method_info.go | 3 +-- command/acl_auth_method_update.go | 2 +- .../docs/commands/acl/auth-method/create.mdx | 20 +++++++------- .../docs/commands/acl/auth-method/info.mdx | 17 +++++++----- .../docs/commands/acl/auth-method/update.mdx | 20 +++++++------- 7 files changed, 50 insertions(+), 41 deletions(-) diff --git a/command/acl_auth_method.go b/command/acl_auth_method.go index a5b0a24e6..cb501de7b 100644 --- a/command/acl_auth_method.go +++ b/command/acl_auth_method.go @@ -57,6 +57,16 @@ func (a *ACLAuthMethodCommand) Name() string { return "acl auth-method" } // Run satisfies the cli.Command Run function. func (a *ACLAuthMethodCommand) Run(_ []string) int { return cli.RunResultHelp } +// outputAuthMethod can be used to output the auth method to the UI within the +// passed meta object. +func outputAuthMethod(meta Meta, authMethod *api.ACLAuthMethod) { + meta.Ui.Output(formatAuthMethod(authMethod)) + if authMethod.Config != nil { + meta.Ui.Output(meta.Colorize().Color("\n[bold]Auth Method Config[reset]\n")) + meta.Ui.Output(formatAuthMethodConfig(authMethod.Config)) + } +} + // formatAuthMethod formats and converts the ACL auth method API object into a // string KV representation suitable for console output. func formatAuthMethod(authMethod *api.ACLAuthMethod) string { @@ -66,22 +76,14 @@ func formatAuthMethod(authMethod *api.ACLAuthMethod) string { fmt.Sprintf("Locality|%s", authMethod.TokenLocality), fmt.Sprintf("MaxTokenTTL|%s", authMethod.MaxTokenTTL.String()), fmt.Sprintf("Default|%t", authMethod.Default), + fmt.Sprintf("Create Index|%d", authMethod.CreateIndex), + fmt.Sprintf("Modify Index|%d", authMethod.ModifyIndex), } - - if authMethod.Config != nil { - out = append(out, formatAuthMethodConfig(authMethod.Config)...) - } - out = append(out, - []string{fmt.Sprintf("Create Index|%d", authMethod.CreateIndex), - fmt.Sprintf("Modify Index|%d", authMethod.ModifyIndex), - }..., - ) - return formatKV(out) } -func formatAuthMethodConfig(config *api.ACLAuthMethodConfig) []string { - return []string{ +func formatAuthMethodConfig(config *api.ACLAuthMethodConfig) string { + out := []string{ fmt.Sprintf("OIDC Discovery URL|%s", config.OIDCDiscoveryURL), fmt.Sprintf("OIDC Client ID|%s", config.OIDCClientID), fmt.Sprintf("OIDC Client Secret|%s", config.OIDCClientSecret), @@ -93,6 +95,7 @@ func formatAuthMethodConfig(config *api.ACLAuthMethodConfig) []string { fmt.Sprintf("Claim mappings|%s", strings.Join(formatMap(config.ClaimMappings), "; ")), fmt.Sprintf("List claim mappings|%s", strings.Join(formatMap(config.ListClaimMappings), "; ")), } + return formatKV(out) } func formatMap(m map[string]string) []string { diff --git a/command/acl_auth_method_create.go b/command/acl_auth_method_create.go index 15e32979e..9cb7147d8 100644 --- a/command/acl_auth_method_create.go +++ b/command/acl_auth_method_create.go @@ -197,6 +197,6 @@ func (a *ACLAuthMethodCreateCommand) Run(args []string) int { return 0 } - a.Ui.Output(fmt.Sprintf("Created ACL auth method:\n%s", formatAuthMethod(method))) + outputAuthMethod(a.Meta, method) return 0 } diff --git a/command/acl_auth_method_info.go b/command/acl_auth_method_info.go index 5aa9c7ec9..4ba99918b 100644 --- a/command/acl_auth_method_info.go +++ b/command/acl_auth_method_info.go @@ -110,7 +110,6 @@ func (a *ACLAuthMethodInfoCommand) Run(args []string) int { } // Format the output. - a.Ui.Output(formatAuthMethod(method)) - + outputAuthMethod(a.Meta, method) return 0 } diff --git a/command/acl_auth_method_update.go b/command/acl_auth_method_update.go index 3705bc616..c2f1c17f7 100644 --- a/command/acl_auth_method_update.go +++ b/command/acl_auth_method_update.go @@ -214,7 +214,7 @@ func (a *ACLAuthMethodUpdateCommand) Run(args []string) int { return 0 } - a.Ui.Output(fmt.Sprintf("Updated ACL auth method:\n%s", formatAuthMethod(method))) + outputAuthMethod(a.Meta, method) return 0 } diff --git a/website/content/docs/commands/acl/auth-method/create.mdx b/website/content/docs/commands/acl/auth-method/create.mdx index 3e1dadafd..039e9a37e 100644 --- a/website/content/docs/commands/acl/auth-method/create.mdx +++ b/website/content/docs/commands/acl/auth-method/create.mdx @@ -55,12 +55,16 @@ Create a new ACL Auth Method: ```shell-session $ nomad acl auth-method create -name "example-acl-auth-method" -type "OIDC" -max-token-ttl "1h" -token-locality "local" -config "@config.json" -Created ACL auth method: -Name = example-acl-auth-method -Type = OIDC -Locality = local -MaxTokenTTL = 1h0m0s -Default = false +Name = example-acl-auth-method +Type = OIDC +Locality = local +MaxTokenTTL = 1h0m0s +Default = false +Create Index = 14 +Modify Index = 14 + +Auth Method Config + OIDC Discovery URL = https://my-corp-app-name.auth0.com/ OIDC Client ID = V1RPi2MYptMV1RPi2MYptMV1RPi2MYpt OIDC Client Secret = example-client-secret @@ -70,8 +74,6 @@ Discovery CA pem = Signing algorithms = Claim mappings = {http://example.com/first_name: first_name}; {http://example.com/last_name: last_name} List claim mappings = {http://nomad.com/groups: groups} -Create Index = 14 -Modify Index = 14 ``` Example config file: @@ -97,4 +99,4 @@ Example config file: } ``` -[configuration]: /nomad/api-docs/acl/auth-methods#config \ No newline at end of file +[configuration]: /nomad/api-docs/acl/auth-methods#config diff --git a/website/content/docs/commands/acl/auth-method/info.mdx b/website/content/docs/commands/acl/auth-method/info.mdx index 903642242..7633c0838 100644 --- a/website/content/docs/commands/acl/auth-method/info.mdx +++ b/website/content/docs/commands/acl/auth-method/info.mdx @@ -34,11 +34,16 @@ Fetch information about an existing ACL Auth Method: ```shell-session $ nomad acl auth-method info example-acl-auth-method -Name = example-acl-auth-method -Type = OIDC -Locality = local -MaxTokenTTL = 1h0m0s -Default = false +Name = example-acl-auth-method +Type = OIDC +Locality = local +MaxTokenTTL = 1h0m0s +Default = false +Create Index = 14 +Modify Index = 14 + +Auth Method Config + OIDC Discovery URL = https://my-corp-app-name.auth0.com/ OIDC Client ID = V1RPi2MYptMV1RPi2MYptMV1RPi2MYpt OIDC Client Secret = example-client-secret @@ -48,6 +53,4 @@ Discovery CA pem = Signing algorithms = Claim mappings = {http://example.com/first_name: first_name}; {http://example.com/last_name: last_name} List claim mappings = {http://nomad.com/groups: groups} -Create Index = 14 -Modify Index = 33 ``` diff --git a/website/content/docs/commands/acl/auth-method/update.mdx b/website/content/docs/commands/acl/auth-method/update.mdx index a9e20f578..359d03908 100644 --- a/website/content/docs/commands/acl/auth-method/update.mdx +++ b/website/content/docs/commands/acl/auth-method/update.mdx @@ -63,13 +63,17 @@ Update an existing ACL auth method: ```shell-session $ nomad acl auth-method update -token-locality "global" -config @config.json example-acl-auth-method -Updated ACL auth method: -Name = example-acl-auth-method -Type = OIDC -Locality = global -MaxTokenTTL = 1h0m0s -Default = false -OIDC Discovery URL = https://my-new-corp-app-name.auth0.com/ +Name = example-acl-auth-method +Type = OIDC +Locality = global +MaxTokenTTL = 1h0m0s +Default = false +Create Index = 14 +Modify Index = 33 + +Auth Method Config + +OIDC Discovery URL = https://my-corp-app-name.auth0.com/ OIDC Client ID = V1RPi2MYptMV1RPi2MYptMV1RPi2MYpt OIDC Client Secret = example-client-secret Bound audiences = V1RPi2MYptMV1RPi2MYptMV1RPi2MYpt @@ -78,6 +82,4 @@ Discovery CA pem = Signing algorithms = Claim mappings = {http://example.com/first_name: first_name}; {http://example.com/last_name: last_name} List claim mappings = {http://nomad.com/groups: groups} -Create Index = 14 -Modify Index = 33 ```