Skip to content

Commit

Permalink
Return data and metadata of a secret
Browse files Browse the repository at this point in the history
  • Loading branch information
slarwise committed Aug 3, 2024
1 parent f0ccc0e commit b4cb5a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ func main() {
key = "/" + key
}
secret := vault.getSecret(key)
for key, val := range secret {
fmt.Printf("%s: %v\n", key, val)
output, err := json.MarshalIndent(secret, "", " ")
if err != nil {
fatal("Could not marshal secret as json: %s", err)
}
fmt.Println(string(output))
} else {
fatal("Subcommand must be one of `tree` or `get`, got %s", subcommand)
}
Expand Down Expand Up @@ -153,7 +155,12 @@ func (v VaultClient) listDir(name string) []DirEnt {
return entries
}

type Secret map[string]interface{}
type Secret struct {
Data struct {
Data map[string]interface{} `json:"data"`
Metadata map[string]interface{} `json:"metadata"`
} `json:"data"`
}

func (v VaultClient) getSecret(name string) Secret {
url := fmt.Sprintf("%s/v1/%s/data%s", v.Addr, v.Mount, name)
Expand All @@ -175,13 +182,7 @@ func (v VaultClient) getSecret(name string) Secret {
if err != nil {
panic(err)
}
getResponse := struct {
Data struct {
Data map[string]interface{}
}
}{}
if err := json.Unmarshal(body, &getResponse); err != nil {
panic(err)
}
return getResponse.Data.Data
var secret Secret
json.Unmarshal(body, &secret)
return secret
}
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func TestGetSecret(t *testing.T) {
Mount: "secret",
}
secret := vault.getSecret("/bar/baz")
data, found := secret["c"]
data, found := secret.Data.Data["c"]
if !found || data != "d" {
t.Fatalf("Expected secret to have data `c=d`, got %v", secret)
t.Fatalf("Expected secret to have data `c=d`, got %v", secret.Data.Data)
}
}

Expand Down

0 comments on commit b4cb5a5

Please sign in to comment.