2022-11-29 14:07:04 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
2023-01-11 20:04:57 +00:00
|
|
|
"strconv"
|
2022-11-29 14:07:04 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/internalshared/configutil"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
// logFlags are the 'log' related flags that can be shared across commands.
|
|
|
|
type logFlags struct {
|
|
|
|
flagCombineLogs bool
|
|
|
|
flagLogLevel string
|
|
|
|
flagLogFormat string
|
|
|
|
flagLogFile string
|
2023-01-11 20:04:57 +00:00
|
|
|
flagLogRotateBytes int
|
2022-11-29 14:07:04 +00:00
|
|
|
flagLogRotateDuration string
|
2023-01-11 20:04:57 +00:00
|
|
|
flagLogRotateMaxFiles int
|
2022-11-29 14:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// valuesProvider has the intention of providing a way to supply a func with a
|
|
|
|
// way to retrieve values for flags and environment variables without having to
|
2023-01-11 20:04:57 +00:00
|
|
|
// directly call a specific implementation.
|
|
|
|
// The reasoning for its existence is to facilitate testing.
|
2022-11-29 14:07:04 +00:00
|
|
|
type valuesProvider struct {
|
2023-01-11 20:04:57 +00:00
|
|
|
flagProvider func(string) (flag.Value, bool)
|
|
|
|
envVarProvider func(string) (string, bool)
|
2022-11-29 14:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addLogFlags will add the set of 'log' related flags to a flag set.
|
|
|
|
func (f *FlagSet) addLogFlags(l *logFlags) {
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: flagNameCombineLogs,
|
|
|
|
Target: &l.flagCombineLogs,
|
|
|
|
Default: false,
|
|
|
|
Hidden: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: flagNameLogLevel,
|
|
|
|
Target: &l.flagLogLevel,
|
|
|
|
Default: notSetValue,
|
|
|
|
EnvVar: EnvVaultLogLevel,
|
|
|
|
Completion: complete.PredictSet("trace", "debug", "info", "warn", "error"),
|
|
|
|
Usage: "Log verbosity level. Supported values (in order of detail) are " +
|
|
|
|
"\"trace\", \"debug\", \"info\", \"warn\", and \"error\".",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: flagNameLogFormat,
|
|
|
|
Target: &l.flagLogFormat,
|
|
|
|
Default: notSetValue,
|
|
|
|
EnvVar: EnvVaultLogFormat,
|
|
|
|
Completion: complete.PredictSet("standard", "json"),
|
|
|
|
Usage: `Log format. Supported values are "standard" and "json".`,
|
|
|
|
})
|
|
|
|
|
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: flagNameLogFile,
|
|
|
|
Target: &l.flagLogFile,
|
|
|
|
Usage: "Path to the log file that Vault should use for logging",
|
|
|
|
})
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
f.IntVar(&IntVar{
|
2022-11-29 14:07:04 +00:00
|
|
|
Name: flagNameLogRotateBytes,
|
|
|
|
Target: &l.flagLogRotateBytes,
|
|
|
|
Usage: "Number of bytes that should be written to a log before it needs to be rotated. " +
|
|
|
|
"Unless specified, there is no limit to the number of bytes that can be written to a log file",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: flagNameLogRotateDuration,
|
|
|
|
Target: &l.flagLogRotateDuration,
|
|
|
|
Usage: "The maximum duration a log should be written to before it needs to be rotated. " +
|
|
|
|
"Must be a duration value such as 30s",
|
|
|
|
})
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
f.IntVar(&IntVar{
|
2022-11-29 14:07:04 +00:00
|
|
|
Name: flagNameLogRotateMaxFiles,
|
|
|
|
Target: &l.flagLogRotateMaxFiles,
|
|
|
|
Usage: "The maximum number of older log file archives to keep",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
// envVarValue attempts to get a named value from the environment variables.
|
|
|
|
// The value will be returned as a string along with a boolean value indiciating
|
|
|
|
// to the caller whether the named env var existed.
|
|
|
|
func envVarValue(key string) (string, bool) {
|
|
|
|
if key == "" {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return os.LookupEnv(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flagValue attempts to find the named flag in a set of FlagSets.
|
|
|
|
// The flag.Value is returned if it was specified, and the boolean value indicates
|
|
|
|
// to the caller if the flag was specified by the end user.
|
|
|
|
func (f *FlagSets) flagValue(flagName string) (flag.Value, bool) {
|
|
|
|
var result flag.Value
|
2022-11-29 14:07:04 +00:00
|
|
|
var isFlagSpecified bool
|
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
f.Visit(func(fl *flag.Flag) {
|
|
|
|
if fl.Name == flagName {
|
2023-01-11 20:04:57 +00:00
|
|
|
result = fl.Value
|
2022-11-29 14:07:04 +00:00
|
|
|
isFlagSpecified = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, isFlagSpecified
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
// overrideValue uses the provided keys to check CLI flags and environment
|
2022-11-29 14:07:04 +00:00
|
|
|
// variables for values that may be used to override any specified configuration.
|
2023-01-11 20:04:57 +00:00
|
|
|
func (p *valuesProvider) overrideValue(flagKey, envVarKey string) (string, bool) {
|
2022-11-29 14:07:04 +00:00
|
|
|
var result string
|
2023-01-11 20:04:57 +00:00
|
|
|
found := true
|
2022-11-29 14:07:04 +00:00
|
|
|
|
|
|
|
flg, flgFound := p.flagProvider(flagKey)
|
|
|
|
env, envFound := p.envVarProvider(envVarKey)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case flgFound:
|
2023-01-11 20:04:57 +00:00
|
|
|
result = flg.String()
|
2022-11-29 14:07:04 +00:00
|
|
|
case envFound:
|
|
|
|
result = env
|
|
|
|
default:
|
2023-01-11 20:04:57 +00:00
|
|
|
found = false
|
2022-11-29 14:07:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
return result, found
|
2022-11-29 14:07:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
// applyLogConfigOverrides will accept a shared config and specifically attempt to update the 'log' related config keys.
|
|
|
|
// For each 'log' key, we aggregate file config, env vars and CLI flags to select the one with the highest precedence.
|
2022-11-29 14:07:04 +00:00
|
|
|
// This method mutates the config object passed into it.
|
2023-01-11 20:04:57 +00:00
|
|
|
func (f *FlagSets) applyLogConfigOverrides(config *configutil.SharedConfig) {
|
2022-11-29 14:07:04 +00:00
|
|
|
p := &valuesProvider{
|
2023-01-11 20:04:57 +00:00
|
|
|
flagProvider: f.flagValue,
|
|
|
|
envVarProvider: envVarValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update log level
|
|
|
|
if val, found := p.overrideValue(flagNameLogLevel, EnvVaultLogLevel); found {
|
|
|
|
config.LogLevel = val
|
2022-11-29 14:07:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:04:57 +00:00
|
|
|
// Update log format
|
|
|
|
if val, found := p.overrideValue(flagNameLogFormat, EnvVaultLogFormat); found {
|
|
|
|
config.LogFormat = val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update log file name
|
|
|
|
if val, found := p.overrideValue(flagNameLogFile, ""); found {
|
|
|
|
config.LogFile = val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update log rotation duration
|
|
|
|
if val, found := p.overrideValue(flagNameLogRotateDuration, ""); found {
|
|
|
|
config.LogRotateDuration = val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update log max files
|
|
|
|
if val, found := p.overrideValue(flagNameLogRotateMaxFiles, ""); found {
|
|
|
|
config.LogRotateMaxFiles, _ = strconv.Atoi(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update log rotation max bytes
|
|
|
|
if val, found := p.overrideValue(flagNameLogRotateBytes, ""); found {
|
|
|
|
config.LogRotateBytes, _ = strconv.Atoi(val)
|
|
|
|
}
|
2022-11-29 14:07:04 +00:00
|
|
|
}
|