externalsecret_controller_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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"
  26. "github.com/external-secrets/external-secrets/pkg/provider/fake"
  27. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  28. )
  29. var fakeProvider *fake.Client
  30. var _ = Describe("ExternalSecret controller", func() {
  31. const (
  32. ExternalSecretName = "test-es"
  33. ExternalSecretStore = "test-store"
  34. ExternalSecretTargetSecretName = "test-secret"
  35. timeout = time.Second * 5
  36. interval = time.Millisecond * 250
  37. )
  38. var ExternalSecretNamespace string
  39. BeforeEach(func() {
  40. var err error
  41. ExternalSecretNamespace, err = CreateNamespace("test-ns", k8sClient)
  42. Expect(err).ToNot(HaveOccurred())
  43. Expect(k8sClient.Create(context.Background(), &esv1alpha1.SecretStore{
  44. ObjectMeta: metav1.ObjectMeta{
  45. Name: ExternalSecretStore,
  46. Namespace: ExternalSecretNamespace,
  47. },
  48. Spec: esv1alpha1.SecretStoreSpec{
  49. Provider: &esv1alpha1.SecretStoreProvider{
  50. AWSSM: &esv1alpha1.AWSSMProvider{},
  51. },
  52. },
  53. })).To(Succeed())
  54. })
  55. AfterEach(func() {
  56. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  57. ObjectMeta: metav1.ObjectMeta{
  58. Name: ExternalSecretNamespace,
  59. },
  60. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  61. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  62. ObjectMeta: metav1.ObjectMeta{
  63. Name: ExternalSecretStore,
  64. Namespace: ExternalSecretNamespace,
  65. },
  66. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  67. })
  68. Context("When updating ExternalSecret Status", func() {
  69. It("should set the condition eventually", func() {
  70. By("creating an ExternalSecret")
  71. ctx := context.Background()
  72. es := &esv1alpha1.ExternalSecret{
  73. ObjectMeta: metav1.ObjectMeta{
  74. Name: ExternalSecretName,
  75. Namespace: ExternalSecretNamespace,
  76. },
  77. Spec: esv1alpha1.ExternalSecretSpec{
  78. SecretStoreRef: esv1alpha1.SecretStoreRef{
  79. Name: ExternalSecretStore,
  80. },
  81. Target: esv1alpha1.ExternalSecretTarget{
  82. Name: ExternalSecretTargetSecretName,
  83. },
  84. },
  85. }
  86. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  87. esLookupKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  88. createdES := &esv1alpha1.ExternalSecret{}
  89. Eventually(func() bool {
  90. err := k8sClient.Get(ctx, esLookupKey, createdES)
  91. if err != nil {
  92. return false
  93. }
  94. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  95. if cond == nil || cond.Status != v1.ConditionTrue {
  96. return false
  97. }
  98. return true
  99. }, timeout, interval).Should(BeTrue())
  100. })
  101. })
  102. Context("When syncing ExternalSecret value", func() {
  103. It("should set the secret value and sync labels/annotations", func() {
  104. By("creating an ExternalSecret")
  105. ctx := context.Background()
  106. const targetProp = "targetProperty"
  107. const secretVal = "someValue"
  108. es := &esv1alpha1.ExternalSecret{
  109. ObjectMeta: metav1.ObjectMeta{
  110. Name: ExternalSecretName,
  111. Namespace: ExternalSecretNamespace,
  112. Labels: map[string]string{
  113. "fooobar": "bazz",
  114. "bazzing": "booze",
  115. },
  116. Annotations: map[string]string{
  117. "hihihih": "hehehe",
  118. "harharhra": "yallayalla",
  119. },
  120. },
  121. Spec: esv1alpha1.ExternalSecretSpec{
  122. SecretStoreRef: esv1alpha1.SecretStoreRef{
  123. Name: ExternalSecretStore,
  124. },
  125. Target: esv1alpha1.ExternalSecretTarget{
  126. Name: ExternalSecretTargetSecretName,
  127. },
  128. Data: []esv1alpha1.ExternalSecretData{
  129. {
  130. SecretKey: targetProp,
  131. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  132. Key: "barz",
  133. Property: "bang",
  134. },
  135. },
  136. },
  137. },
  138. }
  139. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  140. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  141. secretLookupKey := types.NamespacedName{
  142. Name: ExternalSecretTargetSecretName,
  143. Namespace: ExternalSecretNamespace}
  144. syncedSecret := &v1.Secret{}
  145. Eventually(func() bool {
  146. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  147. if err != nil {
  148. return false
  149. }
  150. v := syncedSecret.Data[targetProp]
  151. return string(v) == secretVal
  152. }, timeout, interval).Should(BeTrue())
  153. Expect(syncedSecret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  154. Expect(syncedSecret.ObjectMeta.Annotations).To(BeEquivalentTo(es.ObjectMeta.Annotations))
  155. })
  156. It("should fetch secrets using dataFrom", func() {
  157. By("creating an ExternalSecret")
  158. ctx := context.Background()
  159. const secretVal = "someValue"
  160. es := &esv1alpha1.ExternalSecret{
  161. ObjectMeta: metav1.ObjectMeta{
  162. Name: ExternalSecretName,
  163. Namespace: ExternalSecretNamespace,
  164. },
  165. Spec: esv1alpha1.ExternalSecretSpec{
  166. SecretStoreRef: esv1alpha1.SecretStoreRef{
  167. Name: ExternalSecretStore,
  168. },
  169. Target: esv1alpha1.ExternalSecretTarget{
  170. Name: ExternalSecretTargetSecretName,
  171. },
  172. DataFrom: []esv1alpha1.ExternalSecretDataRemoteRef{
  173. {
  174. Key: "barz",
  175. },
  176. },
  177. },
  178. }
  179. fakeProvider.WithGetSecretMap(map[string][]byte{
  180. "foo": []byte("bar"),
  181. "baz": []byte("bang"),
  182. }, nil)
  183. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  184. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  185. secretLookupKey := types.NamespacedName{
  186. Name: ExternalSecretTargetSecretName,
  187. Namespace: ExternalSecretNamespace}
  188. syncedSecret := &v1.Secret{}
  189. Eventually(func() bool {
  190. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  191. if err != nil {
  192. return false
  193. }
  194. x := syncedSecret.Data["foo"]
  195. y := syncedSecret.Data["baz"]
  196. return string(x) == "bar" && string(y) == "bang"
  197. }, timeout, interval).Should(BeTrue())
  198. })
  199. It("should set an error condition when provider errors", func() {
  200. By("creating an ExternalSecret")
  201. ctx := context.Background()
  202. const targetProp = "targetProperty"
  203. es := &esv1alpha1.ExternalSecret{
  204. ObjectMeta: metav1.ObjectMeta{
  205. Name: ExternalSecretName,
  206. Namespace: ExternalSecretNamespace,
  207. },
  208. Spec: esv1alpha1.ExternalSecretSpec{
  209. SecretStoreRef: esv1alpha1.SecretStoreRef{
  210. Name: ExternalSecretStore,
  211. },
  212. Target: esv1alpha1.ExternalSecretTarget{
  213. Name: ExternalSecretTargetSecretName,
  214. },
  215. Data: []esv1alpha1.ExternalSecretData{
  216. {
  217. SecretKey: targetProp,
  218. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  219. Key: "barz",
  220. Property: "bang",
  221. },
  222. },
  223. },
  224. },
  225. }
  226. fakeProvider.WithGetSecret(nil, fmt.Errorf("artificial testing error"))
  227. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  228. esLookupKey := types.NamespacedName{
  229. Name: ExternalSecretName,
  230. Namespace: ExternalSecretNamespace}
  231. createdES := &esv1alpha1.ExternalSecret{}
  232. Eventually(func() bool {
  233. err := k8sClient.Get(ctx, esLookupKey, createdES)
  234. if err != nil {
  235. return false
  236. }
  237. // condition must be false
  238. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  239. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  240. return false
  241. }
  242. return true
  243. }, timeout, interval).Should(BeTrue())
  244. })
  245. It("should set an error condition when store does not exist", func() {
  246. By("creating an ExternalSecret")
  247. ctx := context.Background()
  248. const targetProp = "targetProperty"
  249. es := &esv1alpha1.ExternalSecret{
  250. ObjectMeta: metav1.ObjectMeta{
  251. Name: ExternalSecretName,
  252. Namespace: ExternalSecretNamespace,
  253. },
  254. Spec: esv1alpha1.ExternalSecretSpec{
  255. SecretStoreRef: esv1alpha1.SecretStoreRef{
  256. Name: "storeshouldnotexist",
  257. },
  258. Target: esv1alpha1.ExternalSecretTarget{
  259. Name: ExternalSecretTargetSecretName,
  260. },
  261. Data: []esv1alpha1.ExternalSecretData{
  262. {
  263. SecretKey: targetProp,
  264. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  265. Key: "barz",
  266. Property: "bang",
  267. },
  268. },
  269. },
  270. },
  271. }
  272. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  273. esLookupKey := types.NamespacedName{
  274. Name: ExternalSecretName,
  275. Namespace: ExternalSecretNamespace}
  276. createdES := &esv1alpha1.ExternalSecret{}
  277. Eventually(func() bool {
  278. err := k8sClient.Get(ctx, esLookupKey, createdES)
  279. if err != nil {
  280. return false
  281. }
  282. // condition must be false
  283. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  284. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  285. return false
  286. }
  287. return true
  288. }, timeout, interval).Should(BeTrue())
  289. })
  290. It("should set an error condition when store provider constructor fails", func() {
  291. By("creating an ExternalSecret")
  292. ctx := context.Background()
  293. const targetProp = "targetProperty"
  294. es := &esv1alpha1.ExternalSecret{
  295. ObjectMeta: metav1.ObjectMeta{
  296. Name: ExternalSecretName,
  297. Namespace: ExternalSecretNamespace,
  298. },
  299. Spec: esv1alpha1.ExternalSecretSpec{
  300. SecretStoreRef: esv1alpha1.SecretStoreRef{
  301. Name: ExternalSecretStore,
  302. },
  303. Target: esv1alpha1.ExternalSecretTarget{
  304. Name: ExternalSecretTargetSecretName,
  305. },
  306. Data: []esv1alpha1.ExternalSecretData{
  307. {
  308. SecretKey: targetProp,
  309. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  310. Key: "barz",
  311. Property: "bang",
  312. },
  313. },
  314. },
  315. },
  316. }
  317. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  318. string) (provider.Provider, error) {
  319. return nil, fmt.Errorf("artificial constructor error")
  320. })
  321. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  322. esLookupKey := types.NamespacedName{
  323. Name: ExternalSecretName,
  324. Namespace: ExternalSecretNamespace}
  325. createdES := &esv1alpha1.ExternalSecret{}
  326. Eventually(func() bool {
  327. err := k8sClient.Get(ctx, esLookupKey, createdES)
  328. if err != nil {
  329. return false
  330. }
  331. // condition must be false
  332. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  333. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  334. return false
  335. }
  336. return true
  337. }, timeout, interval).Should(BeTrue())
  338. })
  339. It("should not process stores with mismatching controller field", func() {
  340. By("creating an ExternalSecret")
  341. ctx := context.Background()
  342. storeName := "example-ts-foo"
  343. Expect(k8sClient.Create(context.Background(), &esv1alpha1.SecretStore{
  344. ObjectMeta: metav1.ObjectMeta{
  345. Name: storeName,
  346. Namespace: ExternalSecretNamespace,
  347. },
  348. Spec: esv1alpha1.SecretStoreSpec{
  349. Controller: "some-other-controller",
  350. Provider: &esv1alpha1.SecretStoreProvider{
  351. AWSSM: &esv1alpha1.AWSSMProvider{},
  352. },
  353. },
  354. })).To(Succeed())
  355. defer func() {
  356. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  357. ObjectMeta: metav1.ObjectMeta{
  358. Name: storeName,
  359. Namespace: ExternalSecretNamespace,
  360. },
  361. })).To(Succeed())
  362. }()
  363. es := &esv1alpha1.ExternalSecret{
  364. ObjectMeta: metav1.ObjectMeta{
  365. Name: ExternalSecretName,
  366. Namespace: ExternalSecretNamespace,
  367. },
  368. Spec: esv1alpha1.ExternalSecretSpec{
  369. SecretStoreRef: esv1alpha1.SecretStoreRef{
  370. Name: storeName,
  371. },
  372. Target: esv1alpha1.ExternalSecretTarget{
  373. Name: ExternalSecretTargetSecretName,
  374. },
  375. Data: []esv1alpha1.ExternalSecretData{
  376. {
  377. SecretKey: "doesnothing",
  378. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  379. Key: "barz",
  380. Property: "bang",
  381. },
  382. },
  383. },
  384. },
  385. }
  386. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  387. secretLookupKey := types.NamespacedName{
  388. Name: ExternalSecretName,
  389. Namespace: ExternalSecretNamespace,
  390. }
  391. // COND
  392. createdES := &esv1alpha1.ExternalSecret{}
  393. Consistently(func() bool {
  394. err := k8sClient.Get(ctx, secretLookupKey, createdES)
  395. if err != nil {
  396. return false
  397. }
  398. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  399. return cond == nil
  400. }, timeout, interval).Should(BeTrue())
  401. })
  402. })
  403. })
  404. // CreateNamespace creates a new namespace in the cluster.
  405. func CreateNamespace(baseName string, c client.Client) (string, error) {
  406. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  407. ns := &v1.Namespace{
  408. ObjectMeta: metav1.ObjectMeta{
  409. GenerateName: genName,
  410. },
  411. }
  412. var err error
  413. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  414. err = c.Create(context.Background(), ns)
  415. if err != nil {
  416. return false, nil
  417. }
  418. return true, nil
  419. })
  420. if err != nil {
  421. return "", err
  422. }
  423. return ns.Name, nil
  424. }
  425. func init() {
  426. fakeProvider = fake.New()
  427. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  428. AWSSM: &esv1alpha1.AWSSMProvider{},
  429. })
  430. }