mirror of https://github.com/hashicorp/boundary
Add ssh private key to CLI (#2265)
parent
ef5ac07f02
commit
271cc8f781
@ -0,0 +1,8 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package credentials
|
||||
|
||||
type SshPrivateKeyAttributes struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
PrivateKey string `json:"private_key,omitempty"`
|
||||
PrivateKeyHmac string `json:"private_key_hmac,omitempty"`
|
||||
}
|
||||
@ -0,0 +1,258 @@
|
||||
// Code generated by "make cli"; DO NOT EDIT.
|
||||
package credentialscmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/credentials"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/common"
|
||||
"github.com/hashicorp/go-secure-stdlib/strutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
func initSshPrivateKeyFlags() {
|
||||
flagsOnce.Do(func() {
|
||||
extraFlags := extraSshPrivateKeyActionsFlagsMapFunc()
|
||||
for k, v := range extraFlags {
|
||||
flagsSshPrivateKeyMap[k] = append(flagsSshPrivateKeyMap[k], v...)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
_ cli.Command = (*SshPrivateKeyCommand)(nil)
|
||||
_ cli.CommandAutocomplete = (*SshPrivateKeyCommand)(nil)
|
||||
)
|
||||
|
||||
type SshPrivateKeyCommand struct {
|
||||
*base.Command
|
||||
|
||||
Func string
|
||||
|
||||
plural string
|
||||
|
||||
extraSshPrivateKeyCmdVars
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) AutocompleteArgs() complete.Predictor {
|
||||
initSshPrivateKeyFlags()
|
||||
return complete.PredictAnything
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) AutocompleteFlags() complete.Flags {
|
||||
initSshPrivateKeyFlags()
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) Synopsis() string {
|
||||
if extra := extraSshPrivateKeySynopsisFunc(c); extra != "" {
|
||||
return extra
|
||||
}
|
||||
|
||||
synopsisStr := "credential"
|
||||
|
||||
synopsisStr = fmt.Sprintf("%s %s", "ssh-private-key-type", synopsisStr)
|
||||
|
||||
return common.SynopsisFunc(c.Func, synopsisStr)
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) Help() string {
|
||||
initSshPrivateKeyFlags()
|
||||
|
||||
var helpStr string
|
||||
helpMap := common.HelpMap("credential")
|
||||
|
||||
switch c.Func {
|
||||
|
||||
default:
|
||||
|
||||
helpStr = c.extraSshPrivateKeyHelpFunc(helpMap)
|
||||
|
||||
}
|
||||
|
||||
// Keep linter from complaining if we don't actually generate code using it
|
||||
_ = helpMap
|
||||
return helpStr
|
||||
}
|
||||
|
||||
var flagsSshPrivateKeyMap = map[string][]string{
|
||||
|
||||
"create": {"credential-store-id", "name", "description"},
|
||||
|
||||
"update": {"id", "name", "description", "version"},
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) Flags() *base.FlagSets {
|
||||
if len(flagsSshPrivateKeyMap[c.Func]) == 0 {
|
||||
return c.FlagSet(base.FlagSetNone)
|
||||
}
|
||||
|
||||
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
|
||||
f := set.NewFlagSet("Command Options")
|
||||
common.PopulateCommonFlags(c.Command, f, "ssh-private-key-type credential", flagsSshPrivateKeyMap, c.Func)
|
||||
|
||||
extraSshPrivateKeyFlagsFunc(c, set, f)
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) Run(args []string) int {
|
||||
initSshPrivateKeyFlags()
|
||||
|
||||
switch c.Func {
|
||||
case "":
|
||||
return cli.RunResultHelp
|
||||
|
||||
}
|
||||
|
||||
c.plural = "ssh-private-key-type credential"
|
||||
switch c.Func {
|
||||
case "list":
|
||||
c.plural = "ssh-private-key-type credentials"
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.PrintCliError(err)
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
if strutil.StrListContains(flagsSshPrivateKeyMap[c.Func], "id") && c.FlagId == "" {
|
||||
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var opts []credentials.Option
|
||||
|
||||
if strutil.StrListContains(flagsSshPrivateKeyMap[c.Func], "credential-store-id") {
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
if c.FlagCredentialStoreId == "" {
|
||||
c.PrintCliError(errors.New("CredentialStore ID must be passed in via -credential-store-id or BOUNDARY_CREDENTIAL_STORE_ID"))
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if c.WrapperCleanupFunc != nil {
|
||||
defer func() {
|
||||
if err := c.WrapperCleanupFunc(); err != nil {
|
||||
c.PrintCliError(fmt.Errorf("Error cleaning kms wrapper: %w", err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
if err != nil {
|
||||
c.PrintCliError(fmt.Errorf("Error creating API client: %w", err))
|
||||
return base.CommandCliError
|
||||
}
|
||||
credentialsClient := credentials.NewClient(client)
|
||||
|
||||
switch c.FlagName {
|
||||
case "":
|
||||
case "null":
|
||||
opts = append(opts, credentials.DefaultName())
|
||||
default:
|
||||
opts = append(opts, credentials.WithName(c.FlagName))
|
||||
}
|
||||
|
||||
switch c.FlagDescription {
|
||||
case "":
|
||||
case "null":
|
||||
opts = append(opts, credentials.DefaultDescription())
|
||||
default:
|
||||
opts = append(opts, credentials.WithDescription(c.FlagDescription))
|
||||
}
|
||||
|
||||
if c.FlagFilter != "" {
|
||||
opts = append(opts, credentials.WithFilter(c.FlagFilter))
|
||||
}
|
||||
|
||||
var version uint32
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "update":
|
||||
switch c.FlagVersion {
|
||||
case 0:
|
||||
opts = append(opts, credentials.WithAutomaticVersioning(true))
|
||||
default:
|
||||
version = uint32(c.FlagVersion)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ok := extraSshPrivateKeyFlagsHandlingFunc(c, f, &opts); !ok {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
result, err = credentialsClient.Create(c.Context, "ssh_private_key", c.FlagCredentialStoreId, opts...)
|
||||
|
||||
case "update":
|
||||
result, err = credentialsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraSshPrivateKeyActions(c, result, err, credentialsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
var opts []base.Option
|
||||
|
||||
opts = append(opts, base.WithAttributeFieldPrefix("ssh_private_key"))
|
||||
|
||||
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...)
|
||||
return base.CommandApiError
|
||||
}
|
||||
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
output, err := printCustomSshPrivateKeyActionOutput(c)
|
||||
if err != nil {
|
||||
c.PrintCliError(err)
|
||||
return base.CommandUserError
|
||||
}
|
||||
if output {
|
||||
return base.CommandSuccess
|
||||
}
|
||||
|
||||
switch c.Func {
|
||||
|
||||
}
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
|
||||
return base.CommandSuccess
|
||||
}
|
||||
|
||||
var (
|
||||
extraSshPrivateKeyActionsFlagsMapFunc = func() map[string][]string { return nil }
|
||||
extraSshPrivateKeySynopsisFunc = func(*SshPrivateKeyCommand) string { return "" }
|
||||
extraSshPrivateKeyFlagsFunc = func(*SshPrivateKeyCommand, *base.FlagSets, *base.FlagSet) {}
|
||||
extraSshPrivateKeyFlagsHandlingFunc = func(*SshPrivateKeyCommand, *base.FlagSets, *[]credentials.Option) bool { return true }
|
||||
executeExtraSshPrivateKeyActions = func(_ *SshPrivateKeyCommand, inResult api.GenericResult, inErr error, _ *credentials.Client, _ uint32, _ []credentials.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
}
|
||||
printCustomSshPrivateKeyActionOutput = func(*SshPrivateKeyCommand) (bool, error) { return false, nil }
|
||||
)
|
||||
@ -0,0 +1,98 @@
|
||||
package credentialscmd
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/boundary/api/credentials"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
||||
)
|
||||
|
||||
func init() {
|
||||
extraSshPrivateKeyFlagsFunc = extraSshPrivateKeyFlagsFuncImpl
|
||||
extraSshPrivateKeyActionsFlagsMapFunc = extraSshPrivateKeyActionsFlagsMapFuncImpl
|
||||
extraSshPrivateKeyFlagsHandlingFunc = extraSshPrivateKeyFlagHandlingFuncImpl
|
||||
}
|
||||
|
||||
type extraSshPrivateKeyCmdVars struct {
|
||||
flagUsername string
|
||||
flagPrivateKey string
|
||||
}
|
||||
|
||||
func extraSshPrivateKeyActionsFlagsMapFuncImpl() map[string][]string {
|
||||
flags := map[string][]string{
|
||||
"create": {
|
||||
usernameFlagName,
|
||||
privateKeyFlagName,
|
||||
},
|
||||
}
|
||||
flags["update"] = flags["create"]
|
||||
return flags
|
||||
}
|
||||
|
||||
func extraSshPrivateKeyFlagsFuncImpl(c *SshPrivateKeyCommand, set *base.FlagSets, _ *base.FlagSet) {
|
||||
f := set.NewFlagSet("SSH Private Key Credential Options")
|
||||
|
||||
for _, name := range flagsSshPrivateKeyMap[c.Func] {
|
||||
switch name {
|
||||
case usernameFlagName:
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: usernameFlagName,
|
||||
Target: &c.flagUsername,
|
||||
Usage: "The username associated with the credential.",
|
||||
})
|
||||
case privateKeyFlagName:
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: privateKeyFlagName,
|
||||
Target: &c.flagPrivateKey,
|
||||
Usage: "The SSH private key associated with the credential. This can be the value itself, refer to a file on disk (file://) from which the value will be read, or an env var (env://) from which the value will be read.",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func extraSshPrivateKeyFlagHandlingFuncImpl(c *SshPrivateKeyCommand, _ *base.FlagSets, opts *[]credentials.Option) bool {
|
||||
switch c.flagUsername {
|
||||
case "":
|
||||
default:
|
||||
*opts = append(*opts, credentials.WithSshPrivateKeyCredentialUsername(c.flagUsername))
|
||||
}
|
||||
switch c.flagPrivateKey {
|
||||
case "":
|
||||
default:
|
||||
privateKey, err := parseutil.ParsePath(c.flagPrivateKey)
|
||||
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
|
||||
c.UI.Error("Error parsing private key flag: " + err.Error())
|
||||
return false
|
||||
}
|
||||
*opts = append(*opts, credentials.WithSshPrivateKeyCredentialPrivateKey(privateKey))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCommand) extraSshPrivateKeyHelpFunc(_ map[string]func() string) string {
|
||||
var helpStr string
|
||||
switch c.Func {
|
||||
case "create":
|
||||
helpStr = base.WrapForHelpText([]string{
|
||||
"Usage: boundary credentials create ssh-private-key -credential-store-id [options] [args]",
|
||||
"",
|
||||
" Create an SSH private key credential. Example:",
|
||||
"",
|
||||
` $ boundary credentials create ssh-private-key -credential-store-id csvlt_1234567890 -username user -private-key file:///home/user/.ssh/id_ed25519`,
|
||||
"",
|
||||
"",
|
||||
})
|
||||
|
||||
case "update":
|
||||
helpStr = base.WrapForHelpText([]string{
|
||||
"Usage: boundary credentials update ssh-private-key [options] [args]",
|
||||
"",
|
||||
" Update an SSH private key credential given its ID. Example:",
|
||||
"",
|
||||
` $ boundary credentials update ssh-private-key -id clvlt_1234567890 -name devops -description "For DevOps usage"`,
|
||||
"",
|
||||
"",
|
||||
})
|
||||
}
|
||||
return helpStr + c.Flags().Help()
|
||||
}
|
||||
Loading…
Reference in new issue