oracle_test.go 5.5 KB

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