2020-08-27 16:38:44 -04:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-06-24 13:08:13 -07:00
package config
import (
2022-03-16 17:04:07 -04:00
"context"
"errors"
2020-06-24 13:08:13 -07:00
"fmt"
2020-08-27 16:38:44 -04:00
"net/textproto"
2020-06-24 13:08:13 -07:00
"os"
"strings"
2022-04-06 16:40:47 -04:00
"github.com/hashicorp/boundary/globals"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/cmd/base"
2022-03-16 17:04:07 -04:00
kms_plugin_assets "github.com/hashicorp/boundary/plugins/kms"
2021-09-08 12:15:42 -04:00
"github.com/hashicorp/boundary/sdk/wrapper"
2022-03-16 17:04:07 -04:00
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
configutil "github.com/hashicorp/go-secure-stdlib/configutil/v2"
"github.com/hashicorp/go-secure-stdlib/pluginutil/v2"
2020-06-24 13:08:13 -07:00
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
2021-01-07 10:02:32 -05:00
var (
_ cli . Command = ( * EncryptDecryptCommand )( nil )
_ cli . CommandAutocomplete = ( * EncryptDecryptCommand )( nil )
)
2020-06-24 13:08:13 -07:00
type EncryptDecryptCommand struct {
* base . Command
2020-08-27 16:38:44 -04:00
Func string
2020-06-24 13:08:13 -07:00
2020-08-27 16:38:44 -04:00
flagConfig string
flagConfigKms string
2020-06-24 13:08:13 -07:00
flagOverwrite bool
flagStrip bool
}
func ( c * EncryptDecryptCommand ) Synopsis () string {
2020-08-27 16:38:44 -04:00
return fmt . Sprintf ( "%s sensitive values in Boundary's configuration file" , textproto . CanonicalMIMEHeaderKey ( c . Func ))
2020-06-24 13:08:13 -07:00
}
func ( c * EncryptDecryptCommand ) Help () string {
2020-08-27 16:38:44 -04:00
var args [] string
args = append ( args ,
"Usage: boundary config {{func}} [options] [args]" ,
"" ,
" {{upperfunc}} sensitive values in a Boundary's configuration file. These values must be marked with {{{{func}}()}} as appropriate. Example:" ,
"" ,
` foo = " {{ encrypt ( bar ) }} "` ,
"" ,
" By default this will print out the new configuration. To overwrite into the same file use the -overwrite flag." ,
"" ,
" $ boundary config {{func}} -overwrite config.hcl" ,
"" ,
` In order for this command to perform its task, a "kms" block must be defined within a configuration file. Example:` ,
"" ,
` kms "aead" {` ,
` purpose = "config"` ,
` aead_type = "aes-gcm"` ,
` key = "7xtkEoS5EXPbgynwd+dDLHopaCqK8cq0Rpep4eooaTs="` ,
` }` ,
"" ,
` The "kms" block can be defined in the configuration file or via the -config flag. If defined in the configuration file, only string parameters are supported, and the markers must be inside the quote marks delimiting the string. Additionally, if the block is defined inline, do NOT use an an "aead" block with the key defined in the configuration file as it provides no protection.` ,
"" ,
"" ,
)
for i , line := range args {
2022-01-05 14:38:38 -05:00
args [ i ] = strings . Replace (
2020-08-27 16:38:44 -04:00
strings . Replace (
2022-01-05 14:38:38 -05:00
line , "{{func}}" , c . Func , - 1 ,
),
"{{upperfunc}}" , textproto . CanonicalMIMEHeaderKey ( c . Func ), - 1 ,
)
2020-08-27 16:38:44 -04:00
}
return base . WrapForHelpText ( args ) + c . Flags (). Help ()
2020-06-24 13:08:13 -07:00
}
func ( c * EncryptDecryptCommand ) Flags () * base . FlagSets {
2020-08-27 16:38:44 -04:00
set := c . FlagSet ( base . FlagSetNone )
2020-06-24 13:08:13 -07:00
f := set . NewFlagSet ( "Command Options" )
2020-08-27 16:38:44 -04:00
f . StringVar ( & base . StringVar {
Name : "config-kms" ,
Target : & c . flagConfigKms ,
Completion : complete . PredictOr (
complete . PredictFiles ( "*.hcl" ),
complete . PredictFiles ( "*.json" ),
),
Usage : `If specified, the given file will be parsed for a "kms" block with purpose "config" and will use it to perform the command. If not set, the command will expect a block inline with the configuration file, and will only be able to support quoted string parameters.` ,
})
f . StringVar ( & base . StringVar {
Name : "config" ,
Target : & c . flagConfig ,
Completion : complete . PredictOr (
complete . PredictFiles ( "*.hcl" ),
complete . PredictFiles ( "*.json" ),
),
Usage : `The configuration file upon which to perform encryption or decryption` ,
})
2020-06-24 13:08:13 -07:00
f . BoolVar ( & base . BoolVar {
Name : "overwrite" ,
Target : & c . flagOverwrite ,
Usage : "Overwrite the existing file." ,
})
f . BoolVar ( & base . BoolVar {
Name : "strip" ,
Target : & c . flagStrip ,
Usage : "Strip the declarations from the file afterwards." ,
})
return set
}
func ( c * EncryptDecryptCommand ) AutocompleteArgs () complete . Predictor {
return complete . PredictAnything
}
func ( c * EncryptDecryptCommand ) AutocompleteFlags () complete . Flags {
return c . Flags (). Completions ()
}
func ( c * EncryptDecryptCommand ) Run ( args [] string ) ( ret int ) {
f := c . Flags ()
if err := f . Parse ( args ); err != nil {
c . UI . Error ( err . Error ())
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-06-24 13:08:13 -07:00
}
2020-08-27 16:38:44 -04:00
switch c . flagConfig {
case "" :
c . UI . Error ( `Missing required parameter -config` )
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-08-27 16:38:44 -04:00
default :
c . flagConfig = strings . TrimSpace ( c . flagConfig )
2020-06-24 13:08:13 -07:00
}
2020-08-27 16:38:44 -04:00
kmsDefFile := c . flagConfig
switch c . flagConfigKms {
case "" :
default :
kmsDefFile = strings . TrimSpace ( c . flagConfigKms )
2020-06-24 13:08:13 -07:00
}
2022-03-16 17:04:07 -04:00
wrapper , cleanupFunc , err := wrapper . GetWrapperFromPath (
c . Context ,
kmsDefFile ,
2022-04-06 16:40:47 -04:00
globals . KmsPurposeConfig ,
2022-03-16 17:04:07 -04:00
configutil . WithPluginOptions (
pluginutil . WithPluginsMap ( kms_plugin_assets . BuiltinKmsPlugins ()),
pluginutil . WithPluginsFilesystem ( kms_plugin_assets . KmsPluginPrefix , kms_plugin_assets . FileSystem ()),
),
// TODO: How would we want to expose this kind of log to users when
// using recovery configs? Generally with normal CLI commands we
// don't print out all of these logs. We may want a logger with a
// custom writer behind our existing gate where we print nothing
// unless there is an error, then dump all of it.
configutil . WithLogger ( hclog . NewNullLogger ()),
)
2020-06-24 13:08:13 -07:00
if err != nil {
2020-08-27 16:38:44 -04:00
c . UI . Error ( err . Error ())
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-06-24 13:08:13 -07:00
}
2020-08-27 16:38:44 -04:00
if wrapper == nil {
c . UI . Error ( `No wrapper with "config" purpose found"` )
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-06-24 13:08:13 -07:00
}
2022-03-16 17:04:07 -04:00
if cleanupFunc != nil {
defer func () {
if err := cleanupFunc (); err != nil {
c . UI . Warn ( fmt . Errorf ( "Error cleaning up KMS wrapper: %w" , err ). Error ())
}
}()
2020-06-24 13:08:13 -07:00
}
2022-03-16 17:04:07 -04:00
if ifWrapper , ok := wrapper .( wrapping . InitFinalizer ); ok {
if err := ifWrapper . Init ( c . Context ); err != nil && ! errors . Is ( err , wrapping . ErrFunctionNotImplemented ) {
c . UI . Error ( fmt . Errorf ( "Error initializing KMS: %w" , err ). Error ())
return base . CommandUserError
2020-08-27 16:38:44 -04:00
}
2022-03-16 17:04:07 -04:00
defer func () {
if err := ifWrapper . Finalize ( context . Background ()); err != nil && ! errors . Is ( err , wrapping . ErrFunctionNotImplemented ) {
c . UI . Warn ( fmt . Errorf ( "Error encountered when finalizing KMS: %w" , err ). Error ())
}
}()
}
2020-06-24 13:08:13 -07:00
2024-02-14 17:14:31 -05:00
d , err := os . ReadFile ( c . flagConfig )
2020-06-24 13:08:13 -07:00
if err != nil {
2020-08-27 16:38:44 -04:00
c . UI . Error ( fmt . Errorf ( "Error reading config file: %w" , err ). Error ())
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-06-24 13:08:13 -07:00
}
2020-08-27 16:38:44 -04:00
raw := string ( d )
2020-06-24 13:08:13 -07:00
2020-08-27 16:38:44 -04:00
raw , err = configutil . EncryptDecrypt ( raw , c . Func == "decrypt" , c . flagStrip , wrapper )
2020-06-24 13:08:13 -07:00
if err != nil {
2020-08-27 16:38:44 -04:00
c . UI . Error ( fmt . Errorf ( "Error %sing via kms: %w" , c . Func , err ). Error ())
2021-03-04 20:05:17 -05:00
return base . CommandCliError
2020-06-24 13:08:13 -07:00
}
if ! c . flagOverwrite {
c . UI . Output ( raw )
2021-03-04 20:05:17 -05:00
return base . CommandSuccess
2020-06-24 13:08:13 -07:00
}
2020-08-27 16:38:44 -04:00
file , err := os . Create ( c . flagConfig )
2020-06-24 13:08:13 -07:00
if err != nil {
c . UI . Error ( fmt . Errorf ( "Error opening file for writing: %w" , err ). Error ())
2021-03-04 20:05:17 -05:00
return base . CommandCliError
2020-06-24 13:08:13 -07:00
}
defer func () {
if err := file . Close (); err != nil {
c . UI . Error ( fmt . Errorf ( "Error closing file after writing: %w" , err ). Error ())
2021-03-04 20:05:17 -05:00
ret = base . CommandCliError
2020-06-24 13:08:13 -07:00
}
}()
n , err := file . WriteString ( raw )
if err != nil {
c . UI . Error ( fmt . Errorf ( "Error writing to file: %w" , err ). Error ())
2021-03-04 20:05:17 -05:00
return base . CommandCliError
2020-06-24 13:08:13 -07:00
}
if n != len ( raw ) {
c . UI . Error ( fmt . Sprintf ( "Wrong number of bytes written to file, expected %d, wrote %d" , len ( raw ), n ))
}
2020-08-27 16:38:44 -04:00
return
2020-06-24 13:08:13 -07:00
}