akeyless_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 akeyless
  13. import (
  14. "context"
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  20. fakeakeyless "github.com/external-secrets/external-secrets/pkg/provider/akeyless/fake"
  21. )
  22. type akeylessTestCase struct {
  23. mockClient *fakeakeyless.AkeylessMockClient
  24. apiInput *fakeakeyless.Input
  25. apiOutput *fakeakeyless.Output
  26. ref *esv1alpha1.ExternalSecretDataRemoteRef
  27. expectError string
  28. expectedSecret string
  29. // for testing secretmap
  30. expectedData map[string][]byte
  31. }
  32. func makeValidAkeylessTestCase() *akeylessTestCase {
  33. smtc := akeylessTestCase{
  34. mockClient: &fakeakeyless.AkeylessMockClient{},
  35. apiInput: makeValidInput(),
  36. ref: makeValidRef(),
  37. apiOutput: makeValidOutput(),
  38. expectError: "",
  39. expectedSecret: "",
  40. expectedData: map[string][]byte{},
  41. }
  42. smtc.mockClient.WithValue(smtc.apiInput, smtc.apiOutput)
  43. return &smtc
  44. }
  45. func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
  46. return &esv1alpha1.ExternalSecretDataRemoteRef{
  47. Extract: esv1alpha1.ExternalSecretExtract{
  48. Key: "test-secret",
  49. Version: "1",
  50. },
  51. }
  52. }
  53. func makeValidInput() *fakeakeyless.Input {
  54. return &fakeakeyless.Input{
  55. SecretName: "name",
  56. Version: 0,
  57. Token: "token",
  58. }
  59. }
  60. func makeValidOutput() *fakeakeyless.Output {
  61. return &fakeakeyless.Output{
  62. Value: "secret-val",
  63. Err: nil,
  64. }
  65. }
  66. func makeValidAkeylessTestCaseCustom(tweaks ...func(smtc *akeylessTestCase)) *akeylessTestCase {
  67. smtc := makeValidAkeylessTestCase()
  68. for _, fn := range tweaks {
  69. fn(smtc)
  70. }
  71. smtc.mockClient.WithValue(smtc.apiInput, smtc.apiOutput)
  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 *akeylessTestCase) {
  77. smtc.apiOutput.Err = fmt.Errorf("oh no")
  78. smtc.expectError = "oh no"
  79. }
  80. var setNilMockClient = func(smtc *akeylessTestCase) {
  81. smtc.mockClient = nil
  82. smtc.expectError = errUninitalizedAkeylessProvider
  83. }
  84. func TestAkeylessGetSecret(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 *akeylessTestCase) {
  89. smtc.apiOutput = &fakeakeyless.Output{
  90. Value: secretValue,
  91. Err: nil,
  92. }
  93. smtc.expectedSecret = secretValue
  94. }
  95. successCases := []*akeylessTestCase{
  96. makeValidAkeylessTestCaseCustom(setAPIErr),
  97. makeValidAkeylessTestCaseCustom(setSecretString),
  98. makeValidAkeylessTestCaseCustom(setNilMockClient),
  99. }
  100. sm := Akeyless{}
  101. for k, v := range successCases {
  102. sm.Client = v.mockClient
  103. fmt.Println(*v.ref)
  104. out, err := sm.GetSecret(context.Background(), *v.ref)
  105. if !ErrorContains(err, v.expectError) {
  106. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  107. }
  108. if string(out) != v.expectedSecret {
  109. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  110. }
  111. }
  112. }
  113. func TestGetSecretMap(t *testing.T) {
  114. // good case: default version & deserialization
  115. setDeserialization := func(smtc *akeylessTestCase) {
  116. smtc.apiOutput.Value = `{"foo":"bar"}`
  117. smtc.expectedData["foo"] = []byte("bar")
  118. }
  119. // bad case: invalid json
  120. setInvalidJSON := func(smtc *akeylessTestCase) {
  121. smtc.apiOutput.Value = `-----------------`
  122. smtc.expectError = "unable to unmarshal secret"
  123. }
  124. successCases := []*akeylessTestCase{
  125. makeValidAkeylessTestCaseCustom(setDeserialization),
  126. makeValidAkeylessTestCaseCustom(setInvalidJSON),
  127. makeValidAkeylessTestCaseCustom(setAPIErr),
  128. makeValidAkeylessTestCaseCustom(setNilMockClient),
  129. }
  130. sm := Akeyless{}
  131. for k, v := range successCases {
  132. sm.Client = v.mockClient
  133. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  134. if !ErrorContains(err, v.expectError) {
  135. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  136. }
  137. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  138. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  139. }
  140. }
  141. }
  142. func ErrorContains(out error, want string) bool {
  143. if out == nil {
  144. return want == ""
  145. }
  146. if want == "" {
  147. return false
  148. }
  149. return strings.Contains(out.Error(), want)
  150. }