secretsmanager_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package secretmanager
  13. import (
  14. "context"
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "github.com/google/go-cmp/cmp"
  19. secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. fakesm "github.com/external-secrets/external-secrets/pkg/provider/gcp/secretmanager/fake"
  22. )
  23. type secretManagerTestCase struct {
  24. mockClient *fakesm.MockSMClient
  25. apiInput *secretmanagerpb.AccessSecretVersionRequest
  26. apiOutput *secretmanagerpb.AccessSecretVersionResponse
  27. ref *esv1alpha1.ExternalSecretDataRemoteRef
  28. projectID string
  29. apiErr error
  30. expectError string
  31. expectedSecret string
  32. // for testing secretmap
  33. expectedData map[string]string
  34. }
  35. func makeValidSecretManagerTestCase() *secretManagerTestCase {
  36. smtc := secretManagerTestCase{
  37. mockClient: &fakesm.MockSMClient{},
  38. apiInput: makeValidAPIInput(),
  39. ref: makeValidRef(),
  40. apiOutput: makeValidAPIOutput(),
  41. projectID: "default",
  42. apiErr: nil,
  43. expectError: "",
  44. expectedSecret: "",
  45. expectedData: map[string]string{},
  46. }
  47. smtc.mockClient.NilClose()
  48. smtc.mockClient.WithValue(context.Background(), smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  49. return &smtc
  50. }
  51. func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
  52. return &esv1alpha1.ExternalSecretDataRemoteRef{
  53. Key: "/baz",
  54. Version: "default",
  55. }
  56. }
  57. func makeValidAPIInput() *secretmanagerpb.AccessSecretVersionRequest {
  58. return &secretmanagerpb.AccessSecretVersionRequest{
  59. Name: "projects/default/secrets//baz/versions/default",
  60. }
  61. }
  62. func makeValidAPIOutput() *secretmanagerpb.AccessSecretVersionResponse {
  63. return &secretmanagerpb.AccessSecretVersionResponse{
  64. Payload: &secretmanagerpb.SecretPayload{
  65. Data: []byte{},
  66. },
  67. }
  68. }
  69. func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTestCase)) *secretManagerTestCase {
  70. smtc := makeValidSecretManagerTestCase()
  71. for _, fn := range tweaks {
  72. fn(smtc)
  73. }
  74. smtc.mockClient.WithValue(context.Background(), smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  75. return smtc
  76. }
  77. // This case can be shared by both GetSecret and GetSecretMap tests.
  78. // bad case: set apiErr.
  79. var setAPIErr = func(smtc *secretManagerTestCase) {
  80. smtc.apiErr = fmt.Errorf("oh no")
  81. smtc.expectError = "oh no"
  82. }
  83. // test the sm<->gcp interface
  84. // make sure correct values are passed and errors are handled accordingly.
  85. func TestSecretManagerGetSecret(t *testing.T) {
  86. // good case: default version is set
  87. // key is passed in, output is sent back
  88. setSecretString := func(smtc *secretManagerTestCase) {
  89. smtc.apiOutput.Payload.Data = []byte("testtesttest")
  90. smtc.expectedSecret = "testtesttest"
  91. }
  92. // good case: custom version set
  93. setCustomVersion := func(smtc *secretManagerTestCase) {
  94. smtc.ref.Version = "1234"
  95. smtc.apiInput.Name = "projects/default/secrets//baz/versions/1234"
  96. smtc.apiOutput.Payload.Data = []byte("FOOBA!")
  97. smtc.expectedSecret = "FOOBA!"
  98. }
  99. successCases := []*secretManagerTestCase{
  100. makeValidSecretManagerTestCase(),
  101. makeValidSecretManagerTestCaseCustom(setSecretString),
  102. makeValidSecretManagerTestCaseCustom(setCustomVersion),
  103. makeValidSecretManagerTestCaseCustom(setAPIErr),
  104. }
  105. sm := ProviderGCP{}
  106. for k, v := range successCases {
  107. sm.projectID = v.projectID
  108. sm.SecretManagerClient = v.mockClient
  109. out, err := sm.GetSecret(context.Background(), *v.ref)
  110. if !ErrorContains(err, v.expectError) {
  111. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  112. }
  113. if string(out) != v.expectedSecret {
  114. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  115. }
  116. }
  117. }
  118. func TestGetSecretMap(t *testing.T) {
  119. // good case: default version & deserialization
  120. setDeserialization := func(smtc *secretManagerTestCase) {
  121. smtc.apiOutput.Payload.Data = []byte(`{"foo":"bar"}`)
  122. smtc.expectedData["foo"] = "bar"
  123. }
  124. // bad case: invalid json
  125. setInvalidJSON := func(smtc *secretManagerTestCase) {
  126. smtc.apiOutput.Payload.Data = []byte(`-----------------`)
  127. smtc.expectError = "unable to unmarshal secret"
  128. }
  129. successCases := []*secretManagerTestCase{
  130. makeValidSecretManagerTestCaseCustom(setDeserialization),
  131. makeValidSecretManagerTestCaseCustom(setAPIErr),
  132. makeValidSecretManagerTestCaseCustom(setInvalidJSON),
  133. }
  134. sm := ProviderGCP{}
  135. for k, v := range successCases {
  136. sm.projectID = v.projectID
  137. sm.SecretManagerClient = v.mockClient
  138. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  139. if !ErrorContains(err, v.expectError) {
  140. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  141. }
  142. if cmp.Equal(out, v.expectedData) {
  143. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  144. }
  145. }
  146. }
  147. func ErrorContains(out error, want string) bool {
  148. if out == nil {
  149. return want == ""
  150. }
  151. if want == "" {
  152. return false
  153. }
  154. return strings.Contains(out.Error(), want)
  155. }