Skip to content

Commit

Permalink
refactor: Changed GetRemoteSecrets path to "kv/data/secret/test1" to …
Browse files Browse the repository at this point in the history
…get secrets efficiently
  • Loading branch information
Keloran committed Jun 30, 2024
1 parent 3eb6cd5 commit 9c7ae77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions vault.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vault_helper

import (
"context"
"encoding/json"
"github.com/bugfixes/go-bugfixes/logs"
"github.com/hashicorp/vault/api"
Expand Down Expand Up @@ -60,10 +61,13 @@ type VaultClient interface {

type Vault struct {
Client VaultClient
Context context.Context

Address string
Token string
Lease int
KVSecrets []KVSecret

Lease int
KVSecrets []KVSecret
}

type Details struct {
Expand All @@ -85,7 +89,7 @@ type KVSecretData struct {
func NewVault(address, token string) *Vault {
cfg := api.DefaultConfig()
cfg.Address = address
client, _ := api.NewClient(cfg) // Handle error appropriately
client, _ := api.NewClient(cfg)

return &Vault{
Client: &RealVaultClient{Client: client},
Expand All @@ -94,6 +98,11 @@ func NewVault(address, token string) *Vault {
}
}

func (v *Vault) SetContext(ctx context.Context) *Vault {
v.Context = ctx
return v
}

func (v *Vault) GetSecrets(path string) error {
if strings.HasPrefix(path, ".") || strings.HasPrefix(path, "/") {
return v.GetLocalSecrets(path)
Expand Down
4 changes: 2 additions & 2 deletions vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestGetSecret(t *testing.T) {
testcontainers.WithImage("hashicorp/vault:1.13.0"),
vault.WithToken("root-token"),
vault.WithInitCommand("secrets enable transit", "write -f transit/keys/my-key"),
vault.WithInitCommand("kv put secret/test foo1=bar"))
vault.WithInitCommand("kv put secret/test1 foo1=bar"))
assert.Nil(t, err)
defer func() {
err := vaultContainer.Terminate(ctx)
Expand All @@ -135,7 +135,7 @@ func TestGetSecret(t *testing.T) {
assert.Nil(t, err)

v := NewVault(address, "root-token")
err = v.GetRemoteSecrets("kv/data/secret/test")
err = v.GetRemoteSecrets("kv/data/secret/test1")
assert.Nil(t, err)

secret, err := v.GetSecret("foo1")
Expand Down

0 comments on commit 9c7ae77

Please sign in to comment.