|
|
@@ -1 +1,176 @@
|
|
|
+/*
|
|
|
+Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+you may not use this file except in compliance with the License.
|
|
|
+You may obtain a copy of the License at
|
|
|
+
|
|
|
+ http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+
|
|
|
+Unless required by applicable law or agreed to in writing, software
|
|
|
+distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+See the License for the specific language governing permissions and
|
|
|
+limitations under the License.
|
|
|
+*/
|
|
|
package secretmanager
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "fmt"
|
|
|
+ "strings"
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ "github.com/google/go-cmp/cmp"
|
|
|
+ secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
|
|
|
+
|
|
|
+ esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
|
|
|
+ fakesm "github.com/external-secrets/external-secrets/pkg/provider/gcp/secretmanager/fake"
|
|
|
+)
|
|
|
+
|
|
|
+type secretManagerTestCase struct {
|
|
|
+ mockClient *fakesm.MockSMClient
|
|
|
+ apiInput *secretmanagerpb.AccessSecretVersionRequest
|
|
|
+ apiOutput *secretmanagerpb.AccessSecretVersionResponse
|
|
|
+ ref *esv1alpha1.ExternalSecretDataRemoteRef
|
|
|
+ projectID string
|
|
|
+ apiErr error
|
|
|
+ expectError string
|
|
|
+ expectedSecret string
|
|
|
+ // for testing secretmap
|
|
|
+ expectedData map[string]string
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidSecretManagerTestCase() *secretManagerTestCase {
|
|
|
+ smtc := secretManagerTestCase{
|
|
|
+ mockClient: &fakesm.MockSMClient{},
|
|
|
+ apiInput: makeValidAPIInput(),
|
|
|
+ ref: makeValidRef(),
|
|
|
+ apiOutput: makeValidAPIOutput(),
|
|
|
+ projectID: "default",
|
|
|
+ apiErr: nil,
|
|
|
+ expectError: "",
|
|
|
+ expectedSecret: "",
|
|
|
+ expectedData: map[string]string{},
|
|
|
+ }
|
|
|
+ smtc.mockClient.NilClose()
|
|
|
+ smtc.mockClient.WithValue(context.Background(), smtc.apiInput, smtc.apiOutput, smtc.apiErr)
|
|
|
+ return &smtc
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
|
|
|
+ return &esv1alpha1.ExternalSecretDataRemoteRef{
|
|
|
+ Key: "/baz",
|
|
|
+ Version: "default",
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidAPIInput() *secretmanagerpb.AccessSecretVersionRequest {
|
|
|
+ return &secretmanagerpb.AccessSecretVersionRequest{
|
|
|
+ Name: "projects/default/secrets//baz/versions/default",
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidAPIOutput() *secretmanagerpb.AccessSecretVersionResponse {
|
|
|
+ return &secretmanagerpb.AccessSecretVersionResponse{
|
|
|
+ Payload: &secretmanagerpb.SecretPayload{
|
|
|
+ Data: []byte{},
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTestCase)) *secretManagerTestCase {
|
|
|
+ smtc := makeValidSecretManagerTestCase()
|
|
|
+ for _, fn := range tweaks {
|
|
|
+ fn(smtc)
|
|
|
+ }
|
|
|
+ smtc.mockClient.WithValue(context.Background(), smtc.apiInput, smtc.apiOutput, smtc.apiErr)
|
|
|
+ return smtc
|
|
|
+}
|
|
|
+
|
|
|
+// This case can be shared by both GetSecret and GetSecretMap tests.
|
|
|
+// bad case: set apiErr.
|
|
|
+var setAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiErr = fmt.Errorf("oh no")
|
|
|
+ smtc.expectError = "oh no"
|
|
|
+}
|
|
|
+
|
|
|
+// test the sm<->gcp interface
|
|
|
+// make sure correct values are passed and errors are handled accordingly.
|
|
|
+func TestSecretManagerGetSecret(t *testing.T) {
|
|
|
+ // good case: default version is set
|
|
|
+ // key is passed in, output is sent back
|
|
|
+ setSecretString := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiOutput.Payload.Data = []byte("testtesttest")
|
|
|
+ smtc.expectedSecret = "testtesttest"
|
|
|
+ }
|
|
|
+
|
|
|
+ // good case: custom version set
|
|
|
+ setCustomVersion := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.ref.Version = "1234"
|
|
|
+ smtc.apiInput.Name = "projects/default/secrets//baz/versions/1234"
|
|
|
+ smtc.apiOutput.Payload.Data = []byte("FOOBA!")
|
|
|
+ smtc.expectedSecret = "FOOBA!"
|
|
|
+ }
|
|
|
+
|
|
|
+ successCases := []*secretManagerTestCase{
|
|
|
+ makeValidSecretManagerTestCase(),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setSecretString),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setCustomVersion),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setAPIErr),
|
|
|
+ }
|
|
|
+
|
|
|
+ sm := ProviderGCP{}
|
|
|
+ for k, v := range successCases {
|
|
|
+ sm.projectID = v.projectID
|
|
|
+ sm.SecretManagerClient = v.mockClient
|
|
|
+ out, err := sm.GetSecret(context.Background(), *v.ref)
|
|
|
+ if !ErrorContains(err, v.expectError) {
|
|
|
+ t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
|
|
|
+ }
|
|
|
+ if string(out) != v.expectedSecret {
|
|
|
+ t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestGetSecretMap(t *testing.T) {
|
|
|
+ // good case: default version & deserialization
|
|
|
+ setDeserialization := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiOutput.Payload.Data = []byte(`{"foo":"bar"}`)
|
|
|
+ smtc.expectedData["foo"] = "bar"
|
|
|
+ }
|
|
|
+
|
|
|
+ // bad case: invalid json
|
|
|
+ setInvalidJSON := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiOutput.Payload.Data = []byte(`-----------------`)
|
|
|
+ smtc.expectError = "unable to unmarshal secret"
|
|
|
+ }
|
|
|
+
|
|
|
+ successCases := []*secretManagerTestCase{
|
|
|
+ makeValidSecretManagerTestCaseCustom(setDeserialization),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setAPIErr),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setInvalidJSON),
|
|
|
+ }
|
|
|
+
|
|
|
+ sm := ProviderGCP{}
|
|
|
+ for k, v := range successCases {
|
|
|
+ sm.projectID = v.projectID
|
|
|
+ sm.SecretManagerClient = v.mockClient
|
|
|
+ out, err := sm.GetSecretMap(context.Background(), *v.ref)
|
|
|
+ if !ErrorContains(err, v.expectError) {
|
|
|
+ t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
|
|
|
+ }
|
|
|
+ if cmp.Equal(out, v.expectedData) {
|
|
|
+ t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func ErrorContains(out error, want string) bool {
|
|
|
+ if out == nil {
|
|
|
+ return want == ""
|
|
|
+ }
|
|
|
+ if want == "" {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return strings.Contains(out.Error(), want)
|
|
|
+}
|