Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove LocalSecretsPath from Details struct and update corresponding methods #32

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading