From 4bd902e759d3059124f771e414d247d1a4c8f594 Mon Sep 17 00:00:00 2001 From: Ryan D Date: Tue, 7 Mar 2023 23:02:52 +1100 Subject: [PATCH] Added support for loading secret value from file --- main.go | 1 - prompt.go | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 3c4342c..b4ec684 100644 --- a/main.go +++ b/main.go @@ -183,7 +183,6 @@ func rotate(filename string) { } PromptClear(os.Stdout) - // TODO: parse filenames and read file contents // TODO: support creating new sealed secrets from scratch newSecrets := secrets.ToValues() secretYAML, err := createSecretYAML( diff --git a/prompt.go b/prompt.go index fd10b73..7a4907f 100644 --- a/prompt.go +++ b/prompt.go @@ -16,9 +16,10 @@ type PromptSecrets struct { } type PromptSecretInput struct { - key string - kind PromptSecretInput_Kind - value string + key string + kind PromptSecretInput_Kind + value string + valueFromFile string } type PromptSecretInput_Kind string @@ -39,8 +40,12 @@ func (s *PromptSecrets) InitKeys(keys []string) { func (s PromptSecrets) ToValues() map[string]string { values := map[string]string{} for _, s := range s.secrets { - // todo:read file - if s.kind != PromptSecretInput_Kind_None { + if s.kind == PromptSecretInput_Kind_None { + continue + } + if s.kind == PromptSecretInput_Kind_File { + values[s.key] = s.valueFromFile + } else { values[s.key] = s.value } } @@ -70,11 +75,12 @@ func (s *PromptSecrets) Enter(redo int, input io.Reader, output io.Writer) (err key: secret.key, value: strings.TrimSpace(value), } - valueStatInfo, valueStatErr := os.Stat(value) + valueFromFile, readFileErr := os.ReadFile(value) if value == "" { s.secrets[i].kind = PromptSecretInput_Kind_None - } else if valueStatErr == nil && !valueStatInfo.IsDir() { + } else if readFileErr == nil { s.secrets[i].kind = PromptSecretInput_Kind_File + s.secrets[i].valueFromFile = string(valueFromFile) } else { s.secrets[i].kind = PromptSecretInput_Kind_String }