oracle_test.go 5.8 KB

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