gitlab_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // for testing secretmap
  36. expectedData map[string][]byte
  37. }
  38. func makeValidSecretManagerTestCase() *secretManagerTestCase {
  39. smtc := secretManagerTestCase{
  40. mockClient: &fakegitlab.GitlabMockClient{},
  41. apiInputProjectID: makeValidAPIInputProjectID(),
  42. apiInputKey: makeValidAPIInputKey(),
  43. ref: makeValidRef(),
  44. projectID: nil,
  45. apiOutput: makeValidAPIOutput(),
  46. apiResponse: makeValidAPIResponse(),
  47. apiErr: nil,
  48. expectError: "",
  49. expectedSecret: "",
  50. expectedData: map[string][]byte{},
  51. }
  52. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
  53. return &smtc
  54. }
  55. func makeValidRef() *esv1beta1.ExternalSecretDataRemoteRef {
  56. return &esv1beta1.ExternalSecretDataRemoteRef{
  57. Key: "test-secret",
  58. Version: "default",
  59. }
  60. }
  61. func makeValidAPIInputProjectID() string {
  62. return "testID"
  63. }
  64. func makeValidAPIInputKey() string {
  65. return "testKey"
  66. }
  67. func makeValidAPIResponse() *gitlab.Response {
  68. return &gitlab.Response{
  69. Response: &http.Response{
  70. StatusCode: http.StatusOK,
  71. },
  72. }
  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.apiResponse, 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 setListAPIErr = func(smtc *secretManagerTestCase) {
  95. err := fmt.Errorf("oh no")
  96. smtc.apiErr = err
  97. smtc.expectError = fmt.Errorf(errList, err).Error()
  98. }
  99. var setListAPIRespNil = func(smtc *secretManagerTestCase) {
  100. smtc.apiResponse = nil
  101. smtc.expectError = errAuth
  102. }
  103. var setListAPIRespBadCode = func(smtc *secretManagerTestCase) {
  104. smtc.apiResponse.StatusCode = http.StatusUnauthorized
  105. smtc.expectError = errAuth
  106. }
  107. var setNilMockClient = func(smtc *secretManagerTestCase) {
  108. smtc.mockClient = nil
  109. smtc.expectError = errUninitalizedGitlabProvider
  110. }
  111. // test the sm<->gcp interface
  112. // make sure correct values are passed and errors are handled accordingly.
  113. func TestGitlabSecretManagerGetSecret(t *testing.T) {
  114. secretValue := "changedvalue"
  115. // good case: default version is set
  116. // key is passed in, output is sent back
  117. setSecretString := func(smtc *secretManagerTestCase) {
  118. smtc.apiOutput = &gitlab.ProjectVariable{
  119. Key: "testkey",
  120. Value: "changedvalue",
  121. }
  122. smtc.expectedSecret = secretValue
  123. }
  124. successCases := []*secretManagerTestCase{
  125. makeValidSecretManagerTestCaseCustom(setSecretString),
  126. makeValidSecretManagerTestCaseCustom(setAPIErr),
  127. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  128. }
  129. sm := Gitlab{}
  130. for k, v := range successCases {
  131. sm.client = v.mockClient
  132. out, err := sm.GetSecret(context.Background(), *v.ref)
  133. if !ErrorContains(err, v.expectError) {
  134. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  135. }
  136. if string(out) != v.expectedSecret {
  137. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  138. }
  139. }
  140. }
  141. func TestValidate(t *testing.T) {
  142. successCases := []*secretManagerTestCase{
  143. makeValidSecretManagerTestCaseCustom(),
  144. makeValidSecretManagerTestCaseCustom(setListAPIErr),
  145. makeValidSecretManagerTestCaseCustom(setListAPIRespNil),
  146. makeValidSecretManagerTestCaseCustom(setListAPIRespBadCode),
  147. }
  148. sm := Gitlab{}
  149. for k, v := range successCases {
  150. sm.client = v.mockClient
  151. t.Logf("%+v", v)
  152. err := sm.Validate()
  153. if !ErrorContains(err, v.expectError) {
  154. t.Errorf("[%d], unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  155. }
  156. }
  157. }
  158. func TestGetSecretMap(t *testing.T) {
  159. // good case: default version & deserialization
  160. setDeserialization := func(smtc *secretManagerTestCase) {
  161. smtc.apiOutput.Value = `{"foo":"bar"}`
  162. smtc.expectedData["foo"] = []byte("bar")
  163. }
  164. // bad case: invalid json
  165. setInvalidJSON := func(smtc *secretManagerTestCase) {
  166. smtc.apiOutput.Value = `-----------------`
  167. smtc.expectError = "unable to unmarshal secret"
  168. }
  169. successCases := []*secretManagerTestCase{
  170. makeValidSecretManagerTestCaseCustom(setDeserialization),
  171. makeValidSecretManagerTestCaseCustom(setInvalidJSON),
  172. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  173. makeValidSecretManagerTestCaseCustom(setAPIErr),
  174. }
  175. sm := Gitlab{}
  176. for k, v := range successCases {
  177. sm.client = v.mockClient
  178. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  179. if !ErrorContains(err, v.expectError) {
  180. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  181. }
  182. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  183. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  184. }
  185. }
  186. }
  187. func ErrorContains(out error, want string) bool {
  188. if out == nil {
  189. return want == ""
  190. }
  191. if want == "" {
  192. return false
  193. }
  194. return strings.Contains(out.Error(), want)
  195. }