Skip to content

Commit

Permalink
Merge pull request #32 from Keloran/update-local-secrets-function
Browse files Browse the repository at this point in the history
refactor: Remove LocalSecretsPath from Details struct and update corresponding methods
  • Loading branch information
Keloran authored Jun 30, 2024
2 parents 7104cd9 + bc976b9 commit f905993
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
9 changes: 8 additions & 1 deletion vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ type Vault struct {
type Details struct {
CredPath string `env:"VAULT_CRED_PATH" envDefault:"secret/data/chewedfeed/creds"`
DetailsPath string `env:"VAULT_DETAILS_PATH" envDefault:"secret/data/chewedfeed/details"`
LocalSecretsPath string `env:"VAULT_LOCAL_SECRETS_PATH" envDefault:"/secrets"`

ExpireTime time.Time
}
Expand All @@ -94,6 +93,14 @@ func NewVault(address, token string) *Vault {
}
}

func (v *Vault) GetSecrets(path string) error {
if strings.HasPrefix(path, ".") || strings.HasPrefix(path, "/") {
return v.GetLocalSecrets(path)
}

return v.GetRemoteSecrets(path)
}

func (v *Vault) GetLocalSecrets(path string) error {
if path == "" {
return logs.Local().Errorf("path: %s, err: %s", path, "no path provided")
Expand Down
46 changes: 45 additions & 1 deletion vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,50 @@ import (
"github.com/stretchr/testify/assert"
)

func TestLocalSecrets(t *testing.T) {
mockLogical := &MockLogical{
MockRead: func(path string) (*api.Secret, error) {
// Return a mock Secret for testing purposes
return &api.Secret{
Data: map[string]interface{}{
"keycloak-realm": "test-realm",
},
}, nil
},
}

mockClient := &MockVaultClient{
MockLogical: func() LogicalClient {
return mockLogical
},
MockSetToken: func(token string) {
// Do nothing or validate the token
},
}

v := &Vault{
Client: mockClient,
Address: "mockaddress",
Token: "mocktoken",
}

// Test path secret
err := v.GetSecrets("./test_data.json")
assert.Nil(t, err)

localSecret, err := v.GetSecret("keycloak-realm")
assert.Nil(t, err)
assert.Equal(t, "test_realm", localSecret)

// test remote
err = v.GetSecrets("mockpath")
assert.Nil(t, err)

remoteSecret, err := v.GetSecret("keycloak-realm")
assert.Nil(t, err)
assert.Equal(t, "test-realm", remoteSecret)
}

func TestParseJSON(t *testing.T) {
v := &Vault{}

Expand All @@ -33,7 +77,7 @@ func TestParseDATA(t *testing.T) {
assert.Equal(t, "test_secret", secret)
}

func TestGetSecrets(t *testing.T) {
func TestGetRemoteSecrets(t *testing.T) {
mockLogical := &MockLogical{
MockRead: func(path string) (*api.Secret, error) {
// Return a mock Secret for testing purposes
Expand Down

0 comments on commit f905993

Please sign in to comment.