2019-04-26 17:49:28 +00:00
|
|
|
package authmethodupdate
|
|
|
|
|
|
|
|
import (
|
2020-05-04 21:21:28 +00:00
|
|
|
"encoding/json"
|
2019-04-26 17:49:28 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-02-15 15:06:05 +00:00
|
|
|
"strings"
|
2020-05-04 22:02:57 +00:00
|
|
|
"time"
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2020-02-15 15:06:05 +00:00
|
|
|
"github.com/hashicorp/consul/command/acl/authmethod"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/hashicorp/consul/command/helpers"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
|
|
|
|
|
|
|
name string
|
|
|
|
|
2020-06-01 16:44:47 +00:00
|
|
|
displayName string
|
|
|
|
description string
|
|
|
|
maxTokenTTL time.Duration
|
|
|
|
tokenLocality string
|
|
|
|
config string
|
2020-05-04 22:02:57 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
k8sHost string
|
|
|
|
k8sCACert string
|
|
|
|
k8sServiceAccountJWT string
|
|
|
|
|
|
|
|
noMerge bool
|
|
|
|
showMeta bool
|
2020-02-15 15:06:05 +00:00
|
|
|
format string
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
testStdin io.Reader
|
2020-05-07 22:08:42 +00:00
|
|
|
|
|
|
|
enterpriseCmd
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
|
|
|
|
c.flags.BoolVar(
|
|
|
|
&c.showMeta,
|
|
|
|
"meta",
|
|
|
|
false,
|
|
|
|
"Indicates that auth method metadata such "+
|
2019-05-01 21:11:23 +00:00
|
|
|
"as the raft indices should be shown for each entry.",
|
2019-04-26 17:49:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c.flags.StringVar(
|
|
|
|
&c.name,
|
|
|
|
"name",
|
|
|
|
"",
|
|
|
|
"The auth method name.",
|
|
|
|
)
|
|
|
|
|
2020-05-04 20:18:25 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.displayName,
|
|
|
|
"display-name",
|
|
|
|
"",
|
|
|
|
"An optional name to use instead of the name when displaying this auth method in a UI.",
|
|
|
|
)
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.description,
|
|
|
|
"description",
|
|
|
|
"",
|
|
|
|
"A description of the auth method.",
|
|
|
|
)
|
|
|
|
|
2020-05-04 22:02:57 +00:00
|
|
|
c.flags.DurationVar(
|
|
|
|
&c.maxTokenTTL,
|
|
|
|
"max-token-ttl",
|
|
|
|
0,
|
|
|
|
"Duration of time all tokens created by this auth method should be valid for",
|
|
|
|
)
|
2020-06-01 16:44:47 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.tokenLocality,
|
|
|
|
"token-locality",
|
|
|
|
"",
|
|
|
|
"Defines the kind of token that this auth method should produce. "+
|
|
|
|
"This can be either 'local' or 'global'. If empty the value of 'local' is assumed.",
|
|
|
|
)
|
2020-05-04 22:02:57 +00:00
|
|
|
|
2020-05-04 21:21:28 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.config,
|
|
|
|
"config",
|
|
|
|
"",
|
|
|
|
"The configuration for the auth method. Must be JSON. The config is updated as one field"+
|
|
|
|
"May be prefixed with '@' to indicate that the value is a file path to load the config from. "+
|
|
|
|
"'-' may also be given to indicate that the config are available on stdin. ",
|
|
|
|
)
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.k8sHost,
|
|
|
|
"kubernetes-host",
|
|
|
|
"",
|
|
|
|
"Address of the Kubernetes API server. This flag is required for type=kubernetes.",
|
|
|
|
)
|
|
|
|
c.flags.StringVar(
|
|
|
|
&c.k8sCACert,
|
|
|
|
"kubernetes-ca-cert",
|
|
|
|
"",
|
|
|
|
"PEM encoded CA cert for use by the TLS client used to talk with the "+
|
|
|
|
"Kubernetes API. May be prefixed with '@' to indicate that the "+
|
|
|
|
"value is a file path to load the cert from. "+
|
|
|
|
"This flag is required for type=kubernetes.",
|
|
|
|
)
|
|
|
|
c.flags.StringVar(
|
|
|
|
&c.k8sServiceAccountJWT,
|
|
|
|
"kubernetes-service-account-jwt",
|
|
|
|
"",
|
2019-05-01 21:11:23 +00:00
|
|
|
"A Kubernetes service account JWT used to access the TokenReview API to "+
|
2019-04-26 17:49:28 +00:00
|
|
|
"validate other JWTs during login. "+
|
|
|
|
"This flag is required for type=kubernetes.",
|
|
|
|
)
|
|
|
|
|
|
|
|
c.flags.BoolVar(&c.noMerge, "no-merge", false, "Do not merge the current auth method "+
|
|
|
|
"information with what is provided to the command. Instead overwrite all fields "+
|
|
|
|
"with the exception of the name which is immutable.")
|
2020-05-04 21:21:28 +00:00
|
|
|
|
2020-02-15 15:06:05 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.format,
|
|
|
|
"format",
|
|
|
|
authmethod.PrettyFormat,
|
|
|
|
fmt.Sprintf("Output format {%s}", strings.Join(authmethod.GetSupportedFormats(), "|")),
|
|
|
|
)
|
2020-05-07 22:08:42 +00:00
|
|
|
|
|
|
|
c.initEnterpriseFlags()
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2021-07-21 19:45:24 +00:00
|
|
|
flags.Merge(c.flags, c.http.MultiTenancyFlags())
|
2019-04-26 17:49:28 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.name == "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Cannot update an auth method without specifying the -name parameter"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.http.APIClient()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regardless of merge, we need to fetch the prior immutable fields first.
|
|
|
|
currentAuthMethod, _, err := client.ACL().AuthMethodRead(c.name, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error when retrieving current auth method: %v", err))
|
|
|
|
return 1
|
|
|
|
} else if currentAuthMethod == nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Auth method not found with name %q", c.name))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.k8sCACert != "" {
|
|
|
|
c.k8sCACert, err = helpers.LoadDataSource(c.k8sCACert, c.testStdin)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Invalid '-kubernetes-ca-cert' value: %v", err))
|
|
|
|
return 1
|
|
|
|
} else if c.k8sCACert == "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Kubernetes CA Cert is empty"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var method *api.ACLAuthMethod
|
|
|
|
if c.noMerge {
|
|
|
|
method = &api.ACLAuthMethod{
|
2020-06-01 16:44:47 +00:00
|
|
|
Name: currentAuthMethod.Name,
|
|
|
|
Type: currentAuthMethod.Type,
|
|
|
|
DisplayName: c.displayName,
|
|
|
|
Description: c.description,
|
|
|
|
TokenLocality: c.tokenLocality,
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
2020-05-04 22:02:57 +00:00
|
|
|
if c.maxTokenTTL > 0 {
|
|
|
|
method.MaxTokenTTL = c.maxTokenTTL
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:08:42 +00:00
|
|
|
if err := c.enterprisePopulateAuthMethod(method); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-05-04 21:21:28 +00:00
|
|
|
if c.config != "" {
|
|
|
|
if c.k8sHost != "" || c.k8sCACert != "" || c.k8sServiceAccountJWT != "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Cannot use command line arguments with '-config' flag"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
data, err := helpers.LoadDataSource(c.config, c.testStdin)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error loading configuration file: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(data), &method.Config); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing JSON for auth method config: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 22:02:57 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if currentAuthMethod.Type == "kubernetes" {
|
|
|
|
if c.k8sHost == "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Missing required '-kubernetes-host' flag"))
|
|
|
|
return 1
|
|
|
|
} else if c.k8sCACert == "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Missing required '-kubernetes-ca-cert' flag"))
|
|
|
|
return 1
|
|
|
|
} else if c.k8sServiceAccountJWT == "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Missing required '-kubernetes-service-account-jwt' flag"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
method.Config = map[string]interface{}{
|
|
|
|
"Host": c.k8sHost,
|
|
|
|
"CACert": c.k8sCACert,
|
|
|
|
"ServiceAccountJWT": c.k8sServiceAccountJWT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
methodCopy := *currentAuthMethod
|
|
|
|
method = &methodCopy
|
2020-05-04 21:21:28 +00:00
|
|
|
if c.description != "" {
|
|
|
|
method.Description = c.description
|
|
|
|
}
|
2020-05-04 20:18:25 +00:00
|
|
|
if c.displayName != "" {
|
|
|
|
method.DisplayName = c.displayName
|
|
|
|
}
|
2020-05-04 22:02:57 +00:00
|
|
|
if c.maxTokenTTL > 0 {
|
|
|
|
method.MaxTokenTTL = c.maxTokenTTL
|
|
|
|
}
|
2020-06-01 16:44:47 +00:00
|
|
|
if c.tokenLocality != "" {
|
|
|
|
method.TokenLocality = c.tokenLocality
|
|
|
|
}
|
2020-05-07 22:08:42 +00:00
|
|
|
if err := c.enterprisePopulateAuthMethod(method); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2020-05-04 21:21:28 +00:00
|
|
|
if c.config != "" {
|
|
|
|
if c.k8sHost != "" || c.k8sCACert != "" || c.k8sServiceAccountJWT != "" {
|
|
|
|
c.UI.Error(fmt.Sprintf("Cannot use command line arguments with '-config' flag"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
data, err := helpers.LoadDataSource(c.config, c.testStdin)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error loading configuration file: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
2020-05-11 19:21:17 +00:00
|
|
|
// Don't attempt a deep merge.
|
|
|
|
method.Config = make(map[string]interface{})
|
2020-05-04 21:21:28 +00:00
|
|
|
if err := json.Unmarshal([]byte(data), &method.Config); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing JSON for auth method config: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
if method.Config == nil {
|
|
|
|
method.Config = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
if currentAuthMethod.Type == "kubernetes" {
|
|
|
|
if c.k8sHost != "" {
|
|
|
|
method.Config["Host"] = c.k8sHost
|
|
|
|
}
|
|
|
|
if c.k8sCACert != "" {
|
|
|
|
method.Config["CACert"] = c.k8sCACert
|
|
|
|
}
|
|
|
|
if c.k8sServiceAccountJWT != "" {
|
|
|
|
method.Config["ServiceAccountJWT"] = c.k8sServiceAccountJWT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
method, _, err = client.ACL().AuthMethodUpdate(method, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error updating auth method %q: %v", c.name, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:06:05 +00:00
|
|
|
formatter, err := authmethod.NewFormatter(c.format, c.showMeta)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := formatter.FormatAuthMethod(method)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if out != "" {
|
|
|
|
c.UI.Info(out)
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return flags.Usage(c.help, nil)
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:45:24 +00:00
|
|
|
const (
|
|
|
|
synopsis = "Update an ACL auth method"
|
|
|
|
help = `
|
2019-04-26 17:49:28 +00:00
|
|
|
Usage: consul acl auth-method update -name NAME [options]
|
|
|
|
|
|
|
|
Updates an auth method. By default it will merge the auth method
|
|
|
|
information with its current state so that you do not have to provide all
|
|
|
|
parameters. This behavior can be disabled by passing -no-merge.
|
|
|
|
|
|
|
|
Update all editable fields of the auth method:
|
|
|
|
|
|
|
|
$ consul acl auth-method update -name "my-k8s" \
|
|
|
|
-description "new description" \
|
|
|
|
-kubernetes-host "https://new-apiserver.example.com:8443" \
|
2020-10-06 22:44:24 +00:00
|
|
|
-kubernetes-ca-cert @/path/to/new-kube.ca.crt \
|
2019-04-26 17:49:28 +00:00
|
|
|
-kubernetes-service-account-jwt "NEW_JWT_CONTENTS"
|
2020-10-06 23:14:56 +00:00
|
|
|
`
|
2021-07-21 19:45:24 +00:00
|
|
|
)
|