akeyless_test.go 4.6 KB

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