From ce819b5e00ea3b1143699d07d3d5b46c4e9fe3a0 Mon Sep 17 00:00:00 2001 From: Kamil Krzywicki Date: Fri, 16 Feb 2024 14:11:34 +0100 Subject: [PATCH] fix if env var is url caontining mutiple = signs --- pkg/vault/credentials.go | 2 +- pkg/vault/credentials_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/vault/credentials.go b/pkg/vault/credentials.go index 3796124..966e0ee 100644 --- a/pkg/vault/credentials.go +++ b/pkg/vault/credentials.go @@ -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] } diff --git a/pkg/vault/credentials_test.go b/pkg/vault/credentials_test.go index 95be345..6fd5b7a 100644 --- a/pkg/vault/credentials_test.go +++ b/pkg/vault/credentials_test.go @@ -1,6 +1,8 @@ package vault import ( + "maps" + "os" "testing" "github.com/hashicorp/vault/api" @@ -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¶m1=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) + } + +}