pushsecret_controller_test.go 23 KB

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