2022-06-14 15:22:42 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// OperatorRootKeyringRotateCommand is a Command
|
|
|
|
// implementation that rotates the variables encryption key.
|
|
|
|
type OperatorRootKeyringRotateCommand struct {
|
2022-06-14 15:22:42 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRotateCommand) Help() string {
|
2022-06-14 15:22:42 +00:00
|
|
|
helpText := `
|
2022-08-26 18:03:56 +00:00
|
|
|
Usage: nomad operator root keyring rotate [options]
|
2022-06-14 15:22:42 +00:00
|
|
|
|
|
|
|
Generate a new encryption key for all future variables.
|
|
|
|
|
|
|
|
If ACLs are enabled, this command requires a management token.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
|
|
|
|
|
|
|
Keyring Options:
|
|
|
|
|
|
|
|
-full
|
|
|
|
Decrypt all existing variables and re-encrypt with the new key. This command
|
|
|
|
will immediately return and the re-encryption process will run
|
|
|
|
asynchronously on the leader.
|
|
|
|
|
|
|
|
-verbose
|
|
|
|
Show full information.
|
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRotateCommand) Synopsis() string {
|
|
|
|
return "Rotates the root encryption key"
|
2022-06-14 15:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRotateCommand) AutocompleteFlags() complete.Flags {
|
2022-06-14 15:22:42 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-full": complete.PredictNothing,
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRotateCommand) AutocompleteArgs() complete.Predictor {
|
2022-06-14 15:22:42 +00:00
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRotateCommand) Name() string {
|
|
|
|
return "root keyring rotate"
|
2022-06-14 15:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRotateCommand) Run(args []string) int {
|
2022-06-14 15:22:42 +00:00
|
|
|
var rotateFull, verbose bool
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
flags := c.Meta.FlagSet("root keyring rotate", FlagSetClient)
|
2022-06-14 15:22:42 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&rotateFull, "full", false, "full key rotation")
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 0 {
|
|
|
|
c.Ui.Error("This command requires no arguments.")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error creating nomad cli client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Keyring().Rotate(
|
|
|
|
&api.KeyringRotateOptions{Full: rotateFull}, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("error: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
c.Ui.Output(renderVariablesKeysResponse([]*api.RootKeyMeta{resp}, verbose))
|
2022-06-14 15:22:42 +00:00
|
|
|
return 0
|
|
|
|
}
|