client_push_secret_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "errors"
  17. "fmt"
  18. "testing"
  19. v1 "k8s.io/api/core/v1"
  20. "github.com/external-secrets/external-secrets/providers/v1/ovh/fake"
  21. testingfake "github.com/external-secrets/external-secrets/runtime/testing/fake"
  22. )
  23. func TestPushSecret(t *testing.T) {
  24. const (
  25. mySecretRemoteKey = "mysecret"
  26. nonExistentSecretRemoteKey = "non-existent-secret"
  27. emptySecretRemoteKey = "empty-secret"
  28. nilSecretRemoteKey = "nil-secret"
  29. )
  30. secretData := &v1.Secret{
  31. Data: map[string][]byte{
  32. "key1": []byte("value1"),
  33. "key2": []byte("value2"),
  34. },
  35. }
  36. emptyRemoteKey := ""
  37. testCases := map[string]struct {
  38. errshould string
  39. secret *v1.Secret
  40. data testingfake.PushSecretData
  41. okmsClient fake.FakeOkmsClient
  42. }{
  43. "Nil Secret": {
  44. errshould: fmt.Sprintf("failed to push secret at path %q: provided secret is nil", nilSecretRemoteKey),
  45. secret: nil,
  46. data: testingfake.PushSecretData{
  47. RemoteKey: nilSecretRemoteKey,
  48. },
  49. },
  50. "Empty Secret Data": {
  51. errshould: fmt.Sprintf("failed to push secret at path %q: provided secret is empty", emptySecretRemoteKey),
  52. secret: &v1.Secret{
  53. Data: nil,
  54. },
  55. data: testingfake.PushSecretData{
  56. RemoteKey: emptySecretRemoteKey,
  57. },
  58. },
  59. "Empty Remote Key": {
  60. errshould: fmt.Sprintf("failed to push secret at path %q: remote key cannot be empty (spec.data.remoteRef.key)", emptyRemoteKey),
  61. secret: secretData,
  62. data: testingfake.PushSecretData{
  63. RemoteKey: emptyRemoteKey,
  64. },
  65. },
  66. "Non-Existent Remote Key": {
  67. errshould: "",
  68. secret: secretData,
  69. data: testingfake.PushSecretData{
  70. RemoteKey: nonExistentSecretRemoteKey,
  71. },
  72. },
  73. "Existing Remote Key": {
  74. errshould: "",
  75. secret: secretData,
  76. data: testingfake.PushSecretData{
  77. RemoteKey: mySecretRemoteKey,
  78. },
  79. },
  80. "Secret Key": {
  81. errshould: "",
  82. secret: secretData,
  83. data: testingfake.PushSecretData{
  84. RemoteKey: mySecretRemoteKey,
  85. SecretKey: "key1",
  86. },
  87. },
  88. "Property": {
  89. errshould: "",
  90. secret: secretData,
  91. data: testingfake.PushSecretData{
  92. RemoteKey: mySecretRemoteKey,
  93. Property: "property",
  94. },
  95. },
  96. "Custom PostSecretV2 Error": {
  97. errshould: fmt.Sprintf("failed to push secret at path %q: could not create remote secret: custom error", mySecretRemoteKey),
  98. secret: secretData,
  99. data: testingfake.PushSecretData{
  100. RemoteKey: mySecretRemoteKey,
  101. },
  102. okmsClient: fake.FakeOkmsClient{
  103. // A non-existent secret is referenced to trigger Post instead of Put
  104. GetSecretV2Fn: fake.NewGetSecretV2Fn(nonExistentSecretRemoteKey, nil),
  105. PostSecretV2Fn: fake.NewPostSecretV2Fn(errors.New("custom error")),
  106. },
  107. },
  108. "Custom PutSecretV2 Error": {
  109. errshould: fmt.Sprintf("failed to push secret at path %q: could not update remote secret: custom error", mySecretRemoteKey),
  110. secret: secretData,
  111. data: testingfake.PushSecretData{
  112. RemoteKey: mySecretRemoteKey,
  113. },
  114. okmsClient: fake.FakeOkmsClient{
  115. // An existing secret is referenced to trigger Put instead of Post
  116. GetSecretV2Fn: fake.NewGetSecretV2Fn("nested-secret", nil),
  117. PutSecretV2Fn: fake.NewPutSecretV2Fn(errors.New("custom error")),
  118. },
  119. },
  120. "Custom GetSecretV2 Error": {
  121. errshould: fmt.Sprintf("failed to push secret at path %q: failed to parse the following okms error: custom error", mySecretRemoteKey),
  122. secret: secretData,
  123. data: testingfake.PushSecretData{
  124. RemoteKey: mySecretRemoteKey,
  125. },
  126. okmsClient: fake.FakeOkmsClient{
  127. GetSecretV2Fn: fake.NewGetSecretV2Fn("", errors.New("custom error")),
  128. },
  129. },
  130. }
  131. ctx := context.Background()
  132. for name, testCase := range testCases {
  133. t.Run(name, func(t *testing.T) {
  134. cl := ovhClient{
  135. okmsClient: testCase.okmsClient,
  136. }
  137. err := cl.PushSecret(ctx, testCase.secret, testCase.data)
  138. if testCase.errshould != "" {
  139. if err == nil {
  140. t.Errorf("\nexpected error: %s\nactual error: <nil>\n\n", testCase.errshould)
  141. } else if err.Error() != testCase.errshould {
  142. t.Errorf("\nexpected error: %s\nactual error: %v\n\n", testCase.errshould, err)
  143. }
  144. } else if err != nil {
  145. t.Errorf("\nunexpected error: %v\n\n", err)
  146. }
  147. })
  148. }
  149. }