2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
|
|
// OperatorRootKeyringRemoveCommand is a Command
|
|
|
|
// implementation that handles removeing variables encryption
|
2022-06-14 15:22:42 +00:00
|
|
|
// keys from a keyring.
|
2022-08-26 18:03:56 +00:00
|
|
|
type OperatorRootKeyringRemoveCommand struct {
|
2022-06-14 15:22:42 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRemoveCommand) Help() string {
|
2022-06-14 15:22:42 +00:00
|
|
|
helpText := `
|
2022-08-26 18:03:56 +00:00
|
|
|
Usage: nomad operator root keyring remove [options] <key ID>
|
2022-06-14 15:22:42 +00:00
|
|
|
|
|
|
|
Remove an encryption key from the cluster. This operation may only be
|
|
|
|
performed on keys that are not the active key.
|
|
|
|
|
|
|
|
If ACLs are enabled, this command requires a management token.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRemoveCommand) Synopsis() string {
|
|
|
|
return "Removes a root encryption key"
|
2022-06-14 15:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRemoveCommand) AutocompleteFlags() complete.Flags {
|
2022-06-14 15:22:42 +00:00
|
|
|
return c.Meta.AutocompleteFlags(FlagSetClient)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRemoveCommand) AutocompleteArgs() complete.Predictor {
|
2022-06-14 15:22:42 +00:00
|
|
|
return complete.PredictAnything
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRemoveCommand) Name() string {
|
|
|
|
return "root keyring remove"
|
2022-06-14 15:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (c *OperatorRootKeyringRemoveCommand) Run(args []string) int {
|
2022-06-14 15:22:42 +00:00
|
|
|
var verbose bool
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
flags := c.Meta.FlagSet("root keyring remove", FlagSetClient)
|
2022-06-14 15:22:42 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error("This command requires one argument: <key ID>")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
removeKey := args[0]
|
|
|
|
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error creating nomad cli client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
_, err = client.Keyring().Delete(&api.KeyringDeleteOptions{
|
|
|
|
KeyID: removeKey,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("error: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.Ui.Output(fmt.Sprintf("Removed encryption key %s", removeKey))
|
|
|
|
return 0
|
|
|
|
}
|