From 886a6a1b423ecf4b1e94d4031a1f070d189455be 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 --- go.mod | 1 + go.sum | 5 +++-- pkg/vault/credentials.go | 2 +- pkg/vault/credentials_test.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 42cc600..9753599 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/hashicorp/vault/sdk v0.1.13 github.com/prometheus/client_golang v1.8.0 github.com/sirupsen/logrus v1.7.0 + golang.org/x/exp v0.0.0-20240213143201-ec583247a57a gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 k8s.io/api v0.19.3 diff --git a/go.sum b/go.sum index 24d5270..835a68a 100644 --- a/go.sum +++ b/go.sum @@ -149,8 +149,8 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -419,6 +419,8 @@ golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= +golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE= +golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -538,7 +540,6 @@ golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= 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..fe44ff2 100644 --- a/pkg/vault/credentials_test.go +++ b/pkg/vault/credentials_test.go @@ -1,6 +1,8 @@ package vault import ( + "golang.org/x/exp/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) + } + +}