gitlab_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 gitlab
  13. import (
  14. "context"
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. gitlab "github.com/xanzy/go-gitlab"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. fakegitlab "github.com/external-secrets/external-secrets/pkg/provider/gitlab/fake"
  22. )
  23. type secretManagerTestCase struct {
  24. mockClient *fakegitlab.GitlabMockClient
  25. apiInputProjectID string
  26. apiInputKey string
  27. apiOutput *gitlab.ProjectVariable
  28. ref *esv1alpha1.ExternalSecretDataRemoteRef
  29. refFrom *esv1alpha1.ExternalSecretDataFromRemoteRef
  30. projectID *string
  31. apiErr error
  32. expectError string
  33. expectedSecret string
  34. // for testing secretmap
  35. expectedData map[string][]byte
  36. }
  37. func makeValidSecretManagerTestCase() *secretManagerTestCase {
  38. smtc := secretManagerTestCase{
  39. mockClient: &fakegitlab.GitlabMockClient{},
  40. apiInputProjectID: makeValidAPIInputProjectID(),
  41. apiInputKey: makeValidAPIInputKey(),
  42. ref: makeValidRef(),
  43. refFrom: makeValidRefFrom(),
  44. projectID: nil,
  45. apiOutput: makeValidAPIOutput(),
  46. apiErr: nil,
  47. expectError: "",
  48. expectedSecret: "",
  49. expectedData: map[string][]byte{},
  50. }
  51. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
  52. return &smtc
  53. }
  54. func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
  55. return &esv1alpha1.ExternalSecretDataRemoteRef{
  56. Key: "test-secret",
  57. Version: "default",
  58. }
  59. }
  60. func makeValidRefFrom() *esv1alpha1.ExternalSecretDataFromRemoteRef {
  61. return &esv1alpha1.ExternalSecretDataFromRemoteRef{
  62. Extract: esv1alpha1.ExternalSecretExtract{
  63. Key: "test-secret",
  64. Version: "default",
  65. },
  66. }
  67. }
  68. func makeValidAPIInputProjectID() string {
  69. return "testID"
  70. }
  71. func makeValidAPIInputKey() string {
  72. return "testKey"
  73. }
  74. func makeValidAPIOutput() *gitlab.ProjectVariable {
  75. return &gitlab.ProjectVariable{
  76. Key: "testKey",
  77. Value: "",
  78. }
  79. }
  80. func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTestCase)) *secretManagerTestCase {
  81. smtc := makeValidSecretManagerTestCase()
  82. for _, fn := range tweaks {
  83. fn(smtc)
  84. }
  85. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
  86. return smtc
  87. }
  88. // This case can be shared by both GetSecret and GetSecretMap tests.
  89. // bad case: set apiErr.
  90. var setAPIErr = func(smtc *secretManagerTestCase) {
  91. smtc.apiErr = fmt.Errorf("oh no")
  92. smtc.expectError = "oh no"
  93. }
  94. var setNilMockClient = func(smtc *secretManagerTestCase) {
  95. smtc.mockClient = nil
  96. smtc.expectError = errUninitalizedGitlabProvider
  97. }
  98. // test the sm<->gcp interface
  99. // make sure correct values are passed and errors are handled accordingly.
  100. func TestGitlabSecretManagerGetSecret(t *testing.T) {
  101. secretValue := "changedvalue"
  102. // good case: default version is set
  103. // key is passed in, output is sent back
  104. setSecretString := func(smtc *secretManagerTestCase) {
  105. smtc.apiOutput = &gitlab.ProjectVariable{
  106. Key: "testkey",
  107. Value: "changedvalue",
  108. }
  109. smtc.expectedSecret = secretValue
  110. }
  111. successCases := []*secretManagerTestCase{
  112. makeValidSecretManagerTestCaseCustom(setSecretString),
  113. makeValidSecretManagerTestCaseCustom(setAPIErr),
  114. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  115. }
  116. sm := Gitlab{}
  117. for k, v := range successCases {
  118. sm.client = v.mockClient
  119. out, err := sm.GetSecret(context.Background(), *v.ref)
  120. if !ErrorContains(err, v.expectError) {
  121. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  122. }
  123. if string(out) != v.expectedSecret {
  124. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  125. }
  126. }
  127. }
  128. func TestGetSecretMap(t *testing.T) {
  129. // good case: default version & deserialization
  130. setDeserialization := func(smtc *secretManagerTestCase) {
  131. smtc.apiOutput.Value = `{"foo":"bar"}`
  132. smtc.expectedData["foo"] = []byte("bar")
  133. }
  134. // bad case: invalid json
  135. setInvalidJSON := func(smtc *secretManagerTestCase) {
  136. smtc.apiOutput.Value = `-----------------`
  137. smtc.expectError = "unable to unmarshal secret"
  138. }
  139. successCases := []*secretManagerTestCase{
  140. makeValidSecretManagerTestCaseCustom(setDeserialization),
  141. makeValidSecretManagerTestCaseCustom(setInvalidJSON),
  142. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  143. makeValidSecretManagerTestCaseCustom(setAPIErr),
  144. }
  145. sm := Gitlab{}
  146. for k, v := range successCases {
  147. sm.client = v.mockClient
  148. out, err := sm.GetSecretMap(context.Background(), *v.refFrom)
  149. if !ErrorContains(err, v.expectError) {
  150. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  151. }
  152. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  153. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  154. }
  155. }
  156. }
  157. func ErrorContains(out error, want string) bool {
  158. if out == nil {
  159. return want == ""
  160. }
  161. if want == "" {
  162. return false
  163. }
  164. return strings.Contains(out.Error(), want)
  165. }