2021-06-22 22:26:26 -04:00
// Copyright (c) HashiCorp, Inc.
2023-01-24 13:01:20 +00:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2021-06-22 22:26:26 -04:00
package credentiallibrariescmd
import (
"github.com/hashicorp/boundary/api/credentiallibraries"
"github.com/hashicorp/boundary/internal/cmd/base"
2021-07-14 17:50:15 -07:00
"github.com/hashicorp/go-secure-stdlib/parseutil"
2021-06-22 22:26:26 -04:00
)
func init () {
extraVaultFlagsFunc = extraVaultFlagsFuncImpl
extraVaultActionsFlagsMapFunc = extraVaultActionsFlagsMapFuncImpl
extraVaultFlagsHandlingFunc = extraVaultFlagHandlingFuncImpl
}
2023-01-24 13:01:20 +00:00
type extraVaultCmdVars extraVaultGenericCmdVars
2021-06-22 22:26:26 -04:00
func extraVaultActionsFlagsMapFuncImpl () map [ string ][] string {
2023-01-24 13:01:20 +00:00
return extraVaultGenericActionsFlagsMapFuncImpl ()
2021-06-22 22:26:26 -04:00
}
func extraVaultFlagsFuncImpl ( c * VaultCommand , set * base . FlagSets , _ * base . FlagSet ) {
f := set . NewFlagSet ( "Vault Credential Library Options" )
for _ , name := range flagsVaultMap [ c . Func ] {
switch name {
case pathFlagName :
f . StringVar ( & base . StringVar {
Name : pathFlagName ,
Target : & c . flagPath ,
Usage : "The path in vault to request credentials from." ,
})
case httpMethodFlagName :
f . StringVar ( & base . StringVar {
Name : httpMethodFlagName ,
Target : & c . flagHttpMethod ,
Usage : "The http method the library should use when communicating with vault." ,
})
case httpRequestBodyFlagName :
f . StringVar ( & base . StringVar {
Name : httpRequestBodyFlagName ,
Target : & c . flagHttpRequestBody ,
Usage : "The http request body the library uses to communicate with vault. 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." ,
})
2021-10-27 14:16:22 -07:00
case credentialTypeFlagName :
f . StringVar ( & base . StringVar {
Name : credentialTypeFlagName ,
Target : & c . flagCredentialType ,
Usage : "The type of credential this library will issue, defaults to Unspecified." ,
})
case credentialMappingFlagName :
f . CombinationSliceVar ( & base . CombinationSliceVar {
Name : credentialMappingFlagName ,
Target : & c . flagCredentialMapping ,
KvSplit : true ,
Usage : "The credential mapping override." ,
})
2021-06-22 22:26:26 -04:00
}
}
}
2021-10-27 14:16:22 -07:00
func extraVaultFlagHandlingFuncImpl ( c * VaultCommand , _ * base . FlagSets , opts * [] credentiallibraries . Option ) bool {
2021-06-22 22:26:26 -04:00
switch c . flagPath {
case "" :
default :
* opts = append ( * opts , credentiallibraries . WithVaultCredentialLibraryPath ( c . flagPath ))
}
switch c . flagHttpMethod {
case "" :
case "null" :
* opts = append ( * opts , credentiallibraries . DefaultVaultCredentialLibraryHttpMethod ())
default :
* opts = append ( * opts , credentiallibraries . WithVaultCredentialLibraryHttpMethod ( c . flagHttpMethod ))
}
switch c . flagHttpRequestBody {
case "" :
case "null" :
* opts = append ( * opts , credentiallibraries . DefaultVaultCredentialLibraryHttpRequestBody ())
default :
2021-07-14 17:50:15 -07:00
rb , _ := parseutil . ParsePath ( c . flagHttpRequestBody )
2021-06-22 22:26:26 -04:00
* opts = append ( * opts , credentiallibraries . WithVaultCredentialLibraryHttpRequestBody ( rb ))
}
2021-10-27 14:16:22 -07:00
switch c . flagCredentialType {
case "" :
case "null" :
* opts = append ( * opts , credentiallibraries . DefaultCredentialType ())
default :
* opts = append ( * opts , credentiallibraries . WithCredentialType ( c . flagCredentialType ))
}
switch len ( c . flagCredentialMapping ) {
case 0 :
case 1 :
2023-01-04 09:44:11 -08:00
if len ( c . flagCredentialMapping [ 0 ]. Keys ) == 1 && c . flagCredentialMapping [ 0 ]. Keys [ 0 ] == "null" && c . flagCredentialMapping [ 0 ]. Value == nil {
2021-10-27 14:16:22 -07:00
* opts = append ( * opts , credentiallibraries . DefaultCredentialMappingOverrides ())
break
}
fallthrough
default :
2022-10-25 18:29:38 -05:00
mappings := make ( map [ string ] any , len ( c . flagCredentialMapping ))
2021-10-27 14:16:22 -07:00
for _ , mapping := range c . flagCredentialMapping {
switch {
2023-01-04 09:44:11 -08:00
case len ( mapping . Keys ) != 1 || mapping . Keys [ 0 ] == "" || mapping . Value == nil || mapping . Value . GetValue () == "" :
2021-10-27 14:16:22 -07:00
// mapping override does not support key segments (e.g. 'x.y=z')
c . UI . Error ( "Credential mapping override must be in the format 'key=value', 'key=null' to clear field or 'null' to clear all." )
return false
2023-01-04 09:44:11 -08:00
case mapping . Value . GetValue () == "null" :
2021-10-27 14:16:22 -07:00
// user provided 'key=null' indicating the field specific override should
// be cleared, set map value to nil
mappings [ mapping . Keys [ 0 ]] = nil
default :
2023-01-04 09:44:11 -08:00
mappings [ mapping . Keys [ 0 ]] = mapping . Value . GetValue ()
2021-10-27 14:16:22 -07:00
}
}
* opts = append ( * opts , credentiallibraries . WithCredentialMappingOverrides ( mappings ))
}
2021-06-22 22:26:26 -04:00
return true
}
2021-10-27 14:16:22 -07:00
func ( c * VaultCommand ) extraVaultHelpFunc ( _ map [ string ] func () string ) string {
2021-06-22 22:26:26 -04:00
var helpStr string
switch c . Func {
case "create" :
helpStr = base . WrapForHelpText ([] string {
2023-01-24 13:01:20 +00:00
"Deprecated: use 'boundary credential-libraries create vault-generic' instead." ,
2021-06-22 22:26:26 -04:00
"Usage: boundary credential-libraries create vault -credential-store-id [options] [args]" ,
"" ,
" Create a vault-type credential library. Example:" ,
"" ,
` $ boundary credential-libraries create vault -credential-store-id csvlt_1234567890 -vault-path "/some/path"` ,
"" ,
"" ,
})
case "update" :
helpStr = base . WrapForHelpText ([] string {
2023-01-24 13:01:20 +00:00
"Deprecated: use 'boundary credential-libraries update vault-generic' instead." ,
2021-06-22 22:26:26 -04:00
"Usage: boundary credential-libraries update vault [options] [args]" ,
"" ,
" Update a vault-type credential library given its ID. Example:" ,
"" ,
` $ boundary credential-libraries update vault -id clvlt_1234567890 -name devops -description "For DevOps usage"` ,
"" ,
"" ,
})
}
return helpStr + c . Flags (). Help ()
}