gitlab_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "net/http"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. gitlab "github.com/xanzy/go-gitlab"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. fakegitlab "github.com/external-secrets/external-secrets/pkg/provider/gitlab/fake"
  23. )
  24. type secretManagerTestCase struct {
  25. mockClient *fakegitlab.GitlabMockClient
  26. apiInputProjectID string
  27. apiInputKey string
  28. apiOutput *gitlab.ProjectVariable
  29. apiResponse *gitlab.Response
  30. ref *esv1beta1.ExternalSecretDataRemoteRef
  31. projectID *string
  32. apiErr error
  33. expectError string
  34. expectedSecret string
  35. expectedValidationResult esv1beta1.ValidationResult
  36. // for testing secretmap
  37. expectedData map[string][]byte
  38. }
  39. func makeValidSecretManagerTestCase() *secretManagerTestCase {
  40. smtc := secretManagerTestCase{
  41. mockClient: &fakegitlab.GitlabMockClient{},
  42. apiInputProjectID: makeValidAPIInputProjectID(),
  43. apiInputKey: makeValidAPIInputKey(),
  44. ref: makeValidRef(),
  45. projectID: nil,
  46. apiOutput: makeValidAPIOutput(),
  47. apiResponse: makeValidAPIResponse(),
  48. apiErr: nil,
  49. expectError: "",
  50. expectedSecret: "",
  51. expectedValidationResult: esv1beta1.ValidationResultReady,
  52. expectedData: map[string][]byte{},
  53. }
  54. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
  55. return &smtc
  56. }
  57. func makeValidRef() *esv1beta1.ExternalSecretDataRemoteRef {
  58. return &esv1beta1.ExternalSecretDataRemoteRef{
  59. Key: "test-secret",
  60. Version: "default",
  61. }
  62. }
  63. func makeValidAPIInputProjectID() string {
  64. return "testID"
  65. }
  66. func makeValidAPIInputKey() string {
  67. return "testKey"
  68. }
  69. func makeValidAPIResponse() *gitlab.Response {
  70. return &gitlab.Response{
  71. Response: &http.Response{
  72. StatusCode: http.StatusOK,
  73. },
  74. }
  75. }
  76. func makeValidAPIOutput() *gitlab.ProjectVariable {
  77. return &gitlab.ProjectVariable{
  78. Key: "testKey",
  79. Value: "",
  80. }
  81. }
  82. func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTestCase)) *secretManagerTestCase {
  83. smtc := makeValidSecretManagerTestCase()
  84. for _, fn := range tweaks {
  85. fn(smtc)
  86. }
  87. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
  88. return smtc
  89. }
  90. // This case can be shared by both GetSecret and GetSecretMap tests.
  91. // bad case: set apiErr.
  92. var setAPIErr = func(smtc *secretManagerTestCase) {
  93. smtc.apiErr = fmt.Errorf("oh no")
  94. smtc.expectError = "oh no"
  95. smtc.expectedValidationResult = esv1beta1.ValidationResultError
  96. }
  97. var setListAPIErr = func(smtc *secretManagerTestCase) {
  98. err := fmt.Errorf("oh no")
  99. smtc.apiErr = err
  100. smtc.expectError = fmt.Errorf(errList, err).Error()
  101. smtc.expectedValidationResult = esv1beta1.ValidationResultError
  102. }
  103. var setListAPIRespNil = func(smtc *secretManagerTestCase) {
  104. smtc.apiResponse = nil
  105. smtc.expectError = errAuth
  106. smtc.expectedValidationResult = esv1beta1.ValidationResultError
  107. }
  108. var setListAPIRespBadCode = func(smtc *secretManagerTestCase) {
  109. smtc.apiResponse.StatusCode = http.StatusUnauthorized
  110. smtc.expectError = errAuth
  111. smtc.expectedValidationResult = esv1beta1.ValidationResultError
  112. }
  113. var setNilMockClient = func(smtc *secretManagerTestCase) {
  114. smtc.mockClient = nil
  115. smtc.expectError = errUninitalizedGitlabProvider
  116. }
  117. // test the sm<->gcp interface
  118. // make sure correct values are passed and errors are handled accordingly.
  119. func TestGitlabSecretManagerGetSecret(t *testing.T) {
  120. secretValue := "changedvalue"
  121. // good case: default version is set
  122. // key is passed in, output is sent back
  123. setSecretString := func(smtc *secretManagerTestCase) {
  124. smtc.apiOutput = &gitlab.ProjectVariable{
  125. Key: "testkey",
  126. Value: "changedvalue",
  127. }
  128. smtc.expectedSecret = secretValue
  129. }
  130. successCases := []*secretManagerTestCase{
  131. makeValidSecretManagerTestCaseCustom(setSecretString),
  132. makeValidSecretManagerTestCaseCustom(setAPIErr),
  133. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  134. }
  135. sm := Gitlab{}
  136. for k, v := range successCases {
  137. sm.client = v.mockClient
  138. out, err := sm.GetSecret(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 string(out) != v.expectedSecret {
  143. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  144. }
  145. }
  146. }
  147. func TestValidate(t *testing.T) {
  148. successCases := []*secretManagerTestCase{
  149. makeValidSecretManagerTestCaseCustom(),
  150. makeValidSecretManagerTestCaseCustom(setListAPIErr),
  151. makeValidSecretManagerTestCaseCustom(setListAPIRespNil),
  152. makeValidSecretManagerTestCaseCustom(setListAPIRespBadCode),
  153. }
  154. sm := Gitlab{}
  155. for k, v := range successCases {
  156. sm.client = v.mockClient
  157. t.Logf("%+v", v)
  158. validationResult, err := sm.Validate()
  159. if !ErrorContains(err, v.expectError) {
  160. t.Errorf("[%d], unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  161. }
  162. if validationResult != v.expectedValidationResult {
  163. t.Errorf("[%d], unexpected validationResult: %s, expected: '%s'", k, validationResult, v.expectedValidationResult)
  164. }
  165. }
  166. }
  167. func TestGetSecretMap(t *testing.T) {
  168. // good case: default version & deserialization
  169. setDeserialization := func(smtc *secretManagerTestCase) {
  170. smtc.apiOutput.Value = `{"foo":"bar"}`
  171. smtc.expectedData["foo"] = []byte("bar")
  172. }
  173. // bad case: invalid json
  174. setInvalidJSON := func(smtc *secretManagerTestCase) {
  175. smtc.apiOutput.Value = `-----------------`
  176. smtc.expectError = "unable to unmarshal secret"
  177. }
  178. successCases := []*secretManagerTestCase{
  179. makeValidSecretManagerTestCaseCustom(setDeserialization),
  180. makeValidSecretManagerTestCaseCustom(setInvalidJSON),
  181. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  182. makeValidSecretManagerTestCaseCustom(setAPIErr),
  183. }
  184. sm := Gitlab{}
  185. for k, v := range successCases {
  186. sm.client = v.mockClient
  187. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  188. if !ErrorContains(err, v.expectError) {
  189. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  190. }
  191. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  192. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  193. }
  194. }
  195. }
  196. func ErrorContains(out error, want string) bool {
  197. if out == nil {
  198. return want == ""
  199. }
  200. if want == "" {
  201. return false
  202. }
  203. return strings.Contains(out.Error(), want)
  204. }