Pull out license commands, and make the OSS changes needed for the license inspect PR in ent. (#11783)

This commit is contained in:
Nick Cabatoff 2021-06-07 20:44:20 +02:00 committed by GitHub
parent 3e9bb12dc2
commit bfae4e610b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 164 deletions

View File

@ -75,6 +75,11 @@ const (
EnvVaultCLINoColor = `VAULT_CLI_NO_COLOR`
// EnvVaultFormat is the output format
EnvVaultFormat = `VAULT_FORMAT`
// EnvVaultLicense is an env var used in Vault Enterprise to provide a license blob
EnvVaultLicense = "VAULT_LICENSE"
// EnvVaultLicensePath is an env var used in Vault Enterprise to provide a
// path to a license file on disk
EnvVaultLicensePath = "VAULT_LICENSE_PATH"
// flagNameAddress is the flag used in the base command to read in the
// address of the Vault server.
@ -167,6 +172,8 @@ var (
"consul": csr.NewServiceRegistration,
"kubernetes": ksr.NewServiceRegistration,
}
initCommandsEnt = func(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {}
)
// Commands is the mapping of all the available commands.
@ -296,16 +303,6 @@ 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(),
@ -730,6 +727,8 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
}, nil
}
}
initCommandsEnt(ui, serverCmdUi, runOpts)
}
// MakeShutdownCh returns a channel that can be used for shutdown

View File

@ -1,37 +0,0 @@
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
}

View File

@ -1,114 +0,0 @@
package command
import (
"fmt"
"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
}
var path string
if c.signed {
path = "sys/license/signed"
} else {
path = "sys/license"
}
secret, err := client.Logical().Read(path)
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)
}
}

View File

@ -1148,10 +1148,10 @@ func (c *ServerCommand) Run(args []string) int {
}
}
if envLicensePath := os.Getenv("VAULT_LICENSE_PATH"); envLicensePath != "" {
if envLicensePath := os.Getenv(EnvVaultLicensePath); envLicensePath != "" {
config.LicensePath = envLicensePath
}
if envLicense := os.Getenv("VAULT_LICENSE"); envLicense != "" {
if envLicense := os.Getenv(EnvVaultLicense); envLicense != "" {
config.License = envLicense
}

View File

@ -25,7 +25,7 @@ import (
func init() {
if signed := os.Getenv("VAULT_LICENSE_CI"); signed != "" {
os.Setenv("VAULT_LICENSE", signed)
os.Setenv(EnvVaultLicense, signed)
}
}