Skip to content

Commit

Permalink
Cache fetching secrets when interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
slarwise committed Aug 7, 2024
1 parent 92ed3e5 commit e83842d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {
}
fmt.Println(string(output))
case "interactive":
cachedSecrets = make(map[string]Secret)
slog.SetLogLoggerLevel(slog.LevelError)
screen, err := tcell.NewScreen()
if err != nil {
Expand Down Expand Up @@ -266,7 +267,12 @@ type Secret struct {
} `json:"data"`
}

var cachedSecrets map[string]Secret

func (v VaultClient) getSecret(name string) (Secret, error) {
if secret, found := cachedSecrets[name]; found {
return secret, nil
}
url := fmt.Sprintf("%s/v1/%s/data%s", v.Addr, v.Mount, name)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down Expand Up @@ -294,6 +300,7 @@ func (v VaultClient) getSecret(name string) (Secret, error) {
if response.StatusCode != 200 && isErrorForRealForReal {
return Secret{}, fmt.Errorf("Got %s on url %s", response.Status, url)
}
cachedSecrets[name] = secret
return secret, nil
}

Expand Down

0 comments on commit e83842d

Please sign in to comment.