externalsecret_controller_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 refresh secret value", func() {
  157. By("creating an ExternalSecret")
  158. ctx := context.Background()
  159. const targetProp = "targetProperty"
  160. const secretVal = "someValue"
  161. es := &esv1alpha1.ExternalSecret{
  162. ObjectMeta: metav1.ObjectMeta{
  163. Name: ExternalSecretName,
  164. Namespace: ExternalSecretNamespace,
  165. },
  166. Spec: esv1alpha1.ExternalSecretSpec{
  167. RefreshInterval: &metav1.Duration{Duration: time.Second},
  168. SecretStoreRef: esv1alpha1.SecretStoreRef{
  169. Name: ExternalSecretStore,
  170. },
  171. Target: esv1alpha1.ExternalSecretTarget{
  172. Name: ExternalSecretTargetSecretName,
  173. },
  174. Data: []esv1alpha1.ExternalSecretData{
  175. {
  176. SecretKey: targetProp,
  177. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  178. Key: "barz",
  179. },
  180. },
  181. },
  182. },
  183. }
  184. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  185. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  186. secretLookupKey := types.NamespacedName{
  187. Name: ExternalSecretTargetSecretName,
  188. Namespace: ExternalSecretNamespace}
  189. syncedSecret := &v1.Secret{}
  190. Eventually(func() bool {
  191. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  192. if err != nil {
  193. return false
  194. }
  195. v := syncedSecret.Data[targetProp]
  196. return string(v) == secretVal
  197. }, timeout, interval).Should(BeTrue())
  198. newValue := "NEW VALUE"
  199. fakeProvider.WithGetSecret([]byte(newValue), nil)
  200. Eventually(func() bool {
  201. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  202. if err != nil {
  203. return false
  204. }
  205. v := syncedSecret.Data[targetProp]
  206. return string(v) == newValue
  207. }, timeout, interval).Should(BeTrue())
  208. })
  209. It("should fetch secrets using dataFrom", func() {
  210. By("creating an ExternalSecret")
  211. ctx := context.Background()
  212. const secretVal = "someValue"
  213. es := &esv1alpha1.ExternalSecret{
  214. ObjectMeta: metav1.ObjectMeta{
  215. Name: ExternalSecretName,
  216. Namespace: ExternalSecretNamespace,
  217. },
  218. Spec: esv1alpha1.ExternalSecretSpec{
  219. SecretStoreRef: esv1alpha1.SecretStoreRef{
  220. Name: ExternalSecretStore,
  221. },
  222. Target: esv1alpha1.ExternalSecretTarget{
  223. Name: ExternalSecretTargetSecretName,
  224. },
  225. DataFrom: []esv1alpha1.ExternalSecretDataRemoteRef{
  226. {
  227. Key: "barz",
  228. },
  229. },
  230. },
  231. }
  232. fakeProvider.WithGetSecretMap(map[string][]byte{
  233. "foo": []byte("bar"),
  234. "baz": []byte("bang"),
  235. }, nil)
  236. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  237. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  238. secretLookupKey := types.NamespacedName{
  239. Name: ExternalSecretTargetSecretName,
  240. Namespace: ExternalSecretNamespace}
  241. syncedSecret := &v1.Secret{}
  242. Eventually(func() bool {
  243. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  244. if err != nil {
  245. return false
  246. }
  247. x := syncedSecret.Data["foo"]
  248. y := syncedSecret.Data["baz"]
  249. return string(x) == "bar" && string(y) == "bang"
  250. }, timeout, interval).Should(BeTrue())
  251. })
  252. It("should set an error condition when provider errors", func() {
  253. By("creating an ExternalSecret")
  254. ctx := context.Background()
  255. const targetProp = "targetProperty"
  256. es := &esv1alpha1.ExternalSecret{
  257. ObjectMeta: metav1.ObjectMeta{
  258. Name: ExternalSecretName,
  259. Namespace: ExternalSecretNamespace,
  260. },
  261. Spec: esv1alpha1.ExternalSecretSpec{
  262. SecretStoreRef: esv1alpha1.SecretStoreRef{
  263. Name: ExternalSecretStore,
  264. },
  265. Target: esv1alpha1.ExternalSecretTarget{
  266. Name: ExternalSecretTargetSecretName,
  267. },
  268. Data: []esv1alpha1.ExternalSecretData{
  269. {
  270. SecretKey: targetProp,
  271. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  272. Key: "barz",
  273. Property: "bang",
  274. },
  275. },
  276. },
  277. },
  278. }
  279. fakeProvider.WithGetSecret(nil, fmt.Errorf("artificial testing error"))
  280. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  281. esLookupKey := types.NamespacedName{
  282. Name: ExternalSecretName,
  283. Namespace: ExternalSecretNamespace}
  284. createdES := &esv1alpha1.ExternalSecret{}
  285. Eventually(func() bool {
  286. err := k8sClient.Get(ctx, esLookupKey, createdES)
  287. if err != nil {
  288. return false
  289. }
  290. // condition must be false
  291. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  292. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  293. return false
  294. }
  295. return true
  296. }, timeout, interval).Should(BeTrue())
  297. })
  298. It("should set an error condition when store does not exist", func() {
  299. By("creating an ExternalSecret")
  300. ctx := context.Background()
  301. const targetProp = "targetProperty"
  302. es := &esv1alpha1.ExternalSecret{
  303. ObjectMeta: metav1.ObjectMeta{
  304. Name: ExternalSecretName,
  305. Namespace: ExternalSecretNamespace,
  306. },
  307. Spec: esv1alpha1.ExternalSecretSpec{
  308. SecretStoreRef: esv1alpha1.SecretStoreRef{
  309. Name: "storeshouldnotexist",
  310. },
  311. Target: esv1alpha1.ExternalSecretTarget{
  312. Name: ExternalSecretTargetSecretName,
  313. },
  314. Data: []esv1alpha1.ExternalSecretData{
  315. {
  316. SecretKey: targetProp,
  317. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  318. Key: "barz",
  319. Property: "bang",
  320. },
  321. },
  322. },
  323. },
  324. }
  325. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  326. esLookupKey := types.NamespacedName{
  327. Name: ExternalSecretName,
  328. Namespace: ExternalSecretNamespace}
  329. createdES := &esv1alpha1.ExternalSecret{}
  330. Eventually(func() bool {
  331. err := k8sClient.Get(ctx, esLookupKey, createdES)
  332. if err != nil {
  333. return false
  334. }
  335. // condition must be false
  336. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  337. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  338. return false
  339. }
  340. return true
  341. }, timeout, interval).Should(BeTrue())
  342. })
  343. It("should set an error condition when store provider constructor fails", func() {
  344. By("creating an ExternalSecret")
  345. ctx := context.Background()
  346. const targetProp = "targetProperty"
  347. es := &esv1alpha1.ExternalSecret{
  348. ObjectMeta: metav1.ObjectMeta{
  349. Name: ExternalSecretName,
  350. Namespace: ExternalSecretNamespace,
  351. },
  352. Spec: esv1alpha1.ExternalSecretSpec{
  353. SecretStoreRef: esv1alpha1.SecretStoreRef{
  354. Name: ExternalSecretStore,
  355. },
  356. Target: esv1alpha1.ExternalSecretTarget{
  357. Name: ExternalSecretTargetSecretName,
  358. },
  359. Data: []esv1alpha1.ExternalSecretData{
  360. {
  361. SecretKey: targetProp,
  362. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  363. Key: "barz",
  364. Property: "bang",
  365. },
  366. },
  367. },
  368. },
  369. }
  370. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  371. string) (provider.Provider, error) {
  372. return nil, fmt.Errorf("artificial constructor error")
  373. })
  374. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  375. esLookupKey := types.NamespacedName{
  376. Name: ExternalSecretName,
  377. Namespace: ExternalSecretNamespace}
  378. createdES := &esv1alpha1.ExternalSecret{}
  379. Eventually(func() bool {
  380. err := k8sClient.Get(ctx, esLookupKey, createdES)
  381. if err != nil {
  382. return false
  383. }
  384. // condition must be false
  385. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  386. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  387. return false
  388. }
  389. return true
  390. }, timeout, interval).Should(BeTrue())
  391. })
  392. It("should not process stores with mismatching controller field", func() {
  393. By("creating an ExternalSecret")
  394. ctx := context.Background()
  395. storeName := "example-ts-foo"
  396. Expect(k8sClient.Create(context.Background(), &esv1alpha1.SecretStore{
  397. ObjectMeta: metav1.ObjectMeta{
  398. Name: storeName,
  399. Namespace: ExternalSecretNamespace,
  400. },
  401. Spec: esv1alpha1.SecretStoreSpec{
  402. Controller: "some-other-controller",
  403. Provider: &esv1alpha1.SecretStoreProvider{
  404. AWSSM: &esv1alpha1.AWSSMProvider{},
  405. },
  406. },
  407. })).To(Succeed())
  408. defer func() {
  409. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  410. ObjectMeta: metav1.ObjectMeta{
  411. Name: storeName,
  412. Namespace: ExternalSecretNamespace,
  413. },
  414. })).To(Succeed())
  415. }()
  416. es := &esv1alpha1.ExternalSecret{
  417. ObjectMeta: metav1.ObjectMeta{
  418. Name: ExternalSecretName,
  419. Namespace: ExternalSecretNamespace,
  420. },
  421. Spec: esv1alpha1.ExternalSecretSpec{
  422. SecretStoreRef: esv1alpha1.SecretStoreRef{
  423. Name: storeName,
  424. },
  425. Target: esv1alpha1.ExternalSecretTarget{
  426. Name: ExternalSecretTargetSecretName,
  427. },
  428. Data: []esv1alpha1.ExternalSecretData{
  429. {
  430. SecretKey: "doesnothing",
  431. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  432. Key: "barz",
  433. Property: "bang",
  434. },
  435. },
  436. },
  437. },
  438. }
  439. Expect(k8sClient.Create(ctx, es)).Should(Succeed())
  440. secretLookupKey := types.NamespacedName{
  441. Name: ExternalSecretName,
  442. Namespace: ExternalSecretNamespace,
  443. }
  444. // COND
  445. createdES := &esv1alpha1.ExternalSecret{}
  446. Consistently(func() bool {
  447. err := k8sClient.Get(ctx, secretLookupKey, createdES)
  448. if err != nil {
  449. return false
  450. }
  451. cond := GetExternalSecretCondition(createdES.Status, esv1alpha1.ExternalSecretReady)
  452. return cond == nil
  453. }, timeout, interval).Should(BeTrue())
  454. })
  455. })
  456. })
  457. // CreateNamespace creates a new namespace in the cluster.
  458. func CreateNamespace(baseName string, c client.Client) (string, error) {
  459. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  460. ns := &v1.Namespace{
  461. ObjectMeta: metav1.ObjectMeta{
  462. GenerateName: genName,
  463. },
  464. }
  465. var err error
  466. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  467. err = c.Create(context.Background(), ns)
  468. if err != nil {
  469. return false, nil
  470. }
  471. return true, nil
  472. })
  473. if err != nil {
  474. return "", err
  475. }
  476. return ns.Name, nil
  477. }
  478. func init() {
  479. fakeProvider = fake.New()
  480. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  481. AWSSM: &esv1alpha1.AWSSMProvider{},
  482. })
  483. }