2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-05-23 21:44:41 +00:00
|
|
|
package set
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-11-10 16:26:01 +00:00
|
|
|
"os"
|
2018-05-23 21:44:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"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
|
|
|
|
|
|
|
|
// flags
|
2021-01-29 21:38:11 +00:00
|
|
|
configFile flags.StringValue
|
|
|
|
forceWithoutCrossSigning bool
|
2018-05-23 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.Var(&c.configFile, "config-file",
|
|
|
|
"The path to the config file to use.")
|
2021-01-29 21:38:11 +00:00
|
|
|
c.flags.BoolVar(&c.forceWithoutCrossSigning, "force-without-cross-signing", false,
|
|
|
|
"Indicates that the CA reconfiguration should go ahead even if the current "+
|
|
|
|
"CA is unable to cross sign certificates. This risks temporary connection "+
|
|
|
|
"failures during the rollout as new leafs will be rejected by proxies that "+
|
|
|
|
"have not yet observed the new root cert but is the only option if a CA that "+
|
|
|
|
"doesn't support cross signing needs to be reconfigured or mirated away from.")
|
2018-05-23 21:44:41 +00:00
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
if err == flag.ErrHelp {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a client.
|
|
|
|
client, err := c.http.APIClient()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.configFile.String() == "" {
|
|
|
|
c.UI.Error("The -config-file flag is required")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
bytes, err := os.ReadFile(c.configFile.String())
|
2018-05-23 21:44:41 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error reading config file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var config api.CAConfig
|
|
|
|
if err := json.Unmarshal(bytes, &config); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing config file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2021-01-29 21:38:11 +00:00
|
|
|
config.ForceWithoutCrossSigning = c.forceWithoutCrossSigning
|
2018-05-23 21:44:41 +00:00
|
|
|
|
|
|
|
// Set the new configuration.
|
|
|
|
if _, err := client.Connect().CASetConfig(&config, nil); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error setting CA configuration: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.UI.Output("Configuration updated!")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Modify the current Connect CA configuration"
|
|
|
|
const help = `
|
|
|
|
Usage: consul connect ca set-config [options]
|
|
|
|
|
2018-05-25 17:27:04 +00:00
|
|
|
Modifies the current Connect Certificate Authority (CA) configuration.
|
2018-05-23 21:44:41 +00:00
|
|
|
`
|