externalsecret_controller_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. FIt("should not process stores with mismatching controller field", func() {
  147. By("creating an ExternalSecret")
  148. ctx := context.Background()
  149. storeName := "example-ts-foo"
  150. Expect(k8sClient.Create(context.Background(), &esv1alpha1.SecretStore{
  151. ObjectMeta: metav1.ObjectMeta{
  152. Name: storeName,
  153. Namespace: ExternalSecretNamespace,
  154. },
  155. Spec: esv1alpha1.SecretStoreSpec{
  156. Controller: "some-other-controller",
  157. Provider: &esv1alpha1.SecretStoreProvider{
  158. AWSSM: &esv1alpha1.AWSSMProvider{},
  159. },
  160. },
  161. })).To(Succeed())
  162. defer func() {
  163. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  164. ObjectMeta: metav1.ObjectMeta{
  165. Name: storeName,
  166. Namespace: ExternalSecretNamespace,
  167. },
  168. })).To(Succeed())
  169. }()
  170. es := &esv1alpha1.ExternalSecret{
  171. ObjectMeta: metav1.ObjectMeta{
  172. Name: ExternalSecretName,
  173. Namespace: ExternalSecretNamespace,
  174. },
  175. Spec: esv1alpha1.ExternalSecretSpec{
  176. SecretStoreRef: esv1alpha1.SecretStoreRef{
  177. Name: storeName,
  178. },
  179. Target: esv1alpha1.ExternalSecretTarget{
  180. Name: ExternalSecretTargetSecretName,
  181. },
  182. Data: []esv1alpha1.ExternalSecretData{
  183. {
  184. SecretKey: "doesnothing",
  185. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  186. Key: "barz",
  187. Property: "bang",
  188. },
  189. },
  190. },
  191. },
  192. }
  193. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  194. secretLookupKey := types.NamespacedName{
  195. Name: ExternalSecretName,
  196. Namespace: ExternalSecretNamespace,
  197. }
  198. // COND
  199. createdES := &esv1alpha1.ExternalSecret{}
  200. Consistently(func() bool {
  201. err := k8sClient.Get(ctx, secretLookupKey, createdES)
  202. if err != nil {
  203. return false
  204. }
  205. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  206. if cond == nil {
  207. return true
  208. }
  209. return false
  210. }, timeout, interval).Should(BeTrue())
  211. })
  212. // TODO:
  213. // * do not find store
  214. // * missing store provider
  215. // * provider client constructor error
  216. // * getproviderSecretData error
  217. // * check sync failed condition
  218. })
  219. // CreateNamespace creates a new namespace in the cluster.
  220. func CreateNamespace(baseName string, c client.Client) (string, error) {
  221. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  222. ns := &v1.Namespace{
  223. ObjectMeta: metav1.ObjectMeta{
  224. GenerateName: genName,
  225. },
  226. }
  227. var err error
  228. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  229. err = c.Create(context.Background(), ns)
  230. if err != nil {
  231. return false, nil
  232. }
  233. return true, nil
  234. })
  235. if err != nil {
  236. return "", err
  237. }
  238. return ns.Name, nil
  239. }
  240. func init() {
  241. fakeProvider = fake.New()
  242. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  243. AWSSM: &esv1alpha1.AWSSMProvider{},
  244. })
  245. }