Skip to content

Commit ce819b5

Browse files
committed
fix if env var is url caontining mutiple = signs
1 parent 9e482e1 commit ce819b5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

pkg/vault/credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *Credentials) EnvVars() map[string]string {
3939
envMap := make(map[string]string)
4040

4141
for _, v := range os.Environ() {
42-
splitEnv := strings.Split(v, "=")
42+
splitEnv := strings.SplitN(v, "=", 2)
4343
envMap[splitEnv[0]] = splitEnv[1]
4444
}
4545

pkg/vault/credentials_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package vault
22

33
import (
4+
"maps"
5+
"os"
46
"testing"
57

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

2931
}
32+
33+
func TestEnvVars(t *testing.T) {
34+
c := Credentials{
35+
Username: "user",
36+
Password: "passwd",
37+
}
38+
input := map[string]string{
39+
"DB_URL": "http://localhost:9001",
40+
"DB_URL_PARAMS": "http://localhost:9001?sslmode=force",
41+
"DB_URL_MORE_PARAMS": "http://localhost:9001?sslmode=force&param1=val1&par2=something",
42+
}
43+
expected_output := map[string]string{
44+
"Username": c.Username,
45+
"Password": c.Password,
46+
}
47+
48+
maps.Copy(expected_output, input)
49+
os.Clearenv()
50+
for k, v := range input {
51+
defer os.Unsetenv(k)
52+
err := os.Setenv(k, v)
53+
if err != nil {
54+
t.Error(err)
55+
}
56+
}
57+
58+
result := c.EnvVars()
59+
60+
if !maps.Equal(expected_output, result) {
61+
t.Errorf("Result: %v is not equalt to expected result: %v", result, expected_output)
62+
}
63+
64+
}

0 commit comments

Comments
 (0)