Skip to content

Commit

Permalink
Introduce a "vault license get" command (hashicorp#11526)
Browse files Browse the repository at this point in the history
  • Loading branch information
raskchanky committed May 4, 2021
1 parent 2feeb39 commit ed1727c
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
10 changes: 10 additions & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
BaseCommand: getBaseCommand(),
}, nil
},
"license": func() (cli.Command, error) {
return &LicenseCommand{
BaseCommand: getBaseCommand(),
}, nil
},
"license get": func() (cli.Command, error) {
return &LicenseGetCommand{
BaseCommand: getBaseCommand(),
}, nil
},
"list": func() (cli.Command, error) {
return &ListCommand{
BaseCommand: getBaseCommand(),
Expand Down
37 changes: 37 additions & 0 deletions command/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package command

import (
"strings"

"github.com/mitchellh/cli"
)

var _ cli.Command = (*LicenseCommand)(nil)

type LicenseCommand struct {
*BaseCommand
}

func (c *LicenseCommand) Synopsis() string {
return "Interact with licenses"
}

func (c *LicenseCommand) Help() string {
helpText := `
Usage: vault license <subcommand> [options] [args]
This command groups subcommands for interacting with Vault licenses.
Get the current Vault license:
$ vault license get
Please see the individual subcommand help for detailed usage information.
`

return strings.TrimSpace(helpText)
}

func (c *LicenseCommand) Run(args []string) int {
return cli.RunResultHelp
}
108 changes: 108 additions & 0 deletions command/license_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package command

import (
"fmt"
"strconv"
"strings"

"github.com/mitchellh/cli"
"github.com/posener/complete"
)

var (
_ cli.Command = (*LicenseGetCommand)(nil)
_ cli.CommandAutocomplete = (*LicenseGetCommand)(nil)
)

type LicenseGetCommand struct {
*BaseCommand

signed bool
}

func (c *LicenseGetCommand) Synopsis() string {
return "Get an existing license"
}

func (c *LicenseGetCommand) Help() string {
helpText := `
Usage: vault license get [options]
Get the currently installed license, if any:
$ vault license get
Get the currently installed license, if any, and output its contents as a signed blob:
$ vault license get -signed
` + c.Flags().Help()

return strings.TrimSpace(helpText)
}

func (c *LicenseGetCommand) Flags() *FlagSets {
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)

f := set.NewFlagSet("License Options")
f.BoolVar(&BoolVar{
Name: "signed",
Target: &c.signed,
Usage: "Whether to return a signed blob from the API.",
})

return set
}

func (c *LicenseGetCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *LicenseGetCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}

func (c *LicenseGetCommand) Run(args []string) int {
f := c.Flags()

if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}

args = f.Args()
if len(args) > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
return 1
}

client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}

secret, err := client.Logical().ReadWithData("sys/license", map[string][]string{"signed": {strconv.FormatBool(c.signed)}})
if err != nil {
c.UI.Error(fmt.Sprintf("Error retrieving license: %s", err))
return 2
}

if secret == nil {
c.UI.Error("License not found")
return 2
}

if c.signed {
blob := secret.Data["signed"].(string)
if blob == "" {
c.UI.Output("License not found or using a temporary license.")
return 2
} else {
c.UI.Output(blob)
return 0
}
} else {
return OutputSecret(c.UI, secret)
}
}

0 comments on commit ed1727c

Please sign in to comment.