externalsecret_controller_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 externalsecret
  13. import (
  14. "context"
  15. "fmt"
  16. "time"
  17. . "github.com/onsi/ginkgo"
  18. . "github.com/onsi/gomega"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  25. "github.com/external-secrets/external-secrets/pkg/provider/fake"
  26. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  27. )
  28. var fakeProvider *fake.Client
  29. var _ = Describe("ExternalSecret controller", func() {
  30. const (
  31. ExternalSecretName = "test-es"
  32. ExternalSecretStore = "test-store"
  33. ExternalSecretTargetSecretName = "test-secret"
  34. timeout = time.Second * 5
  35. interval = time.Millisecond * 250
  36. )
  37. var ExternalSecretNamespace string
  38. BeforeEach(func() {
  39. var err error
  40. ExternalSecretNamespace, err = CreateNamespace("test-ns", k8sClient)
  41. Expect(err).ToNot(HaveOccurred())
  42. Expect(k8sClient.Create(context.Background(), &esv1alpha1.SecretStore{
  43. ObjectMeta: metav1.ObjectMeta{
  44. Name: ExternalSecretStore,
  45. Namespace: ExternalSecretNamespace,
  46. },
  47. Spec: esv1alpha1.SecretStoreSpec{
  48. Provider: &esv1alpha1.SecretStoreProvider{
  49. AWSSM: &esv1alpha1.AWSSMProvider{},
  50. },
  51. },
  52. })).To(Succeed())
  53. })
  54. AfterEach(func() {
  55. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  56. ObjectMeta: metav1.ObjectMeta{
  57. Name: ExternalSecretNamespace,
  58. },
  59. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  60. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  61. ObjectMeta: metav1.ObjectMeta{
  62. Name: ExternalSecretStore,
  63. Namespace: ExternalSecretNamespace,
  64. },
  65. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  66. })
  67. Context("When updating ExternalSecret Status", func() {
  68. It("should set the condition eventually", func() {
  69. By("creating an ExternalSecret")
  70. ctx := context.Background()
  71. es := &esv1alpha1.ExternalSecret{
  72. ObjectMeta: metav1.ObjectMeta{
  73. Name: ExternalSecretName,
  74. Namespace: ExternalSecretNamespace,
  75. },
  76. Spec: esv1alpha1.ExternalSecretSpec{
  77. SecretStoreRef: esv1alpha1.SecretStoreRef{
  78. Name: ExternalSecretStore,
  79. },
  80. Target: esv1alpha1.ExternalSecretTarget{
  81. Name: ExternalSecretTargetSecretName,
  82. },
  83. },
  84. }
  85. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  86. esLookupKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  87. createdES := &esv1alpha1.ExternalSecret{}
  88. Eventually(func() bool {
  89. err := k8sClient.Get(ctx, esLookupKey, createdES)
  90. if err != nil {
  91. return false
  92. }
  93. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  94. if cond == nil || cond.Status != v1.ConditionTrue {
  95. return false
  96. }
  97. return true
  98. }, timeout, interval).Should(BeTrue())
  99. })
  100. })
  101. Context("When syncing ExternalSecret value", func() {
  102. It("should set the secret value", func() {
  103. By("creating an ExternalSecret")
  104. ctx := context.Background()
  105. const targetProp = "targetProperty"
  106. const secretVal = "someValue"
  107. es := &esv1alpha1.ExternalSecret{
  108. ObjectMeta: metav1.ObjectMeta{
  109. Name: ExternalSecretName,
  110. Namespace: ExternalSecretNamespace,
  111. },
  112. Spec: esv1alpha1.ExternalSecretSpec{
  113. SecretStoreRef: esv1alpha1.SecretStoreRef{
  114. Name: ExternalSecretStore,
  115. },
  116. Target: esv1alpha1.ExternalSecretTarget{
  117. Name: ExternalSecretTargetSecretName,
  118. },
  119. Data: []esv1alpha1.ExternalSecretData{
  120. {
  121. SecretKey: targetProp,
  122. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  123. Key: "barz",
  124. Property: "bang",
  125. },
  126. },
  127. },
  128. },
  129. }
  130. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  131. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  132. secretLookupKey := types.NamespacedName{
  133. Name: ExternalSecretTargetSecretName,
  134. Namespace: ExternalSecretNamespace}
  135. syncedSecret := &v1.Secret{}
  136. Eventually(func() bool {
  137. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  138. if err != nil {
  139. return false
  140. }
  141. v := syncedSecret.Data[targetProp]
  142. return string(v) == secretVal
  143. }, timeout, interval).Should(BeTrue())
  144. })
  145. })
  146. })
  147. // CreateNamespace creates a new namespace in the cluster.
  148. func CreateNamespace(baseName string, c client.Client) (string, error) {
  149. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  150. ns := &v1.Namespace{
  151. ObjectMeta: metav1.ObjectMeta{
  152. GenerateName: genName,
  153. },
  154. }
  155. var err error
  156. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  157. err = c.Create(context.Background(), ns)
  158. if err != nil {
  159. return false, nil
  160. }
  161. return true, nil
  162. })
  163. if err != nil {
  164. return "", err
  165. }
  166. return ns.Name, nil
  167. }
  168. func init() {
  169. fakeProvider = fake.New()
  170. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  171. AWSSM: &esv1alpha1.AWSSMProvider{},
  172. })
  173. }