From 25aacc9b2fb8f01105cf2aed726dc8248d2e9805 Mon Sep 17 00:00:00 2001 From: Jonas-Taha El Sesiy Date: Fri, 3 Jan 2025 09:22:26 -0800 Subject: [PATCH] Add support for helm secrets (#54) Prior to this change, the plugin always assumed secrets to be of type `Opaque`. This change introduces the secret type for (de)serialization and adds a small interface that is used to keep the core logic mostly unchanged but handle different types of secrets. Only support for helm was added for now but expanding to more types should be straight-forward Fixes #50 --- hack/kind-bootstrap.sh | 22 ++++++++ pkg/cmd/decode.go | 51 ++++++++++++++++++ pkg/cmd/decode_test.go | 105 ++++++++++++++++++++++++++++++++++++ pkg/cmd/types.go | 34 ++++++++++++ pkg/cmd/types_test.go | 87 ++++++++++++++++++++---------- pkg/cmd/view-secret.go | 13 +++-- pkg/cmd/view-secret_test.go | 55 +++++++++++++++++-- 7 files changed, 328 insertions(+), 39 deletions(-) create mode 100644 pkg/cmd/decode.go create mode 100644 pkg/cmd/decode_test.go diff --git a/hack/kind-bootstrap.sh b/hack/kind-bootstrap.sh index da89fc8..c396191 100755 --- a/hack/kind-bootstrap.sh +++ b/hack/kind-bootstrap.sh @@ -68,3 +68,25 @@ kind: Namespace metadata: name: empty EOF + +## 'helm' namespace +kubectl apply -f - <": { secret, + Opaque, []string{ "TEST_CONN_STR='mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin'", "TEST_PASSWORD='secret\n'", @@ -133,6 +141,7 @@ func TestProcessSecret(t *testing.T) { }, "view-secret ": { secretSingle, + Opaque, []string{"secret"}, []string{fmt.Sprintf(singleKeyDescription, "SINGLE_PASSWORD")}, "", @@ -140,9 +149,29 @@ func TestProcessSecret(t *testing.T) { nil, "", }, - "view-secret test TEST_PASSWORD": {secret, []string{"secret"}, nil, "TEST_PASSWORD", false, nil, ""}, + "view-secret ": { + secretHelm, + Helm, + []string{"helm-test"}, + []string{fmt.Sprintf(singleKeyDescription, "release")}, + "", + false, + nil, + "", + }, + "view-secret test TEST_PASSWORD": { + secret, + Opaque, + []string{"secret"}, + nil, + "TEST_PASSWORD", + false, + nil, + "", + }, "view-secret test -a": { secret, + Opaque, []string{ "TEST_CONN_STR='mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin'", "TEST_PASSWORD='secret\n'", @@ -154,8 +183,26 @@ func TestProcessSecret(t *testing.T) { nil, "", }, - "view-secret test NONE": {secret, nil, nil, "NONE", false, ErrSecretKeyNotFound, ""}, - "view-secret ": {secretEmpty, nil, nil, "", false, ErrSecretEmpty, ""}, + "view-secret test NONE": { + secret, + Opaque, + nil, + nil, + "NONE", + false, + ErrSecretKeyNotFound, + "", + }, + "view-secret ": { + secretEmpty, + Opaque, + nil, + nil, + "", + false, + ErrSecretEmpty, + "", + }, } for name, test := range tests { @@ -170,7 +217,7 @@ func TestProcessSecret(t *testing.T) { readBuf = *strings.NewReader(test.feedkeys) } - err := ProcessSecret(&stdOutBuf, &stdErrBuf, &readBuf, Secret{Data: test.secretData}, test.secretKey, test.decodeAll) + err := ProcessSecret(&stdOutBuf, &stdErrBuf, &readBuf, Secret{Data: test.secretData, Type: test.secretType}, test.secretKey, test.decodeAll) if test.err != nil { assert.Equal(t, err, test.err)