oracle_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. vault "github.com/oracle/oci-go-sdk/v45/vault"
  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 secretManagerTestCase struct {
  25. mockClient *fakeoracle.OracleMockClient
  26. apiInput *vault.GetSecretRequest
  27. apiOutput *vault.GetSecretResponse
  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 makeValidSecretManagerTestCase() *secretManagerTestCase {
  36. smtc := secretManagerTestCase{
  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() *vault.GetSecretRequest {
  56. return &vault.GetSecretRequest{
  57. SecretId: utilpointer.StringPtr("test-secret"),
  58. }
  59. }
  60. func makeValidAPIOutput() *vault.GetSecretResponse {
  61. return &vault.GetSecretResponse{
  62. Etag: utilpointer.StringPtr("test-name"),
  63. Secret: vault.Secret{},
  64. }
  65. }
  66. func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTestCase)) *secretManagerTestCase {
  67. smtc := makeValidSecretManagerTestCase()
  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 *secretManagerTestCase) {
  77. smtc.apiErr = fmt.Errorf("oh no")
  78. smtc.expectError = "oh no"
  79. }
  80. var setNilMockClient = func(smtc *secretManagerTestCase) {
  81. smtc.mockClient = nil
  82. smtc.expectError = errUninitalizedOracleProvider
  83. }
  84. func TestOracleSecretManagerGetSecret(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 *secretManagerTestCase) {
  89. smtc.apiOutput = &vault.GetSecretResponse{
  90. Etag: utilpointer.StringPtr("test-name"),
  91. Secret: vault.Secret{
  92. CompartmentId: utilpointer.StringPtr("test-compartment-id"),
  93. Id: utilpointer.StringPtr("test-id"),
  94. SecretName: utilpointer.StringPtr("changedvalue"),
  95. },
  96. }
  97. smtc.expectedSecret = secretValue
  98. }
  99. successCases := []*secretManagerTestCase{
  100. makeValidSecretManagerTestCaseCustom(setAPIErr),
  101. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  102. makeValidSecretManagerTestCaseCustom(setSecretString),
  103. }
  104. sm := KeyManagementService{}
  105. for k, v := range successCases {
  106. sm.Client = v.mockClient
  107. fmt.Println(*v.ref)
  108. out, err := sm.GetSecret(context.Background(), *v.ref)
  109. if !ErrorContains(err, v.expectError) {
  110. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  111. }
  112. if string(out) != v.expectedSecret {
  113. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  114. }
  115. }
  116. }
  117. func TestGetSecretMap(t *testing.T) {
  118. // good case: default version & deserialization
  119. setDeserialization := func(smtc *secretManagerTestCase) {
  120. smtc.apiOutput.SecretName = utilpointer.StringPtr(`{"foo":"bar"}`)
  121. smtc.expectedData["foo"] = []byte("bar")
  122. }
  123. // bad case: invalid json
  124. setInvalidJSON := func(smtc *secretManagerTestCase) {
  125. smtc.apiOutput.SecretName = utilpointer.StringPtr(`-----------------`)
  126. smtc.expectError = "unable to unmarshal secret"
  127. }
  128. successCases := []*secretManagerTestCase{
  129. makeValidSecretManagerTestCaseCustom(setDeserialization),
  130. makeValidSecretManagerTestCaseCustom(setInvalidJSON),
  131. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  132. makeValidSecretManagerTestCaseCustom(setAPIErr),
  133. }
  134. sm := KeyManagementService{}
  135. for k, v := range successCases {
  136. sm.Client = v.mockClient
  137. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  138. if !ErrorContains(err, v.expectError) {
  139. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  140. }
  141. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  142. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  143. }
  144. }
  145. }
  146. func ErrorContains(out error, want string) bool {
  147. if out == nil {
  148. return want == ""
  149. }
  150. if want == "" {
  151. return false
  152. }
  153. return strings.Contains(out.Error(), want)
  154. }