pushsecret_controller_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 pushsecret
  13. import (
  14. "bytes"
  15. "context"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "time"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  28. ctest "github.com/external-secrets/external-secrets/pkg/controllers/commontest"
  29. "github.com/external-secrets/external-secrets/pkg/controllers/pushsecret/psmetrics"
  30. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  31. )
  32. var (
  33. fakeProvider *fake.Client
  34. timeout = time.Second * 10
  35. interval = time.Millisecond * 250
  36. )
  37. type testCase struct {
  38. store v1beta1.GenericStore
  39. pushsecret *v1alpha1.PushSecret
  40. secret *v1.Secret
  41. assert func(pushsecret *v1alpha1.PushSecret, secret *v1.Secret) bool
  42. }
  43. func init() {
  44. fakeProvider = fake.New()
  45. v1beta1.ForceRegister(fakeProvider, &v1beta1.SecretStoreProvider{
  46. Fake: &v1beta1.FakeProvider{},
  47. })
  48. psmetrics.SetUpMetrics()
  49. }
  50. func checkCondition(status v1alpha1.PushSecretStatus, cond v1alpha1.PushSecretStatusCondition) bool {
  51. for _, condition := range status.Conditions {
  52. if condition.Message == cond.Message &&
  53. condition.Reason == cond.Reason &&
  54. condition.Status == cond.Status &&
  55. condition.Type == cond.Type {
  56. return true
  57. }
  58. }
  59. return false
  60. }
  61. type testTweaks func(*testCase)
  62. var _ = Describe("ExternalSecret controller", func() {
  63. const (
  64. PushSecretName = "test-es"
  65. PushSecretStore = "test-store"
  66. SecretName = "test-secret"
  67. )
  68. var PushSecretNamespace string
  69. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  70. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  71. if t, err := strconv.Atoi(customTimeout); err == nil {
  72. timeout = time.Second * time.Duration(t)
  73. }
  74. }
  75. BeforeEach(func() {
  76. var err error
  77. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  78. Expect(err).ToNot(HaveOccurred())
  79. fakeProvider.Reset()
  80. })
  81. AfterEach(func() {
  82. k8sClient.Delete(context.Background(), &v1alpha1.PushSecret{
  83. ObjectMeta: metav1.ObjectMeta{
  84. Name: PushSecretName,
  85. Namespace: PushSecretNamespace,
  86. },
  87. })
  88. // give a time for reconciler to remove finalizers before removing SecretStores
  89. // TODO: Secret Stores should have finalizers bound to PushSecrets if DeletionPolicy == Delete
  90. time.Sleep(2 * time.Second)
  91. k8sClient.Delete(context.Background(), &v1beta1.SecretStore{
  92. ObjectMeta: metav1.ObjectMeta{
  93. Name: PushSecretStore,
  94. Namespace: PushSecretNamespace,
  95. },
  96. })
  97. k8sClient.Delete(context.Background(), &v1beta1.ClusterSecretStore{
  98. ObjectMeta: metav1.ObjectMeta{
  99. Name: PushSecretStore,
  100. },
  101. })
  102. k8sClient.Delete(context.Background(), &v1.Secret{
  103. ObjectMeta: metav1.ObjectMeta{
  104. Name: SecretName,
  105. Namespace: PushSecretNamespace,
  106. },
  107. })
  108. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  109. ObjectMeta: metav1.ObjectMeta{
  110. Name: PushSecretNamespace,
  111. },
  112. })).To(Succeed())
  113. })
  114. makeDefaultTestcase := func() *testCase {
  115. return &testCase{
  116. pushsecret: &v1alpha1.PushSecret{
  117. ObjectMeta: metav1.ObjectMeta{
  118. Name: PushSecretName,
  119. Namespace: PushSecretNamespace,
  120. },
  121. Spec: v1alpha1.PushSecretSpec{
  122. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  123. {
  124. Name: PushSecretStore,
  125. Kind: "SecretStore",
  126. },
  127. },
  128. Selector: v1alpha1.PushSecretSelector{
  129. Secret: v1alpha1.PushSecretSecret{
  130. Name: SecretName,
  131. },
  132. },
  133. Data: []v1alpha1.PushSecretData{
  134. {
  135. Match: v1alpha1.PushSecretMatch{
  136. SecretKey: "key",
  137. RemoteRef: v1alpha1.PushSecretRemoteRef{
  138. RemoteKey: "path/to/key",
  139. },
  140. },
  141. },
  142. },
  143. },
  144. },
  145. secret: &v1.Secret{
  146. ObjectMeta: metav1.ObjectMeta{
  147. Name: SecretName,
  148. Namespace: PushSecretNamespace,
  149. },
  150. Data: map[string][]byte{
  151. "key": []byte("value"),
  152. },
  153. },
  154. store: &v1beta1.SecretStore{
  155. ObjectMeta: metav1.ObjectMeta{
  156. Name: PushSecretStore,
  157. Namespace: PushSecretNamespace,
  158. },
  159. TypeMeta: metav1.TypeMeta{
  160. Kind: "SecretStore",
  161. },
  162. Spec: v1beta1.SecretStoreSpec{
  163. Provider: &v1beta1.SecretStoreProvider{
  164. Fake: &v1beta1.FakeProvider{
  165. Data: []v1beta1.FakeProviderData{},
  166. },
  167. },
  168. },
  169. },
  170. }
  171. }
  172. // if target Secret name is not specified it should use the ExternalSecret name.
  173. syncSuccessfully := func(tc *testCase) {
  174. fakeProvider.SetSecretFn = func() error {
  175. return nil
  176. }
  177. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  178. Eventually(func() bool {
  179. By("checking if Provider value got updated")
  180. secretValue := secret.Data["key"]
  181. providerValue, ok := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  182. if !ok {
  183. return false
  184. }
  185. got := providerValue.Value
  186. return bytes.Equal(got, secretValue)
  187. }, time.Second*10, time.Second).Should(BeTrue())
  188. return true
  189. }
  190. }
  191. // if target Secret name is not specified it should use the ExternalSecret name.
  192. syncAndDeleteSuccessfully := func(tc *testCase) {
  193. fakeProvider.SetSecretFn = func() error {
  194. return nil
  195. }
  196. tc.pushsecret = &v1alpha1.PushSecret{
  197. ObjectMeta: metav1.ObjectMeta{
  198. Name: PushSecretName,
  199. Namespace: PushSecretNamespace,
  200. },
  201. Spec: v1alpha1.PushSecretSpec{
  202. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  203. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  204. {
  205. Name: PushSecretStore,
  206. Kind: "SecretStore",
  207. },
  208. },
  209. Selector: v1alpha1.PushSecretSelector{
  210. Secret: v1alpha1.PushSecretSecret{
  211. Name: SecretName,
  212. },
  213. },
  214. Data: []v1alpha1.PushSecretData{
  215. {
  216. Match: v1alpha1.PushSecretMatch{
  217. SecretKey: "key",
  218. RemoteRef: v1alpha1.PushSecretRemoteRef{
  219. RemoteKey: "path/to/key",
  220. },
  221. },
  222. },
  223. },
  224. },
  225. }
  226. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  227. ps.Spec.Data[0].Match.RemoteRef.RemoteKey = "different-key"
  228. updatedPS := &v1alpha1.PushSecret{}
  229. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  230. Eventually(func() bool {
  231. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  232. By("checking if Provider value got updated")
  233. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  234. if err != nil {
  235. return false
  236. }
  237. key, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf("SecretStore/%v", PushSecretStore)]["different-key"]
  238. if !ok {
  239. return false
  240. }
  241. return key.Match.SecretKey == "key"
  242. }, time.Second*10, time.Second).Should(BeTrue())
  243. return true
  244. }
  245. }
  246. failDelete := func(tc *testCase) {
  247. fakeProvider.SetSecretFn = func() error {
  248. return nil
  249. }
  250. fakeProvider.DeleteSecretFn = func() error {
  251. return fmt.Errorf("Nope")
  252. }
  253. tc.pushsecret = &v1alpha1.PushSecret{
  254. ObjectMeta: metav1.ObjectMeta{
  255. Name: PushSecretName,
  256. Namespace: PushSecretNamespace,
  257. },
  258. Spec: v1alpha1.PushSecretSpec{
  259. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  260. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  261. {
  262. Name: PushSecretStore,
  263. Kind: "SecretStore",
  264. },
  265. },
  266. Selector: v1alpha1.PushSecretSelector{
  267. Secret: v1alpha1.PushSecretSecret{
  268. Name: SecretName,
  269. },
  270. },
  271. Data: []v1alpha1.PushSecretData{
  272. {
  273. Match: v1alpha1.PushSecretMatch{
  274. SecretKey: "key",
  275. RemoteRef: v1alpha1.PushSecretRemoteRef{
  276. RemoteKey: "path/to/key",
  277. },
  278. },
  279. },
  280. },
  281. },
  282. }
  283. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  284. ps.Spec.Data[0].Match.RemoteRef.RemoteKey = "different-key"
  285. updatedPS := &v1alpha1.PushSecret{}
  286. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  287. Eventually(func() bool {
  288. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  289. By("checking if synced secrets correspond to both keys")
  290. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  291. if err != nil {
  292. return false
  293. }
  294. _, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf("SecretStore/%v", PushSecretStore)]["different-key"]
  295. if !ok {
  296. return false
  297. }
  298. _, ok = updatedPS.Status.SyncedPushSecrets[fmt.Sprintf("SecretStore/%v", PushSecretStore)]["path/to/key"]
  299. return ok
  300. }, time.Second*10, time.Second).Should(BeTrue())
  301. return true
  302. }
  303. }
  304. failDeleteStore := func(tc *testCase) {
  305. fakeProvider.SetSecretFn = func() error {
  306. return nil
  307. }
  308. fakeProvider.DeleteSecretFn = func() error {
  309. return fmt.Errorf("boom")
  310. }
  311. tc.pushsecret.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyDelete
  312. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  313. secondStore := &v1beta1.SecretStore{
  314. ObjectMeta: metav1.ObjectMeta{
  315. Name: "new-store",
  316. Namespace: PushSecretNamespace,
  317. },
  318. TypeMeta: metav1.TypeMeta{
  319. Kind: "SecretStore",
  320. },
  321. Spec: v1beta1.SecretStoreSpec{
  322. Provider: &v1beta1.SecretStoreProvider{
  323. Fake: &v1beta1.FakeProvider{
  324. Data: []v1beta1.FakeProviderData{},
  325. },
  326. },
  327. },
  328. }
  329. Expect(k8sClient.Create(context.Background(), secondStore, &client.CreateOptions{})).Should(Succeed())
  330. ps.Spec.SecretStoreRefs[0].Name = "new-store"
  331. updatedPS := &v1alpha1.PushSecret{}
  332. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  333. Eventually(func() bool {
  334. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  335. By("checking if Provider value got updated")
  336. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  337. if err != nil {
  338. return false
  339. }
  340. syncedLen := len(updatedPS.Status.SyncedPushSecrets)
  341. return syncedLen == 2
  342. }, time.Second*10, time.Second).Should(BeTrue())
  343. return true
  344. }
  345. }
  346. deleteWholeStore := func(tc *testCase) {
  347. fakeProvider.SetSecretFn = func() error {
  348. return nil
  349. }
  350. fakeProvider.DeleteSecretFn = func() error {
  351. return nil
  352. }
  353. tc.pushsecret.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyDelete
  354. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  355. secondStore := &v1beta1.SecretStore{
  356. ObjectMeta: metav1.ObjectMeta{
  357. Name: "new-store",
  358. Namespace: PushSecretNamespace,
  359. },
  360. TypeMeta: metav1.TypeMeta{
  361. Kind: "SecretStore",
  362. },
  363. Spec: v1beta1.SecretStoreSpec{
  364. Provider: &v1beta1.SecretStoreProvider{
  365. Fake: &v1beta1.FakeProvider{
  366. Data: []v1beta1.FakeProviderData{},
  367. },
  368. },
  369. },
  370. }
  371. Expect(k8sClient.Create(context.Background(), secondStore, &client.CreateOptions{})).Should(Succeed())
  372. ps.Spec.SecretStoreRefs[0].Name = "new-store"
  373. updatedPS := &v1alpha1.PushSecret{}
  374. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  375. Eventually(func() bool {
  376. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  377. By("checking if Provider value got updated")
  378. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  379. if err != nil {
  380. return false
  381. }
  382. key, ok := updatedPS.Status.SyncedPushSecrets["SecretStore/new-store"]["path/to/key"]
  383. if !ok {
  384. return false
  385. }
  386. syncedLen := len(updatedPS.Status.SyncedPushSecrets)
  387. if syncedLen != 1 {
  388. return false
  389. }
  390. return key.Match.SecretKey == "key"
  391. }, time.Second*10, time.Second).Should(BeTrue())
  392. return true
  393. }
  394. }
  395. // if target Secret name is not specified it should use the ExternalSecret name.
  396. syncMatchingLabels := func(tc *testCase) {
  397. fakeProvider.SetSecretFn = func() error {
  398. return nil
  399. }
  400. fakeProvider.DeleteSecretFn = func() error {
  401. return nil
  402. }
  403. tc.pushsecret = &v1alpha1.PushSecret{
  404. ObjectMeta: metav1.ObjectMeta{
  405. Name: PushSecretName,
  406. Namespace: PushSecretNamespace,
  407. },
  408. Spec: v1alpha1.PushSecretSpec{
  409. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  410. {
  411. LabelSelector: &metav1.LabelSelector{
  412. MatchLabels: map[string]string{
  413. "foo": "bar",
  414. },
  415. },
  416. Kind: "SecretStore",
  417. },
  418. },
  419. Selector: v1alpha1.PushSecretSelector{
  420. Secret: v1alpha1.PushSecretSecret{
  421. Name: SecretName,
  422. },
  423. },
  424. Data: []v1alpha1.PushSecretData{
  425. {
  426. Match: v1alpha1.PushSecretMatch{
  427. SecretKey: "key",
  428. RemoteRef: v1alpha1.PushSecretRemoteRef{
  429. RemoteKey: "path/to/key",
  430. },
  431. },
  432. },
  433. },
  434. },
  435. }
  436. tc.store = &v1beta1.SecretStore{
  437. TypeMeta: metav1.TypeMeta{
  438. Kind: "SecretStore",
  439. },
  440. ObjectMeta: metav1.ObjectMeta{
  441. Name: PushSecretStore,
  442. Namespace: PushSecretNamespace,
  443. Labels: map[string]string{
  444. "foo": "bar",
  445. },
  446. },
  447. Spec: v1beta1.SecretStoreSpec{
  448. Provider: &v1beta1.SecretStoreProvider{
  449. Fake: &v1beta1.FakeProvider{
  450. Data: []v1beta1.FakeProviderData{},
  451. },
  452. },
  453. },
  454. }
  455. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  456. secretValue := secret.Data["key"]
  457. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  458. expected := v1alpha1.PushSecretStatusCondition{
  459. Type: v1alpha1.PushSecretReady,
  460. Status: v1.ConditionTrue,
  461. Reason: v1alpha1.ReasonSynced,
  462. Message: "PushSecret synced successfully",
  463. }
  464. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  465. }
  466. }
  467. syncWithClusterStore := func(tc *testCase) {
  468. fakeProvider.SetSecretFn = func() error {
  469. return nil
  470. }
  471. tc.store = &v1beta1.ClusterSecretStore{
  472. TypeMeta: metav1.TypeMeta{
  473. Kind: "ClusterSecretStore",
  474. },
  475. ObjectMeta: metav1.ObjectMeta{
  476. Name: PushSecretStore,
  477. },
  478. Spec: v1beta1.SecretStoreSpec{
  479. Provider: &v1beta1.SecretStoreProvider{
  480. Fake: &v1beta1.FakeProvider{
  481. Data: []v1beta1.FakeProviderData{},
  482. },
  483. },
  484. },
  485. }
  486. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  487. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  488. secretValue := secret.Data["key"]
  489. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  490. expected := v1alpha1.PushSecretStatusCondition{
  491. Type: v1alpha1.PushSecretReady,
  492. Status: v1.ConditionTrue,
  493. Reason: v1alpha1.ReasonSynced,
  494. Message: "PushSecret synced successfully",
  495. }
  496. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  497. }
  498. }
  499. // if target Secret name is not specified it should use the ExternalSecret name.
  500. syncWithClusterStoreMatchingLabels := func(tc *testCase) {
  501. fakeProvider.SetSecretFn = func() error {
  502. return nil
  503. }
  504. tc.pushsecret = &v1alpha1.PushSecret{
  505. ObjectMeta: metav1.ObjectMeta{
  506. Name: PushSecretName,
  507. Namespace: PushSecretNamespace,
  508. },
  509. Spec: v1alpha1.PushSecretSpec{
  510. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  511. {
  512. LabelSelector: &metav1.LabelSelector{
  513. MatchLabels: map[string]string{
  514. "foo": "bar",
  515. },
  516. },
  517. Kind: "ClusterSecretStore",
  518. },
  519. },
  520. Selector: v1alpha1.PushSecretSelector{
  521. Secret: v1alpha1.PushSecretSecret{
  522. Name: SecretName,
  523. },
  524. },
  525. Data: []v1alpha1.PushSecretData{
  526. {
  527. Match: v1alpha1.PushSecretMatch{
  528. SecretKey: "key",
  529. RemoteRef: v1alpha1.PushSecretRemoteRef{
  530. RemoteKey: "path/to/key",
  531. },
  532. },
  533. },
  534. },
  535. },
  536. }
  537. tc.store = &v1beta1.ClusterSecretStore{
  538. ObjectMeta: metav1.ObjectMeta{
  539. Name: PushSecretStore,
  540. Labels: map[string]string{
  541. "foo": "bar",
  542. },
  543. },
  544. Spec: v1beta1.SecretStoreSpec{
  545. Provider: &v1beta1.SecretStoreProvider{
  546. Fake: &v1beta1.FakeProvider{
  547. Data: []v1beta1.FakeProviderData{},
  548. },
  549. },
  550. },
  551. }
  552. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  553. secretValue := secret.Data["key"]
  554. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  555. expected := v1alpha1.PushSecretStatusCondition{
  556. Type: v1alpha1.PushSecretReady,
  557. Status: v1.ConditionTrue,
  558. Reason: v1alpha1.ReasonSynced,
  559. Message: "PushSecret synced successfully",
  560. }
  561. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  562. }
  563. }
  564. // if target Secret name is not specified it should use the ExternalSecret name.
  565. failNoSecret := func(tc *testCase) {
  566. fakeProvider.SetSecretFn = func() error {
  567. return nil
  568. }
  569. tc.secret = nil
  570. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  571. expected := v1alpha1.PushSecretStatusCondition{
  572. Type: v1alpha1.PushSecretReady,
  573. Status: v1.ConditionFalse,
  574. Reason: v1alpha1.ReasonErrored,
  575. Message: "could not get source secret",
  576. }
  577. return checkCondition(ps.Status, expected)
  578. }
  579. }
  580. // if target Secret name is not specified it should use the ExternalSecret name.
  581. failNoSecretKey := func(tc *testCase) {
  582. fakeProvider.SetSecretFn = func() error {
  583. return nil
  584. }
  585. tc.pushsecret.Spec.Data[0].Match.SecretKey = "unexisting"
  586. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  587. expected := v1alpha1.PushSecretStatusCondition{
  588. Type: v1alpha1.PushSecretReady,
  589. Status: v1.ConditionFalse,
  590. Reason: v1alpha1.ReasonErrored,
  591. Message: "set secret failed: secret key unexisting does not exist",
  592. }
  593. return checkCondition(ps.Status, expected)
  594. }
  595. }
  596. // if target Secret name is not specified it should use the ExternalSecret name.
  597. failNoSecretStore := func(tc *testCase) {
  598. fakeProvider.SetSecretFn = func() error {
  599. return nil
  600. }
  601. tc.store = nil
  602. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  603. expected := v1alpha1.PushSecretStatusCondition{
  604. Type: v1alpha1.PushSecretReady,
  605. Status: v1.ConditionFalse,
  606. Reason: v1alpha1.ReasonErrored,
  607. Message: "could not get SecretStore \"test-store\", secretstores.external-secrets.io \"test-store\" not found",
  608. }
  609. return checkCondition(ps.Status, expected)
  610. }
  611. }
  612. // if target Secret name is not specified it should use the ExternalSecret name.
  613. failNoClusterStore := func(tc *testCase) {
  614. fakeProvider.SetSecretFn = func() error {
  615. return nil
  616. }
  617. tc.store = nil
  618. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  619. tc.pushsecret.Spec.SecretStoreRefs[0].Name = "unexisting"
  620. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  621. expected := v1alpha1.PushSecretStatusCondition{
  622. Type: v1alpha1.PushSecretReady,
  623. Status: v1.ConditionFalse,
  624. Reason: v1alpha1.ReasonErrored,
  625. Message: "could not get ClusterSecretStore \"unexisting\", clustersecretstores.external-secrets.io \"unexisting\" not found",
  626. }
  627. return checkCondition(ps.Status, expected)
  628. }
  629. } // if target Secret name is not specified it should use the ExternalSecret name.
  630. setSecretFail := func(tc *testCase) {
  631. fakeProvider.SetSecretFn = func() error {
  632. return fmt.Errorf("boom")
  633. }
  634. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  635. expected := v1alpha1.PushSecretStatusCondition{
  636. Type: v1alpha1.PushSecretReady,
  637. Status: v1.ConditionFalse,
  638. Reason: v1alpha1.ReasonErrored,
  639. Message: "set secret failed: could not write remote ref key to target secretstore test-store: boom",
  640. }
  641. return checkCondition(ps.Status, expected)
  642. }
  643. }
  644. // if target Secret name is not specified it should use the ExternalSecret name.
  645. newClientFail := func(tc *testCase) {
  646. fakeProvider.NewFn = func(context.Context, v1beta1.GenericStore, client.Client, string) (v1beta1.SecretsClient, error) {
  647. return nil, fmt.Errorf("boom")
  648. }
  649. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  650. expected := v1alpha1.PushSecretStatusCondition{
  651. Type: v1alpha1.PushSecretReady,
  652. Status: v1.ConditionFalse,
  653. Reason: v1alpha1.ReasonErrored,
  654. Message: "set secret failed: could not get secrets client for store test-store: boom",
  655. }
  656. return checkCondition(ps.Status, expected)
  657. }
  658. }
  659. DescribeTable("When reconciling a PushSecret",
  660. func(tweaks ...testTweaks) {
  661. tc := makeDefaultTestcase()
  662. for _, tweak := range tweaks {
  663. tweak(tc)
  664. }
  665. ctx := context.Background()
  666. By("creating a secret store, secret and pushsecret")
  667. if tc.store != nil {
  668. Expect(k8sClient.Create(ctx, tc.store)).To(Succeed())
  669. }
  670. if tc.secret != nil {
  671. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  672. }
  673. if tc.pushsecret != nil {
  674. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  675. }
  676. time.Sleep(2 * time.Second)
  677. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  678. createdPS := &v1alpha1.PushSecret{}
  679. By("checking the pushSecret condition")
  680. Eventually(func() bool {
  681. err := k8sClient.Get(ctx, psKey, createdPS)
  682. if err != nil {
  683. return false
  684. }
  685. return tc.assert(createdPS, tc.secret)
  686. }, timeout, interval).Should(BeTrue())
  687. // this must be optional so we can test faulty es configuration
  688. },
  689. Entry("should sync", syncSuccessfully),
  690. Entry("should delete if DeletionPolicy=Delete", syncAndDeleteSuccessfully),
  691. Entry("should track deletion tasks if Delete fails", failDelete),
  692. Entry("should track deleted stores if Delete fails", failDeleteStore),
  693. Entry("should delete all secrets if SecretStore changes", deleteWholeStore),
  694. Entry("should sync to stores matching labels", syncMatchingLabels),
  695. Entry("should sync with ClusterStore", syncWithClusterStore),
  696. Entry("should sync with ClusterStore matching labels", syncWithClusterStoreMatchingLabels),
  697. Entry("should fail if Secret is not created", failNoSecret),
  698. Entry("should fail if Secret Key does not exist", failNoSecretKey),
  699. Entry("should fail if SetSecret fails", setSecretFail),
  700. Entry("should fail if no valid SecretStore", failNoSecretStore),
  701. Entry("should fail if no valid ClusterSecretStore", failNoClusterStore),
  702. Entry("should fail if NewClient fails", newClientFail),
  703. )
  704. })