client_get_all_secrets_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ovh
  14. import (
  15. "context"
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. "github.com/external-secrets/external-secrets/providers/v1/ovh/fake"
  22. )
  23. func TestGetAllSecrets(t *testing.T) {
  24. path2 := "pattern2/test"
  25. slashPath := "pattern//slash"
  26. emptySecretPath := "empty"
  27. nilSecretPath := "nil"
  28. noMatchRegexp := "^noMatch.*$"
  29. invalidRegexp := "\\wa\\w([a]"
  30. testCases := map[string]struct {
  31. should map[string][]byte
  32. errshould string
  33. kube kclient.Client
  34. refFind esv1.ExternalSecretFind
  35. okmsClient fake.FakeOkmsClient
  36. }{
  37. "Empty Secret Found": {
  38. errshould: fmt.Sprintf("failed to retrieve multiple secrets: failed to retrieve secret at path %q: secret version data is missing", emptySecretPath+"/empty-secret"),
  39. refFind: esv1.ExternalSecretFind{
  40. Path: &emptySecretPath,
  41. },
  42. },
  43. "Nil Secret Found": {
  44. errshould: fmt.Sprintf("failed to retrieve multiple secrets: failed to retrieve secret at path %q: secret version data is missing", nilSecretPath+"/nil-secret"),
  45. refFind: esv1.ExternalSecretFind{
  46. Path: &nilSecretPath,
  47. },
  48. },
  49. "Invalid Regex": {
  50. errshould: fmt.Sprintf("failed to retrieve multiple secrets: could not parse regex: error parsing regexp: missing closing ): `%s`", invalidRegexp),
  51. refFind: esv1.ExternalSecretFind{
  52. Name: &esv1.FindName{
  53. RegExp: invalidRegexp,
  54. },
  55. },
  56. },
  57. "Empty Regex": {
  58. should: map[string][]byte{
  59. "mysecret": []byte(`{"key1":"value1","key2":"value2"}`),
  60. "nested-secret": []byte(`{"users":{"alice":{"age":"23"},"baptist":{"age":"27"}}}`),
  61. "pattern2/test/test-secret": []byte("{\"key4\":\"value4\"}"),
  62. "pattern2/test/test.secret": []byte("{\"key5\":\"value5\"}"),
  63. "pattern2/secret": []byte("{\"key6\":\"value6\"}"),
  64. },
  65. refFind: esv1.ExternalSecretFind{
  66. Name: &esv1.FindName{
  67. RegExp: "",
  68. },
  69. },
  70. },
  71. "No Regexp Match": {
  72. refFind: esv1.ExternalSecretFind{
  73. Name: &esv1.FindName{
  74. RegExp: noMatchRegexp,
  75. },
  76. },
  77. should: map[string][]byte{},
  78. },
  79. "Regex pattern containing '.' or '-' only": {
  80. should: map[string][]byte{
  81. "nested-secret": []byte(`{"users":{"alice":{"age":"23"},"baptist":{"age":"27"}}}`),
  82. "pattern2/test/test-secret": []byte("{\"key4\":\"value4\"}"),
  83. "pattern2/test/test.secret": []byte("{\"key5\":\"value5\"}"),
  84. },
  85. refFind: esv1.ExternalSecretFind{
  86. Name: &esv1.FindName{
  87. RegExp: ".*[.-].*",
  88. },
  89. },
  90. },
  91. "Path pattern2/test": {
  92. should: map[string][]byte{
  93. "pattern2/test/test-secret": []byte("{\"key4\":\"value4\"}"),
  94. "pattern2/test/test.secret": []byte("{\"key5\":\"value5\"}"),
  95. },
  96. refFind: esv1.ExternalSecretFind{
  97. Path: &path2,
  98. },
  99. },
  100. "Path pattern//wrong": {
  101. should: map[string][]byte{
  102. "/pattern2/test/testsecret": []byte("{\"key4\":\"value4\"}"),
  103. "pattern2/test/test//secret": []byte("{\"key5\":\"value5\"}"),
  104. },
  105. refFind: esv1.ExternalSecretFind{
  106. Path: &slashPath,
  107. },
  108. errshould: "failed to retrieve multiple secrets: invalid path \"pattern//slash\": cannot start with a / or contain a //",
  109. },
  110. "Secrets found without path": {
  111. should: map[string][]byte{
  112. "mysecret": []byte(`{"key1":"value1","key2":"value2"}`),
  113. "nested-secret": []byte(`{"users":{"alice":{"age":"23"},"baptist":{"age":"27"}}}`),
  114. "pattern2/test/test-secret": []byte("{\"key4\":\"value4\"}"),
  115. "pattern2/test/test.secret": []byte("{\"key5\":\"value5\"}"),
  116. "pattern2/secret": []byte("{\"key6\":\"value6\"}"),
  117. },
  118. refFind: esv1.ExternalSecretFind{
  119. Path: nil,
  120. },
  121. },
  122. }
  123. ctx := context.Background()
  124. for name, testCase := range testCases {
  125. t.Run(name, func(t *testing.T) {
  126. cl := &ovhClient{
  127. okmsClient: testCase.okmsClient,
  128. kube: testCase.kube,
  129. }
  130. secrets, err := cl.GetAllSecrets(ctx, testCase.refFind)
  131. if testCase.errshould != "" {
  132. if err == nil {
  133. t.Errorf("\nexpected value: %s\nactual value: <nil>\n\n", testCase.errshould)
  134. } else if err.Error() != testCase.errshould {
  135. t.Errorf("\nexpected value: %s\nactual value: %v\n\n", testCase.errshould, err)
  136. }
  137. return
  138. } else if err != nil {
  139. t.Errorf("\nunexpected error: %v\n\n", err)
  140. return
  141. }
  142. if !reflect.DeepEqual(testCase.should, secrets) {
  143. t.Errorf("\nexpected value: %v\nactual value: %v\n\n", convertByteMapToStringMap(testCase.should), convertByteMapToStringMap(secrets))
  144. }
  145. })
  146. }
  147. }
  148. func convertByteMapToStringMap(m map[string][]byte) map[string]string {
  149. newMap := make(map[string]string)
  150. for key, value := range m {
  151. newMap[key] = string(value)
  152. }
  153. return newMap
  154. }