lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 9c7f86eede5b9d57c5b4f122182f7f84a1c1da3f
parent b83e31744e3b1465ab9b19d23a272e93a0a5c603
Author: Sean Enck <sean@ttypty.com>
Date:   Wed, 26 Jul 2023 18:44:49 -0400

specify these as structs

Diffstat:
Minternal/backend/query.go | 2+-
Minternal/inputs/env.go | 18++++++++++++------
Minternal/inputs/vars.go | 39++++++++++++++++-----------------------
Minternal/inputs/vars_test.go | 20++++++++++----------
Minternal/platform/clipboard.go | 2+-
5 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/internal/backend/query.go b/internal/backend/query.go @@ -128,7 +128,7 @@ func (t *Transaction) QueryCallback(args QueryOptions) ([]QueryEntity, error) { } var hashLength int if jsonMode == inputs.JSONDataOutputHash { - hashLength, err = inputs.GetHashLength() + hashLength, err = inputs.EnvHashLength.Get() if err != nil { return nil, err } diff --git a/internal/inputs/env.go b/internal/inputs/env.go @@ -28,6 +28,12 @@ var isYesNoArgs = []string{yes, no} type ( // SystemPlatform represents the platform lockbox is running on. SystemPlatform string + environmentInt struct { + key string + defaultValue int + shortDesc string + allowZero bool + } ) // Shlex will do simple shell command lex-ing @@ -68,9 +74,9 @@ func isYesNoEnv(defaultValue bool, envKey string) (bool, error) { return false, fmt.Errorf("invalid yes/no env value for %s", envKey) } -func getPositiveIntEnv(defaultVal int, key, desc string, canBeZero bool) (int, error) { - val := defaultVal - use := os.Getenv(key) +func (e environmentInt) Get() (int, error) { + val := e.defaultValue + use := os.Getenv(e.key) if use != "" { i, err := strconv.Atoi(use) if err != nil { @@ -78,17 +84,17 @@ func getPositiveIntEnv(defaultVal int, key, desc string, canBeZero bool) (int, e } invalid := false check := "" - if canBeZero { + if e.allowZero { check = "=" } switch i { case 0: - invalid = !canBeZero + invalid = !e.allowZero default: invalid = i < 0 } if invalid { - return -1, fmt.Errorf("%s must be >%s 0", desc, check) + return -1, fmt.Errorf("%s must be >%s 0", e.shortDesc, check) } val = i } diff --git a/internal/inputs/vars.go b/internal/inputs/vars.go @@ -33,8 +33,7 @@ const ( // PlatformEnv is the platform lb is running on. PlatformEnv = prefixKey + "PLATFORM" // StoreEnv is the location of the filesystem store that lb is operating on. - StoreEnv = prefixKey + "STORE" - clipMaxEnv = clipBaseEnv + "MAX" + StoreEnv = prefixKey + "STORE" // ColorBetweenEnv is a comma-delimited list of times to color totp outputs (e.g. 0:5,30:35 which is the default). ColorBetweenEnv = fieldTOTPEnv + "_BETWEEN" // MaxTOTPTime indicate how long TOTP tokens will be shown @@ -42,13 +41,12 @@ const ( // ClipPasteEnv allows overriding the clipboard paste command ClipPasteEnv = clipBaseEnv + "PASTE" // ClipCopyEnv allows overriding the clipboard copy command - ClipCopyEnv = clipBaseEnv + "COPY" - clipOSC52Env = clipBaseEnv + "OSC52" - defaultTOTPField = "totp" - commandArgsExample = "[cmd args...]" - defaultMaxClipboard = 45 - detectedValue = "(detected)" - noTOTPEnv = prefixKey + "NOTOTP" + ClipCopyEnv = clipBaseEnv + "COPY" + clipOSC52Env = clipBaseEnv + "OSC52" + defaultTOTPField = "totp" + commandArgsExample = "[cmd args...]" + detectedValue = "(detected)" + noTOTPEnv = prefixKey + "NOTOTP" // HookDirEnv represents a stored location for user hooks HookDirEnv = prefixKey + "HOOKDIR" // ModTimeEnv is modtime override ability for entries @@ -59,8 +57,6 @@ const ( MaxTOTPTimeDefault = "120" // JSONDataOutputEnv controls how JSON is output JSONDataOutputEnv = prefixKey + "JSON_DATA_OUTPUT" - defaultHashLength = 0 - hashJSONLengthEnv = JSONDataOutputEnv + "_HASH_LENGTH" // JSONDataOutputHash means output data is hashed JSONDataOutputHash JSONOutputMode = "hash" // JSONDataOutputBlank means an empty entry is set @@ -69,6 +65,13 @@ const ( JSONDataOutputRaw JSONOutputMode = "plaintext" ) +var ( + // EnvClipboardMax gets the maximum clipboard time + EnvClipboardMax = environmentInt{key: clipBaseEnv + "MAX", shortDesc: "clipboard max time", allowZero: false, defaultValue: 45} + // EnvHashLength handles the hashing output length + EnvHashLength = environmentInt{key: JSONDataOutputEnv + "_HASH_LENGTH", shortDesc: "hash length", allowZero: true, defaultValue: 0} +) + type ( // JSONOutputMode is the output mode definition JSONOutputMode string @@ -117,16 +120,6 @@ func GetReKey(args []string) ([]string, error) { return out, nil } -// GetClipboardMax will get max time to keep an entry in the clipboard before clearing -func GetClipboardMax() (int, error) { - return getPositiveIntEnv(defaultMaxClipboard, clipMaxEnv, "clipboard max time", false) -} - -// GetHashLength will get the maximum hash length allowed in JSON output hashing mode -func GetHashLength() (int, error) { - return getPositiveIntEnv(defaultHashLength, hashJSONLengthEnv, "hash length", true) -} - // GetKey will get the encryption key setup for lb func GetKey() ([]byte, error) { useKeyMode := os.Getenv(keyModeEnv) @@ -233,7 +226,7 @@ func ListEnvironmentVariables(showValues bool) []string { results = append(results, e.formatEnvironmentVariable(false, ColorBetweenEnv, TOTPDefaultBetween, "override when to set totp generated outputs to different colors, must be a\nlist of one (or more) rules where a semicolon delimits the start and end\nsecond (0-60 for each)", []string{"start:end,start:end,start:end..."})) results = append(results, e.formatEnvironmentVariable(false, ClipPasteEnv, detectedValue, "override the detected platform paste command", []string{commandArgsExample})) results = append(results, e.formatEnvironmentVariable(false, ClipCopyEnv, detectedValue, "override the detected platform copy command", []string{commandArgsExample})) - results = append(results, e.formatEnvironmentVariable(false, clipMaxEnv, fmt.Sprintf("%d", defaultMaxClipboard), "override the amount of time before totp clears the clipboard (e.g. 10),\nmust be an integer", []string{"integer"})) + results = append(results, e.formatEnvironmentVariable(false, EnvClipboardMax.key, fmt.Sprintf("%d", EnvClipboardMax.defaultValue), "override the amount of time before totp clears the clipboard (e.g. 10),\nmust be an integer", []string{"integer"})) results = append(results, e.formatEnvironmentVariable(false, PlatformEnv, detectedValue, "override the detected platform", PlatformSet())) results = append(results, e.formatEnvironmentVariable(false, noTOTPEnv, no, "disable TOTP integrations", isYesNoArgs)) results = append(results, e.formatEnvironmentVariable(false, HookDirEnv, "", "the path to hooks to execute on actions against the database", []string{"directory"})) @@ -241,7 +234,7 @@ func ListEnvironmentVariables(showValues bool) []string { results = append(results, e.formatEnvironmentVariable(false, KeyFileEnv, "", "additional keyfile to access/protect the database", []string{"keyfile"})) results = append(results, e.formatEnvironmentVariable(false, ModTimeEnv, ModTimeFormat, fmt.Sprintf("input modification time to set for the entry\n(expected format: %s)", ModTimeFormat), []string{"modtime"})) results = append(results, e.formatEnvironmentVariable(false, JSONDataOutputEnv, string(JSONDataOutputHash), fmt.Sprintf("changes what the data field in JSON outputs will contain\nuse '%s' with CAUTION", JSONDataOutputRaw), []string{string(JSONDataOutputRaw), string(JSONDataOutputHash), string(JSONDataOutputBlank)})) - results = append(results, e.formatEnvironmentVariable(false, hashJSONLengthEnv, fmt.Sprintf("%d", defaultHashLength), fmt.Sprintf("maximum hash length the JSON output should contain\nwhen '%s' mode is set for JSON output", JSONDataOutputHash), []string{"integer"})) + results = append(results, e.formatEnvironmentVariable(false, EnvHashLength.key, fmt.Sprintf("%d", EnvHashLength.defaultValue), fmt.Sprintf("maximum hash length the JSON output should contain\nwhen '%s' mode is set for JSON output", JSONDataOutputHash), []string{"integer"})) return results } diff --git a/internal/inputs/vars_test.go b/internal/inputs/vars_test.go @@ -149,25 +149,25 @@ func TestReKey(t *testing.T) { func TestGetClipboardMax(t *testing.T) { os.Setenv("LOCKBOX_CLIP_MAX", "") defer os.Clearenv() - max, err := inputs.GetClipboardMax() + max, err := inputs.EnvClipboardMax.Get() if err != nil || max != 45 { t.Error("invalid clipboard read") } os.Setenv("LOCKBOX_CLIP_MAX", "1") - max, err = inputs.GetClipboardMax() + max, err = inputs.EnvClipboardMax.Get() if err != nil || max != 1 { t.Error("invalid clipboard read") } os.Setenv("LOCKBOX_CLIP_MAX", "-1") - if _, err := inputs.GetClipboardMax(); err == nil || err.Error() != "clipboard max time must be > 0" { + if _, err := inputs.EnvClipboardMax.Get(); err == nil || err.Error() != "clipboard max time must be > 0" { t.Errorf("invalid err: %v", err) } os.Setenv("LOCKBOX_CLIP_MAX", "alk;ja") - if _, err := inputs.GetClipboardMax(); err == nil || err.Error() != "strconv.Atoi: parsing \"alk;ja\": invalid syntax" { + if _, err := inputs.EnvClipboardMax.Get(); err == nil || err.Error() != "strconv.Atoi: parsing \"alk;ja\": invalid syntax" { t.Errorf("invalid err: %v", err) } os.Setenv("LOCKBOX_CLIP_MAX", "0") - if _, err := inputs.GetClipboardMax(); err == nil || err.Error() != "clipboard max time must be > 0" { + if _, err := inputs.EnvClipboardMax.Get(); err == nil || err.Error() != "clipboard max time must be > 0" { t.Errorf("invalid err: %v", err) } } @@ -175,26 +175,26 @@ func TestGetClipboardMax(t *testing.T) { func TestGetHashLength(t *testing.T) { os.Setenv("LOCKBOX_JSON_DATA_OUTPUT_HASH_LENGTH", "") defer os.Clearenv() - val, err := inputs.GetHashLength() + val, err := inputs.EnvHashLength.Get() if err != nil || val != 0 { t.Error("invalid hash read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT_HASH_LENGTH", "1") - val, err = inputs.GetHashLength() + val, err = inputs.EnvHashLength.Get() if err != nil || val != 1 { t.Error("invalid hash read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT_HASH_LENGTH", "0") - val, err = inputs.GetHashLength() + val, err = inputs.EnvHashLength.Get() if err != nil || val != 0 { t.Error("invalid hash read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT_HASH_LENGTH", "-1") - if _, err := inputs.GetHashLength(); err == nil || err.Error() != "hash length must be >= 0" { + if _, err := inputs.EnvHashLength.Get(); err == nil || err.Error() != "hash length must be >= 0" { t.Errorf("invalid err: %v", err) } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT_HASH_LENGTH", "-aoaofaij;p1") - if _, err := inputs.GetHashLength(); err == nil || err.Error() != "strconv.Atoi: parsing \"-aoaofaij;p1\": invalid syntax" { + if _, err := inputs.EnvHashLength.Get(); err == nil || err.Error() != "strconv.Atoi: parsing \"-aoaofaij;p1\": invalid syntax" { t.Errorf("invalid err: %v", err) } } diff --git a/internal/platform/clipboard.go b/internal/platform/clipboard.go @@ -23,7 +23,7 @@ type ( ) func newClipboard(copying, pasting []string) (Clipboard, error) { - max, err := inputs.GetClipboardMax() + max, err := inputs.EnvClipboardMax.Get() if err != nil { return Clipboard{}, err }