Skip to content

Commit

Permalink
refactor: Simplify if conditions in vault and mock files
Browse files Browse the repository at this point in the history
Removed unnecessary if conditions and aligned code in `vault.go` and
`mock.go` files for better readability.
  • Loading branch information
Keloran committed Jun 30, 2024
1 parent 9c7ae77 commit d06adae
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 113 deletions.
8 changes: 4 additions & 4 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type MockVaultHelper struct {
}

func (m *MockVaultHelper) GetSecrets(path string) error {
if path == "" {
return fmt.Errorf("path not found %s", path)
}
if path == "" {
return fmt.Errorf("path not found %s", path)
}

return nil
return nil
}

func (m *MockVaultHelper) GetRemoteSecrets(path string) error {
Expand Down
126 changes: 63 additions & 63 deletions vault.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package vault_helper

import (
"context"
"encoding/json"
"context"
"encoding/json"
"github.com/bugfixes/go-bugfixes/logs"
"github.com/hashicorp/vault/api"
"os"
"strings"
"time"
"os"
"strings"
"time"
)

type VaultDetails struct {
Address string
Token string
Address string
Token string

CredPath string
DetailsPath string
LocalSecretsPath string
CredPath string
DetailsPath string
LocalSecretsPath string

ExpireTime time.Time
ExpireTime time.Time
}

type VaultHelper interface {
GetSecrets(path string) error
GetSecrets(path string) error
GetRemoteSecrets(path string) error
GetLocalSecrets(path string) error
GetLocalSecrets(path string) error
GetSecret(key string) (string, error)
Secrets() []KVSecret
LeaseDuration() int
Expand Down Expand Up @@ -60,21 +60,21 @@ type VaultClient interface {
}

type Vault struct {
Client VaultClient
Context context.Context
Client VaultClient
Context context.Context

Address string
Token string
Address string
Token string

Lease int
KVSecrets []KVSecret
Lease int
KVSecrets []KVSecret
}

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"`
CredPath string `env:"VAULT_CRED_PATH" envDefault:"secret/data/chewedfeed/creds"`
DetailsPath string `env:"VAULT_DETAILS_PATH" envDefault:"secret/data/chewedfeed/details"`

ExpireTime time.Time
ExpireTime time.Time
}

type KVSecret struct {
Expand All @@ -99,60 +99,60 @@ func NewVault(address, token string) *Vault {
}

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

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

return v.GetRemoteSecrets(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")
}

file, err := os.ReadFile(path)
if err != nil {
return logs.Local().Errorf("reading of local file: %s, err: %v", path, err)
}

if strings.HasSuffix(path, ".json") {
jdata, err := ParseJSON(file)
if err != nil {
return logs.Local().Errorf("failed to parse local JSON file: %s, err: %v", string(file), err)
}
secrets, err := ParseData(jdata, "")
if err != nil {
return logs.Local().Errorf("failed to parse post json data: %+v, err: %v", jdata, err)
}

v.KVSecrets = secrets
} else {
fstrng := string(file)
data, err := ParseDATA(fstrng)
if err != nil {
return logs.Local().Errorf("failed to parse local DATA file: %s, err: %v", fstrng, err)
}
secrets, err := ParseData(data, "")
if err != nil {
return logs.Local().Errorf("failed to parse post local data: %+v, err: %v", data, err)
}
v.KVSecrets = secrets
}

return nil
if path == "" {
return logs.Local().Errorf("path: %s, err: %s", path, "no path provided")
}

file, err := os.ReadFile(path)
if err != nil {
return logs.Local().Errorf("reading of local file: %s, err: %v", path, err)
}

if strings.HasSuffix(path, ".json") {
jdata, err := ParseJSON(file)
if err != nil {
return logs.Local().Errorf("failed to parse local JSON file: %s, err: %v", string(file), err)
}
secrets, err := ParseData(jdata, "")
if err != nil {
return logs.Local().Errorf("failed to parse post json data: %+v, err: %v", jdata, err)
}

v.KVSecrets = secrets
} else {
fstrng := string(file)
data, err := ParseDATA(fstrng)
if err != nil {
return logs.Local().Errorf("failed to parse local DATA file: %s, err: %v", fstrng, err)
}
secrets, err := ParseData(data, "")
if err != nil {
return logs.Local().Errorf("failed to parse post local data: %+v, err: %v", data, err)
}
v.KVSecrets = secrets
}

return nil
}

func ParseJSON(data []byte) (map[string]interface{}, error) {
var parsedData map[string]interface{}
err := json.Unmarshal(data, &parsedData)
if err != nil {
return nil, logs.Local().Errorf("error unmarshalling JSON: %v", err)
return nil, logs.Local().Errorf("error unmarshalling JSON: %v", err)
}
return parsedData, nil
}
Expand Down
90 changes: 44 additions & 46 deletions vault_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package vault_helper

import (
"context"
"context"
"testing"

"github.com/hashicorp/vault/api"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/vault"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/vault"
)

func TestLocalSecrets(t *testing.T) {
mockLogical := &MockLogical{
mockLogical := &MockLogical{
MockRead: func(path string) (*api.Secret, error) {
// Return a mock Secret for testing purposes
return &api.Secret{
Expand All @@ -37,47 +37,45 @@ func TestLocalSecrets(t *testing.T) {
Token: "mocktoken",
}

// Test path secret
err := v.GetSecrets("./test_data.json")
assert.Nil(t, err)
// 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)
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)
// 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)
remoteSecret, err := v.GetSecret("keycloak-realm")
assert.Nil(t, err)
assert.Equal(t, "test-realm", remoteSecret)
}

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

err := v.GetLocalSecrets("./test_data.json")
assert.Nil(t, err)
v := &Vault{}

err := v.GetLocalSecrets("./test_data.json")
assert.Nil(t, err)

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

assert.Equal(t, "test_realm", secret)
assert.Equal(t, "test_realm", secret)
}

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

err := v.GetLocalSecrets("./test_data")
assert.Nil(t, err)
v := &Vault{}

err := v.GetLocalSecrets("./test_data")
assert.Nil(t, err)

secret, err := v.GetSecret("keycloak-secret")
assert.Nil(t, err)
secret, err := v.GetSecret("keycloak-secret")
assert.Nil(t, err)

assert.Equal(t, "test_secret", secret)
assert.Equal(t, "test_secret", secret)
}

func TestGetRemoteSecrets(t *testing.T) {
Expand Down Expand Up @@ -119,22 +117,22 @@ func TestNewVault(t *testing.T) {
}

func TestGetSecret(t *testing.T) {
ctx := context.Background()
vaultContainer, err := vault.RunContainer(ctx,
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/test1 foo1=bar"))
assert.Nil(t, err)
defer func() {
err := vaultContainer.Terminate(ctx)
assert.Nil(t, err)
}()

address, err := vaultContainer.HttpHostAddress(ctx)
assert.Nil(t, err)

v := NewVault(address, "root-token")
ctx := context.Background()
vaultContainer, err := vault.RunContainer(ctx,
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/test1 foo1=bar"))
assert.Nil(t, err)
defer func() {
err := vaultContainer.Terminate(ctx)
assert.Nil(t, err)
}()

address, err := vaultContainer.HttpHostAddress(ctx)
assert.Nil(t, err)

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

Expand Down

0 comments on commit d06adae

Please sign in to comment.