Skip to content

Commit

Permalink
Added support for loading secret value from file
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan0x44 committed Mar 7, 2023
1 parent 924b14b commit 4bd902e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 13 additions & 7 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4bd902e

Please sign in to comment.