Skip to content

Commit

Permalink
Merge pull request #29 from Keloran/addedLocal
Browse files Browse the repository at this point in the history
Added local
  • Loading branch information
Keloran authored Jun 28, 2024
2 parents beb7ea7 + ff79478 commit c790e83
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
2 changes: 2 additions & 0 deletions test_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data: map[bugfixes-agentid:test_agentid bugfixes-secret:test_secret influx-bucket:test_bucket influx-hostname:http://test.test influx-org:test_org influx-token:test_token keycloak-client:test_client keycloak-realm:test_realm keycloak-secret:test_secret rds-db:test_db rds-hostname:test.test rds-port:5432 stripe-secret:test_secret]
metadata: map[created_time:2024-06-13T19:40:42.663263331Z custom_metadata:<nil> deletion_time: destroyed:false version:4]
1 change: 1 addition & 0 deletions test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"bugfixes-agentid":"test_agent","bugfixes-secret":"test_secret","influx-bucket":"test_bucket","influx-hostname":"http://test.test","influx-org":"test_org","influx-token":"test_token","keycloak-client":"test_client","keycloak-realm":"test_realm","keycloak-secret":"test_secret","rds-db":"test_db","rds-hostname":"test.test","rds-port":"5432","stripe-secret":"test_secret"}
87 changes: 86 additions & 1 deletion vault.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package vault_helper

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

Expand All @@ -12,6 +15,7 @@ type VaultDetails struct {

CredPath string
DetailsPath string
LocalSecretsPath string

ExpireTime time.Time
}
Expand Down Expand Up @@ -63,6 +67,7 @@ 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 @@ -88,7 +93,53 @@ func NewVault(address, token string) *Vault {
}
}

func (v *Vault) GetSecrets(path string) error {
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
}

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 parsedData, nil
}

func (v *Vault) GetRemoteSecrets(path string) error {
if path == "" {
return logs.Local().Errorf("path: %s, err: %s", path, "no path provided")
}
Expand Down Expand Up @@ -127,6 +178,40 @@ func (v *Vault) GetSecret(key string) (string, error) {
return "", logs.Local().Errorf("key: '%s' not found", key)
}

func ParseDATA(data string) (map[string]interface{}, error) {
parsedData := make(map[string]interface{})
lines := strings.Split(data, "\n")
for _, line := range lines {
parts := strings.SplitN(line, ": ", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])

// Remove the "map[" and "]" parts from the value string
if strings.HasPrefix(value, "map[") {
value = strings.TrimPrefix(value, "map[")
value = strings.TrimSuffix(value, "]")

innerMap := make(map[string]interface{})
innerParts := strings.Split(value, " ")
for _, innerPart := range innerParts {
innerKV := strings.SplitN(innerPart, ":", 2)
if len(innerKV) != 2 {
continue
}
innerMap[innerKV[0]] = innerKV[1]
}
parsedData[key] = innerMap
} else {
parsedData[key] = value
}
}

return parsedData, nil
}

func ParseData(data map[string]interface{}, filterName string) ([]KVSecret, error) {
var secrets []KVSecret
for k, v := range data {
Expand Down
30 changes: 28 additions & 2 deletions vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ import (
"github.com/stretchr/testify/assert"
)

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

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


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

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

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

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


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

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

func TestGetSecrets(t *testing.T) {
mockLogical := &MockLogical{
MockRead: func(path string) (*api.Secret, error) {
Expand Down Expand Up @@ -35,7 +61,7 @@ func TestGetSecrets(t *testing.T) {
Token: "mocktoken",
}

err := v.GetSecrets("mockpath")
err := v.GetRemoteSecrets("mockpath")
assert.Nil(t, err)
// Add more assertions based on the expected behavior
}
Expand Down Expand Up @@ -73,7 +99,7 @@ func TestGetSecret(t *testing.T) {
Token: "mocktoken",
}

err := v.GetSecrets("mockpath")
err := v.GetRemoteSecrets("mockpath")
assert.Nil(t, err)

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

0 comments on commit c790e83

Please sign in to comment.