oracle_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 oracle
  13. import (
  14. "context"
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. secrets "github.com/oracle/oci-go-sdk/v45/secrets"
  20. utilpointer "k8s.io/utils/pointer"
  21. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  22. fakeoracle "github.com/external-secrets/external-secrets/pkg/provider/oracle/fake"
  23. )
  24. type vaultTestCase struct {
  25. mockClient *fakeoracle.OracleMockClient
  26. apiInput *secrets.GetSecretBundleRequest
  27. apiOutput *secrets.GetSecretBundleResponse
  28. ref *esv1alpha1.ExternalSecretDataRemoteRef
  29. apiErr error
  30. expectError string
  31. expectedSecret string
  32. // for testing secretmap
  33. expectedData map[string][]byte
  34. }
  35. func makeValidVaultTestCase() *vaultTestCase {
  36. smtc := vaultTestCase{
  37. mockClient: &fakeoracle.OracleMockClient{},
  38. apiInput: makeValidAPIInput(),
  39. ref: makeValidRef(),
  40. apiOutput: makeValidAPIOutput(),
  41. apiErr: nil,
  42. expectError: "",
  43. expectedSecret: "",
  44. expectedData: map[string][]byte{},
  45. }
  46. smtc.mockClient.WithValue(*smtc.apiInput, *smtc.apiOutput, smtc.apiErr)
  47. return &smtc
  48. }
  49. func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
  50. return &esv1alpha1.ExternalSecretDataRemoteRef{
  51. Key: "test-secret",
  52. Version: "default",
  53. }
  54. }
  55. func makeValidAPIInput() *secrets.GetSecretBundleRequest {
  56. return &secrets.GetSecretBundleRequest{
  57. SecretId: utilpointer.StringPtr("test-secret"),
  58. }
  59. }
  60. func makeValidAPIOutput() *secrets.GetSecretBundleResponse {
  61. return &secrets.GetSecretBundleResponse{
  62. Etag: utilpointer.StringPtr("test-name"),
  63. SecretBundle: secrets.SecretBundle{},
  64. }
  65. }
  66. func makeValidVaultTestCaseCustom(tweaks ...func(smtc *vaultTestCase)) *vaultTestCase {
  67. smtc := makeValidVaultTestCase()
  68. for _, fn := range tweaks {
  69. fn(smtc)
  70. }
  71. smtc.mockClient.WithValue(*smtc.apiInput, *smtc.apiOutput, smtc.apiErr)
  72. return smtc
  73. }
  74. // This case can be shared by both GetSecret and GetSecretMap tests.
  75. // bad case: set apiErr.
  76. var setAPIErr = func(smtc *vaultTestCase) {
  77. smtc.apiErr = fmt.Errorf("oh no")
  78. smtc.expectError = "oh no"
  79. }
  80. var setNilMockClient = func(smtc *vaultTestCase) {
  81. smtc.mockClient = nil
  82. smtc.expectError = errUninitalizedOracleProvider
  83. }
  84. func TestOracleVaultGetSecret(t *testing.T) {
  85. secretValue := "changedvalue"
  86. // good case: default version is set
  87. // key is passed in, output is sent back
  88. setSecretString := func(smtc *vaultTestCase) {
  89. smtc.apiOutput = &secrets.GetSecretBundleResponse{
  90. Etag: utilpointer.StringPtr("test-name"),
  91. SecretBundle: secrets.SecretBundle{
  92. SecretId: utilpointer.StringPtr("test-id"),
  93. VersionNumber: utilpointer.Int64(1),
  94. SecretBundleContent: secrets.Base64SecretBundleContentDetails{
  95. Content: utilpointer.StringPtr(secretValue),
  96. },
  97. },
  98. }
  99. smtc.expectedSecret = secretValue
  100. }
  101. successCases := []*vaultTestCase{
  102. makeValidVaultTestCaseCustom(setAPIErr),
  103. makeValidVaultTestCaseCustom(setNilMockClient),
  104. makeValidVaultTestCaseCustom(setSecretString),
  105. }
  106. sm := VaultManagementService{}
  107. for k, v := range successCases {
  108. sm.Client = v.mockClient
  109. fmt.Println(*v.ref)
  110. out, err := sm.GetSecret(context.Background(), *v.ref)
  111. if !ErrorContains(err, v.expectError) {
  112. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  113. }
  114. if string(out) != v.expectedSecret {
  115. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  116. }
  117. }
  118. }
  119. func TestGetSecretMap(t *testing.T) {
  120. // good case: default version & deserialization
  121. setDeserialization := func(smtc *vaultTestCase) {
  122. smtc.apiOutput.SecretName = utilpointer.StringPtr(`{"foo":"bar"}`)
  123. smtc.expectedData["foo"] = []byte("bar")
  124. }
  125. // bad case: invalid json
  126. setInvalidJSON := func(smtc *vaultTestCase) {
  127. smtc.apiOutput.SecretName = utilpointer.StringPtr(`-----------------`)
  128. smtc.expectError = "unable to unmarshal secret"
  129. }
  130. successCases := []*vaultTestCase{
  131. makeValidVaultTestCaseCustom(setDeserialization),
  132. makeValidVaultTestCaseCustom(setInvalidJSON),
  133. makeValidVaultTestCaseCustom(setNilMockClient),
  134. makeValidVaultTestCaseCustom(setAPIErr),
  135. }
  136. sm := VaultManagementService{}
  137. for k, v := range successCases {
  138. sm.Client = v.mockClient
  139. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  140. if !ErrorContains(err, v.expectError) {
  141. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  142. }
  143. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  144. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  145. }
  146. }
  147. }
  148. func ErrorContains(out error, want string) bool {
  149. if out == nil {
  150. return want == ""
  151. }
  152. if want == "" {
  153. return false
  154. }
  155. return strings.Contains(out.Error(), want)
  156. }