helpers_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 v2
  14. import (
  15. "context"
  16. "testing"
  17. "time"
  18. corev1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. "sigs.k8s.io/controller-runtime/pkg/client/fake"
  23. "github.com/external-secrets/external-secrets-e2e/framework"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  26. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  27. . "github.com/onsi/gomega"
  28. )
  29. func TestNewKubernetesStoreProviderUsesProvidedCABundle(t *testing.T) {
  30. t.Helper()
  31. RegisterTestingT(t)
  32. provider := NewKubernetesStoreProvider("remote-ns", "eso-auth", nil, []byte("inline-ca"))
  33. Expect(provider).NotTo(BeNil())
  34. Expect(provider.Kubernetes).NotTo(BeNil())
  35. Expect(provider.Kubernetes.Server.CABundle).To(Equal([]byte("inline-ca")))
  36. Expect(provider.Kubernetes.Server.CAProvider).To(BeNil())
  37. Expect(provider.Kubernetes.Auth).NotTo(BeNil())
  38. Expect(provider.Kubernetes.Auth.ServiceAccount).To(Equal(&esmeta.ServiceAccountSelector{
  39. Name: "eso-auth",
  40. Namespace: nil,
  41. }))
  42. }
  43. func TestGetClusterCABundleWaitsForRootCAConfigMap(t *testing.T) {
  44. t.Helper()
  45. RegisterTestingT(t)
  46. scheme := runtime.NewScheme()
  47. Expect(corev1.AddToScheme(scheme)).To(Succeed())
  48. cl := fake.NewClientBuilder().WithScheme(scheme).Build()
  49. f := &framework.Framework{
  50. CRClient: cl,
  51. }
  52. go func() {
  53. time.Sleep(25 * time.Millisecond)
  54. Expect(cl.Create(context.Background(), &corev1.ConfigMap{
  55. ObjectMeta: metav1.ObjectMeta{
  56. Name: "kube-root-ca.crt",
  57. Namespace: "test",
  58. },
  59. Data: map[string]string{
  60. "ca.crt": "root-ca-data",
  61. },
  62. })).To(Succeed())
  63. }()
  64. Expect(GetClusterCABundle(f, "test")).To(Equal([]byte("root-ca-data")))
  65. }
  66. func TestCreateRuntimeSecretStoreCreatesStoreAndProviderClass(t *testing.T) {
  67. t.Helper()
  68. RegisterTestingT(t)
  69. scheme := runtime.NewScheme()
  70. Expect(esv1.AddToScheme(scheme)).To(Succeed())
  71. Expect(esv1alpha1.AddToScheme(scheme)).To(Succeed())
  72. cl := fake.NewClientBuilder().WithScheme(scheme).Build()
  73. f := &framework.Framework{CRClient: cl}
  74. store := CreateRuntimeSecretStore(f, "test", "example", "provider-fake.test.svc:8080", &esv1.StoreProviderRef{
  75. APIVersion: "provider.external-secrets.io/v2alpha1",
  76. Kind: "Fake",
  77. Name: "fake-config",
  78. })
  79. Expect(store).NotTo(BeNil())
  80. Expect(store.Spec.RuntimeRef).NotTo(BeNil())
  81. Expect(store.Spec.RuntimeRef.Name).To(Equal(runtimeClassName("example")))
  82. Expect(store.Spec.RuntimeRef.Kind).To(BeEmpty())
  83. Expect(store.Spec.Provider).To(BeNil())
  84. Expect(store.Spec.ProviderRef).NotTo(BeNil())
  85. Expect(store.Spec.ProviderRef.Name).To(Equal("fake-config"))
  86. var persistedStore esv1.SecretStore
  87. err := cl.Get(context.Background(), client.ObjectKey{Name: "example", Namespace: "test"}, &persistedStore)
  88. Expect(err).NotTo(HaveOccurred())
  89. Expect(persistedStore.Spec.Provider).To(BeNil())
  90. Expect(persistedStore.Spec.ProviderRef).NotTo(BeNil())
  91. Expect(persistedStore.Spec.ProviderRef.Kind).To(Equal("Fake"))
  92. var runtimeClass esv1alpha1.ProviderClass
  93. err = cl.Get(context.Background(), client.ObjectKey{Name: runtimeClassName("example"), Namespace: "test"}, &runtimeClass)
  94. Expect(err).NotTo(HaveOccurred())
  95. Expect(runtimeClass.Spec.Address).To(Equal("provider-fake.test.svc:8080"))
  96. }
  97. func TestCreateRuntimeClusterSecretStoreCreatesStoreAndClusterProviderClass(t *testing.T) {
  98. t.Helper()
  99. RegisterTestingT(t)
  100. scheme := runtime.NewScheme()
  101. Expect(esv1.AddToScheme(scheme)).To(Succeed())
  102. Expect(esv1alpha1.AddToScheme(scheme)).To(Succeed())
  103. cl := fake.NewClientBuilder().WithScheme(scheme).Build()
  104. f := &framework.Framework{CRClient: cl}
  105. store := CreateRuntimeClusterSecretStore(f, "example", "provider-fake.external-secrets-system.svc:8080", &esv1.StoreProviderRef{
  106. APIVersion: "provider.external-secrets.io/v2alpha1",
  107. Kind: "Fake",
  108. Name: "fake-config",
  109. }, []esv1.ClusterSecretStoreCondition{{
  110. Namespaces: []string{"tenant-a"},
  111. }})
  112. Expect(store).NotTo(BeNil())
  113. Expect(store.Spec.RuntimeRef).NotTo(BeNil())
  114. Expect(store.Spec.RuntimeRef.Name).To(Equal(runtimeClassName("example")))
  115. Expect(store.Spec.RuntimeRef.Kind).To(BeEmpty())
  116. Expect(store.Spec.Conditions).To(HaveLen(1))
  117. Expect(store.Spec.Provider).To(BeNil())
  118. Expect(store.Spec.ProviderRef).NotTo(BeNil())
  119. Expect(store.Spec.ProviderRef.Name).To(Equal("fake-config"))
  120. var persistedStore esv1.ClusterSecretStore
  121. err := cl.Get(context.Background(), client.ObjectKey{Name: "example"}, &persistedStore)
  122. Expect(err).NotTo(HaveOccurred())
  123. Expect(persistedStore.Spec.Provider).To(BeNil())
  124. Expect(persistedStore.Spec.ProviderRef).NotTo(BeNil())
  125. Expect(persistedStore.Spec.ProviderRef.Kind).To(Equal("Fake"))
  126. var runtimeClass esv1alpha1.ClusterProviderClass
  127. err = cl.Get(context.Background(), client.ObjectKey{Name: runtimeClassName("example")}, &runtimeClass)
  128. Expect(err).NotTo(HaveOccurred())
  129. Expect(runtimeClass.Spec.Address).To(Equal("provider-fake.external-secrets-system.svc:8080"))
  130. }
  131. func TestWaitForSecretStoreConditionMatchesReadyStatus(t *testing.T) {
  132. t.Helper()
  133. RegisterTestingT(t)
  134. scheme := runtime.NewScheme()
  135. Expect(esv1.AddToScheme(scheme)).To(Succeed())
  136. store := &esv1.SecretStore{
  137. ObjectMeta: metav1.ObjectMeta{
  138. Name: "example",
  139. Namespace: "test",
  140. },
  141. Status: esv1.SecretStoreStatus{
  142. Conditions: []esv1.SecretStoreStatusCondition{{
  143. Type: esv1.SecretStoreReady,
  144. Status: corev1.ConditionTrue,
  145. }},
  146. },
  147. }
  148. cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(store).Build()
  149. f := &framework.Framework{CRClient: cl}
  150. got := WaitForSecretStoreCondition(f, "test", "example", metav1.ConditionTrue, 100*time.Millisecond)
  151. Expect(got).NotTo(BeNil())
  152. Expect(got.GetName()).To(Equal("example"))
  153. }
  154. func TestWaitForClusterSecretStoreConditionMatchesReadyStatus(t *testing.T) {
  155. t.Helper()
  156. RegisterTestingT(t)
  157. scheme := runtime.NewScheme()
  158. Expect(esv1.AddToScheme(scheme)).To(Succeed())
  159. store := &esv1.ClusterSecretStore{
  160. ObjectMeta: metav1.ObjectMeta{
  161. Name: "example",
  162. },
  163. Status: esv1.SecretStoreStatus{
  164. Conditions: []esv1.SecretStoreStatusCondition{{
  165. Type: esv1.SecretStoreReady,
  166. Status: corev1.ConditionTrue,
  167. }},
  168. },
  169. }
  170. cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(store).Build()
  171. f := &framework.Framework{CRClient: cl}
  172. got := WaitForClusterSecretStoreCondition(f, "example", metav1.ConditionTrue, 100*time.Millisecond)
  173. Expect(got).NotTo(BeNil())
  174. Expect(got.GetName()).To(Equal("example"))
  175. }