2022-06-07 20:06:44 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2022-06-07 20:06:44 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OperatorSecureVariablesKeyringCommand is a Command implementation
|
|
|
|
// that handles querying, installing, and removing secure variables
|
|
|
|
// encryption keys from a keyring.
|
|
|
|
type OperatorSecureVariablesKeyringCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSecureVariablesKeyringCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad operator secure-variables keyring [options]
|
|
|
|
|
|
|
|
Manages encryption keys used for storing secure variables. This command may be
|
|
|
|
used to examine active encryption keys in the cluster, rotate keys, add new
|
|
|
|
keys from backups, or remove unused keys.
|
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
If ACLs are enabled, all subcommands requires a management token.
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
Rotate the encryption key:
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
$ nomad operator secure-variables keyring rotate
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
List all encryption key metadata:
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
$ nomad operator secure-variables keyring list
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
Remove an encryption key from the keyring:
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
$ nomad operator secure-variables keyring remove <key ID>
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
Install an encryption key from backup:
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
$ nomad operator secure-variables keyring install <path to .json file>
|
2022-06-07 20:06:44 +00:00
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
Please see individual subcommand help for detailed usage information.
|
2022-06-07 20:06:44 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSecureVariablesKeyringCommand) Synopsis() string {
|
|
|
|
return "Manages secure variables encryption keys"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSecureVariablesKeyringCommand) AutocompleteFlags() complete.Flags {
|
2022-06-14 15:22:42 +00:00
|
|
|
return c.Meta.AutocompleteFlags(FlagSetClient)
|
2022-06-07 20:06:44 +00:00
|
|
|
}
|
2022-06-14 15:22:42 +00:00
|
|
|
|
2022-06-07 20:06:44 +00:00
|
|
|
func (c *OperatorSecureVariablesKeyringCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
func (c *OperatorSecureVariablesKeyringCommand) Name() string {
|
|
|
|
return "secure-variables keyring"
|
|
|
|
}
|
2022-06-07 20:06:44 +00:00
|
|
|
|
|
|
|
func (c *OperatorSecureVariablesKeyringCommand) Run(args []string) int {
|
2022-06-14 15:22:42 +00:00
|
|
|
return cli.RunResultHelp
|
2022-06-07 20:06:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 15:22:42 +00:00
|
|
|
// renderSecureVariablesKeysResponse is a helper for formatting the
|
|
|
|
// keyring API responses
|
|
|
|
func renderSecureVariablesKeysResponse(keys []*api.RootKeyMeta, verbose bool) string {
|
2022-06-07 20:06:44 +00:00
|
|
|
length := fullId
|
|
|
|
if !verbose {
|
|
|
|
length = 8
|
|
|
|
}
|
|
|
|
out := make([]string, len(keys)+1)
|
2022-07-07 17:48:38 +00:00
|
|
|
out[0] = "Key|State|Create Time"
|
2022-06-07 20:06:44 +00:00
|
|
|
i := 1
|
|
|
|
for _, k := range keys {
|
|
|
|
out[i] = fmt.Sprintf("%s|%v|%s",
|
2022-07-07 17:48:38 +00:00
|
|
|
k.KeyID[:length], k.State, formatTime(k.CreateTime))
|
2022-06-07 20:06:44 +00:00
|
|
|
i = i + 1
|
|
|
|
}
|
2022-06-14 15:22:42 +00:00
|
|
|
return formatList(out)
|
2022-06-07 20:06:44 +00:00
|
|
|
}
|