client_get_secret_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 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 TestGetSecret(t *testing.T) {
  24. mySecretRemoteKey := "mysecret"
  25. myNestedSecretRemoteKey := "nested-secret"
  26. nonExistentSecretRemoteKey := "non-existent-secret"
  27. emptySecretRemoteKey := "empty-secret"
  28. nilSecretRemoteKey := "nil-secret"
  29. property := "key1"
  30. nestedProperty := "users.alice.age"
  31. invalidProperty := "invalid-property"
  32. testCases := map[string]struct {
  33. should string
  34. errshould string
  35. kube kclient.Client
  36. ref esv1.ExternalSecretDataRemoteRef
  37. okmsClient fake.FakeOkmsClient
  38. }{
  39. "Valid Secret": {
  40. should: "{\"key1\":\"value1\",\"key2\":\"value2\"}",
  41. ref: esv1.ExternalSecretDataRemoteRef{
  42. Key: mySecretRemoteKey,
  43. },
  44. },
  45. "Non-existent Secret": {
  46. errshould: "failed to parse the following okms error: Secret does not exist",
  47. ref: esv1.ExternalSecretDataRemoteRef{
  48. Key: nonExistentSecretRemoteKey,
  49. },
  50. },
  51. "Secret with nil data": {
  52. errshould: fmt.Sprintf("failed to retrieve secret at path %q: secret version data is missing", nilSecretRemoteKey),
  53. ref: esv1.ExternalSecretDataRemoteRef{
  54. Key: nilSecretRemoteKey,
  55. },
  56. },
  57. "Secret without empty data": {
  58. errshould: fmt.Sprintf("failed to retrieve secret at path %q: secret version data is missing", emptySecretRemoteKey),
  59. ref: esv1.ExternalSecretDataRemoteRef{
  60. Key: emptySecretRemoteKey,
  61. },
  62. },
  63. "Fetch MetaDataPolicy": {
  64. errshould: fmt.Sprintf("failed to retrieve secret at path %q: fetch metadata policy not supported", mySecretRemoteKey),
  65. ref: esv1.ExternalSecretDataRemoteRef{
  66. Key: mySecretRemoteKey,
  67. MetadataPolicy: "Fetch",
  68. },
  69. },
  70. "Property": {
  71. should: "value1",
  72. ref: esv1.ExternalSecretDataRemoteRef{
  73. Key: mySecretRemoteKey,
  74. Property: property,
  75. },
  76. },
  77. "Nested Property": {
  78. should: "23",
  79. ref: esv1.ExternalSecretDataRemoteRef{
  80. Key: myNestedSecretRemoteKey,
  81. Property: nestedProperty,
  82. },
  83. },
  84. "Invalid Property": {
  85. errshould: fmt.Sprintf("failed to retrieve secret at path %q: secret property %q not found", mySecretRemoteKey, invalidProperty),
  86. ref: esv1.ExternalSecretDataRemoteRef{
  87. Key: mySecretRemoteKey,
  88. Property: invalidProperty,
  89. },
  90. },
  91. "Error case": {
  92. errshould: fmt.Sprintf("failed to retrieve secret at path %q: failed to parse the following okms error: custom error", mySecretRemoteKey),
  93. ref: esv1.ExternalSecretDataRemoteRef{
  94. Key: mySecretRemoteKey,
  95. Property: invalidProperty,
  96. },
  97. okmsClient: fake.FakeOkmsClient{
  98. GetSecretV2Fn: fake.NewGetSecretV2Fn(mySecretRemoteKey, errors.New("custom error")),
  99. },
  100. },
  101. }
  102. for name, testCase := range testCases {
  103. t.Run(name, func(t *testing.T) {
  104. ctx := context.Background()
  105. cl := &ovhClient{
  106. okmsClient: testCase.okmsClient,
  107. kube: testCase.kube,
  108. }
  109. secret, err := cl.GetSecret(ctx, testCase.ref)
  110. if testCase.errshould != "" {
  111. if err == nil {
  112. t.Errorf("\nexpected error: %s\nactual error: <nil>\n\n", testCase.errshould)
  113. } else if err.Error() != testCase.errshould {
  114. t.Errorf("\nexpected error: %s\nactual error: %v\n\n", testCase.errshould, err)
  115. }
  116. return
  117. } else if err != nil {
  118. t.Errorf("\nunexpected error: %v\n\n", err)
  119. return
  120. }
  121. if testCase.should != "" && string(secret) != testCase.should {
  122. t.Errorf("\nexpected value: %q\nactual value: %q\n\n", testCase.should, string(secret))
  123. }
  124. })
  125. }
  126. }