From 421d60177b06c54ee1ee5ab5cce42ccb2c4eaf55 Mon Sep 17 00:00:00 2001 From: Marco Zoveralli Date: Thu, 10 Jun 2021 14:41:16 +0200 Subject: [PATCH] fix(decode): fixing decode function for base64 secrets --- cmd/synchronizer/main.go | 2 +- cmd/synchronizer/main_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/synchronizer/main.go b/cmd/synchronizer/main.go index fc3aebf..ce0c179 100644 --- a/cmd/synchronizer/main.go +++ b/cmd/synchronizer/main.go @@ -334,7 +334,7 @@ func getEnv(key, fallback string) string { func decode(s string) ([]byte, error) { switch { case strings.HasPrefix(s, "base64:"): - return base64.StdEncoding.DecodeString(strings.TrimLeft(s, "base64:")) + return base64.StdEncoding.DecodeString(strings.TrimPrefix(s, "base64:")) default: return []byte(s), nil } diff --git a/cmd/synchronizer/main_test.go b/cmd/synchronizer/main_test.go index 3c87b61..310129c 100644 --- a/cmd/synchronizer/main_test.go +++ b/cmd/synchronizer/main_test.go @@ -11,6 +11,7 @@ import ( const ( quote = "The fool don‘t think he is wise, but the wise man knows himself to be a fool." + trivialString = "h" ) func TestDecode(t *testing.T) { @@ -27,6 +28,13 @@ func TestDecode(t *testing.T) { assert.Equal(t, quote, string(res)) }) + t.Run("base64 encoded", func(t *testing.T) { + str := "base64:" + base64.StdEncoding.EncodeToString([]byte(trivialString)) + res, err := decode(str) + assert.NoError(t, err) + assert.Equal(t, trivialString, string(res)) + }) + t.Run("base64 decode fails", func(t *testing.T) { str := "base64:" + quote _, err := decode(str)