provider_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 conjur
  13. import (
  14. "context"
  15. "fmt"
  16. "testing"
  17. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  18. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  19. fakeconjur "github.com/external-secrets/external-secrets/pkg/provider/conjur/fake"
  20. )
  21. var (
  22. svcURL = "https://example.com"
  23. svcUser = "user"
  24. svcApikey = "apikey"
  25. svcAccount = "account1"
  26. )
  27. type secretManagerTestCase struct {
  28. err error
  29. refKey string
  30. }
  31. func TestConjurGetSecret(t *testing.T) {
  32. p := Provider{}
  33. p.ConjurClient = &fakeconjur.ConjurMockClient{}
  34. testCases := []*secretManagerTestCase{
  35. {
  36. err: nil,
  37. refKey: "secret",
  38. },
  39. {
  40. err: fmt.Errorf("error"),
  41. refKey: "error",
  42. },
  43. }
  44. for _, tc := range testCases {
  45. ref := makeValidRef(tc.refKey)
  46. _, err := p.GetSecret(context.Background(), *ref)
  47. if tc.err != nil && err != nil && err.Error() != tc.err.Error() {
  48. t.Errorf("test failed! want %v, got %v", tc.err, err)
  49. } else if tc.err == nil && err != nil {
  50. t.Errorf("want nil got err %v", err)
  51. } else if tc.err != nil && err == nil {
  52. t.Errorf("want err %v got nil", tc.err)
  53. }
  54. }
  55. }
  56. func makeValidRef(k string) *esv1beta1.ExternalSecretDataRemoteRef {
  57. return &esv1beta1.ExternalSecretDataRemoteRef{
  58. Key: k,
  59. Version: "default",
  60. }
  61. }
  62. type ValidateStoreTestCase struct {
  63. store *esv1beta1.SecretStore
  64. err error
  65. }
  66. func TestValidateStore(t *testing.T) {
  67. testCases := []ValidateStoreTestCase{
  68. {
  69. store: makeSecretStore(svcURL, svcUser, svcApikey, svcAccount),
  70. err: nil,
  71. },
  72. {
  73. store: makeSecretStore("", svcUser, svcApikey, svcAccount),
  74. err: fmt.Errorf("conjur URL cannot be empty"),
  75. },
  76. {
  77. store: makeSecretStore(svcURL, "", svcApikey, svcAccount),
  78. err: fmt.Errorf("missing Auth.Apikey.UserRef"),
  79. },
  80. {
  81. store: makeSecretStore(svcURL, svcUser, "", svcAccount),
  82. err: fmt.Errorf("missing Auth.Apikey.ApiKeyRef"),
  83. },
  84. {
  85. store: makeSecretStore(svcURL, svcUser, svcApikey, ""),
  86. err: fmt.Errorf("missing Auth.ApiKey.Account"),
  87. },
  88. }
  89. p := Provider{}
  90. for _, tc := range testCases {
  91. err := p.ValidateStore(tc.store)
  92. if tc.err != nil && err != nil && err.Error() != tc.err.Error() {
  93. t.Errorf("test failed! want %v, got %v", tc.err, err)
  94. } else if tc.err == nil && err != nil {
  95. t.Errorf("want nil got err %v", err)
  96. } else if tc.err != nil && err == nil {
  97. t.Errorf("want err %v got nil", tc.err)
  98. }
  99. }
  100. }
  101. func makeSecretStore(svcURL, svcUser, svcApikey, svcAccount string) *esv1beta1.SecretStore {
  102. uref := &esmeta.SecretKeySelector{
  103. Name: "user",
  104. Key: "conjur-hostid",
  105. }
  106. if svcUser == "" {
  107. uref = nil
  108. }
  109. aref := &esmeta.SecretKeySelector{
  110. Name: "apikey",
  111. Key: "conjur-apikey",
  112. }
  113. if svcApikey == "" {
  114. aref = nil
  115. }
  116. store := &esv1beta1.SecretStore{
  117. Spec: esv1beta1.SecretStoreSpec{
  118. Provider: &esv1beta1.SecretStoreProvider{
  119. Conjur: &esv1beta1.ConjurProvider{
  120. URL: svcURL,
  121. Auth: esv1beta1.ConjurAuth{
  122. Apikey: &esv1beta1.ConjurApikey{
  123. Account: svcAccount,
  124. UserRef: uref,
  125. APIKeyRef: aref,
  126. },
  127. },
  128. },
  129. },
  130. },
  131. }
  132. return store
  133. }