common_unit_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package secretstore
  14. import (
  15. "context"
  16. "testing"
  17. corev1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. )
  21. type fakeCapabilityClient struct {
  22. caps esapi.SecretStoreCapabilities
  23. }
  24. func (f *fakeCapabilityClient) GetSecret(context.Context, esapi.ExternalSecretDataRemoteRef) ([]byte, error) {
  25. return nil, nil
  26. }
  27. func (f *fakeCapabilityClient) PushSecret(context.Context, *corev1.Secret, esapi.PushSecretData) error {
  28. return nil
  29. }
  30. func (f *fakeCapabilityClient) DeleteSecret(context.Context, esapi.PushSecretRemoteRef) error {
  31. return nil
  32. }
  33. func (f *fakeCapabilityClient) SecretExists(context.Context, esapi.PushSecretRemoteRef) (bool, error) {
  34. return false, nil
  35. }
  36. func (f *fakeCapabilityClient) Validate() (esapi.ValidationResult, error) {
  37. return esapi.ValidationResultReady, nil
  38. }
  39. func (f *fakeCapabilityClient) GetSecretMap(context.Context, esapi.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  40. return nil, nil
  41. }
  42. func (f *fakeCapabilityClient) GetAllSecrets(context.Context, esapi.ExternalSecretFind) (map[string][]byte, error) {
  43. return nil, nil
  44. }
  45. func (f *fakeCapabilityClient) Close(context.Context) error {
  46. return nil
  47. }
  48. func (f *fakeCapabilityClient) Capabilities(context.Context) (esapi.SecretStoreCapabilities, error) {
  49. return f.caps, nil
  50. }
  51. func TestResolveStoreCapabilitiesUsesRemoteClientWhenProviderRefIsSet(t *testing.T) {
  52. store := &esapi.SecretStore{
  53. Spec: esapi.SecretStoreSpec{
  54. RuntimeRef: &esapi.StoreRuntimeRef{Name: "fake-runtime"},
  55. ProviderRef: &esapi.StoreProviderRef{
  56. APIVersion: "provider.external-secrets.io/v2alpha1",
  57. Kind: "Fake",
  58. Name: "fake-config",
  59. },
  60. },
  61. }
  62. client := &fakeCapabilityClient{caps: esapi.SecretStoreReadWrite}
  63. caps, err := resolveStoreCapabilities(context.Background(), store, client)
  64. if err != nil {
  65. t.Fatalf("resolveStoreCapabilities() error = %v", err)
  66. }
  67. if caps != esapi.SecretStoreReadWrite {
  68. t.Fatalf("expected %v, got %v", esapi.SecretStoreReadWrite, caps)
  69. }
  70. }
  71. func TestShouldDeferClusterStoreValidation(t *testing.T) {
  72. t.Run("defer caller dependent cluster store", func(t *testing.T) {
  73. store := &esapi.ClusterSecretStore{
  74. TypeMeta: metav1.TypeMeta{Kind: esapi.ClusterSecretStoreKind},
  75. Spec: esapi.SecretStoreSpec{
  76. RuntimeRef: &esapi.StoreRuntimeRef{Name: "fake-runtime"},
  77. ProviderRef: &esapi.StoreProviderRef{
  78. APIVersion: "provider.external-secrets.io/v2alpha1",
  79. Kind: "Fake",
  80. Name: "fake-config",
  81. },
  82. },
  83. }
  84. if !shouldDeferClusterStoreValidation(store, "") {
  85. t.Fatalf("expected cluster store validation to defer without caller namespace")
  86. }
  87. })
  88. t.Run("do not defer namespaced provider ref", func(t *testing.T) {
  89. store := &esapi.ClusterSecretStore{
  90. TypeMeta: metav1.TypeMeta{Kind: esapi.ClusterSecretStoreKind},
  91. Spec: esapi.SecretStoreSpec{
  92. RuntimeRef: &esapi.StoreRuntimeRef{Name: "fake-runtime"},
  93. ProviderRef: &esapi.StoreProviderRef{
  94. APIVersion: "provider.external-secrets.io/v2alpha1",
  95. Kind: "Fake",
  96. Name: "fake-config",
  97. Namespace: "provider-ns",
  98. },
  99. },
  100. }
  101. if shouldDeferClusterStoreValidation(store, "") {
  102. t.Fatalf("did not expect namespaced provider ref to defer validation")
  103. }
  104. })
  105. t.Run("do not defer when caller namespace is available", func(t *testing.T) {
  106. store := &esapi.ClusterSecretStore{
  107. TypeMeta: metav1.TypeMeta{Kind: esapi.ClusterSecretStoreKind},
  108. Spec: esapi.SecretStoreSpec{
  109. RuntimeRef: &esapi.StoreRuntimeRef{Name: "fake-runtime"},
  110. ProviderRef: &esapi.StoreProviderRef{
  111. APIVersion: "provider.external-secrets.io/v2alpha1",
  112. Kind: "Fake",
  113. Name: "fake-config",
  114. },
  115. },
  116. }
  117. if shouldDeferClusterStoreValidation(store, "workload-ns") {
  118. t.Fatalf("did not expect validation to defer when caller namespace is known")
  119. }
  120. })
  121. }