pushsecret_controller_test.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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. "errors"
  17. "fmt"
  18. "os"
  19. "strconv"
  20. "time"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  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. . "github.com/onsi/ginkgo/v2"
  32. . "github.com/onsi/gomega"
  33. )
  34. var (
  35. fakeProvider *fake.Client
  36. timeout = time.Second * 10
  37. interval = time.Millisecond * 250
  38. )
  39. type testCase struct {
  40. store esv1.GenericStore
  41. managedStore1 esv1.GenericStore
  42. managedStore2 esv1.GenericStore
  43. unmanagedStore1 esv1.GenericStore
  44. unmanagedStore2 esv1.GenericStore
  45. pushsecret *v1alpha1.PushSecret
  46. secret *v1.Secret
  47. assert func(pushsecret *v1alpha1.PushSecret, secret *v1.Secret) bool
  48. }
  49. func init() {
  50. fakeProvider = fake.New()
  51. esv1.ForceRegister(fakeProvider, &esv1.SecretStoreProvider{
  52. Fake: &esv1.FakeProvider{},
  53. }, esv1.MaintenanceStatusMaintained)
  54. psmetrics.SetUpMetrics()
  55. }
  56. func checkCondition(status v1alpha1.PushSecretStatus, cond v1alpha1.PushSecretStatusCondition) bool {
  57. for _, condition := range status.Conditions {
  58. if condition.Message == cond.Message &&
  59. condition.Reason == cond.Reason &&
  60. condition.Status == cond.Status &&
  61. condition.Type == cond.Type {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. type testTweaks func(*testCase)
  68. var _ = Describe("PushSecret controller", func() {
  69. const (
  70. PushSecretName = "test-ps"
  71. PushSecretStore = "test-store"
  72. SecretName = "test-secret"
  73. )
  74. var PushSecretNamespace, OtherNamespace string
  75. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  76. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  77. if t, err := strconv.Atoi(customTimeout); err == nil {
  78. timeout = time.Second * time.Duration(t)
  79. }
  80. }
  81. BeforeEach(func() {
  82. var err error
  83. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  84. Expect(err).ToNot(HaveOccurred())
  85. OtherNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  86. Expect(err).ToNot(HaveOccurred())
  87. fakeProvider.Reset()
  88. Expect(k8sClient.Create(context.Background(), &genv1alpha1.Fake{
  89. TypeMeta: metav1.TypeMeta{
  90. Kind: "Fake",
  91. APIVersion: "generators.external-secrets.io/v1alpha1",
  92. },
  93. ObjectMeta: metav1.ObjectMeta{
  94. Name: "test",
  95. Namespace: PushSecretNamespace,
  96. },
  97. Spec: genv1alpha1.FakeSpec{
  98. Data: map[string]string{
  99. "key": "foo-bar-from-generator",
  100. },
  101. }})).ToNot(HaveOccurred())
  102. })
  103. AfterEach(func() {
  104. k8sClient.Delete(context.Background(), &v1alpha1.PushSecret{
  105. ObjectMeta: metav1.ObjectMeta{
  106. Name: PushSecretName,
  107. Namespace: PushSecretNamespace,
  108. },
  109. })
  110. // give a time for reconciler to remove finalizers before removing SecretStores
  111. time.Sleep(2 * time.Second)
  112. k8sClient.Delete(context.Background(), &esv1.SecretStore{
  113. ObjectMeta: metav1.ObjectMeta{
  114. Name: PushSecretStore,
  115. Namespace: PushSecretNamespace,
  116. },
  117. })
  118. k8sClient.Delete(context.Background(), &esv1.ClusterSecretStore{
  119. ObjectMeta: metav1.ObjectMeta{
  120. Name: PushSecretStore,
  121. },
  122. })
  123. k8sClient.Delete(context.Background(), &v1.Secret{
  124. ObjectMeta: metav1.ObjectMeta{
  125. Name: SecretName,
  126. Namespace: PushSecretNamespace,
  127. },
  128. })
  129. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  130. ObjectMeta: metav1.ObjectMeta{
  131. Name: PushSecretNamespace,
  132. },
  133. })).To(Succeed())
  134. })
  135. const (
  136. defaultKey = "key"
  137. defaultVal = "value"
  138. defaultPath = "path/to/key"
  139. otherKey = "other-key"
  140. otherVal = "other-value"
  141. otherPath = "path/to/other-key"
  142. newKey = "new-key"
  143. newVal = "new-value"
  144. storePrefixTemplate = "SecretStore/%v"
  145. )
  146. makeDefaultTestcase := func() *testCase {
  147. return &testCase{
  148. pushsecret: &v1alpha1.PushSecret{
  149. ObjectMeta: metav1.ObjectMeta{
  150. Name: PushSecretName,
  151. Namespace: PushSecretNamespace,
  152. },
  153. Spec: v1alpha1.PushSecretSpec{
  154. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  155. {
  156. Name: PushSecretStore,
  157. Kind: "SecretStore",
  158. },
  159. },
  160. Selector: v1alpha1.PushSecretSelector{
  161. Secret: &v1alpha1.PushSecretSecret{
  162. Name: SecretName,
  163. },
  164. },
  165. Data: []v1alpha1.PushSecretData{
  166. {
  167. Match: v1alpha1.PushSecretMatch{
  168. SecretKey: defaultKey,
  169. RemoteRef: v1alpha1.PushSecretRemoteRef{
  170. RemoteKey: defaultPath,
  171. },
  172. },
  173. },
  174. },
  175. },
  176. },
  177. secret: &v1.Secret{
  178. ObjectMeta: metav1.ObjectMeta{
  179. Name: SecretName,
  180. Namespace: PushSecretNamespace,
  181. },
  182. Data: map[string][]byte{
  183. defaultKey: []byte(defaultVal),
  184. },
  185. },
  186. store: &esv1.SecretStore{
  187. ObjectMeta: metav1.ObjectMeta{
  188. Name: PushSecretStore,
  189. Namespace: PushSecretNamespace,
  190. },
  191. TypeMeta: metav1.TypeMeta{
  192. Kind: "SecretStore",
  193. },
  194. Spec: esv1.SecretStoreSpec{
  195. Provider: &esv1.SecretStoreProvider{
  196. Fake: &esv1.FakeProvider{
  197. Data: []esv1.FakeProviderData{},
  198. },
  199. },
  200. },
  201. },
  202. }
  203. }
  204. // if target Secret name is not specified it should use the ExternalSecret name.
  205. syncSuccessfully := func(tc *testCase) {
  206. fakeProvider.SetSecretFn = func() error {
  207. return nil
  208. }
  209. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  210. Eventually(func() bool {
  211. By("checking if Provider value got updated")
  212. secretValue := secret.Data[defaultKey]
  213. setSecretArgs := fakeProvider.GetPushSecretData()
  214. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  215. if !ok {
  216. return false
  217. }
  218. got := providerValue.Value
  219. return bytes.Equal(got, secretValue)
  220. }, time.Second*10, time.Second).Should(BeTrue())
  221. return true
  222. }
  223. }
  224. updateIfNotExists := func(tc *testCase) {
  225. fakeProvider.SetSecretFn = func() error {
  226. return nil
  227. }
  228. fakeProvider.SecretExistsFn = func(ctx context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  229. setSecretArgs := fakeProvider.GetPushSecretData()
  230. _, ok := setSecretArgs[ref.GetRemoteKey()]
  231. return ok, nil
  232. }
  233. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  234. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  235. Consistently(func() bool {
  236. By("updating the secret value")
  237. tc.secret.Data[defaultKey] = []byte(newVal)
  238. Expect(k8sClient.Update(context.Background(), secret, &client.UpdateOptions{})).Should(Succeed())
  239. By("checking if Provider value does not get updated")
  240. setSecretArgs := fakeProvider.GetPushSecretData()
  241. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  242. if !ok {
  243. return false
  244. }
  245. got := providerValue.Value
  246. return bytes.Equal(got, []byte(defaultVal))
  247. }, time.Second*10, time.Second).Should(BeTrue())
  248. return true
  249. }
  250. }
  251. updateIfNotExistsPartialSecrets := func(tc *testCase) {
  252. fakeProvider.SetSecretFn = func() error {
  253. return nil
  254. }
  255. fakeProvider.SecretExistsFn = func(ctx context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  256. setSecretArgs := fakeProvider.GetPushSecretData()
  257. _, ok := setSecretArgs[ref.GetRemoteKey()]
  258. return ok, nil
  259. }
  260. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  261. tc.pushsecret.Spec.Data = append(tc.pushsecret.Spec.Data, v1alpha1.PushSecretData{
  262. Match: v1alpha1.PushSecretMatch{
  263. SecretKey: otherKey,
  264. RemoteRef: v1alpha1.PushSecretRemoteRef{
  265. RemoteKey: otherPath,
  266. },
  267. },
  268. })
  269. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  270. Eventually(func() bool {
  271. tc.secret.Data[defaultKey] = []byte(newVal) // change initial value in secret
  272. tc.secret.Data[otherKey] = []byte(otherVal)
  273. By("checking if only not existing Provider value got updated")
  274. Expect(k8sClient.Update(context.Background(), secret, &client.UpdateOptions{})).Should(Succeed())
  275. setSecretArgs := fakeProvider.GetPushSecretData()
  276. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  277. if !ok {
  278. return false
  279. }
  280. got := providerValue.Value
  281. otherProviderValue, ok := setSecretArgs[ps.Spec.Data[1].Match.RemoteRef.RemoteKey]
  282. if !ok {
  283. return false
  284. }
  285. gotOther := otherProviderValue.Value
  286. return bytes.Equal(gotOther, tc.secret.Data[otherKey]) && bytes.Equal(got, []byte(defaultVal))
  287. }, time.Second*10, time.Second).Should(BeTrue())
  288. return true
  289. }
  290. }
  291. updateIfNotExistsSyncStatus := func(tc *testCase) {
  292. fakeProvider.SetSecretFn = func() error {
  293. return nil
  294. }
  295. fakeProvider.SecretExistsFn = func(ctx context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  296. setSecretArgs := fakeProvider.GetPushSecretData()
  297. _, ok := setSecretArgs[ref.GetRemoteKey()]
  298. return ok, nil
  299. }
  300. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  301. tc.pushsecret.Spec.Data = append(tc.pushsecret.Spec.Data, v1alpha1.PushSecretData{
  302. Match: v1alpha1.PushSecretMatch{
  303. SecretKey: otherKey,
  304. RemoteRef: v1alpha1.PushSecretRemoteRef{
  305. RemoteKey: otherPath,
  306. },
  307. },
  308. })
  309. tc.secret.Data[defaultKey] = []byte(newVal)
  310. tc.secret.Data[otherKey] = []byte(otherVal)
  311. updatedPS := &v1alpha1.PushSecret{}
  312. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  313. Eventually(func() bool {
  314. By("checking if PushSecret status gets updated correctly with UpdatePolicy=IfNotExists")
  315. Expect(k8sClient.Update(context.Background(), secret, &client.UpdateOptions{})).Should(Succeed())
  316. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  317. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  318. if err != nil {
  319. return false
  320. }
  321. _, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath]
  322. if !ok {
  323. return false
  324. }
  325. _, ok = updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][otherPath]
  326. if !ok {
  327. return false
  328. }
  329. expected := v1alpha1.PushSecretStatusCondition{
  330. Type: v1alpha1.PushSecretReady,
  331. Status: v1.ConditionTrue,
  332. Reason: v1alpha1.ReasonSynced,
  333. Message: "PushSecret synced successfully. Existing secrets in providers unchanged.",
  334. }
  335. return checkCondition(ps.Status, expected)
  336. }, time.Second*10, time.Second).Should(BeTrue())
  337. return true
  338. }
  339. }
  340. updateIfNotExistsSyncFailed := func(tc *testCase) {
  341. fakeProvider.SetSecretFn = func() error {
  342. return nil
  343. }
  344. fakeProvider.SecretExistsFn = func(ctx context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  345. return false, errors.New("don't know")
  346. }
  347. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  348. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  349. Eventually(func() bool {
  350. By("checking if sync failed if secret existence cannot be verified in Provider")
  351. expected := v1alpha1.PushSecretStatusCondition{
  352. Type: v1alpha1.PushSecretReady,
  353. Status: v1.ConditionFalse,
  354. Reason: v1alpha1.ReasonErrored,
  355. Message: "set secret failed: could not verify if secret exists in store: don't know",
  356. }
  357. return checkCondition(ps.Status, expected)
  358. }, time.Second*10, time.Second).Should(BeTrue())
  359. return true
  360. }
  361. }
  362. syncSuccessfullyReusingKeys := func(tc *testCase) {
  363. fakeProvider.SetSecretFn = func() error {
  364. return nil
  365. }
  366. tc.pushsecret = &v1alpha1.PushSecret{
  367. ObjectMeta: metav1.ObjectMeta{
  368. Name: PushSecretName,
  369. Namespace: PushSecretNamespace,
  370. },
  371. Spec: v1alpha1.PushSecretSpec{
  372. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  373. {
  374. Name: PushSecretStore,
  375. Kind: "SecretStore",
  376. },
  377. },
  378. Selector: v1alpha1.PushSecretSelector{
  379. Secret: &v1alpha1.PushSecretSecret{
  380. Name: SecretName,
  381. },
  382. },
  383. Data: []v1alpha1.PushSecretData{
  384. {
  385. Match: v1alpha1.PushSecretMatch{
  386. SecretKey: "otherKey",
  387. RemoteRef: v1alpha1.PushSecretRemoteRef{
  388. RemoteKey: defaultPath,
  389. },
  390. },
  391. },
  392. },
  393. Template: &esv1.ExternalSecretTemplate{
  394. Metadata: esv1.ExternalSecretTemplateMetadata{
  395. Labels: map[string]string{
  396. "foos": "ball",
  397. },
  398. Annotations: map[string]string{
  399. "hihi": "ga",
  400. },
  401. },
  402. Type: v1.SecretTypeOpaque,
  403. EngineVersion: esv1.TemplateEngineV2,
  404. Data: map[string]string{
  405. defaultKey: "{{ .key | toString | upper }} was templated",
  406. "otherKey": "{{ .key | toString | upper }} was also templated",
  407. },
  408. },
  409. },
  410. }
  411. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  412. Eventually(func() bool {
  413. By("checking if Provider value got updated")
  414. setSecretArgs := fakeProvider.GetPushSecretData()
  415. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  416. if !ok {
  417. return false
  418. }
  419. got := providerValue.Value
  420. return bytes.Equal(got, []byte("VALUE was also templated"))
  421. }, time.Second*10, time.Second).Should(BeTrue())
  422. return true
  423. }
  424. }
  425. // if target Secret name is not specified it should use the ExternalSecret name.
  426. syncSuccessfullyWithTemplate := func(tc *testCase) {
  427. fakeProvider.SetSecretFn = func() error {
  428. return nil
  429. }
  430. tc.pushsecret = &v1alpha1.PushSecret{
  431. ObjectMeta: metav1.ObjectMeta{
  432. Name: PushSecretName,
  433. Namespace: PushSecretNamespace,
  434. },
  435. Spec: v1alpha1.PushSecretSpec{
  436. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  437. {
  438. Name: PushSecretStore,
  439. Kind: "SecretStore",
  440. },
  441. },
  442. Selector: v1alpha1.PushSecretSelector{
  443. Secret: &v1alpha1.PushSecretSecret{
  444. Name: SecretName,
  445. },
  446. },
  447. Data: []v1alpha1.PushSecretData{
  448. {
  449. Match: v1alpha1.PushSecretMatch{
  450. SecretKey: defaultKey,
  451. RemoteRef: v1alpha1.PushSecretRemoteRef{
  452. RemoteKey: defaultPath,
  453. },
  454. },
  455. },
  456. },
  457. Template: &esv1.ExternalSecretTemplate{
  458. Metadata: esv1.ExternalSecretTemplateMetadata{
  459. Labels: map[string]string{
  460. "foos": "ball",
  461. },
  462. Annotations: map[string]string{
  463. "hihi": "ga",
  464. },
  465. },
  466. Type: v1.SecretTypeOpaque,
  467. EngineVersion: esv1.TemplateEngineV2,
  468. Data: map[string]string{
  469. defaultKey: "{{ .key | toString | upper }} was templated",
  470. },
  471. },
  472. },
  473. }
  474. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  475. Eventually(func() bool {
  476. By("checking if Provider value got updated")
  477. setSecretArgs := fakeProvider.GetPushSecretData()
  478. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  479. if !ok {
  480. return false
  481. }
  482. got := providerValue.Value
  483. return bytes.Equal(got, []byte("VALUE was templated"))
  484. }, time.Second*10, time.Second).Should(BeTrue())
  485. return true
  486. }
  487. }
  488. // if target Secret name is not specified it should use the ExternalSecret name.
  489. syncAndDeleteSuccessfully := func(tc *testCase) {
  490. fakeProvider.SetSecretFn = func() error {
  491. return nil
  492. }
  493. tc.pushsecret = &v1alpha1.PushSecret{
  494. ObjectMeta: metav1.ObjectMeta{
  495. Name: PushSecretName,
  496. Namespace: PushSecretNamespace,
  497. },
  498. Spec: v1alpha1.PushSecretSpec{
  499. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  500. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  501. {
  502. Name: PushSecretStore,
  503. Kind: "SecretStore",
  504. },
  505. },
  506. Selector: v1alpha1.PushSecretSelector{
  507. Secret: &v1alpha1.PushSecretSecret{
  508. Name: SecretName,
  509. },
  510. },
  511. Data: []v1alpha1.PushSecretData{
  512. {
  513. Match: v1alpha1.PushSecretMatch{
  514. SecretKey: defaultKey,
  515. RemoteRef: v1alpha1.PushSecretRemoteRef{
  516. RemoteKey: defaultPath,
  517. },
  518. },
  519. },
  520. },
  521. },
  522. }
  523. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  524. ps.Spec.Data[0].Match.RemoteRef.RemoteKey = newKey
  525. updatedPS := &v1alpha1.PushSecret{}
  526. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  527. Eventually(func() bool {
  528. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  529. By("checking if Provider value got updated")
  530. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  531. if err != nil {
  532. return false
  533. }
  534. key, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][newKey]
  535. if !ok {
  536. return false
  537. }
  538. return key.Match.SecretKey == defaultKey
  539. }, time.Second*10, time.Second).Should(BeTrue())
  540. return true
  541. }
  542. }
  543. // if PushSecret's DeletionPolicy is cleared, it should delete successfully
  544. syncChangePolicyAndDeleteSuccessfully := func(tc *testCase) {
  545. fakeProvider.SetSecretFn = func() error {
  546. return nil
  547. }
  548. tc.pushsecret = &v1alpha1.PushSecret{
  549. ObjectMeta: metav1.ObjectMeta{
  550. Name: PushSecretName,
  551. Namespace: PushSecretNamespace,
  552. },
  553. Spec: v1alpha1.PushSecretSpec{
  554. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  555. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  556. {
  557. Name: PushSecretStore,
  558. Kind: "SecretStore",
  559. },
  560. },
  561. Selector: v1alpha1.PushSecretSelector{
  562. Secret: &v1alpha1.PushSecretSecret{
  563. Name: SecretName,
  564. },
  565. },
  566. Data: []v1alpha1.PushSecretData{
  567. {
  568. Match: v1alpha1.PushSecretMatch{
  569. SecretKey: defaultKey,
  570. RemoteRef: v1alpha1.PushSecretRemoteRef{
  571. RemoteKey: defaultPath,
  572. },
  573. },
  574. },
  575. },
  576. },
  577. }
  578. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  579. ps.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyNone
  580. updatedPS := &v1alpha1.PushSecret{}
  581. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  582. Expect(k8sClient.Delete(context.Background(), ps, &client.DeleteOptions{})).Should(Succeed())
  583. Eventually(func() bool {
  584. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  585. By("checking if Get PushSecret returns not found")
  586. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  587. if err != nil && client.IgnoreNotFound(err) == nil {
  588. return true
  589. }
  590. return false
  591. }, time.Second*10, time.Second).Should(BeTrue())
  592. return true
  593. }
  594. }
  595. failDelete := func(tc *testCase) {
  596. fakeProvider.SetSecretFn = func() error {
  597. return nil
  598. }
  599. fakeProvider.DeleteSecretFn = func() error {
  600. return errors.New("Nope")
  601. }
  602. tc.pushsecret = &v1alpha1.PushSecret{
  603. ObjectMeta: metav1.ObjectMeta{
  604. Name: PushSecretName,
  605. Namespace: PushSecretNamespace,
  606. },
  607. Spec: v1alpha1.PushSecretSpec{
  608. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  609. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  610. {
  611. Name: PushSecretStore,
  612. Kind: "SecretStore",
  613. },
  614. },
  615. Selector: v1alpha1.PushSecretSelector{
  616. Secret: &v1alpha1.PushSecretSecret{
  617. Name: SecretName,
  618. },
  619. },
  620. Data: []v1alpha1.PushSecretData{
  621. {
  622. Match: v1alpha1.PushSecretMatch{
  623. SecretKey: defaultKey,
  624. RemoteRef: v1alpha1.PushSecretRemoteRef{
  625. RemoteKey: defaultPath,
  626. },
  627. },
  628. },
  629. },
  630. },
  631. }
  632. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  633. ps.Spec.Data[0].Match.RemoteRef.RemoteKey = newKey
  634. updatedPS := &v1alpha1.PushSecret{}
  635. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  636. Eventually(func() bool {
  637. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  638. By("checking if synced secrets correspond to both keys")
  639. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  640. if err != nil {
  641. return false
  642. }
  643. _, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][newKey]
  644. if !ok {
  645. return false
  646. }
  647. _, ok = updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath]
  648. return ok
  649. }, time.Second*10, time.Second).Should(BeTrue())
  650. return true
  651. }
  652. }
  653. failDeleteStore := func(tc *testCase) {
  654. fakeProvider.SetSecretFn = func() error {
  655. return nil
  656. }
  657. fakeProvider.DeleteSecretFn = func() error {
  658. return errors.New("boom")
  659. }
  660. tc.pushsecret.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyDelete
  661. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  662. secondStore := &esv1.SecretStore{
  663. ObjectMeta: metav1.ObjectMeta{
  664. Name: "new-store",
  665. Namespace: PushSecretNamespace,
  666. },
  667. TypeMeta: metav1.TypeMeta{
  668. Kind: "SecretStore",
  669. },
  670. Spec: esv1.SecretStoreSpec{
  671. Provider: &esv1.SecretStoreProvider{
  672. Fake: &esv1.FakeProvider{
  673. Data: []esv1.FakeProviderData{},
  674. },
  675. },
  676. },
  677. }
  678. Expect(k8sClient.Create(context.Background(), secondStore, &client.CreateOptions{})).Should(Succeed())
  679. ps.Spec.SecretStoreRefs[0].Name = "new-store"
  680. updatedPS := &v1alpha1.PushSecret{}
  681. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  682. Eventually(func() bool {
  683. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  684. By("checking if Provider value got updated")
  685. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  686. if err != nil {
  687. return false
  688. }
  689. syncedLen := len(updatedPS.Status.SyncedPushSecrets)
  690. return syncedLen == 2
  691. }, time.Second*10, time.Second).Should(BeTrue())
  692. return true
  693. }
  694. }
  695. deleteWholeStore := func(tc *testCase) {
  696. fakeProvider.SetSecretFn = func() error {
  697. return nil
  698. }
  699. fakeProvider.DeleteSecretFn = func() error {
  700. return nil
  701. }
  702. tc.pushsecret.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyDelete
  703. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  704. secondStore := &esv1.SecretStore{
  705. ObjectMeta: metav1.ObjectMeta{
  706. Name: "new-store",
  707. Namespace: PushSecretNamespace,
  708. },
  709. TypeMeta: metav1.TypeMeta{
  710. Kind: "SecretStore",
  711. },
  712. Spec: esv1.SecretStoreSpec{
  713. Provider: &esv1.SecretStoreProvider{
  714. Fake: &esv1.FakeProvider{
  715. Data: []esv1.FakeProviderData{},
  716. },
  717. },
  718. },
  719. }
  720. Expect(k8sClient.Create(context.Background(), secondStore, &client.CreateOptions{})).Should(Succeed())
  721. ps.Spec.SecretStoreRefs[0].Name = "new-store"
  722. updatedPS := &v1alpha1.PushSecret{}
  723. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  724. Eventually(func() bool {
  725. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  726. By("checking if Provider value got updated")
  727. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  728. if err != nil {
  729. return false
  730. }
  731. key, ok := updatedPS.Status.SyncedPushSecrets["SecretStore/new-store"][defaultPath]
  732. if !ok {
  733. return false
  734. }
  735. syncedLen := len(updatedPS.Status.SyncedPushSecrets)
  736. if syncedLen != 1 {
  737. return false
  738. }
  739. return key.Match.SecretKey == defaultKey
  740. }, time.Second*10, time.Second).Should(BeTrue())
  741. return true
  742. }
  743. }
  744. // if conversion strategy is defined, revert the keys based on the strategy.
  745. syncSuccessfullyWithConversionStrategy := func(tc *testCase) {
  746. fakeProvider.SetSecretFn = func() error {
  747. return nil
  748. }
  749. tc.pushsecret = &v1alpha1.PushSecret{
  750. ObjectMeta: metav1.ObjectMeta{
  751. Name: PushSecretName,
  752. Namespace: PushSecretNamespace,
  753. },
  754. Spec: v1alpha1.PushSecretSpec{
  755. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  756. {
  757. Name: PushSecretStore,
  758. Kind: "SecretStore",
  759. },
  760. },
  761. Selector: v1alpha1.PushSecretSelector{
  762. Secret: &v1alpha1.PushSecretSecret{
  763. Name: SecretName,
  764. },
  765. },
  766. Data: []v1alpha1.PushSecretData{
  767. {
  768. ConversionStrategy: v1alpha1.PushSecretConversionReverseUnicode,
  769. Match: v1alpha1.PushSecretMatch{
  770. SecretKey: "some-array[0].entity",
  771. RemoteRef: v1alpha1.PushSecretRemoteRef{
  772. RemoteKey: defaultPath,
  773. },
  774. },
  775. },
  776. },
  777. },
  778. }
  779. tc.secret = &v1.Secret{
  780. ObjectMeta: metav1.ObjectMeta{
  781. Name: SecretName,
  782. Namespace: PushSecretNamespace,
  783. },
  784. Data: map[string][]byte{
  785. "some-array_U005b_0_U005d_.entity": []byte("value"),
  786. },
  787. }
  788. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  789. Eventually(func() bool {
  790. By("checking if Provider value got updated")
  791. secretValue := secret.Data["some-array_U005b_0_U005d_.entity"]
  792. setSecretArgs := fakeProvider.GetPushSecretData()
  793. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  794. if !ok {
  795. return false
  796. }
  797. got := providerValue.Value
  798. return bytes.Equal(got, secretValue)
  799. }, time.Second*10, time.Second).Should(BeTrue())
  800. return true
  801. }
  802. }
  803. // if target Secret name is not specified it should use the ExternalSecret name.
  804. syncMatchingLabels := func(tc *testCase) {
  805. fakeProvider.SetSecretFn = func() error {
  806. return nil
  807. }
  808. fakeProvider.DeleteSecretFn = func() error {
  809. return nil
  810. }
  811. tc.pushsecret = &v1alpha1.PushSecret{
  812. ObjectMeta: metav1.ObjectMeta{
  813. Name: PushSecretName,
  814. Namespace: PushSecretNamespace,
  815. },
  816. Spec: v1alpha1.PushSecretSpec{
  817. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  818. {
  819. LabelSelector: &metav1.LabelSelector{
  820. MatchLabels: map[string]string{
  821. "foo": "bar",
  822. },
  823. },
  824. Kind: "SecretStore",
  825. Name: PushSecretStore,
  826. },
  827. },
  828. Selector: v1alpha1.PushSecretSelector{
  829. Secret: &v1alpha1.PushSecretSecret{
  830. Name: SecretName,
  831. },
  832. },
  833. Data: []v1alpha1.PushSecretData{
  834. {
  835. Match: v1alpha1.PushSecretMatch{
  836. SecretKey: defaultKey,
  837. RemoteRef: v1alpha1.PushSecretRemoteRef{
  838. RemoteKey: defaultPath,
  839. },
  840. },
  841. },
  842. },
  843. },
  844. }
  845. tc.store = &esv1.SecretStore{
  846. TypeMeta: metav1.TypeMeta{
  847. Kind: "SecretStore",
  848. },
  849. ObjectMeta: metav1.ObjectMeta{
  850. Name: PushSecretStore,
  851. Namespace: PushSecretNamespace,
  852. Labels: map[string]string{
  853. "foo": "bar",
  854. },
  855. },
  856. Spec: esv1.SecretStoreSpec{
  857. Provider: &esv1.SecretStoreProvider{
  858. Fake: &esv1.FakeProvider{
  859. Data: []esv1.FakeProviderData{},
  860. },
  861. },
  862. },
  863. }
  864. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  865. secretValue := secret.Data[defaultKey]
  866. setSecretArgs := fakeProvider.GetPushSecretData()
  867. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  868. expected := v1alpha1.PushSecretStatusCondition{
  869. Type: v1alpha1.PushSecretReady,
  870. Status: v1.ConditionTrue,
  871. Reason: v1alpha1.ReasonSynced,
  872. Message: "PushSecret synced successfully",
  873. }
  874. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  875. }
  876. }
  877. syncWithClusterStore := func(tc *testCase) {
  878. fakeProvider.SetSecretFn = func() error {
  879. return nil
  880. }
  881. tc.store = &esv1.ClusterSecretStore{
  882. TypeMeta: metav1.TypeMeta{
  883. Kind: "ClusterSecretStore",
  884. },
  885. ObjectMeta: metav1.ObjectMeta{
  886. Name: PushSecretStore,
  887. },
  888. Spec: esv1.SecretStoreSpec{
  889. Provider: &esv1.SecretStoreProvider{
  890. Fake: &esv1.FakeProvider{
  891. Data: []esv1.FakeProviderData{},
  892. },
  893. },
  894. },
  895. }
  896. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  897. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  898. secretValue := secret.Data[defaultKey]
  899. setSecretArgs := fakeProvider.GetPushSecretData()
  900. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  901. expected := v1alpha1.PushSecretStatusCondition{
  902. Type: v1alpha1.PushSecretReady,
  903. Status: v1.ConditionTrue,
  904. Reason: v1alpha1.ReasonSynced,
  905. Message: "PushSecret synced successfully",
  906. }
  907. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  908. }
  909. }
  910. syncWithGenerator := func(tc *testCase) {
  911. fakeProvider.SetSecretFn = func() error {
  912. return nil
  913. }
  914. tc.pushsecret.Spec.Selector.Secret = nil
  915. tc.pushsecret.Spec.Selector.GeneratorRef = &esv1.GeneratorRef{
  916. APIVersion: "generators.external-secrets.io/v1alpha1",
  917. Kind: "Fake",
  918. Name: "test",
  919. }
  920. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  921. setSecretArgs := fakeProvider.GetPushSecretData()
  922. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  923. expected := v1alpha1.PushSecretStatusCondition{
  924. Type: v1alpha1.PushSecretReady,
  925. Status: v1.ConditionTrue,
  926. Reason: v1alpha1.ReasonSynced,
  927. Message: "PushSecret synced successfully",
  928. }
  929. return bytes.Equal([]byte("foo-bar-from-generator"), providerValue) && checkCondition(ps.Status, expected)
  930. }
  931. }
  932. // if target Secret name is not specified it should use the ExternalSecret name.
  933. syncWithClusterStoreMatchingLabels := func(tc *testCase) {
  934. fakeProvider.SetSecretFn = func() error {
  935. return nil
  936. }
  937. tc.pushsecret = &v1alpha1.PushSecret{
  938. ObjectMeta: metav1.ObjectMeta{
  939. Name: PushSecretName,
  940. Namespace: PushSecretNamespace,
  941. },
  942. Spec: v1alpha1.PushSecretSpec{
  943. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  944. {
  945. LabelSelector: &metav1.LabelSelector{
  946. MatchLabels: map[string]string{
  947. "foo": "bar",
  948. },
  949. },
  950. Kind: "ClusterSecretStore",
  951. Name: PushSecretStore,
  952. },
  953. },
  954. Selector: v1alpha1.PushSecretSelector{
  955. Secret: &v1alpha1.PushSecretSecret{
  956. Name: SecretName,
  957. },
  958. },
  959. Data: []v1alpha1.PushSecretData{
  960. {
  961. Match: v1alpha1.PushSecretMatch{
  962. SecretKey: defaultKey,
  963. RemoteRef: v1alpha1.PushSecretRemoteRef{
  964. RemoteKey: defaultPath,
  965. },
  966. },
  967. },
  968. },
  969. },
  970. }
  971. tc.store = &esv1.ClusterSecretStore{
  972. ObjectMeta: metav1.ObjectMeta{
  973. Name: PushSecretStore,
  974. Labels: map[string]string{
  975. "foo": "bar",
  976. },
  977. },
  978. Spec: esv1.SecretStoreSpec{
  979. Provider: &esv1.SecretStoreProvider{
  980. Fake: &esv1.FakeProvider{
  981. Data: []esv1.FakeProviderData{},
  982. },
  983. },
  984. },
  985. }
  986. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  987. secretValue := secret.Data[defaultKey]
  988. setSecretArgs := fakeProvider.GetPushSecretData()
  989. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  990. expected := v1alpha1.PushSecretStatusCondition{
  991. Type: v1alpha1.PushSecretReady,
  992. Status: v1.ConditionTrue,
  993. Reason: v1alpha1.ReasonSynced,
  994. Message: "PushSecret synced successfully",
  995. }
  996. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  997. }
  998. }
  999. // if target Secret name is not specified it should use the ExternalSecret name.
  1000. failNoSecret := func(tc *testCase) {
  1001. fakeProvider.SetSecretFn = func() error {
  1002. return nil
  1003. }
  1004. tc.secret = nil
  1005. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1006. expected := v1alpha1.PushSecretStatusCondition{
  1007. Type: v1alpha1.PushSecretReady,
  1008. Status: v1.ConditionFalse,
  1009. Reason: v1alpha1.ReasonErrored,
  1010. Message: "could not get source secret",
  1011. }
  1012. return checkCondition(ps.Status, expected)
  1013. }
  1014. }
  1015. // if target Secret name is not specified it should use the ExternalSecret name.
  1016. failNoSecretKey := func(tc *testCase) {
  1017. fakeProvider.SetSecretFn = func() error {
  1018. return nil
  1019. }
  1020. tc.pushsecret.Spec.Data[0].Match.SecretKey = "unexisting"
  1021. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1022. expected := v1alpha1.PushSecretStatusCondition{
  1023. Type: v1alpha1.PushSecretReady,
  1024. Status: v1.ConditionFalse,
  1025. Reason: v1alpha1.ReasonErrored,
  1026. Message: "set secret failed: secret key unexisting does not exist",
  1027. }
  1028. return checkCondition(ps.Status, expected)
  1029. }
  1030. }
  1031. // if target Secret name is not specified it should use the ExternalSecret name.
  1032. failNoSecretStore := func(tc *testCase) {
  1033. fakeProvider.SetSecretFn = func() error {
  1034. return nil
  1035. }
  1036. tc.store = nil
  1037. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1038. expected := v1alpha1.PushSecretStatusCondition{
  1039. Type: v1alpha1.PushSecretReady,
  1040. Status: v1.ConditionFalse,
  1041. Reason: v1alpha1.ReasonErrored,
  1042. Message: "could not get SecretStore \"test-store\", secretstores.external-secrets.io \"test-store\" not found",
  1043. }
  1044. return checkCondition(ps.Status, expected)
  1045. }
  1046. }
  1047. // if target Secret name is not specified it should use the ExternalSecret name.
  1048. failNoClusterStore := func(tc *testCase) {
  1049. fakeProvider.SetSecretFn = func() error {
  1050. return nil
  1051. }
  1052. tc.store = nil
  1053. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  1054. tc.pushsecret.Spec.SecretStoreRefs[0].Name = "unexisting"
  1055. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1056. expected := v1alpha1.PushSecretStatusCondition{
  1057. Type: v1alpha1.PushSecretReady,
  1058. Status: v1.ConditionFalse,
  1059. Reason: v1alpha1.ReasonErrored,
  1060. Message: "could not get ClusterSecretStore \"unexisting\", clustersecretstores.external-secrets.io \"unexisting\" not found",
  1061. }
  1062. return checkCondition(ps.Status, expected)
  1063. }
  1064. }
  1065. // if target Secret name is not specified it should use the ExternalSecret name.
  1066. setSecretFail := func(tc *testCase) {
  1067. fakeProvider.SetSecretFn = func() error {
  1068. return errors.New("boom")
  1069. }
  1070. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1071. expected := v1alpha1.PushSecretStatusCondition{
  1072. Type: v1alpha1.PushSecretReady,
  1073. Status: v1.ConditionFalse,
  1074. Reason: v1alpha1.ReasonErrored,
  1075. Message: "set secret failed: could not write remote ref key to target secretstore test-store: boom",
  1076. }
  1077. return checkCondition(ps.Status, expected)
  1078. }
  1079. }
  1080. // if target Secret name is not specified it should use the ExternalSecret name.
  1081. newClientFail := func(tc *testCase) {
  1082. fakeProvider.NewFn = func(context.Context, esv1.GenericStore, client.Client, string) (esv1.SecretsClient, error) {
  1083. return nil, errors.New("boom")
  1084. }
  1085. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1086. expected := v1alpha1.PushSecretStatusCondition{
  1087. Type: v1alpha1.PushSecretReady,
  1088. Status: v1.ConditionFalse,
  1089. Reason: v1alpha1.ReasonErrored,
  1090. Message: "set secret failed: could not get secrets client for store test-store: boom",
  1091. }
  1092. return checkCondition(ps.Status, expected)
  1093. }
  1094. }
  1095. // SecretStores in different namespace than PushSecret should not be selected.
  1096. secretStoreDifferentNamespace := func(tc *testCase) {
  1097. fakeProvider.SetSecretFn = func() error {
  1098. return nil
  1099. }
  1100. // Create the SecretStore in a different namespace
  1101. tc.store = &esv1.SecretStore{
  1102. ObjectMeta: metav1.ObjectMeta{
  1103. Name: "other-ns-store",
  1104. Namespace: OtherNamespace,
  1105. Labels: map[string]string{
  1106. "foo": "bar",
  1107. },
  1108. },
  1109. TypeMeta: metav1.TypeMeta{
  1110. Kind: "SecretStore",
  1111. },
  1112. Spec: esv1.SecretStoreSpec{
  1113. Provider: &esv1.SecretStoreProvider{
  1114. Fake: &esv1.FakeProvider{
  1115. Data: []esv1.FakeProviderData{},
  1116. },
  1117. },
  1118. },
  1119. }
  1120. // Use label selector to select SecretStores
  1121. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  1122. {
  1123. Kind: "SecretStore",
  1124. LabelSelector: &metav1.LabelSelector{
  1125. MatchLabels: map[string]string{
  1126. "foo": "bar",
  1127. },
  1128. },
  1129. },
  1130. }
  1131. // Should not select the SecretStore in a different namespace
  1132. // (if so, it would fail to find it in the same namespace and be reflected in the status)
  1133. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1134. // Assert that the status is never updated (no SecretStores found)
  1135. Consistently(func() bool {
  1136. err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(ps), ps)
  1137. if err != nil {
  1138. return false
  1139. }
  1140. return len(ps.Status.Conditions) == 0
  1141. }, timeout, interval).Should(BeTrue())
  1142. return true
  1143. }
  1144. }
  1145. // Secrets in different namespace than PushSecret should not be selected.
  1146. secretDifferentNamespace := func(tc *testCase) {
  1147. fakeProvider.SetSecretFn = func() error {
  1148. return nil
  1149. }
  1150. // Create the Secret in a different namespace
  1151. tc.secret = &v1.Secret{
  1152. ObjectMeta: metav1.ObjectMeta{
  1153. Name: SecretName,
  1154. Namespace: OtherNamespace,
  1155. Labels: map[string]string{
  1156. "foo": "bar",
  1157. },
  1158. },
  1159. Data: map[string][]byte{
  1160. defaultKey: []byte(defaultVal),
  1161. },
  1162. }
  1163. // Use label selector to select Secrets
  1164. tc.pushsecret.Spec.Selector.Secret = &v1alpha1.PushSecretSecret{
  1165. Selector: &metav1.LabelSelector{
  1166. MatchLabels: map[string]string{
  1167. "foo": "bar",
  1168. },
  1169. },
  1170. }
  1171. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1172. Eventually(func() bool {
  1173. // We should not be able to reference a secret across namespaces,
  1174. // the map should be empty.
  1175. Expect(fakeProvider.GetPushSecretData()).To(BeEmpty())
  1176. return true
  1177. }, time.Second*10, time.Second).Should(BeTrue())
  1178. return true
  1179. }
  1180. }
  1181. DescribeTable("When reconciling a PushSecret",
  1182. func(tweaks ...testTweaks) {
  1183. tc := makeDefaultTestcase()
  1184. for _, tweak := range tweaks {
  1185. tweak(tc)
  1186. }
  1187. ctx := context.Background()
  1188. By("creating a secret store, secret and pushsecret")
  1189. if tc.store != nil {
  1190. Expect(k8sClient.Create(ctx, tc.store)).To(Succeed())
  1191. }
  1192. if tc.secret != nil {
  1193. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  1194. }
  1195. if tc.pushsecret != nil {
  1196. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  1197. }
  1198. time.Sleep(2 * time.Second) // prevents race conditions during tests causing failures
  1199. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  1200. createdPS := &v1alpha1.PushSecret{}
  1201. By("checking the pushSecret condition")
  1202. Eventually(func() bool {
  1203. err := k8sClient.Get(ctx, psKey, createdPS)
  1204. if err != nil {
  1205. return false
  1206. }
  1207. return tc.assert(createdPS, tc.secret)
  1208. }, timeout, interval).Should(BeTrue())
  1209. // this must be optional so we can test faulty es configuration
  1210. },
  1211. Entry("should sync", syncSuccessfully),
  1212. Entry("should not update existing secret if UpdatePolicy=IfNotExists", updateIfNotExists),
  1213. Entry("should only update parts of secret that don't already exist if UpdatePolicy=IfNotExists", updateIfNotExistsPartialSecrets),
  1214. Entry("should update the PushSecret status correctly if UpdatePolicy=IfNotExists", updateIfNotExistsSyncStatus),
  1215. Entry("should fail if secret existence cannot be verified if UpdatePolicy=IfNotExists", updateIfNotExistsSyncFailed),
  1216. Entry("should sync with template", syncSuccessfullyWithTemplate),
  1217. Entry("should sync with template reusing keys", syncSuccessfullyReusingKeys),
  1218. Entry("should sync with conversion strategy", syncSuccessfullyWithConversionStrategy),
  1219. Entry("should delete if DeletionPolicy=Delete", syncAndDeleteSuccessfully),
  1220. Entry("should delete after DeletionPolicy changed from Delete to None", syncChangePolicyAndDeleteSuccessfully),
  1221. Entry("should track deletion tasks if Delete fails", failDelete),
  1222. Entry("should track deleted stores if Delete fails", failDeleteStore),
  1223. Entry("should delete all secrets if SecretStore changes", deleteWholeStore),
  1224. Entry("should sync to stores matching labels", syncMatchingLabels),
  1225. Entry("should sync with ClusterStore", syncWithClusterStore),
  1226. Entry("should sync with ClusterStore matching labels", syncWithClusterStoreMatchingLabels),
  1227. Entry("should sync with Generator", syncWithGenerator),
  1228. Entry("should fail if Secret is not created", failNoSecret),
  1229. Entry("should fail if Secret Key does not exist", failNoSecretKey),
  1230. Entry("should fail if SetSecret fails", setSecretFail),
  1231. Entry("should fail if no valid SecretStore", failNoSecretStore),
  1232. Entry("should fail if no valid ClusterSecretStore", failNoClusterStore),
  1233. Entry("should fail if NewClient fails", newClientFail),
  1234. Entry("should not sync to SecretStore in different namespace", secretStoreDifferentNamespace),
  1235. Entry("should not reference secret in different namespace", secretDifferentNamespace),
  1236. )
  1237. })
  1238. var _ = Describe("PushSecret Controller Un/Managed Stores", func() {
  1239. const (
  1240. PushSecretName = "test-ps"
  1241. ManagedPushSecretStore1 = "test-managed-store-1"
  1242. ManagedPushSecretStore2 = "test-managed-store-2"
  1243. UnmanagedPushSecretStore1 = "test-unmanaged-store-1"
  1244. UnmanagedPushSecretStore2 = "test-unmanaged-store-2"
  1245. SecretName = "test-secret"
  1246. )
  1247. var PushSecretNamespace string
  1248. PushSecretStores := []string{ManagedPushSecretStore1, ManagedPushSecretStore2, UnmanagedPushSecretStore1, UnmanagedPushSecretStore2}
  1249. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  1250. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  1251. if t, err := strconv.Atoi(customTimeout); err == nil {
  1252. timeout = time.Second * time.Duration(t)
  1253. }
  1254. }
  1255. BeforeEach(func() {
  1256. var err error
  1257. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  1258. Expect(err).ToNot(HaveOccurred())
  1259. fakeProvider.Reset()
  1260. })
  1261. AfterEach(func() {
  1262. k8sClient.Delete(context.Background(), &v1alpha1.PushSecret{
  1263. ObjectMeta: metav1.ObjectMeta{
  1264. Name: PushSecretName,
  1265. Namespace: PushSecretNamespace,
  1266. },
  1267. })
  1268. // give a time for reconciler to remove finalizers before removing SecretStores
  1269. // TODO: Secret Stores should have finalizers bound to PushSecrets if DeletionPolicy == Delete
  1270. time.Sleep(2 * time.Second)
  1271. for _, psstore := range PushSecretStores {
  1272. k8sClient.Delete(context.Background(), &esv1.SecretStore{
  1273. ObjectMeta: metav1.ObjectMeta{
  1274. Name: psstore,
  1275. Namespace: PushSecretNamespace,
  1276. },
  1277. })
  1278. k8sClient.Delete(context.Background(), &esv1.ClusterSecretStore{
  1279. ObjectMeta: metav1.ObjectMeta{
  1280. Name: psstore,
  1281. },
  1282. })
  1283. }
  1284. k8sClient.Delete(context.Background(), &v1.Secret{
  1285. ObjectMeta: metav1.ObjectMeta{
  1286. Name: SecretName,
  1287. Namespace: PushSecretNamespace,
  1288. },
  1289. })
  1290. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  1291. ObjectMeta: metav1.ObjectMeta{
  1292. Name: PushSecretNamespace,
  1293. },
  1294. })).To(Succeed())
  1295. })
  1296. const (
  1297. defaultKey = "key"
  1298. defaultVal = "value"
  1299. defaultPath = "path/to/key"
  1300. )
  1301. makeDefaultTestcase := func() *testCase {
  1302. return &testCase{
  1303. pushsecret: &v1alpha1.PushSecret{
  1304. ObjectMeta: metav1.ObjectMeta{
  1305. Name: PushSecretName,
  1306. Namespace: PushSecretNamespace,
  1307. },
  1308. Spec: v1alpha1.PushSecretSpec{
  1309. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  1310. {
  1311. Name: ManagedPushSecretStore1,
  1312. Kind: "SecretStore",
  1313. },
  1314. },
  1315. Selector: v1alpha1.PushSecretSelector{
  1316. Secret: &v1alpha1.PushSecretSecret{
  1317. Name: SecretName,
  1318. },
  1319. },
  1320. Data: []v1alpha1.PushSecretData{
  1321. {
  1322. Match: v1alpha1.PushSecretMatch{
  1323. SecretKey: defaultKey,
  1324. RemoteRef: v1alpha1.PushSecretRemoteRef{
  1325. RemoteKey: defaultPath,
  1326. },
  1327. },
  1328. },
  1329. },
  1330. },
  1331. },
  1332. secret: &v1.Secret{
  1333. ObjectMeta: metav1.ObjectMeta{
  1334. Name: SecretName,
  1335. Namespace: PushSecretNamespace,
  1336. },
  1337. Data: map[string][]byte{
  1338. defaultKey: []byte(defaultVal),
  1339. },
  1340. },
  1341. managedStore1: &esv1.SecretStore{
  1342. ObjectMeta: metav1.ObjectMeta{
  1343. Name: ManagedPushSecretStore1,
  1344. Namespace: PushSecretNamespace,
  1345. },
  1346. TypeMeta: metav1.TypeMeta{
  1347. Kind: "SecretStore",
  1348. },
  1349. Spec: esv1.SecretStoreSpec{
  1350. Provider: &esv1.SecretStoreProvider{
  1351. Fake: &esv1.FakeProvider{
  1352. Data: []esv1.FakeProviderData{},
  1353. },
  1354. },
  1355. },
  1356. },
  1357. managedStore2: &esv1.SecretStore{
  1358. ObjectMeta: metav1.ObjectMeta{
  1359. Name: ManagedPushSecretStore2,
  1360. Namespace: PushSecretNamespace,
  1361. },
  1362. TypeMeta: metav1.TypeMeta{
  1363. Kind: "SecretStore",
  1364. },
  1365. Spec: esv1.SecretStoreSpec{
  1366. Provider: &esv1.SecretStoreProvider{
  1367. Fake: &esv1.FakeProvider{
  1368. Data: []esv1.FakeProviderData{},
  1369. },
  1370. },
  1371. },
  1372. },
  1373. unmanagedStore1: &esv1.SecretStore{
  1374. ObjectMeta: metav1.ObjectMeta{
  1375. Name: UnmanagedPushSecretStore1,
  1376. Namespace: PushSecretNamespace,
  1377. },
  1378. TypeMeta: metav1.TypeMeta{
  1379. Kind: "SecretStore",
  1380. },
  1381. Spec: esv1.SecretStoreSpec{
  1382. Provider: &esv1.SecretStoreProvider{
  1383. Fake: &esv1.FakeProvider{
  1384. Data: []esv1.FakeProviderData{},
  1385. },
  1386. },
  1387. Controller: "not-managed",
  1388. },
  1389. },
  1390. unmanagedStore2: &esv1.SecretStore{
  1391. ObjectMeta: metav1.ObjectMeta{
  1392. Name: UnmanagedPushSecretStore2,
  1393. Namespace: PushSecretNamespace,
  1394. },
  1395. TypeMeta: metav1.TypeMeta{
  1396. Kind: "SecretStore",
  1397. },
  1398. Spec: esv1.SecretStoreSpec{
  1399. Provider: &esv1.SecretStoreProvider{
  1400. Fake: &esv1.FakeProvider{
  1401. Data: []esv1.FakeProviderData{},
  1402. },
  1403. },
  1404. Controller: "not-managed",
  1405. },
  1406. },
  1407. }
  1408. }
  1409. multipleManagedStoresSyncsSuccessfully := func(tc *testCase) {
  1410. fakeProvider.SetSecretFn = func() error {
  1411. return nil
  1412. }
  1413. tc.pushsecret.Spec.SecretStoreRefs = append(tc.pushsecret.Spec.SecretStoreRefs,
  1414. v1alpha1.PushSecretStoreRef{
  1415. Name: ManagedPushSecretStore2,
  1416. Kind: "SecretStore",
  1417. },
  1418. )
  1419. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1420. Eventually(func() bool {
  1421. By("checking if Provider value got updated")
  1422. secretValue := secret.Data[defaultKey]
  1423. setSecretArgs := fakeProvider.GetPushSecretData()
  1424. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  1425. if !ok {
  1426. return false
  1427. }
  1428. got := providerValue.Value
  1429. return bytes.Equal(got, secretValue)
  1430. }, time.Second*10, time.Second).Should(BeTrue())
  1431. return true
  1432. }
  1433. }
  1434. skipUnmanagedStores := func(tc *testCase) {
  1435. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  1436. {
  1437. Name: UnmanagedPushSecretStore1,
  1438. Kind: "SecretStore",
  1439. },
  1440. {
  1441. Name: UnmanagedPushSecretStore2,
  1442. Kind: "SecretStore",
  1443. },
  1444. }
  1445. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1446. return len(ps.Status.Conditions) == 0
  1447. }
  1448. }
  1449. warnUnmanagedStoresAndSyncManagedStores := func(tc *testCase) {
  1450. fakeProvider.SetSecretFn = func() error {
  1451. return nil
  1452. }
  1453. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  1454. {
  1455. Name: ManagedPushSecretStore1,
  1456. Kind: "SecretStore",
  1457. },
  1458. {
  1459. Name: ManagedPushSecretStore2,
  1460. Kind: "SecretStore",
  1461. },
  1462. {
  1463. Name: UnmanagedPushSecretStore1,
  1464. Kind: "SecretStore",
  1465. },
  1466. {
  1467. Name: UnmanagedPushSecretStore2,
  1468. Kind: "SecretStore",
  1469. },
  1470. }
  1471. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1472. Eventually(func() bool {
  1473. By("checking if Provider value got updated")
  1474. secretValue := secret.Data[defaultKey]
  1475. setSecretArgs := fakeProvider.GetPushSecretData()
  1476. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  1477. if !ok {
  1478. return false
  1479. }
  1480. got := providerValue.Value
  1481. return bytes.Equal(got, secretValue)
  1482. }, time.Second*10, time.Second).Should(BeTrue())
  1483. return true
  1484. }
  1485. }
  1486. DescribeTable("When reconciling a PushSecret with multiple secret stores",
  1487. func(tweaks ...testTweaks) {
  1488. tc := makeDefaultTestcase()
  1489. for _, tweak := range tweaks {
  1490. tweak(tc)
  1491. }
  1492. ctx := context.Background()
  1493. By("creating secret stores, a secret and a pushsecret")
  1494. if tc.managedStore1 != nil {
  1495. Expect(k8sClient.Create(ctx, tc.managedStore1)).To(Succeed())
  1496. }
  1497. if tc.managedStore2 != nil {
  1498. Expect(k8sClient.Create(ctx, tc.managedStore2)).To(Succeed())
  1499. }
  1500. if tc.unmanagedStore1 != nil {
  1501. Expect(k8sClient.Create(ctx, tc.unmanagedStore1)).To(Succeed())
  1502. }
  1503. if tc.unmanagedStore2 != nil {
  1504. Expect(k8sClient.Create(ctx, tc.unmanagedStore2)).To(Succeed())
  1505. }
  1506. if tc.secret != nil {
  1507. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  1508. }
  1509. if tc.pushsecret != nil {
  1510. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  1511. }
  1512. time.Sleep(2 * time.Second) // prevents race conditions during tests causing failures
  1513. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  1514. createdPS := &v1alpha1.PushSecret{}
  1515. By("checking the pushSecret condition")
  1516. Eventually(func() bool {
  1517. err := k8sClient.Get(ctx, psKey, createdPS)
  1518. if err != nil {
  1519. return false
  1520. }
  1521. return tc.assert(createdPS, tc.secret)
  1522. }, timeout, interval).Should(BeTrue())
  1523. // this must be optional so we can test faulty es configuration
  1524. },
  1525. Entry("should sync successfully if there are multiple managed stores", multipleManagedStoresSyncsSuccessfully),
  1526. Entry("should skip unmanaged stores", skipUnmanagedStores),
  1527. Entry("should skip unmanaged stores and sync managed stores", warnUnmanagedStoresAndSyncManagedStores),
  1528. )
  1529. })