Skip to content

Commit

Permalink
fix if env var is url caontining mutiple = signs
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-stc committed Feb 16, 2024
1 parent 9e482e1 commit ce819b5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/vault/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Credentials) EnvVars() map[string]string {
envMap := make(map[string]string)

for _, v := range os.Environ() {
splitEnv := strings.Split(v, "=")
splitEnv := strings.SplitN(v, "=", 2)
envMap[splitEnv[0]] = splitEnv[1]
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/vault/credentials_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package vault

import (
"maps"

Check failure on line 4 in pkg/vault/credentials_test.go

View workflow job for this annotation

GitHub Actions / test

package maps is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/maps)
"os"
"testing"

"github.com/hashicorp/vault/api"
Expand All @@ -27,3 +29,36 @@ func TestCredentials(t *testing.T) {
}

}

func TestEnvVars(t *testing.T) {
c := Credentials{
Username: "user",
Password: "passwd",
}
input := map[string]string{
"DB_URL": "http://localhost:9001",
"DB_URL_PARAMS": "http://localhost:9001?sslmode=force",
"DB_URL_MORE_PARAMS": "http://localhost:9001?sslmode=force&param1=val1&par2=something",
}
expected_output := map[string]string{
"Username": c.Username,
"Password": c.Password,
}

maps.Copy(expected_output, input)
os.Clearenv()
for k, v := range input {
defer os.Unsetenv(k)
err := os.Setenv(k, v)
if err != nil {
t.Error(err)
}
}

result := c.EnvVars()

if !maps.Equal(expected_output, result) {
t.Errorf("Result: %v is not equalt to expected result: %v", result, expected_output)
}

}

0 comments on commit ce819b5

Please sign in to comment.