pushsecret_controller_test.go 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pushsecret
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "os"
  21. "strconv"
  22. "sync/atomic"
  23. "time"
  24. v1 "k8s.io/api/core/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "k8s.io/apimachinery/pkg/types"
  27. "sigs.k8s.io/controller-runtime/pkg/client"
  28. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  29. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  30. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  31. ctest "github.com/external-secrets/external-secrets/pkg/controllers/commontest"
  32. "github.com/external-secrets/external-secrets/pkg/controllers/pushsecret/psmetrics"
  33. "github.com/external-secrets/external-secrets/runtime/testing/fake"
  34. . "github.com/onsi/ginkgo/v2"
  35. . "github.com/onsi/gomega"
  36. )
  37. const (
  38. testAdminUser = "admin"
  39. testLocalhost = "localhost"
  40. testDBHost = "db-host"
  41. testDBPort = "db-port"
  42. testAppName = "app-name"
  43. testDBRegexp = "^db-.*"
  44. testAPIKey = "api-key"
  45. testDuplicateRemoteKeyErr = "duplicate remote key"
  46. )
  47. var (
  48. fakeProvider *fake.Client
  49. timeout = time.Second * 10
  50. interval = time.Millisecond * 250
  51. )
  52. type testCase struct {
  53. store esv1.GenericStore
  54. managedStore1 esv1.GenericStore
  55. managedStore2 esv1.GenericStore
  56. unmanagedStore1 esv1.GenericStore
  57. unmanagedStore2 esv1.GenericStore
  58. pushsecret *v1alpha1.PushSecret
  59. secret *v1.Secret
  60. assert func(pushsecret *v1alpha1.PushSecret, secret *v1.Secret) bool
  61. }
  62. func init() {
  63. fakeProvider = fake.New()
  64. esv1.ForceRegister(fakeProvider, &esv1.SecretStoreProvider{
  65. Fake: &esv1.FakeProvider{},
  66. }, esv1.MaintenanceStatusMaintained)
  67. psmetrics.SetUpMetrics()
  68. }
  69. func checkCondition(status v1alpha1.PushSecretStatus, cond v1alpha1.PushSecretStatusCondition) bool {
  70. for _, condition := range status.Conditions {
  71. if condition.Message == cond.Message &&
  72. condition.Reason == cond.Reason &&
  73. condition.Status == cond.Status &&
  74. condition.Type == cond.Type {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. type testTweaks func(*testCase)
  81. var _ = Describe("PushSecret controller", func() {
  82. const (
  83. PushSecretName = "test-ps"
  84. PushSecretStore = "test-store"
  85. SecretName = "test-secret"
  86. )
  87. var PushSecretNamespace, OtherNamespace string
  88. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  89. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  90. if t, err := strconv.Atoi(customTimeout); err == nil {
  91. timeout = time.Second * time.Duration(t)
  92. }
  93. }
  94. BeforeEach(func() {
  95. var err error
  96. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  97. Expect(err).ToNot(HaveOccurred())
  98. OtherNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  99. Expect(err).ToNot(HaveOccurred())
  100. fakeProvider.Reset()
  101. Expect(k8sClient.Create(context.Background(), &genv1alpha1.Fake{
  102. TypeMeta: metav1.TypeMeta{
  103. Kind: "Fake",
  104. APIVersion: "generators.external-secrets.io/v1alpha1",
  105. },
  106. ObjectMeta: metav1.ObjectMeta{
  107. Name: "test",
  108. Namespace: PushSecretNamespace,
  109. },
  110. Spec: genv1alpha1.FakeSpec{
  111. Data: map[string]string{
  112. "key": "foo-bar-from-generator",
  113. },
  114. }})).ToNot(HaveOccurred())
  115. })
  116. AfterEach(func() {
  117. k8sClient.Delete(context.Background(), &v1alpha1.PushSecret{
  118. ObjectMeta: metav1.ObjectMeta{
  119. Name: PushSecretName,
  120. Namespace: PushSecretNamespace,
  121. },
  122. })
  123. // give a time for reconciler to remove finalizers before removing SecretStores
  124. time.Sleep(2 * time.Second)
  125. k8sClient.Delete(context.Background(), &esv1.SecretStore{
  126. ObjectMeta: metav1.ObjectMeta{
  127. Name: PushSecretStore,
  128. Namespace: PushSecretNamespace,
  129. },
  130. })
  131. k8sClient.Delete(context.Background(), &esv1.ClusterSecretStore{
  132. ObjectMeta: metav1.ObjectMeta{
  133. Name: PushSecretStore,
  134. },
  135. })
  136. k8sClient.Delete(context.Background(), &v1.Secret{
  137. ObjectMeta: metav1.ObjectMeta{
  138. Name: SecretName,
  139. Namespace: PushSecretNamespace,
  140. },
  141. })
  142. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  143. ObjectMeta: metav1.ObjectMeta{
  144. Name: PushSecretNamespace,
  145. },
  146. })).To(Succeed())
  147. })
  148. const (
  149. defaultKey = "key"
  150. defaultVal = "value"
  151. defaultPath = "path/to/key"
  152. otherKey = "other-key"
  153. otherVal = "other-value"
  154. otherPath = "path/to/other-key"
  155. newKey = "new-key"
  156. newVal = "new-value"
  157. storePrefixTemplate = "SecretStore/%v"
  158. )
  159. makeDefaultTestcase := func() *testCase {
  160. return &testCase{
  161. pushsecret: &v1alpha1.PushSecret{
  162. ObjectMeta: metav1.ObjectMeta{
  163. Name: PushSecretName,
  164. Namespace: PushSecretNamespace,
  165. },
  166. Spec: v1alpha1.PushSecretSpec{
  167. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  168. {
  169. Name: PushSecretStore,
  170. Kind: "SecretStore",
  171. },
  172. },
  173. Selector: v1alpha1.PushSecretSelector{
  174. Secret: &v1alpha1.PushSecretSecret{
  175. Name: SecretName,
  176. },
  177. },
  178. Data: []v1alpha1.PushSecretData{
  179. {
  180. Match: v1alpha1.PushSecretMatch{
  181. SecretKey: defaultKey,
  182. RemoteRef: v1alpha1.PushSecretRemoteRef{
  183. RemoteKey: defaultPath,
  184. },
  185. },
  186. },
  187. },
  188. },
  189. },
  190. secret: &v1.Secret{
  191. ObjectMeta: metav1.ObjectMeta{
  192. Name: SecretName,
  193. Namespace: PushSecretNamespace,
  194. },
  195. Data: map[string][]byte{
  196. defaultKey: []byte(defaultVal),
  197. },
  198. },
  199. store: &esv1.SecretStore{
  200. ObjectMeta: metav1.ObjectMeta{
  201. Name: PushSecretStore,
  202. Namespace: PushSecretNamespace,
  203. },
  204. TypeMeta: metav1.TypeMeta{
  205. Kind: "SecretStore",
  206. },
  207. Spec: esv1.SecretStoreSpec{
  208. Provider: &esv1.SecretStoreProvider{
  209. Fake: &esv1.FakeProvider{
  210. Data: []esv1.FakeProviderData{},
  211. },
  212. },
  213. },
  214. },
  215. }
  216. }
  217. // if target Secret name is not specified it should use the ExternalSecret name.
  218. syncSuccessfully := func(tc *testCase) {
  219. fakeProvider.SetSecretFn = func() error {
  220. return nil
  221. }
  222. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  223. Eventually(func() bool {
  224. By("checking if Provider value got updated")
  225. secretValue := secret.Data[defaultKey]
  226. setSecretArgs := fakeProvider.GetPushSecretData()
  227. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  228. if !ok {
  229. return false
  230. }
  231. got := providerValue.Value
  232. return bytes.Equal(got, secretValue)
  233. }, time.Second*10, time.Second).Should(BeTrue())
  234. return true
  235. }
  236. }
  237. updateIfNotExists := func(tc *testCase) {
  238. fakeProvider.SetSecretFn = func() error {
  239. return nil
  240. }
  241. fakeProvider.SecretExistsFn = func(_ context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  242. setSecretArgs := fakeProvider.GetPushSecretData()
  243. _, ok := setSecretArgs[ref.GetRemoteKey()]
  244. return ok, nil
  245. }
  246. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  247. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  248. Consistently(func() bool {
  249. By("updating the secret value")
  250. tc.secret.Data[defaultKey] = []byte(newVal)
  251. Expect(k8sClient.Update(context.Background(), secret, &client.UpdateOptions{})).Should(Succeed())
  252. By("checking if Provider value does not get updated")
  253. setSecretArgs := fakeProvider.GetPushSecretData()
  254. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  255. if !ok {
  256. return false
  257. }
  258. got := providerValue.Value
  259. return bytes.Equal(got, []byte(defaultVal))
  260. }, time.Second*10, time.Second).Should(BeTrue())
  261. return true
  262. }
  263. }
  264. updateIfNotExistsPartialSecrets := func(tc *testCase) {
  265. fakeProvider.SetSecretFn = func() error {
  266. return nil
  267. }
  268. fakeProvider.SecretExistsFn = func(_ context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  269. setSecretArgs := fakeProvider.GetPushSecretData()
  270. _, ok := setSecretArgs[ref.GetRemoteKey()]
  271. return ok, nil
  272. }
  273. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  274. tc.pushsecret.Spec.Data = append(tc.pushsecret.Spec.Data, v1alpha1.PushSecretData{
  275. Match: v1alpha1.PushSecretMatch{
  276. SecretKey: otherKey,
  277. RemoteRef: v1alpha1.PushSecretRemoteRef{
  278. RemoteKey: otherPath,
  279. },
  280. },
  281. })
  282. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  283. Eventually(func() bool {
  284. tc.secret.Data[defaultKey] = []byte(newVal) // change initial value in secret
  285. tc.secret.Data[otherKey] = []byte(otherVal)
  286. By("checking if only not existing Provider value got updated")
  287. Expect(k8sClient.Update(context.Background(), secret, &client.UpdateOptions{})).Should(Succeed())
  288. setSecretArgs := fakeProvider.GetPushSecretData()
  289. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  290. if !ok {
  291. return false
  292. }
  293. got := providerValue.Value
  294. otherProviderValue, ok := setSecretArgs[ps.Spec.Data[1].Match.RemoteRef.RemoteKey]
  295. if !ok {
  296. return false
  297. }
  298. gotOther := otherProviderValue.Value
  299. return bytes.Equal(gotOther, tc.secret.Data[otherKey]) && bytes.Equal(got, []byte(defaultVal))
  300. }, time.Second*10, time.Second).Should(BeTrue())
  301. return true
  302. }
  303. }
  304. updateIfNotExistsSyncStatus := func(tc *testCase) {
  305. fakeProvider.SetSecretFn = func() error {
  306. return nil
  307. }
  308. fakeProvider.SecretExistsFn = func(_ context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  309. setSecretArgs := fakeProvider.GetPushSecretData()
  310. _, ok := setSecretArgs[ref.GetRemoteKey()]
  311. return ok, nil
  312. }
  313. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  314. tc.pushsecret.Spec.Data = append(tc.pushsecret.Spec.Data, v1alpha1.PushSecretData{
  315. Match: v1alpha1.PushSecretMatch{
  316. SecretKey: otherKey,
  317. RemoteRef: v1alpha1.PushSecretRemoteRef{
  318. RemoteKey: otherPath,
  319. },
  320. },
  321. })
  322. tc.secret.Data[defaultKey] = []byte(newVal)
  323. tc.secret.Data[otherKey] = []byte(otherVal)
  324. updatedPS := &v1alpha1.PushSecret{}
  325. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  326. Eventually(func() bool {
  327. By("checking if PushSecret status gets updated correctly with UpdatePolicy=IfNotExists")
  328. Expect(k8sClient.Update(context.Background(), secret, &client.UpdateOptions{})).Should(Succeed())
  329. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  330. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  331. if err != nil {
  332. return false
  333. }
  334. _, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath]
  335. if !ok {
  336. return false
  337. }
  338. _, ok = updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][otherPath]
  339. if !ok {
  340. return false
  341. }
  342. expected := v1alpha1.PushSecretStatusCondition{
  343. Type: v1alpha1.PushSecretReady,
  344. Status: v1.ConditionTrue,
  345. Reason: v1alpha1.ReasonSynced,
  346. Message: "PushSecret synced successfully. Existing secrets in providers unchanged.",
  347. }
  348. return checkCondition(ps.Status, expected)
  349. }, time.Second*10, time.Second).Should(BeTrue())
  350. return true
  351. }
  352. }
  353. updateIfNotExistsSyncFailed := func(tc *testCase) {
  354. fakeProvider.SetSecretFn = func() error {
  355. return nil
  356. }
  357. fakeProvider.SecretExistsFn = func(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  358. return false, errors.New("don't know")
  359. }
  360. tc.pushsecret.Spec.UpdatePolicy = v1alpha1.PushSecretUpdatePolicyIfNotExists
  361. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  362. Eventually(func() bool {
  363. By("checking if sync failed if secret existence cannot be verified in Provider")
  364. expected := v1alpha1.PushSecretStatusCondition{
  365. Type: v1alpha1.PushSecretReady,
  366. Status: v1.ConditionFalse,
  367. Reason: v1alpha1.ReasonErrored,
  368. Message: "set secret failed: could not verify if secret exists in store: don't know",
  369. }
  370. return checkCondition(ps.Status, expected)
  371. }, time.Second*10, time.Second).Should(BeTrue())
  372. return true
  373. }
  374. }
  375. syncSuccessfullyReusingKeys := func(tc *testCase) {
  376. fakeProvider.SetSecretFn = func() error {
  377. return nil
  378. }
  379. tc.pushsecret = &v1alpha1.PushSecret{
  380. ObjectMeta: metav1.ObjectMeta{
  381. Name: PushSecretName,
  382. Namespace: PushSecretNamespace,
  383. },
  384. Spec: v1alpha1.PushSecretSpec{
  385. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  386. {
  387. Name: PushSecretStore,
  388. Kind: "SecretStore",
  389. },
  390. },
  391. Selector: v1alpha1.PushSecretSelector{
  392. Secret: &v1alpha1.PushSecretSecret{
  393. Name: SecretName,
  394. },
  395. },
  396. Data: []v1alpha1.PushSecretData{
  397. {
  398. Match: v1alpha1.PushSecretMatch{
  399. SecretKey: "otherKey",
  400. RemoteRef: v1alpha1.PushSecretRemoteRef{
  401. RemoteKey: defaultPath,
  402. },
  403. },
  404. },
  405. },
  406. Template: &esv1.ExternalSecretTemplate{
  407. Metadata: esv1.ExternalSecretTemplateMetadata{
  408. Labels: map[string]string{
  409. "foos": "ball",
  410. },
  411. Annotations: map[string]string{
  412. "hihi": "ga",
  413. },
  414. Finalizers: []string{"example.com/finalizer"},
  415. },
  416. Type: v1.SecretTypeOpaque,
  417. EngineVersion: esv1.TemplateEngineV2,
  418. Data: map[string]string{
  419. defaultKey: "{{ .key | toString | upper }} was templated",
  420. "otherKey": "{{ .key | toString | upper }} was also templated",
  421. },
  422. },
  423. },
  424. }
  425. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  426. Eventually(func() bool {
  427. By("checking if Provider value got updated")
  428. setSecretArgs := fakeProvider.GetPushSecretData()
  429. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  430. if !ok {
  431. return false
  432. }
  433. got := providerValue.Value
  434. return bytes.Equal(got, []byte("VALUE was also templated"))
  435. }, time.Second*10, time.Second).Should(BeTrue())
  436. return true
  437. }
  438. }
  439. // if target Secret name is not specified it should use the ExternalSecret name.
  440. syncSuccessfullyWithTemplate := func(tc *testCase) {
  441. fakeProvider.SetSecretFn = func() error {
  442. return nil
  443. }
  444. tc.pushsecret = &v1alpha1.PushSecret{
  445. ObjectMeta: metav1.ObjectMeta{
  446. Name: PushSecretName,
  447. Namespace: PushSecretNamespace,
  448. },
  449. Spec: v1alpha1.PushSecretSpec{
  450. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  451. {
  452. Name: PushSecretStore,
  453. Kind: "SecretStore",
  454. },
  455. },
  456. Selector: v1alpha1.PushSecretSelector{
  457. Secret: &v1alpha1.PushSecretSecret{
  458. Name: SecretName,
  459. },
  460. },
  461. Data: []v1alpha1.PushSecretData{
  462. {
  463. Match: v1alpha1.PushSecretMatch{
  464. SecretKey: defaultKey,
  465. RemoteRef: v1alpha1.PushSecretRemoteRef{
  466. RemoteKey: defaultPath,
  467. },
  468. },
  469. },
  470. },
  471. Template: &esv1.ExternalSecretTemplate{
  472. Metadata: esv1.ExternalSecretTemplateMetadata{
  473. Labels: map[string]string{
  474. "foos": "ball",
  475. },
  476. Annotations: map[string]string{
  477. "hihi": "ga",
  478. },
  479. Finalizers: []string{"example.com/finalizer"},
  480. },
  481. Type: v1.SecretTypeOpaque,
  482. EngineVersion: esv1.TemplateEngineV2,
  483. Data: map[string]string{
  484. defaultKey: "{{ .key | toString | upper }} was templated",
  485. },
  486. },
  487. },
  488. }
  489. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  490. Eventually(func() bool {
  491. By("checking if Provider value got updated")
  492. setSecretArgs := fakeProvider.GetPushSecretData()
  493. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  494. if !ok {
  495. return false
  496. }
  497. got := providerValue.Value
  498. return bytes.Equal(got, []byte("VALUE was templated"))
  499. }, time.Second*10, time.Second).Should(BeTrue())
  500. return true
  501. }
  502. }
  503. // if target Secret name is not specified it should use the ExternalSecret name.
  504. syncAndDeleteSuccessfully := func(tc *testCase) {
  505. fakeProvider.SetSecretFn = func() error {
  506. return nil
  507. }
  508. tc.pushsecret = &v1alpha1.PushSecret{
  509. ObjectMeta: metav1.ObjectMeta{
  510. Name: PushSecretName,
  511. Namespace: PushSecretNamespace,
  512. },
  513. Spec: v1alpha1.PushSecretSpec{
  514. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  515. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  516. {
  517. Name: PushSecretStore,
  518. Kind: "SecretStore",
  519. },
  520. },
  521. Selector: v1alpha1.PushSecretSelector{
  522. Secret: &v1alpha1.PushSecretSecret{
  523. Name: SecretName,
  524. },
  525. },
  526. Data: []v1alpha1.PushSecretData{
  527. {
  528. Match: v1alpha1.PushSecretMatch{
  529. SecretKey: defaultKey,
  530. RemoteRef: v1alpha1.PushSecretRemoteRef{
  531. RemoteKey: defaultPath,
  532. },
  533. },
  534. },
  535. },
  536. },
  537. }
  538. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  539. ps.Spec.Data[0].Match.RemoteRef.RemoteKey = newKey
  540. updatedPS := &v1alpha1.PushSecret{}
  541. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  542. Eventually(func() bool {
  543. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  544. By("checking if Provider value got updated")
  545. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  546. if err != nil {
  547. return false
  548. }
  549. key, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][newKey]
  550. if !ok {
  551. return false
  552. }
  553. return key.Match.SecretKey == defaultKey
  554. }, time.Second*10, time.Second).Should(BeTrue())
  555. return true
  556. }
  557. }
  558. // if PushSecret deletes a secret with properties, the status map should be cleaned up correctly
  559. syncAndDeleteWithProperties := func(tc *testCase) {
  560. fakeProvider.SetSecretFn = func() error {
  561. return nil
  562. }
  563. fakeProvider.DeleteSecretFn = func() error {
  564. return nil
  565. }
  566. tc.pushsecret = &v1alpha1.PushSecret{
  567. ObjectMeta: metav1.ObjectMeta{
  568. Name: PushSecretName,
  569. Namespace: PushSecretNamespace,
  570. },
  571. Spec: v1alpha1.PushSecretSpec{
  572. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  573. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  574. {
  575. Name: PushSecretStore,
  576. Kind: "SecretStore",
  577. },
  578. },
  579. Selector: v1alpha1.PushSecretSelector{
  580. Secret: &v1alpha1.PushSecretSecret{
  581. Name: SecretName,
  582. },
  583. },
  584. Data: []v1alpha1.PushSecretData{
  585. {
  586. Match: v1alpha1.PushSecretMatch{
  587. SecretKey: defaultKey,
  588. RemoteRef: v1alpha1.PushSecretRemoteRef{
  589. RemoteKey: defaultPath,
  590. Property: "field1",
  591. },
  592. },
  593. },
  594. {
  595. Match: v1alpha1.PushSecretMatch{
  596. SecretKey: otherKey,
  597. RemoteRef: v1alpha1.PushSecretRemoteRef{
  598. RemoteKey: defaultPath,
  599. Property: "field2",
  600. },
  601. },
  602. },
  603. },
  604. },
  605. }
  606. tc.secret.Data[otherKey] = []byte(otherVal)
  607. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  608. updatedPS := &v1alpha1.PushSecret{}
  609. // Wait for initial sync
  610. Eventually(func() bool {
  611. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  612. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  613. if err != nil {
  614. return false
  615. }
  616. // Check both properties are in status
  617. _, ok1 := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath+"/field1"]
  618. _, ok2 := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath+"/field2"]
  619. return ok1 && ok2
  620. }, time.Second*10, time.Second).Should(BeTrue())
  621. // Remove one property
  622. updatedPS.Spec.Data = []v1alpha1.PushSecretData{
  623. {
  624. Match: v1alpha1.PushSecretMatch{
  625. SecretKey: defaultKey,
  626. RemoteRef: v1alpha1.PushSecretRemoteRef{
  627. RemoteKey: defaultPath,
  628. Property: "field1",
  629. },
  630. },
  631. },
  632. }
  633. Expect(k8sClient.Update(context.Background(), updatedPS, &client.UpdateOptions{})).Should(Succeed())
  634. // Verify the removed property is deleted from status
  635. Eventually(func() bool {
  636. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  637. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  638. if err != nil {
  639. return false
  640. }
  641. // field1 should still exist
  642. _, ok1 := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath+"/field1"]
  643. // field2 should be removed
  644. _, ok2 := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath+"/field2"]
  645. return ok1 && !ok2
  646. }, time.Second*10, time.Second).Should(BeTrue())
  647. return true
  648. }
  649. }
  650. // if PushSecret's DeletionPolicy is cleared, it should delete successfully
  651. syncChangePolicyAndDeleteSuccessfully := func(tc *testCase) {
  652. fakeProvider.SetSecretFn = func() error {
  653. return nil
  654. }
  655. tc.pushsecret = &v1alpha1.PushSecret{
  656. ObjectMeta: metav1.ObjectMeta{
  657. Name: PushSecretName,
  658. Namespace: PushSecretNamespace,
  659. },
  660. Spec: v1alpha1.PushSecretSpec{
  661. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  662. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  663. {
  664. Name: PushSecretStore,
  665. Kind: "SecretStore",
  666. },
  667. },
  668. Selector: v1alpha1.PushSecretSelector{
  669. Secret: &v1alpha1.PushSecretSecret{
  670. Name: SecretName,
  671. },
  672. },
  673. Data: []v1alpha1.PushSecretData{
  674. {
  675. Match: v1alpha1.PushSecretMatch{
  676. SecretKey: defaultKey,
  677. RemoteRef: v1alpha1.PushSecretRemoteRef{
  678. RemoteKey: defaultPath,
  679. },
  680. },
  681. },
  682. },
  683. },
  684. }
  685. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  686. ps.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyNone
  687. updatedPS := &v1alpha1.PushSecret{}
  688. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  689. Expect(k8sClient.Delete(context.Background(), ps, &client.DeleteOptions{})).Should(Succeed())
  690. Eventually(func() bool {
  691. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  692. By("checking if Get PushSecret returns not found")
  693. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  694. if err != nil && client.IgnoreNotFound(err) == nil {
  695. return true
  696. }
  697. return false
  698. }, time.Second*10, time.Second).Should(BeTrue())
  699. return true
  700. }
  701. }
  702. // When source Secret is deleted and DeletionPolicy=Delete, provider secrets should be cleaned up
  703. deleteProviderSecretsOnSourceSecretDeleted := func(tc *testCase) {
  704. var deleteCallCount int32
  705. fakeProvider.SetSecretFn = func() error {
  706. return nil
  707. }
  708. fakeProvider.DeleteSecretFn = func() error {
  709. atomic.AddInt32(&deleteCallCount, 1)
  710. return nil
  711. }
  712. tc.pushsecret = &v1alpha1.PushSecret{
  713. ObjectMeta: metav1.ObjectMeta{
  714. Name: PushSecretName,
  715. Namespace: PushSecretNamespace,
  716. },
  717. Spec: v1alpha1.PushSecretSpec{
  718. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  719. // Short refresh interval so reconciler detects deleted secret quickly
  720. RefreshInterval: &metav1.Duration{Duration: time.Second},
  721. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  722. {
  723. Name: PushSecretStore,
  724. Kind: "SecretStore",
  725. },
  726. },
  727. Selector: v1alpha1.PushSecretSelector{
  728. Secret: &v1alpha1.PushSecretSecret{
  729. Name: SecretName,
  730. },
  731. },
  732. Data: []v1alpha1.PushSecretData{
  733. {
  734. Match: v1alpha1.PushSecretMatch{
  735. SecretKey: defaultKey,
  736. RemoteRef: v1alpha1.PushSecretRemoteRef{
  737. RemoteKey: defaultPath,
  738. },
  739. },
  740. },
  741. },
  742. },
  743. }
  744. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  745. updatedPS := &v1alpha1.PushSecret{}
  746. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  747. // Wait for initial sync
  748. Eventually(func() bool {
  749. By("waiting for initial sync to complete")
  750. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  751. if err != nil {
  752. return false
  753. }
  754. storeKey := fmt.Sprintf(storePrefixTemplate, PushSecretStore)
  755. _, ok := updatedPS.Status.SyncedPushSecrets[storeKey][defaultPath]
  756. return ok
  757. }, time.Second*10, time.Second).Should(BeTrue())
  758. // Delete the source Secret
  759. By("deleting source Secret")
  760. Expect(k8sClient.Delete(context.Background(), secret, &client.DeleteOptions{})).Should(Succeed())
  761. // Verify provider secrets are cleaned up
  762. Eventually(func() bool {
  763. By("checking if provider secrets were deleted")
  764. return atomic.LoadInt32(&deleteCallCount) > 0
  765. }, time.Second*10, time.Second).Should(BeTrue())
  766. // Verify status shows empty synced secrets
  767. Eventually(func() bool {
  768. By("checking if SyncedPushSecrets is empty")
  769. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  770. if err != nil {
  771. return false
  772. }
  773. storeKey := fmt.Sprintf(storePrefixTemplate, PushSecretStore)
  774. secrets, exists := updatedPS.Status.SyncedPushSecrets[storeKey]
  775. return !exists || len(secrets) == 0
  776. }, time.Second*10, time.Second).Should(BeTrue())
  777. return true
  778. }
  779. }
  780. failDelete := func(tc *testCase) {
  781. fakeProvider.SetSecretFn = func() error {
  782. return nil
  783. }
  784. fakeProvider.DeleteSecretFn = func() error {
  785. return errors.New("Nope")
  786. }
  787. tc.pushsecret = &v1alpha1.PushSecret{
  788. ObjectMeta: metav1.ObjectMeta{
  789. Name: PushSecretName,
  790. Namespace: PushSecretNamespace,
  791. },
  792. Spec: v1alpha1.PushSecretSpec{
  793. DeletionPolicy: v1alpha1.PushSecretDeletionPolicyDelete,
  794. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  795. {
  796. Name: PushSecretStore,
  797. Kind: "SecretStore",
  798. },
  799. },
  800. Selector: v1alpha1.PushSecretSelector{
  801. Secret: &v1alpha1.PushSecretSecret{
  802. Name: SecretName,
  803. },
  804. },
  805. Data: []v1alpha1.PushSecretData{
  806. {
  807. Match: v1alpha1.PushSecretMatch{
  808. SecretKey: defaultKey,
  809. RemoteRef: v1alpha1.PushSecretRemoteRef{
  810. RemoteKey: defaultPath,
  811. },
  812. },
  813. },
  814. },
  815. },
  816. }
  817. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  818. ps.Spec.Data[0].Match.RemoteRef.RemoteKey = newKey
  819. updatedPS := &v1alpha1.PushSecret{}
  820. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  821. Eventually(func() bool {
  822. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  823. By("checking if synced secrets correspond to both keys")
  824. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  825. if err != nil {
  826. return false
  827. }
  828. _, ok := updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][newKey]
  829. if !ok {
  830. return false
  831. }
  832. _, ok = updatedPS.Status.SyncedPushSecrets[fmt.Sprintf(storePrefixTemplate, PushSecretStore)][defaultPath]
  833. return ok
  834. }, time.Second*10, time.Second).Should(BeTrue())
  835. return true
  836. }
  837. }
  838. failDeleteStore := func(tc *testCase) {
  839. fakeProvider.SetSecretFn = func() error {
  840. return nil
  841. }
  842. fakeProvider.DeleteSecretFn = func() error {
  843. return errors.New("boom")
  844. }
  845. tc.pushsecret.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyDelete
  846. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  847. secondStore := &esv1.SecretStore{
  848. ObjectMeta: metav1.ObjectMeta{
  849. Name: "new-store",
  850. Namespace: PushSecretNamespace,
  851. },
  852. TypeMeta: metav1.TypeMeta{
  853. Kind: "SecretStore",
  854. },
  855. Spec: esv1.SecretStoreSpec{
  856. Provider: &esv1.SecretStoreProvider{
  857. Fake: &esv1.FakeProvider{
  858. Data: []esv1.FakeProviderData{},
  859. },
  860. },
  861. },
  862. }
  863. Expect(k8sClient.Create(context.Background(), secondStore, &client.CreateOptions{})).Should(Succeed())
  864. ps.Spec.SecretStoreRefs[0].Name = "new-store"
  865. updatedPS := &v1alpha1.PushSecret{}
  866. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  867. Eventually(func() bool {
  868. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  869. By("checking if Provider value got updated")
  870. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  871. if err != nil {
  872. return false
  873. }
  874. syncedLen := len(updatedPS.Status.SyncedPushSecrets)
  875. return syncedLen == 2
  876. }, time.Second*10, time.Second).Should(BeTrue())
  877. return true
  878. }
  879. }
  880. deleteWholeStore := func(tc *testCase) {
  881. fakeProvider.SetSecretFn = func() error {
  882. return nil
  883. }
  884. fakeProvider.DeleteSecretFn = func() error {
  885. return nil
  886. }
  887. tc.pushsecret.Spec.DeletionPolicy = v1alpha1.PushSecretDeletionPolicyDelete
  888. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  889. secondStore := &esv1.SecretStore{
  890. ObjectMeta: metav1.ObjectMeta{
  891. Name: "new-store",
  892. Namespace: PushSecretNamespace,
  893. },
  894. TypeMeta: metav1.TypeMeta{
  895. Kind: "SecretStore",
  896. },
  897. Spec: esv1.SecretStoreSpec{
  898. Provider: &esv1.SecretStoreProvider{
  899. Fake: &esv1.FakeProvider{
  900. Data: []esv1.FakeProviderData{},
  901. },
  902. },
  903. },
  904. }
  905. Expect(k8sClient.Create(context.Background(), secondStore, &client.CreateOptions{})).Should(Succeed())
  906. ps.Spec.SecretStoreRefs[0].Name = "new-store"
  907. updatedPS := &v1alpha1.PushSecret{}
  908. Expect(k8sClient.Update(context.Background(), ps, &client.UpdateOptions{})).Should(Succeed())
  909. Eventually(func() bool {
  910. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  911. By("checking if Provider value got updated")
  912. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  913. if err != nil {
  914. return false
  915. }
  916. key, ok := updatedPS.Status.SyncedPushSecrets["SecretStore/new-store"][defaultPath]
  917. if !ok {
  918. return false
  919. }
  920. syncedLen := len(updatedPS.Status.SyncedPushSecrets)
  921. if syncedLen != 1 {
  922. return false
  923. }
  924. return key.Match.SecretKey == defaultKey
  925. }, time.Second*10, time.Second).Should(BeTrue())
  926. return true
  927. }
  928. }
  929. // if conversion strategy is defined, revert the keys based on the strategy.
  930. syncSuccessfullyWithConversionStrategy := func(tc *testCase) {
  931. fakeProvider.SetSecretFn = func() error {
  932. return nil
  933. }
  934. tc.pushsecret = &v1alpha1.PushSecret{
  935. ObjectMeta: metav1.ObjectMeta{
  936. Name: PushSecretName,
  937. Namespace: PushSecretNamespace,
  938. },
  939. Spec: v1alpha1.PushSecretSpec{
  940. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  941. {
  942. Name: PushSecretStore,
  943. Kind: "SecretStore",
  944. },
  945. },
  946. Selector: v1alpha1.PushSecretSelector{
  947. Secret: &v1alpha1.PushSecretSecret{
  948. Name: SecretName,
  949. },
  950. },
  951. Data: []v1alpha1.PushSecretData{
  952. {
  953. ConversionStrategy: v1alpha1.PushSecretConversionReverseUnicode,
  954. Match: v1alpha1.PushSecretMatch{
  955. SecretKey: "some-array[0].entity",
  956. RemoteRef: v1alpha1.PushSecretRemoteRef{
  957. RemoteKey: defaultPath,
  958. },
  959. },
  960. },
  961. },
  962. },
  963. }
  964. tc.secret = &v1.Secret{
  965. ObjectMeta: metav1.ObjectMeta{
  966. Name: SecretName,
  967. Namespace: PushSecretNamespace,
  968. },
  969. Data: map[string][]byte{
  970. "some-array_U005b_0_U005d_.entity": []byte("value"),
  971. },
  972. }
  973. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  974. Eventually(func() bool {
  975. By("checking if Provider value got updated")
  976. secretValue := secret.Data["some-array_U005b_0_U005d_.entity"]
  977. setSecretArgs := fakeProvider.GetPushSecretData()
  978. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  979. if !ok {
  980. return false
  981. }
  982. got := providerValue.Value
  983. return bytes.Equal(got, secretValue)
  984. }, time.Second*10, time.Second).Should(BeTrue())
  985. return true
  986. }
  987. }
  988. // if target Secret name is not specified it should use the ExternalSecret name.
  989. syncMatchingLabels := func(tc *testCase) {
  990. fakeProvider.SetSecretFn = func() error {
  991. return nil
  992. }
  993. fakeProvider.DeleteSecretFn = func() error {
  994. return nil
  995. }
  996. tc.pushsecret = &v1alpha1.PushSecret{
  997. ObjectMeta: metav1.ObjectMeta{
  998. Name: PushSecretName,
  999. Namespace: PushSecretNamespace,
  1000. },
  1001. Spec: v1alpha1.PushSecretSpec{
  1002. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  1003. {
  1004. LabelSelector: &metav1.LabelSelector{
  1005. MatchLabels: map[string]string{
  1006. "foo": "bar",
  1007. },
  1008. },
  1009. Kind: "SecretStore",
  1010. Name: PushSecretStore,
  1011. },
  1012. },
  1013. Selector: v1alpha1.PushSecretSelector{
  1014. Secret: &v1alpha1.PushSecretSecret{
  1015. Name: SecretName,
  1016. },
  1017. },
  1018. Data: []v1alpha1.PushSecretData{
  1019. {
  1020. Match: v1alpha1.PushSecretMatch{
  1021. SecretKey: defaultKey,
  1022. RemoteRef: v1alpha1.PushSecretRemoteRef{
  1023. RemoteKey: defaultPath,
  1024. },
  1025. },
  1026. },
  1027. },
  1028. },
  1029. }
  1030. tc.store = &esv1.SecretStore{
  1031. TypeMeta: metav1.TypeMeta{
  1032. Kind: "SecretStore",
  1033. },
  1034. ObjectMeta: metav1.ObjectMeta{
  1035. Name: PushSecretStore,
  1036. Namespace: PushSecretNamespace,
  1037. Labels: map[string]string{
  1038. "foo": "bar",
  1039. },
  1040. },
  1041. Spec: esv1.SecretStoreSpec{
  1042. Provider: &esv1.SecretStoreProvider{
  1043. Fake: &esv1.FakeProvider{
  1044. Data: []esv1.FakeProviderData{},
  1045. },
  1046. },
  1047. },
  1048. }
  1049. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1050. secretValue := secret.Data[defaultKey]
  1051. setSecretArgs := fakeProvider.GetPushSecretData()
  1052. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  1053. expected := v1alpha1.PushSecretStatusCondition{
  1054. Type: v1alpha1.PushSecretReady,
  1055. Status: v1.ConditionTrue,
  1056. Reason: v1alpha1.ReasonSynced,
  1057. Message: "PushSecret synced successfully",
  1058. }
  1059. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  1060. }
  1061. }
  1062. syncWithClusterStore := func(tc *testCase) {
  1063. fakeProvider.SetSecretFn = func() error {
  1064. return nil
  1065. }
  1066. tc.store = &esv1.ClusterSecretStore{
  1067. TypeMeta: metav1.TypeMeta{
  1068. Kind: "ClusterSecretStore",
  1069. },
  1070. ObjectMeta: metav1.ObjectMeta{
  1071. Name: PushSecretStore,
  1072. },
  1073. Spec: esv1.SecretStoreSpec{
  1074. Provider: &esv1.SecretStoreProvider{
  1075. Fake: &esv1.FakeProvider{
  1076. Data: []esv1.FakeProviderData{},
  1077. },
  1078. },
  1079. },
  1080. }
  1081. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  1082. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1083. secretValue := secret.Data[defaultKey]
  1084. setSecretArgs := fakeProvider.GetPushSecretData()
  1085. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  1086. expected := v1alpha1.PushSecretStatusCondition{
  1087. Type: v1alpha1.PushSecretReady,
  1088. Status: v1.ConditionTrue,
  1089. Reason: v1alpha1.ReasonSynced,
  1090. Message: "PushSecret synced successfully",
  1091. }
  1092. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  1093. }
  1094. }
  1095. syncWithGenerator := func(tc *testCase) {
  1096. fakeProvider.SetSecretFn = func() error {
  1097. return nil
  1098. }
  1099. tc.pushsecret.Spec.Selector.Secret = nil
  1100. tc.pushsecret.Spec.Selector.GeneratorRef = &esv1.GeneratorRef{
  1101. APIVersion: "generators.external-secrets.io/v1alpha1",
  1102. Kind: "Fake",
  1103. Name: "test",
  1104. }
  1105. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1106. setSecretArgs := fakeProvider.GetPushSecretData()
  1107. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  1108. expected := v1alpha1.PushSecretStatusCondition{
  1109. Type: v1alpha1.PushSecretReady,
  1110. Status: v1.ConditionTrue,
  1111. Reason: v1alpha1.ReasonSynced,
  1112. Message: "PushSecret synced successfully",
  1113. }
  1114. return bytes.Equal([]byte("foo-bar-from-generator"), providerValue) && checkCondition(ps.Status, expected)
  1115. }
  1116. }
  1117. // if target Secret name is not specified it should use the ExternalSecret name.
  1118. syncWithClusterStoreMatchingLabels := func(tc *testCase) {
  1119. fakeProvider.SetSecretFn = func() error {
  1120. return nil
  1121. }
  1122. tc.pushsecret = &v1alpha1.PushSecret{
  1123. ObjectMeta: metav1.ObjectMeta{
  1124. Name: PushSecretName,
  1125. Namespace: PushSecretNamespace,
  1126. },
  1127. Spec: v1alpha1.PushSecretSpec{
  1128. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  1129. {
  1130. LabelSelector: &metav1.LabelSelector{
  1131. MatchLabels: map[string]string{
  1132. "foo": "bar",
  1133. },
  1134. },
  1135. Kind: "ClusterSecretStore",
  1136. Name: PushSecretStore,
  1137. },
  1138. },
  1139. Selector: v1alpha1.PushSecretSelector{
  1140. Secret: &v1alpha1.PushSecretSecret{
  1141. Name: SecretName,
  1142. },
  1143. },
  1144. Data: []v1alpha1.PushSecretData{
  1145. {
  1146. Match: v1alpha1.PushSecretMatch{
  1147. SecretKey: defaultKey,
  1148. RemoteRef: v1alpha1.PushSecretRemoteRef{
  1149. RemoteKey: defaultPath,
  1150. },
  1151. },
  1152. },
  1153. },
  1154. },
  1155. }
  1156. tc.store = &esv1.ClusterSecretStore{
  1157. ObjectMeta: metav1.ObjectMeta{
  1158. Name: PushSecretStore,
  1159. Labels: map[string]string{
  1160. "foo": "bar",
  1161. },
  1162. },
  1163. Spec: esv1.SecretStoreSpec{
  1164. Provider: &esv1.SecretStoreProvider{
  1165. Fake: &esv1.FakeProvider{
  1166. Data: []esv1.FakeProviderData{},
  1167. },
  1168. },
  1169. },
  1170. }
  1171. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1172. secretValue := secret.Data[defaultKey]
  1173. setSecretArgs := fakeProvider.GetPushSecretData()
  1174. providerValue := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  1175. expected := v1alpha1.PushSecretStatusCondition{
  1176. Type: v1alpha1.PushSecretReady,
  1177. Status: v1.ConditionTrue,
  1178. Reason: v1alpha1.ReasonSynced,
  1179. Message: "PushSecret synced successfully",
  1180. }
  1181. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  1182. }
  1183. }
  1184. // if target Secret name is not specified it should use the ExternalSecret name.
  1185. failNoSecret := func(tc *testCase) {
  1186. fakeProvider.SetSecretFn = func() error {
  1187. return nil
  1188. }
  1189. tc.secret = nil
  1190. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1191. expected := v1alpha1.PushSecretStatusCondition{
  1192. Type: v1alpha1.PushSecretReady,
  1193. Status: v1.ConditionFalse,
  1194. Reason: v1alpha1.ReasonErrored,
  1195. Message: "could not get source secret",
  1196. }
  1197. return checkCondition(ps.Status, expected)
  1198. }
  1199. }
  1200. // if target Secret name is not specified it should use the ExternalSecret name.
  1201. failNoSecretKey := func(tc *testCase) {
  1202. fakeProvider.SetSecretFn = func() error {
  1203. return nil
  1204. }
  1205. tc.pushsecret.Spec.Data[0].Match.SecretKey = "unexisting"
  1206. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1207. expected := v1alpha1.PushSecretStatusCondition{
  1208. Type: v1alpha1.PushSecretReady,
  1209. Status: v1.ConditionFalse,
  1210. Reason: v1alpha1.ReasonErrored,
  1211. Message: "set secret failed: secret key unexisting does not exist",
  1212. }
  1213. return checkCondition(ps.Status, expected)
  1214. }
  1215. }
  1216. // if target Secret name is not specified it should use the ExternalSecret name.
  1217. failNoSecretStore := func(tc *testCase) {
  1218. fakeProvider.SetSecretFn = func() error {
  1219. return nil
  1220. }
  1221. tc.store = nil
  1222. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1223. expected := v1alpha1.PushSecretStatusCondition{
  1224. Type: v1alpha1.PushSecretReady,
  1225. Status: v1.ConditionFalse,
  1226. Reason: v1alpha1.ReasonErrored,
  1227. Message: "could not get SecretStore \"test-store\", secretstores.external-secrets.io \"test-store\" not found",
  1228. }
  1229. return checkCondition(ps.Status, expected)
  1230. }
  1231. }
  1232. // if target Secret name is not specified it should use the ExternalSecret name.
  1233. failNoClusterStore := func(tc *testCase) {
  1234. fakeProvider.SetSecretFn = func() error {
  1235. return nil
  1236. }
  1237. tc.store = nil
  1238. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  1239. tc.pushsecret.Spec.SecretStoreRefs[0].Name = "unexisting"
  1240. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1241. expected := v1alpha1.PushSecretStatusCondition{
  1242. Type: v1alpha1.PushSecretReady,
  1243. Status: v1.ConditionFalse,
  1244. Reason: v1alpha1.ReasonErrored,
  1245. Message: "could not get ClusterSecretStore \"unexisting\", clustersecretstores.external-secrets.io \"unexisting\" not found",
  1246. }
  1247. return checkCondition(ps.Status, expected)
  1248. }
  1249. }
  1250. // if target Secret name is not specified it should use the ExternalSecret name.
  1251. setSecretFail := func(tc *testCase) {
  1252. fakeProvider.SetSecretFn = func() error {
  1253. return errors.New("boom")
  1254. }
  1255. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1256. expected := v1alpha1.PushSecretStatusCondition{
  1257. Type: v1alpha1.PushSecretReady,
  1258. Status: v1.ConditionFalse,
  1259. Reason: v1alpha1.ReasonErrored,
  1260. Message: "set secret failed: could not write remote ref key to target secretstore test-store: boom",
  1261. }
  1262. return checkCondition(ps.Status, expected)
  1263. }
  1264. }
  1265. // if target Secret name is not specified it should use the ExternalSecret name.
  1266. newClientFail := func(tc *testCase) {
  1267. fakeProvider.NewFn = func(_ context.Context, _ esv1.GenericStore, _ client.Client, _ string) (esv1.SecretsClient, error) {
  1268. return nil, errors.New("boom")
  1269. }
  1270. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1271. expected := v1alpha1.PushSecretStatusCondition{
  1272. Type: v1alpha1.PushSecretReady,
  1273. Status: v1.ConditionFalse,
  1274. Reason: v1alpha1.ReasonErrored,
  1275. Message: "set secret failed: could not get secrets client for store test-store: boom",
  1276. }
  1277. return checkCondition(ps.Status, expected)
  1278. }
  1279. }
  1280. // SecretStores in different namespace than PushSecret should not be selected.
  1281. secretStoreDifferentNamespace := func(tc *testCase) {
  1282. fakeProvider.SetSecretFn = func() error {
  1283. return nil
  1284. }
  1285. // Create the SecretStore in a different namespace
  1286. tc.store = &esv1.SecretStore{
  1287. ObjectMeta: metav1.ObjectMeta{
  1288. Name: "other-ns-store",
  1289. Namespace: OtherNamespace,
  1290. Labels: map[string]string{
  1291. "foo": "bar",
  1292. },
  1293. },
  1294. TypeMeta: metav1.TypeMeta{
  1295. Kind: "SecretStore",
  1296. },
  1297. Spec: esv1.SecretStoreSpec{
  1298. Provider: &esv1.SecretStoreProvider{
  1299. Fake: &esv1.FakeProvider{
  1300. Data: []esv1.FakeProviderData{},
  1301. },
  1302. },
  1303. },
  1304. }
  1305. // Use label selector to select SecretStores
  1306. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  1307. {
  1308. Kind: "SecretStore",
  1309. LabelSelector: &metav1.LabelSelector{
  1310. MatchLabels: map[string]string{
  1311. "foo": "bar",
  1312. },
  1313. },
  1314. },
  1315. }
  1316. // Should not select the SecretStore in a different namespace
  1317. // (if so, it would fail to find it in the same namespace and be reflected in the status)
  1318. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1319. // Assert that the status is never updated (no SecretStores found)
  1320. Consistently(func() bool {
  1321. err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(ps), ps)
  1322. if err != nil {
  1323. return false
  1324. }
  1325. return len(ps.Status.Conditions) == 0
  1326. }, timeout, interval).Should(BeTrue())
  1327. return true
  1328. }
  1329. }
  1330. // Secrets in different namespace than PushSecret should not be selected.
  1331. secretDifferentNamespace := func(tc *testCase) {
  1332. fakeProvider.SetSecretFn = func() error {
  1333. return nil
  1334. }
  1335. // Create the Secret in a different namespace
  1336. tc.secret = &v1.Secret{
  1337. ObjectMeta: metav1.ObjectMeta{
  1338. Name: SecretName,
  1339. Namespace: OtherNamespace,
  1340. Labels: map[string]string{
  1341. "foo": "bar",
  1342. },
  1343. },
  1344. Data: map[string][]byte{
  1345. defaultKey: []byte(defaultVal),
  1346. },
  1347. }
  1348. // Use label selector to select Secrets
  1349. tc.pushsecret.Spec.Selector.Secret = &v1alpha1.PushSecretSecret{
  1350. Selector: &metav1.LabelSelector{
  1351. MatchLabels: map[string]string{
  1352. "foo": "bar",
  1353. },
  1354. },
  1355. }
  1356. tc.assert = func(_ *v1alpha1.PushSecret, _ *v1.Secret) bool {
  1357. Eventually(func() bool {
  1358. // We should not be able to reference a secret across namespaces,
  1359. // the map should be empty.
  1360. Expect(fakeProvider.GetPushSecretData()).To(BeEmpty())
  1361. return true
  1362. }, time.Second*10, time.Second).Should(BeTrue())
  1363. return true
  1364. }
  1365. }
  1366. // dataTo tests
  1367. syncWithDataToMatchAll := func(tc *testCase) {
  1368. fakeProvider.SetSecretFn = func() error {
  1369. return nil
  1370. }
  1371. // Set up secret with multiple keys
  1372. tc.secret.Data = map[string][]byte{
  1373. testDBHost: []byte(testLocalhost),
  1374. testDBPort: []byte("5432"),
  1375. "db-username": []byte(testAdminUser),
  1376. }
  1377. // Replace data with dataTo that matches all keys
  1378. tc.pushsecret.Spec.Data = nil
  1379. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1380. {
  1381. StoreRef: &v1alpha1.PushSecretStoreRef{
  1382. Name: PushSecretStore,
  1383. },
  1384. // No match pattern means match all
  1385. },
  1386. }
  1387. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1388. Eventually(func() bool {
  1389. By("checking if all keys were pushed to provider")
  1390. setSecretArgs := fakeProvider.GetPushSecretData()
  1391. // All three keys should be pushed
  1392. if len(setSecretArgs) != 3 {
  1393. return false
  1394. }
  1395. // Check each key was pushed with same name
  1396. for key, expectedValue := range secret.Data {
  1397. providerValue, ok := setSecretArgs[key]
  1398. if !ok {
  1399. return false
  1400. }
  1401. if !bytes.Equal(providerValue.Value, expectedValue) {
  1402. return false
  1403. }
  1404. }
  1405. return true
  1406. }, time.Second*10, time.Second).Should(BeTrue())
  1407. return true
  1408. }
  1409. }
  1410. syncWithDataToRegex := func(tc *testCase) {
  1411. fakeProvider.SetSecretFn = func() error {
  1412. return nil
  1413. }
  1414. // Set up secret with multiple keys
  1415. tc.secret.Data = map[string][]byte{
  1416. testDBHost: []byte(testLocalhost),
  1417. testDBPort: []byte("5432"),
  1418. testAppName: []byte("myapp"),
  1419. "app-version": []byte("1.0"),
  1420. }
  1421. // Use dataTo with regex to match only db-* keys
  1422. tc.pushsecret.Spec.Data = nil
  1423. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1424. {
  1425. StoreRef: &v1alpha1.PushSecretStoreRef{
  1426. Name: PushSecretStore,
  1427. },
  1428. Match: &v1alpha1.PushSecretDataToMatch{
  1429. RegExp: testDBRegexp,
  1430. },
  1431. },
  1432. }
  1433. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1434. Eventually(func() bool {
  1435. By("checking if only db-* keys were pushed")
  1436. setSecretArgs := fakeProvider.GetPushSecretData()
  1437. // Only two db-* keys should be pushed
  1438. if len(setSecretArgs) != 2 {
  1439. return false
  1440. }
  1441. // Check db-* keys were pushed
  1442. for key := range setSecretArgs {
  1443. if key != testDBHost && key != testDBPort {
  1444. return false
  1445. }
  1446. }
  1447. return true
  1448. }, time.Second*10, time.Second).Should(BeTrue())
  1449. return true
  1450. }
  1451. }
  1452. syncWithDataToRegexpRewrite := func(tc *testCase) {
  1453. fakeProvider.SetSecretFn = func() error {
  1454. return nil
  1455. }
  1456. // Set up secret with multiple keys
  1457. tc.secret.Data = map[string][]byte{
  1458. testDBHost: []byte(testLocalhost),
  1459. testDBPort: []byte("5432"),
  1460. }
  1461. // Use dataTo with regex rewrite to add prefix
  1462. tc.pushsecret.Spec.Data = nil
  1463. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1464. {
  1465. StoreRef: &v1alpha1.PushSecretStoreRef{
  1466. Name: PushSecretStore,
  1467. },
  1468. Match: &v1alpha1.PushSecretDataToMatch{
  1469. RegExp: testDBRegexp,
  1470. },
  1471. Rewrite: []v1alpha1.PushSecretRewrite{
  1472. {
  1473. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1474. Source: "^db-",
  1475. Target: "app/database/",
  1476. },
  1477. },
  1478. },
  1479. },
  1480. }
  1481. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1482. Eventually(func() bool {
  1483. By("checking if keys were rewritten with prefix")
  1484. setSecretArgs := fakeProvider.GetPushSecretData()
  1485. if len(setSecretArgs) != 2 {
  1486. return false
  1487. }
  1488. // Check keys were rewritten
  1489. _, hasHost := setSecretArgs["app/database/host"]
  1490. _, hasPort := setSecretArgs["app/database/port"]
  1491. return hasHost && hasPort
  1492. }, time.Second*10, time.Second).Should(BeTrue())
  1493. return true
  1494. }
  1495. }
  1496. syncWithDataToTransformRewrite := func(tc *testCase) {
  1497. fakeProvider.SetSecretFn = func() error {
  1498. return nil
  1499. }
  1500. tc.secret.Data = map[string][]byte{
  1501. "username": []byte(testAdminUser),
  1502. }
  1503. // Use dataTo with template transformation
  1504. tc.pushsecret.Spec.Data = nil
  1505. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1506. {
  1507. StoreRef: &v1alpha1.PushSecretStoreRef{
  1508. Name: PushSecretStore,
  1509. },
  1510. Rewrite: []v1alpha1.PushSecretRewrite{
  1511. {
  1512. Transform: &esv1.ExternalSecretRewriteTransform{
  1513. Template: "app/{{ .value | upper }}",
  1514. },
  1515. },
  1516. },
  1517. },
  1518. }
  1519. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1520. Eventually(func() bool {
  1521. By("checking if key was transformed using template")
  1522. setSecretArgs := fakeProvider.GetPushSecretData()
  1523. if len(setSecretArgs) != 1 {
  1524. return false
  1525. }
  1526. // Check key was transformed to uppercase with prefix
  1527. providerValue, ok := setSecretArgs["app/USERNAME"]
  1528. if !ok {
  1529. return false
  1530. }
  1531. return bytes.Equal(providerValue.Value, []byte(testAdminUser))
  1532. }, time.Second*10, time.Second).Should(BeTrue())
  1533. return true
  1534. }
  1535. }
  1536. syncDataToWithDataOverride := func(tc *testCase) {
  1537. fakeProvider.SetSecretFn = func() error {
  1538. return nil
  1539. }
  1540. tc.secret.Data = map[string][]byte{
  1541. "key1": []byte("value1"),
  1542. "key2": []byte("value2"),
  1543. }
  1544. // Use both dataTo and explicit data
  1545. // Explicit data should override dataTo for key1
  1546. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1547. {
  1548. StoreRef: &v1alpha1.PushSecretStoreRef{
  1549. Name: PushSecretStore,
  1550. },
  1551. // Match all keys, no rewrite
  1552. },
  1553. }
  1554. tc.pushsecret.Spec.Data = []v1alpha1.PushSecretData{
  1555. {
  1556. Match: v1alpha1.PushSecretMatch{
  1557. SecretKey: "key1",
  1558. RemoteRef: v1alpha1.PushSecretRemoteRef{
  1559. RemoteKey: "override-key1", // Different remote key
  1560. },
  1561. },
  1562. },
  1563. }
  1564. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1565. Eventually(func() bool {
  1566. By("checking if explicit data overrode dataTo")
  1567. setSecretArgs := fakeProvider.GetPushSecretData()
  1568. // Should have 2 keys: override-key1 and key2
  1569. if len(setSecretArgs) != 2 {
  1570. return false
  1571. }
  1572. // key1 should be pushed as override-key1 (from explicit data)
  1573. _, hasOverride := setSecretArgs["override-key1"]
  1574. // key2 should be pushed as key2 (from dataTo)
  1575. _, hasKey2 := setSecretArgs["key2"]
  1576. return hasOverride && hasKey2
  1577. }, time.Second*10, time.Second).Should(BeTrue())
  1578. return true
  1579. }
  1580. }
  1581. failDataToInvalidRegex := func(tc *testCase) {
  1582. tc.secret.Data = map[string][]byte{
  1583. "key1": []byte("value1"),
  1584. }
  1585. // Use invalid regex pattern
  1586. tc.pushsecret.Spec.Data = nil
  1587. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1588. {
  1589. StoreRef: &v1alpha1.PushSecretStoreRef{
  1590. Name: PushSecretStore,
  1591. },
  1592. Match: &v1alpha1.PushSecretDataToMatch{
  1593. RegExp: "[invalid(regex",
  1594. },
  1595. },
  1596. }
  1597. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1598. Eventually(func() bool {
  1599. By("checking if PushSecret has error condition")
  1600. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1601. if cond == nil {
  1602. return false
  1603. }
  1604. // Should have error status
  1605. return cond.Status == v1.ConditionFalse && cond.Reason == v1alpha1.ReasonErrored
  1606. }, time.Second*10, time.Second).Should(BeTrue())
  1607. return true
  1608. }
  1609. }
  1610. syncWithDataToConversionStrategy := func(tc *testCase) {
  1611. fakeProvider.SetSecretFn = func() error {
  1612. return nil
  1613. }
  1614. // Set up secret with unicode data
  1615. tc.secret.Data = map[string][]byte{
  1616. "unicode-key": []byte("unicode-value-αβγ"),
  1617. "normal-key": []byte("normal-value"),
  1618. }
  1619. // Use dataTo with ConversionStrategy
  1620. tc.pushsecret.Spec.Data = nil
  1621. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1622. {
  1623. StoreRef: &v1alpha1.PushSecretStoreRef{
  1624. Name: PushSecretStore,
  1625. },
  1626. ConversionStrategy: v1alpha1.PushSecretConversionReverseUnicode,
  1627. },
  1628. }
  1629. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1630. Eventually(func() bool {
  1631. By("checking if all keys were pushed with unicode conversion")
  1632. setSecretArgs := fakeProvider.GetPushSecretData()
  1633. // Both keys should be pushed
  1634. if len(setSecretArgs) != 2 {
  1635. return false
  1636. }
  1637. // Verify keys exist (actual unicode encoding is tested in provider tests)
  1638. _, hasUnicode := setSecretArgs["unicode-key"]
  1639. _, hasNormal := setSecretArgs["normal-key"]
  1640. return hasUnicode && hasNormal
  1641. }, timeout, time.Second).Should(BeTrue())
  1642. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1643. return cond != nil && cond.Status == v1.ConditionTrue && cond.Reason == v1alpha1.ReasonSynced
  1644. }
  1645. }
  1646. // Test dataTo with storeRef targeting specific store
  1647. syncWithDataToStoreRef := func(tc *testCase) {
  1648. fakeProvider.SetSecretFn = func() error {
  1649. return nil
  1650. }
  1651. // Create a second store
  1652. secondStoreName := "second-store"
  1653. secondStore := &esv1.SecretStore{
  1654. ObjectMeta: metav1.ObjectMeta{
  1655. Name: secondStoreName,
  1656. Namespace: PushSecretNamespace,
  1657. },
  1658. TypeMeta: metav1.TypeMeta{
  1659. Kind: "SecretStore",
  1660. },
  1661. Spec: esv1.SecretStoreSpec{
  1662. Provider: &esv1.SecretStoreProvider{
  1663. Fake: &esv1.FakeProvider{
  1664. Data: []esv1.FakeProviderData{},
  1665. },
  1666. },
  1667. },
  1668. }
  1669. tc.secret.Data = map[string][]byte{
  1670. testDBHost: []byte(testLocalhost),
  1671. testAPIKey: []byte("secret123"),
  1672. testAppName: []byte("myapp"),
  1673. }
  1674. // Configure multiple stores
  1675. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  1676. {Name: PushSecretStore, Kind: "SecretStore"},
  1677. {Name: secondStoreName, Kind: "SecretStore"},
  1678. }
  1679. tc.pushsecret.Spec.Data = nil
  1680. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1681. {
  1682. // Entry targeting first store only
  1683. StoreRef: &v1alpha1.PushSecretStoreRef{
  1684. Name: PushSecretStore,
  1685. Kind: "SecretStore",
  1686. },
  1687. Match: &v1alpha1.PushSecretDataToMatch{
  1688. RegExp: testDBRegexp,
  1689. },
  1690. },
  1691. {
  1692. // Entry targeting second store only
  1693. StoreRef: &v1alpha1.PushSecretStoreRef{
  1694. Name: secondStoreName,
  1695. Kind: "SecretStore",
  1696. },
  1697. Match: &v1alpha1.PushSecretDataToMatch{
  1698. RegExp: "^api-.*",
  1699. },
  1700. },
  1701. {
  1702. // Entry targeting first store (app-name)
  1703. StoreRef: &v1alpha1.PushSecretStoreRef{
  1704. Name: PushSecretStore,
  1705. Kind: "SecretStore",
  1706. },
  1707. Match: &v1alpha1.PushSecretDataToMatch{
  1708. RegExp: "^app-.*",
  1709. },
  1710. },
  1711. {
  1712. // Entry targeting second store (app-name)
  1713. StoreRef: &v1alpha1.PushSecretStoreRef{
  1714. Name: secondStoreName,
  1715. Kind: "SecretStore",
  1716. },
  1717. Match: &v1alpha1.PushSecretDataToMatch{
  1718. RegExp: "^app-.*",
  1719. },
  1720. },
  1721. }
  1722. // Second store is created by test harness via tc.managedStore2 so it exists before PushSecret
  1723. tc.managedStore2 = secondStore
  1724. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1725. updatedPS := &v1alpha1.PushSecret{}
  1726. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  1727. Eventually(func() bool {
  1728. By("checking if secrets are synced to correct stores")
  1729. err := k8sClient.Get(context.Background(), psKey, updatedPS)
  1730. if err != nil {
  1731. return false
  1732. }
  1733. firstStoreKey := fmt.Sprintf(storePrefixTemplate, PushSecretStore)
  1734. secondStoreKey := "SecretStore/" + secondStoreName
  1735. firstStoreSynced := updatedPS.Status.SyncedPushSecrets[firstStoreKey]
  1736. secondStoreSynced := updatedPS.Status.SyncedPushSecrets[secondStoreKey]
  1737. // First store should have: db-host and app-name (both from storeRef entries)
  1738. _, hasDbHost := firstStoreSynced[testDBHost]
  1739. _, hasAppNameFirst := firstStoreSynced[testAppName]
  1740. // First store should NOT have api-key (targeted to second store)
  1741. _, hasApiKeyFirst := firstStoreSynced[testAPIKey]
  1742. // Second store should have: api-key and app-name (both from storeRef entries)
  1743. _, hasApiKey := secondStoreSynced[testAPIKey]
  1744. _, hasAppNameSecond := secondStoreSynced[testAppName]
  1745. // Second store should NOT have db-host (targeted to first store)
  1746. _, hasDbHostSecond := secondStoreSynced[testDBHost]
  1747. return hasDbHost && hasAppNameFirst && !hasApiKeyFirst &&
  1748. hasApiKey && hasAppNameSecond && !hasDbHostSecond
  1749. }, time.Second*10, time.Second).Should(BeTrue())
  1750. return true
  1751. }
  1752. }
  1753. // Test: Template creates new keys, dataTo matches them
  1754. templateCreatesKeysThenDataToMatches := func(tc *testCase) {
  1755. fakeProvider.SetSecretFn = func() error {
  1756. return nil
  1757. }
  1758. // Source secret has individual components
  1759. tc.secret.Data = map[string][]byte{
  1760. "db_host": []byte(testLocalhost),
  1761. "db_port": []byte("3306"),
  1762. }
  1763. // Template creates connection string from components
  1764. tc.pushsecret.Spec.Template = &esv1.ExternalSecretTemplate{
  1765. Data: map[string]string{
  1766. "mysql-connection": "mysql://{{ .db_host }}:{{ .db_port }}/mydb",
  1767. },
  1768. }
  1769. // dataTo only matches keys ending in -connection (created by template)
  1770. tc.pushsecret.Spec.Data = nil
  1771. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1772. {
  1773. StoreRef: &v1alpha1.PushSecretStoreRef{
  1774. Name: PushSecretStore,
  1775. },
  1776. Match: &v1alpha1.PushSecretDataToMatch{
  1777. RegExp: ".*-connection$",
  1778. },
  1779. },
  1780. }
  1781. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1782. Eventually(func() bool {
  1783. By("checking template key was pushed, not originals")
  1784. setSecretArgs := fakeProvider.GetPushSecretData()
  1785. // Only mysql-connection should be pushed
  1786. _, hasConnection := setSecretArgs["mysql-connection"]
  1787. _, hasDbHost := setSecretArgs["db_host"]
  1788. _, hasDbPort := setSecretArgs["db_port"]
  1789. return hasConnection && !hasDbHost && !hasDbPort
  1790. }, timeout, time.Second).Should(BeTrue())
  1791. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1792. return cond != nil && cond.Status == v1.ConditionTrue && cond.Reason == v1alpha1.ReasonSynced
  1793. }
  1794. }
  1795. // Test: Template + dataTo + explicit data combined
  1796. templateWithDataToAndExplicitData := func(tc *testCase) {
  1797. fakeProvider.SetSecretFn = func() error {
  1798. return nil
  1799. }
  1800. tc.secret.Data = map[string][]byte{
  1801. "token": []byte("abc123"),
  1802. "config-timeout": []byte("30s"),
  1803. "config-retries": []byte("3"),
  1804. }
  1805. // Template creates api-key from token
  1806. tc.pushsecret.Spec.Template = &esv1.ExternalSecretTemplate{
  1807. Data: map[string]string{
  1808. testAPIKey: "Bearer {{ .token }}",
  1809. },
  1810. }
  1811. // dataTo matches config-* keys
  1812. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1813. {
  1814. StoreRef: &v1alpha1.PushSecretStoreRef{
  1815. Name: PushSecretStore,
  1816. },
  1817. Match: &v1alpha1.PushSecretDataToMatch{
  1818. RegExp: "^config-.*",
  1819. },
  1820. },
  1821. }
  1822. // Explicit data for api-key with custom remote path
  1823. tc.pushsecret.Spec.Data = []v1alpha1.PushSecretData{
  1824. {
  1825. Match: v1alpha1.PushSecretMatch{
  1826. SecretKey: testAPIKey,
  1827. RemoteRef: v1alpha1.PushSecretRemoteRef{
  1828. RemoteKey: "credentials/api-key",
  1829. },
  1830. },
  1831. },
  1832. }
  1833. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1834. Eventually(func() bool {
  1835. By("checking all expected keys were pushed")
  1836. setSecretArgs := fakeProvider.GetPushSecretData()
  1837. // api-key should be at credentials/api-key (explicit), config-* from dataTo
  1838. _, hasApiKey := setSecretArgs["credentials/api-key"]
  1839. _, hasTimeout := setSecretArgs["config-timeout"]
  1840. _, hasRetries := setSecretArgs["config-retries"]
  1841. // Original token should NOT be pushed
  1842. _, hasToken := setSecretArgs["token"]
  1843. return hasApiKey && hasTimeout && hasRetries && !hasToken
  1844. }, timeout, time.Second).Should(BeTrue())
  1845. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1846. return cond != nil && cond.Status == v1.ConditionTrue && cond.Reason == v1alpha1.ReasonSynced
  1847. }
  1848. }
  1849. failDataToDuplicateAcrossEntries := func(tc *testCase) {
  1850. tc.secret.Data = map[string][]byte{
  1851. testDBHost: []byte(testLocalhost),
  1852. testDBPort: []byte("5432"),
  1853. }
  1854. // Create two dataTo entries that both produce the same remote key "app/config"
  1855. tc.pushsecret.Spec.Data = nil
  1856. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1857. {
  1858. StoreRef: &v1alpha1.PushSecretStoreRef{
  1859. Name: PushSecretStore,
  1860. },
  1861. Match: &v1alpha1.PushSecretDataToMatch{
  1862. RegExp: "^db-host$",
  1863. },
  1864. Rewrite: []v1alpha1.PushSecretRewrite{
  1865. {
  1866. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1867. Source: ".*",
  1868. Target: "app/config",
  1869. },
  1870. },
  1871. },
  1872. },
  1873. {
  1874. StoreRef: &v1alpha1.PushSecretStoreRef{
  1875. Name: PushSecretStore,
  1876. },
  1877. Match: &v1alpha1.PushSecretDataToMatch{
  1878. RegExp: "^db-port$",
  1879. },
  1880. Rewrite: []v1alpha1.PushSecretRewrite{
  1881. {
  1882. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1883. Source: ".*",
  1884. Target: "app/config",
  1885. },
  1886. },
  1887. },
  1888. },
  1889. }
  1890. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1891. Eventually(func() bool {
  1892. By("checking if PushSecret has error condition for duplicate remote keys")
  1893. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1894. if cond == nil {
  1895. return false
  1896. }
  1897. // Should have error status
  1898. return cond.Status == v1.ConditionFalse && cond.Reason == v1alpha1.ReasonErrored
  1899. }, time.Second*10, time.Second).Should(BeTrue())
  1900. return true
  1901. }
  1902. }
  1903. failDataToAndDataDuplicateRemoteKey := func(tc *testCase) {
  1904. tc.secret.Data = map[string][]byte{
  1905. testDBHost: []byte(testLocalhost),
  1906. testAPIKey: []byte("secret123"),
  1907. }
  1908. // Create dataTo entry and explicit data that map to the same remote key
  1909. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1910. {
  1911. StoreRef: &v1alpha1.PushSecretStoreRef{
  1912. Name: PushSecretStore,
  1913. },
  1914. Match: &v1alpha1.PushSecretDataToMatch{
  1915. RegExp: "^db-host$",
  1916. },
  1917. Rewrite: []v1alpha1.PushSecretRewrite{
  1918. {
  1919. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1920. Source: ".*",
  1921. Target: "myapp/config",
  1922. },
  1923. },
  1924. },
  1925. },
  1926. }
  1927. tc.pushsecret.Spec.Data = []v1alpha1.PushSecretData{
  1928. {
  1929. Match: v1alpha1.PushSecretMatch{
  1930. SecretKey: testAPIKey,
  1931. RemoteRef: v1alpha1.PushSecretRemoteRef{
  1932. RemoteKey: "myapp/config", // Same remote key as dataTo produces
  1933. },
  1934. },
  1935. },
  1936. }
  1937. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1938. Eventually(func() bool {
  1939. By("checking if PushSecret has error condition for duplicate remote keys")
  1940. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1941. if cond == nil {
  1942. return false
  1943. }
  1944. // Should have error status
  1945. return cond.Status == v1.ConditionFalse && cond.Reason == v1alpha1.ReasonErrored
  1946. }, time.Second*10, time.Second).Should(BeTrue())
  1947. return true
  1948. }
  1949. }
  1950. // Note: failDataToMissingStoreRef test removed - missing storeRef is now
  1951. // blocked by CEL validation at admission time
  1952. failDataToStoreRefNotInList := func(tc *testCase) {
  1953. tc.secret.Data = map[string][]byte{
  1954. "key1": []byte("value1"),
  1955. }
  1956. // dataTo with storeRef that doesn't exist in secretStoreRefs
  1957. tc.pushsecret.Spec.Data = nil
  1958. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1959. {
  1960. StoreRef: &v1alpha1.PushSecretStoreRef{
  1961. Name: "non-existent-store", // Not in secretStoreRefs
  1962. },
  1963. },
  1964. }
  1965. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1966. Eventually(func() bool {
  1967. By("checking PushSecret has error for invalid storeRef")
  1968. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  1969. if cond == nil {
  1970. return false
  1971. }
  1972. return cond.Status == v1.ConditionFalse && cond.Reason == v1alpha1.ReasonErrored
  1973. }, time.Second*10, time.Second).Should(BeTrue())
  1974. return true
  1975. }
  1976. }
  1977. failDataToNamedStoreRefWithLabelSelectorRefs := func(tc *testCase) {
  1978. tc.secret.Data = map[string][]byte{
  1979. "key1": []byte("value1"),
  1980. }
  1981. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  1982. {
  1983. LabelSelector: &metav1.LabelSelector{
  1984. MatchLabels: map[string]string{
  1985. "env": "prod",
  1986. },
  1987. },
  1988. },
  1989. }
  1990. tc.pushsecret.Spec.Data = nil
  1991. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  1992. {
  1993. StoreRef: &v1alpha1.PushSecretStoreRef{
  1994. Name: "totally-nonexistent-store",
  1995. },
  1996. },
  1997. }
  1998. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  1999. Eventually(func() bool {
  2000. By("checking PushSecret rejects named storeRef not in secretStoreRefs")
  2001. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  2002. if cond == nil {
  2003. return false
  2004. }
  2005. return cond.Status == v1.ConditionFalse && cond.Reason == v1alpha1.ReasonErrored
  2006. }, time.Second*10, time.Second).Should(BeTrue())
  2007. return true
  2008. }
  2009. }
  2010. failDataToLabelSelectorMatchesNoStore := func(tc *testCase) {
  2011. tc.store = &esv1.SecretStore{
  2012. ObjectMeta: metav1.ObjectMeta{
  2013. Name: PushSecretStore,
  2014. Namespace: PushSecretNamespace,
  2015. Labels: map[string]string{
  2016. "env": "staging",
  2017. },
  2018. },
  2019. TypeMeta: metav1.TypeMeta{
  2020. Kind: "SecretStore",
  2021. },
  2022. Spec: esv1.SecretStoreSpec{
  2023. Provider: &esv1.SecretStoreProvider{
  2024. Fake: &esv1.FakeProvider{
  2025. Data: []esv1.FakeProviderData{},
  2026. },
  2027. },
  2028. },
  2029. }
  2030. tc.secret.Data = map[string][]byte{
  2031. "key1": []byte("value1"),
  2032. }
  2033. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  2034. {
  2035. LabelSelector: &metav1.LabelSelector{
  2036. MatchLabels: map[string]string{
  2037. "env": "staging",
  2038. },
  2039. },
  2040. },
  2041. }
  2042. tc.pushsecret.Spec.Data = nil
  2043. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2044. {
  2045. StoreRef: &v1alpha1.PushSecretStoreRef{
  2046. LabelSelector: &metav1.LabelSelector{
  2047. MatchLabels: map[string]string{
  2048. "env": "production",
  2049. },
  2050. },
  2051. },
  2052. },
  2053. }
  2054. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2055. Eventually(func() bool {
  2056. cond := GetPushSecretCondition(ps.Status.Conditions, v1alpha1.PushSecretReady)
  2057. if cond == nil {
  2058. return false
  2059. }
  2060. return cond.Status == v1.ConditionFalse && cond.Reason == v1alpha1.ReasonErrored
  2061. }, time.Second*10, time.Second).Should(BeTrue())
  2062. return true
  2063. }
  2064. }
  2065. syncWithDataToLabelSelector := func(tc *testCase) {
  2066. fakeProvider.SetSecretFn = func() error {
  2067. return nil
  2068. }
  2069. // Add labels to the store
  2070. tc.store = &esv1.SecretStore{
  2071. ObjectMeta: metav1.ObjectMeta{
  2072. Name: PushSecretStore,
  2073. Namespace: PushSecretNamespace,
  2074. Labels: map[string]string{
  2075. "env": "test",
  2076. },
  2077. },
  2078. TypeMeta: metav1.TypeMeta{
  2079. Kind: "SecretStore",
  2080. },
  2081. Spec: esv1.SecretStoreSpec{
  2082. Provider: &esv1.SecretStoreProvider{
  2083. Fake: &esv1.FakeProvider{
  2084. Data: []esv1.FakeProviderData{},
  2085. },
  2086. },
  2087. },
  2088. }
  2089. tc.secret.Data = map[string][]byte{
  2090. "key1": []byte("value1"),
  2091. }
  2092. // Use labelSelector in secretStoreRefs to select stores by labels
  2093. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  2094. {
  2095. LabelSelector: &metav1.LabelSelector{
  2096. MatchLabels: map[string]string{
  2097. "env": "test",
  2098. },
  2099. },
  2100. },
  2101. }
  2102. // Use labelSelector in dataTo to target stores with matching labels
  2103. tc.pushsecret.Spec.Data = nil
  2104. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2105. {
  2106. StoreRef: &v1alpha1.PushSecretStoreRef{
  2107. LabelSelector: &metav1.LabelSelector{
  2108. MatchLabels: map[string]string{
  2109. "env": "test",
  2110. },
  2111. },
  2112. },
  2113. },
  2114. }
  2115. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2116. Eventually(func() bool {
  2117. By("checking key was pushed via labelSelector")
  2118. setSecretArgs := fakeProvider.GetPushSecretData()
  2119. return len(setSecretArgs) == 1
  2120. }, time.Second*10, time.Second).Should(BeTrue())
  2121. return true
  2122. }
  2123. }
  2124. syncWithDataToDuplicateValues := func(tc *testCase) {
  2125. fakeProvider.SetSecretFn = func() error {
  2126. return nil
  2127. }
  2128. // Keys with same value - tests deterministic key mapping
  2129. tc.secret.Data = map[string][]byte{
  2130. testDBHost: []byte("same-value"),
  2131. "cache-host": []byte("same-value"),
  2132. }
  2133. tc.pushsecret.Spec.Data = nil
  2134. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2135. {
  2136. StoreRef: &v1alpha1.PushSecretStoreRef{
  2137. Name: PushSecretStore,
  2138. },
  2139. Rewrite: []v1alpha1.PushSecretRewrite{
  2140. {
  2141. Regexp: &esv1.ExternalSecretRewriteRegexp{
  2142. Source: "-host$",
  2143. Target: "/endpoint",
  2144. },
  2145. },
  2146. },
  2147. },
  2148. }
  2149. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2150. Eventually(func() bool {
  2151. By("checking both keys are rewritten despite same value")
  2152. setSecretArgs := fakeProvider.GetPushSecretData()
  2153. if len(setSecretArgs) != 2 {
  2154. return false
  2155. }
  2156. _, hasDb := setSecretArgs["db/endpoint"]
  2157. _, hasCache := setSecretArgs["cache/endpoint"]
  2158. return hasDb && hasCache
  2159. }, time.Second*10, time.Second).Should(BeTrue())
  2160. return true
  2161. }
  2162. }
  2163. syncWithDataToMultipleRewrites := func(tc *testCase) {
  2164. fakeProvider.SetSecretFn = func() error {
  2165. return nil
  2166. }
  2167. tc.secret.Data = map[string][]byte{
  2168. "db-username": []byte(testAdminUser),
  2169. }
  2170. // Chain multiple rewrites
  2171. tc.pushsecret.Spec.Data = nil
  2172. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2173. {
  2174. StoreRef: &v1alpha1.PushSecretStoreRef{
  2175. Name: PushSecretStore,
  2176. },
  2177. Rewrite: []v1alpha1.PushSecretRewrite{
  2178. {
  2179. // First rewrite: remove db- prefix
  2180. Regexp: &esv1.ExternalSecretRewriteRegexp{
  2181. Source: "^db-",
  2182. Target: "",
  2183. },
  2184. },
  2185. {
  2186. // Second rewrite: add app/ prefix
  2187. Regexp: &esv1.ExternalSecretRewriteRegexp{
  2188. Source: "^",
  2189. Target: "app/",
  2190. },
  2191. },
  2192. },
  2193. },
  2194. }
  2195. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2196. Eventually(func() bool {
  2197. By("checking if multiple rewrites were applied")
  2198. setSecretArgs := fakeProvider.GetPushSecretData()
  2199. if len(setSecretArgs) != 1 {
  2200. return false
  2201. }
  2202. // db-username -> username -> app/username
  2203. _, ok := setSecretArgs["app/username"]
  2204. return ok
  2205. }, time.Second*10, time.Second).Should(BeTrue())
  2206. return true
  2207. }
  2208. }
  2209. // dataTo bundle mode tests (remoteKey set → all matched keys bundled as JSON)
  2210. syncWithDataToBundleAllKeys := func(tc *testCase) {
  2211. fakeProvider.SetSecretFn = func() error {
  2212. return nil
  2213. }
  2214. tc.secret.Data = map[string][]byte{
  2215. "DB_HOST": []byte(testLocalhost),
  2216. "DB_USER": []byte(testAdminUser),
  2217. }
  2218. tc.pushsecret.Spec.Data = nil
  2219. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2220. {
  2221. StoreRef: &v1alpha1.PushSecretStoreRef{
  2222. Name: PushSecretStore,
  2223. },
  2224. RemoteKey: "secrets-sync-target",
  2225. },
  2226. }
  2227. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2228. Eventually(func() bool {
  2229. By("checking that all keys were bundled into a single provider secret")
  2230. setSecretArgs := fakeProvider.GetPushSecretData()
  2231. // Only one provider secret should be created
  2232. if len(setSecretArgs) != 1 {
  2233. return false
  2234. }
  2235. if bundled, ok := setSecretArgs["secrets-sync-target"]; ok {
  2236. // Value should be a JSON object containing both keys
  2237. var decoded map[string]string
  2238. if json.Unmarshal(bundled.Value, &decoded) != nil {
  2239. return false
  2240. }
  2241. return decoded["DB_HOST"] == testLocalhost && decoded["DB_USER"] == testAdminUser
  2242. }
  2243. return false
  2244. }, time.Second*10, time.Second).Should(BeTrue())
  2245. return true
  2246. }
  2247. }
  2248. syncWithDataToBundleWithRegexFilter := func(tc *testCase) {
  2249. fakeProvider.SetSecretFn = func() error {
  2250. return nil
  2251. }
  2252. tc.secret.Data = map[string][]byte{
  2253. "DB_HOST": []byte(testLocalhost),
  2254. "DB_USER": []byte(testAdminUser),
  2255. "APP_NAME": []byte("myapp"),
  2256. }
  2257. tc.pushsecret.Spec.Data = nil
  2258. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2259. {
  2260. StoreRef: &v1alpha1.PushSecretStoreRef{
  2261. Name: PushSecretStore,
  2262. },
  2263. RemoteKey: "db-bundle",
  2264. Match: &v1alpha1.PushSecretDataToMatch{
  2265. RegExp: "^DB_",
  2266. },
  2267. },
  2268. }
  2269. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2270. Eventually(func() bool {
  2271. By("checking that only matched keys were bundled")
  2272. setSecretArgs := fakeProvider.GetPushSecretData()
  2273. if len(setSecretArgs) != 1 {
  2274. return false
  2275. }
  2276. if bundled, ok := setSecretArgs["db-bundle"]; ok {
  2277. var decoded map[string]string
  2278. if json.Unmarshal(bundled.Value, &decoded) != nil {
  2279. return false
  2280. }
  2281. // Only DB_* keys should be in the bundle, not APP_NAME
  2282. _, hasApp := decoded["APP_NAME"]
  2283. return decoded["DB_HOST"] == testLocalhost &&
  2284. decoded["DB_USER"] == testAdminUser &&
  2285. !hasApp
  2286. }
  2287. return false
  2288. }, time.Second*10, time.Second).Should(BeTrue())
  2289. return true
  2290. }
  2291. }
  2292. syncWithDataToBundleAndPerKeyMixed := func(tc *testCase) {
  2293. fakeProvider.SetSecretFn = func() error {
  2294. return nil
  2295. }
  2296. tc.secret.Data = map[string][]byte{
  2297. "DB_HOST": []byte(testLocalhost),
  2298. "DB_USER": []byte(testAdminUser),
  2299. "API_KEY": []byte("secret-key"),
  2300. }
  2301. tc.pushsecret.Spec.Data = nil
  2302. tc.pushsecret.Spec.DataTo = []v1alpha1.PushSecretDataTo{
  2303. {
  2304. // Bundle mode: DB_* keys → single JSON secret
  2305. StoreRef: &v1alpha1.PushSecretStoreRef{
  2306. Name: PushSecretStore,
  2307. },
  2308. RemoteKey: "db-config",
  2309. Match: &v1alpha1.PushSecretDataToMatch{
  2310. RegExp: "^DB_",
  2311. },
  2312. },
  2313. {
  2314. // Per-key mode: API_KEY → individual secret
  2315. StoreRef: &v1alpha1.PushSecretStoreRef{
  2316. Name: PushSecretStore,
  2317. },
  2318. Match: &v1alpha1.PushSecretDataToMatch{
  2319. RegExp: "^API_",
  2320. },
  2321. },
  2322. }
  2323. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2324. Eventually(func() bool {
  2325. By("checking bundle and per-key entries coexist")
  2326. setSecretArgs := fakeProvider.GetPushSecretData()
  2327. // db-config (bundle) + API_KEY (per-key) = 2 provider secrets
  2328. if len(setSecretArgs) != 2 {
  2329. return false
  2330. }
  2331. if bundled, ok := setSecretArgs["db-config"]; ok {
  2332. var decoded map[string]string
  2333. if json.Unmarshal(bundled.Value, &decoded) != nil {
  2334. return false
  2335. }
  2336. if decoded["DB_HOST"] != testLocalhost || decoded["DB_USER"] != testAdminUser {
  2337. return false
  2338. }
  2339. _, hasAPIKey := setSecretArgs["API_KEY"]
  2340. return hasAPIKey
  2341. }
  2342. return false
  2343. }, time.Second*10, time.Second).Should(BeTrue())
  2344. return true
  2345. }
  2346. }
  2347. DescribeTable("When reconciling a PushSecret",
  2348. func(tweaks ...testTweaks) {
  2349. tc := makeDefaultTestcase()
  2350. for _, tweak := range tweaks {
  2351. tweak(tc)
  2352. }
  2353. ctx := context.Background()
  2354. By("creating a secret store, secret and pushsecret")
  2355. if tc.store != nil {
  2356. Expect(k8sClient.Create(ctx, tc.store)).To(Succeed())
  2357. }
  2358. if tc.managedStore2 != nil {
  2359. Expect(k8sClient.Create(ctx, tc.managedStore2)).To(Succeed())
  2360. }
  2361. if tc.secret != nil {
  2362. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  2363. }
  2364. if tc.pushsecret != nil {
  2365. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  2366. }
  2367. time.Sleep(2 * time.Second) // prevents race conditions during tests causing failures
  2368. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  2369. createdPS := &v1alpha1.PushSecret{}
  2370. By("checking the pushSecret condition")
  2371. Eventually(func() bool {
  2372. err := k8sClient.Get(ctx, psKey, createdPS)
  2373. if err != nil {
  2374. return false
  2375. }
  2376. return tc.assert(createdPS, tc.secret)
  2377. }, timeout, interval).Should(BeTrue())
  2378. // this must be optional so we can test faulty es configuration
  2379. },
  2380. Entry("should sync", syncSuccessfully),
  2381. Entry("should not update existing secret if UpdatePolicy=IfNotExists", updateIfNotExists),
  2382. Entry("should only update parts of secret that don't already exist if UpdatePolicy=IfNotExists", updateIfNotExistsPartialSecrets),
  2383. Entry("should update the PushSecret status correctly if UpdatePolicy=IfNotExists", updateIfNotExistsSyncStatus),
  2384. Entry("should fail if secret existence cannot be verified if UpdatePolicy=IfNotExists", updateIfNotExistsSyncFailed),
  2385. Entry("should sync with template", syncSuccessfullyWithTemplate),
  2386. Entry("should sync with template reusing keys", syncSuccessfullyReusingKeys),
  2387. Entry("should sync with conversion strategy", syncSuccessfullyWithConversionStrategy),
  2388. Entry("should delete if DeletionPolicy=Delete", syncAndDeleteSuccessfully),
  2389. Entry("should delete secrets with properties and update status correctly", syncAndDeleteWithProperties),
  2390. Entry("should delete after DeletionPolicy changed from Delete to None", syncChangePolicyAndDeleteSuccessfully),
  2391. Entry("should cleanup provider secrets when source Secret is deleted", deleteProviderSecretsOnSourceSecretDeleted),
  2392. Entry("should track deletion tasks if Delete fails", failDelete),
  2393. Entry("should track deleted stores if Delete fails", failDeleteStore),
  2394. Entry("should delete all secrets if SecretStore changes", deleteWholeStore),
  2395. Entry("should sync to stores matching labels", syncMatchingLabels),
  2396. Entry("should sync with ClusterStore", syncWithClusterStore),
  2397. Entry("should sync with ClusterStore matching labels", syncWithClusterStoreMatchingLabels),
  2398. Entry("should sync with Generator", syncWithGenerator),
  2399. Entry("should fail if Secret is not created", failNoSecret),
  2400. Entry("should fail if Secret Key does not exist", failNoSecretKey),
  2401. Entry("should fail if SetSecret fails", setSecretFail),
  2402. Entry("should fail if no valid SecretStore", failNoSecretStore),
  2403. Entry("should fail if no valid ClusterSecretStore", failNoClusterStore),
  2404. Entry("should fail if NewClient fails", newClientFail),
  2405. Entry("should not sync to SecretStore in different namespace", secretStoreDifferentNamespace),
  2406. Entry("should not reference secret in different namespace", secretDifferentNamespace),
  2407. Entry("should sync with dataTo matching all keys", syncWithDataToMatchAll),
  2408. Entry("should sync with dataTo using regex pattern", syncWithDataToRegex),
  2409. Entry("should sync with dataTo and regexp rewrite", syncWithDataToRegexpRewrite),
  2410. Entry("should sync with dataTo and transform rewrite", syncWithDataToTransformRewrite),
  2411. Entry("should override dataTo with explicit data", syncDataToWithDataOverride),
  2412. Entry("should sync with dataTo and multiple chained rewrites", syncWithDataToMultipleRewrites),
  2413. Entry("should fail with invalid regex in dataTo", failDataToInvalidRegex),
  2414. Entry("should sync with dataTo and conversion strategy", syncWithDataToConversionStrategy),
  2415. Entry("should sync with dataTo storeRef targeting specific stores", syncWithDataToStoreRef),
  2416. Entry("should match dataTo against template-created keys", templateCreatesKeysThenDataToMatches),
  2417. Entry("should combine template, dataTo and explicit data", templateWithDataToAndExplicitData),
  2418. Entry("should fail with duplicate remote keys across dataTo entries", failDataToDuplicateAcrossEntries),
  2419. Entry("should fail with duplicate remote keys between dataTo and explicit data", failDataToAndDataDuplicateRemoteKey),
  2420. Entry("should fail with dataTo storeRef not in secretStoreRefs", failDataToStoreRefNotInList),
  2421. Entry("should fail with named dataTo storeRef when secretStoreRefs only has labelSelector", failDataToNamedStoreRefWithLabelSelectorRefs),
  2422. Entry("should fail with dataTo labelSelector matching no resolved store", failDataToLabelSelectorMatchesNoStore),
  2423. Entry("should sync with dataTo using labelSelector", syncWithDataToLabelSelector),
  2424. Entry("should sync with dataTo when keys have duplicate values", syncWithDataToDuplicateValues),
  2425. Entry("should bundle all keys into single provider secret with dataTo remoteKey", syncWithDataToBundleAllKeys),
  2426. Entry("should bundle only regex-matched keys with dataTo remoteKey and match filter", syncWithDataToBundleWithRegexFilter),
  2427. Entry("should mix bundle mode and per-key mode in the same dataTo list", syncWithDataToBundleAndPerKeyMixed),
  2428. )
  2429. It("should reject dataTo with both remoteKey and rewrite at admission", func() {
  2430. ns, err := ctest.CreateNamespace("test-ns", k8sClient)
  2431. Expect(err).ToNot(HaveOccurred())
  2432. ps := &v1alpha1.PushSecret{
  2433. ObjectMeta: metav1.ObjectMeta{
  2434. Name: PushSecretName,
  2435. Namespace: ns,
  2436. },
  2437. Spec: v1alpha1.PushSecretSpec{
  2438. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  2439. {Name: PushSecretStore},
  2440. },
  2441. Selector: v1alpha1.PushSecretSelector{
  2442. Secret: &v1alpha1.PushSecretSecret{Name: SecretName},
  2443. },
  2444. DataTo: []v1alpha1.PushSecretDataTo{
  2445. {
  2446. StoreRef: &v1alpha1.PushSecretStoreRef{
  2447. Name: PushSecretStore,
  2448. },
  2449. RemoteKey: "my-bundle",
  2450. Rewrite: []v1alpha1.PushSecretRewrite{
  2451. {
  2452. Regexp: &esv1.ExternalSecretRewriteRegexp{
  2453. Source: "^",
  2454. Target: "prefix/",
  2455. },
  2456. },
  2457. },
  2458. },
  2459. },
  2460. },
  2461. }
  2462. err = k8sClient.Create(context.Background(), ps)
  2463. Expect(err).To(HaveOccurred())
  2464. Expect(err.Error()).To(ContainSubstring("remoteKey and rewrite are mutually exclusive"))
  2465. })
  2466. })
  2467. var _ = Describe("PushSecret Controller Un/Managed Stores", func() {
  2468. const (
  2469. PushSecretName = "test-ps"
  2470. ManagedPushSecretStore1 = "test-managed-store-1"
  2471. ManagedPushSecretStore2 = "test-managed-store-2"
  2472. UnmanagedPushSecretStore1 = "test-unmanaged-store-1"
  2473. UnmanagedPushSecretStore2 = "test-unmanaged-store-2"
  2474. SecretName = "test-secret"
  2475. )
  2476. var PushSecretNamespace string
  2477. PushSecretStores := []string{ManagedPushSecretStore1, ManagedPushSecretStore2, UnmanagedPushSecretStore1, UnmanagedPushSecretStore2}
  2478. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  2479. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  2480. if t, err := strconv.Atoi(customTimeout); err == nil {
  2481. timeout = time.Second * time.Duration(t)
  2482. }
  2483. }
  2484. BeforeEach(func() {
  2485. var err error
  2486. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  2487. Expect(err).ToNot(HaveOccurred())
  2488. fakeProvider.Reset()
  2489. })
  2490. AfterEach(func() {
  2491. k8sClient.Delete(context.Background(), &v1alpha1.PushSecret{
  2492. ObjectMeta: metav1.ObjectMeta{
  2493. Name: PushSecretName,
  2494. Namespace: PushSecretNamespace,
  2495. },
  2496. })
  2497. // give a time for reconciler to remove finalizers before removing SecretStores
  2498. time.Sleep(2 * time.Second)
  2499. for _, psstore := range PushSecretStores {
  2500. k8sClient.Delete(context.Background(), &esv1.SecretStore{
  2501. ObjectMeta: metav1.ObjectMeta{
  2502. Name: psstore,
  2503. Namespace: PushSecretNamespace,
  2504. },
  2505. })
  2506. k8sClient.Delete(context.Background(), &esv1.ClusterSecretStore{
  2507. ObjectMeta: metav1.ObjectMeta{
  2508. Name: psstore,
  2509. },
  2510. })
  2511. }
  2512. k8sClient.Delete(context.Background(), &v1.Secret{
  2513. ObjectMeta: metav1.ObjectMeta{
  2514. Name: SecretName,
  2515. Namespace: PushSecretNamespace,
  2516. },
  2517. })
  2518. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  2519. ObjectMeta: metav1.ObjectMeta{
  2520. Name: PushSecretNamespace,
  2521. },
  2522. })).To(Succeed())
  2523. })
  2524. const (
  2525. defaultKey = "key"
  2526. defaultVal = "value"
  2527. defaultPath = "path/to/key"
  2528. )
  2529. makeDefaultTestcase := func() *testCase {
  2530. return &testCase{
  2531. pushsecret: &v1alpha1.PushSecret{
  2532. ObjectMeta: metav1.ObjectMeta{
  2533. Name: PushSecretName,
  2534. Namespace: PushSecretNamespace,
  2535. },
  2536. Spec: v1alpha1.PushSecretSpec{
  2537. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  2538. {
  2539. Name: ManagedPushSecretStore1,
  2540. Kind: "SecretStore",
  2541. },
  2542. },
  2543. Selector: v1alpha1.PushSecretSelector{
  2544. Secret: &v1alpha1.PushSecretSecret{
  2545. Name: SecretName,
  2546. },
  2547. },
  2548. Data: []v1alpha1.PushSecretData{
  2549. {
  2550. Match: v1alpha1.PushSecretMatch{
  2551. SecretKey: defaultKey,
  2552. RemoteRef: v1alpha1.PushSecretRemoteRef{
  2553. RemoteKey: defaultPath,
  2554. },
  2555. },
  2556. },
  2557. },
  2558. },
  2559. },
  2560. secret: &v1.Secret{
  2561. ObjectMeta: metav1.ObjectMeta{
  2562. Name: SecretName,
  2563. Namespace: PushSecretNamespace,
  2564. },
  2565. Data: map[string][]byte{
  2566. defaultKey: []byte(defaultVal),
  2567. },
  2568. },
  2569. managedStore1: &esv1.SecretStore{
  2570. ObjectMeta: metav1.ObjectMeta{
  2571. Name: ManagedPushSecretStore1,
  2572. Namespace: PushSecretNamespace,
  2573. },
  2574. TypeMeta: metav1.TypeMeta{
  2575. Kind: "SecretStore",
  2576. },
  2577. Spec: esv1.SecretStoreSpec{
  2578. Provider: &esv1.SecretStoreProvider{
  2579. Fake: &esv1.FakeProvider{
  2580. Data: []esv1.FakeProviderData{},
  2581. },
  2582. },
  2583. },
  2584. },
  2585. managedStore2: &esv1.SecretStore{
  2586. ObjectMeta: metav1.ObjectMeta{
  2587. Name: ManagedPushSecretStore2,
  2588. Namespace: PushSecretNamespace,
  2589. },
  2590. TypeMeta: metav1.TypeMeta{
  2591. Kind: "SecretStore",
  2592. },
  2593. Spec: esv1.SecretStoreSpec{
  2594. Provider: &esv1.SecretStoreProvider{
  2595. Fake: &esv1.FakeProvider{
  2596. Data: []esv1.FakeProviderData{},
  2597. },
  2598. },
  2599. },
  2600. },
  2601. unmanagedStore1: &esv1.SecretStore{
  2602. ObjectMeta: metav1.ObjectMeta{
  2603. Name: UnmanagedPushSecretStore1,
  2604. Namespace: PushSecretNamespace,
  2605. },
  2606. TypeMeta: metav1.TypeMeta{
  2607. Kind: "SecretStore",
  2608. },
  2609. Spec: esv1.SecretStoreSpec{
  2610. Provider: &esv1.SecretStoreProvider{
  2611. Fake: &esv1.FakeProvider{
  2612. Data: []esv1.FakeProviderData{},
  2613. },
  2614. },
  2615. Controller: "not-managed",
  2616. },
  2617. },
  2618. unmanagedStore2: &esv1.SecretStore{
  2619. ObjectMeta: metav1.ObjectMeta{
  2620. Name: UnmanagedPushSecretStore2,
  2621. Namespace: PushSecretNamespace,
  2622. },
  2623. TypeMeta: metav1.TypeMeta{
  2624. Kind: "SecretStore",
  2625. },
  2626. Spec: esv1.SecretStoreSpec{
  2627. Provider: &esv1.SecretStoreProvider{
  2628. Fake: &esv1.FakeProvider{
  2629. Data: []esv1.FakeProviderData{},
  2630. },
  2631. },
  2632. Controller: "not-managed",
  2633. },
  2634. },
  2635. }
  2636. }
  2637. multipleManagedStoresSyncsSuccessfully := func(tc *testCase) {
  2638. fakeProvider.SetSecretFn = func() error {
  2639. return nil
  2640. }
  2641. tc.pushsecret.Spec.SecretStoreRefs = append(tc.pushsecret.Spec.SecretStoreRefs,
  2642. v1alpha1.PushSecretStoreRef{
  2643. Name: ManagedPushSecretStore2,
  2644. Kind: "SecretStore",
  2645. },
  2646. )
  2647. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2648. Eventually(func() bool {
  2649. By("checking if Provider value got updated")
  2650. secretValue := secret.Data[defaultKey]
  2651. setSecretArgs := fakeProvider.GetPushSecretData()
  2652. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  2653. if !ok {
  2654. return false
  2655. }
  2656. got := providerValue.Value
  2657. return bytes.Equal(got, secretValue)
  2658. }, time.Second*10, time.Second).Should(BeTrue())
  2659. return true
  2660. }
  2661. }
  2662. skipUnmanagedStores := func(tc *testCase) {
  2663. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  2664. {
  2665. Name: UnmanagedPushSecretStore1,
  2666. Kind: "SecretStore",
  2667. },
  2668. {
  2669. Name: UnmanagedPushSecretStore2,
  2670. Kind: "SecretStore",
  2671. },
  2672. }
  2673. tc.assert = func(ps *v1alpha1.PushSecret, _ *v1.Secret) bool {
  2674. return len(ps.Status.Conditions) == 0
  2675. }
  2676. }
  2677. warnUnmanagedStoresAndSyncManagedStores := func(tc *testCase) {
  2678. fakeProvider.SetSecretFn = func() error {
  2679. return nil
  2680. }
  2681. tc.pushsecret.Spec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  2682. {
  2683. Name: ManagedPushSecretStore1,
  2684. Kind: "SecretStore",
  2685. },
  2686. {
  2687. Name: ManagedPushSecretStore2,
  2688. Kind: "SecretStore",
  2689. },
  2690. {
  2691. Name: UnmanagedPushSecretStore1,
  2692. Kind: "SecretStore",
  2693. },
  2694. {
  2695. Name: UnmanagedPushSecretStore2,
  2696. Kind: "SecretStore",
  2697. },
  2698. }
  2699. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  2700. Eventually(func() bool {
  2701. By("checking if Provider value got updated")
  2702. secretValue := secret.Data[defaultKey]
  2703. setSecretArgs := fakeProvider.GetPushSecretData()
  2704. providerValue, ok := setSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey]
  2705. if !ok {
  2706. return false
  2707. }
  2708. got := providerValue.Value
  2709. return bytes.Equal(got, secretValue)
  2710. }, time.Second*10, time.Second).Should(BeTrue())
  2711. return true
  2712. }
  2713. }
  2714. DescribeTable("When reconciling a PushSecret with multiple secret stores",
  2715. func(tweaks ...testTweaks) {
  2716. tc := makeDefaultTestcase()
  2717. for _, tweak := range tweaks {
  2718. tweak(tc)
  2719. }
  2720. ctx := context.Background()
  2721. By("creating secret stores, a secret and a pushsecret")
  2722. if tc.managedStore1 != nil {
  2723. Expect(k8sClient.Create(ctx, tc.managedStore1)).To(Succeed())
  2724. }
  2725. if tc.managedStore2 != nil {
  2726. Expect(k8sClient.Create(ctx, tc.managedStore2)).To(Succeed())
  2727. }
  2728. if tc.unmanagedStore1 != nil {
  2729. Expect(k8sClient.Create(ctx, tc.unmanagedStore1)).To(Succeed())
  2730. }
  2731. if tc.unmanagedStore2 != nil {
  2732. Expect(k8sClient.Create(ctx, tc.unmanagedStore2)).To(Succeed())
  2733. }
  2734. if tc.secret != nil {
  2735. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  2736. }
  2737. if tc.pushsecret != nil {
  2738. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  2739. }
  2740. time.Sleep(2 * time.Second) // prevents race conditions during tests causing failures
  2741. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  2742. createdPS := &v1alpha1.PushSecret{}
  2743. By("checking the pushSecret condition")
  2744. Eventually(func() bool {
  2745. err := k8sClient.Get(ctx, psKey, createdPS)
  2746. if err != nil {
  2747. return false
  2748. }
  2749. return tc.assert(createdPS, tc.secret)
  2750. }, timeout, interval).Should(BeTrue())
  2751. // this must be optional so we can test faulty es configuration
  2752. },
  2753. Entry("should sync successfully if there are multiple managed stores", multipleManagedStoresSyncsSuccessfully),
  2754. Entry("should skip unmanaged stores", skipUnmanagedStores),
  2755. Entry("should skip unmanaged stores and sync managed stores", warnUnmanagedStoresAndSyncManagedStores),
  2756. )
  2757. })
  2758. var _ = Describe("mergeDataEntries unit tests", func() {
  2759. Describe("resolveSourceKeyConflicts", func() {
  2760. It("should let explicit data override dataTo for same source key", func() {
  2761. secret := &v1.Secret{Data: map[string][]byte{"foo": []byte("v1"), "bar": []byte("v2")}}
  2762. dataTo := []v1alpha1.PushSecretData{
  2763. {Match: v1alpha1.PushSecretMatch{SecretKey: "foo", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "dataTo/foo"}}},
  2764. {Match: v1alpha1.PushSecretMatch{SecretKey: "bar", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "dataTo/bar"}}},
  2765. }
  2766. explicit := []v1alpha1.PushSecretData{
  2767. {Match: v1alpha1.PushSecretMatch{SecretKey: "foo", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "explicit/foo"}}},
  2768. }
  2769. result := resolveSourceKeyConflicts(dataTo, explicit, secret)
  2770. Expect(result).To(HaveLen(2))
  2771. keys := make(map[string]string)
  2772. for _, d := range result {
  2773. keys[d.GetSecretKey()] = d.GetRemoteKey()
  2774. }
  2775. Expect(keys["bar"]).To(Equal("dataTo/bar"))
  2776. Expect(keys["foo"]).To(Equal("explicit/foo"))
  2777. })
  2778. It("should keep all entries when no conflicts", func() {
  2779. secret := &v1.Secret{Data: map[string][]byte{"a": []byte("v1"), "b": []byte("v2")}}
  2780. dataTo := []v1alpha1.PushSecretData{
  2781. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "a"}}},
  2782. }
  2783. explicit := []v1alpha1.PushSecretData{
  2784. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "b"}}},
  2785. }
  2786. result := resolveSourceKeyConflicts(dataTo, explicit, secret)
  2787. Expect(result).To(HaveLen(2))
  2788. })
  2789. It("should handle empty dataTo", func() {
  2790. secret := &v1.Secret{Data: map[string][]byte{"x": []byte("v1")}}
  2791. explicit := []v1alpha1.PushSecretData{
  2792. {Match: v1alpha1.PushSecretMatch{SecretKey: "x", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "x"}}},
  2793. }
  2794. result := resolveSourceKeyConflicts(nil, explicit, secret)
  2795. Expect(result).To(HaveLen(1))
  2796. Expect(result[0].GetSecretKey()).To(Equal("x"))
  2797. })
  2798. It("should handle empty explicit", func() {
  2799. secret := &v1.Secret{Data: map[string][]byte{"y": []byte("v1")}}
  2800. dataTo := []v1alpha1.PushSecretData{
  2801. {Match: v1alpha1.PushSecretMatch{SecretKey: "y", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "y"}}},
  2802. }
  2803. result := resolveSourceKeyConflicts(dataTo, nil, secret)
  2804. Expect(result).To(HaveLen(1))
  2805. Expect(result[0].GetSecretKey()).To(Equal("y"))
  2806. })
  2807. It("should resolve conflicts when explicit data uses ConversionStrategy", func() {
  2808. secret := &v1.Secret{Data: map[string][]byte{
  2809. "some_U002D_key": []byte("value"),
  2810. "other": []byte("other-value"),
  2811. }}
  2812. dataTo := []v1alpha1.PushSecretData{
  2813. {Match: v1alpha1.PushSecretMatch{SecretKey: "some_U002D_key", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "dataTo/some-key"}}},
  2814. {Match: v1alpha1.PushSecretMatch{SecretKey: "other", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "dataTo/other"}}},
  2815. }
  2816. explicit := []v1alpha1.PushSecretData{
  2817. {
  2818. ConversionStrategy: v1alpha1.PushSecretConversionReverseUnicode,
  2819. Match: v1alpha1.PushSecretMatch{SecretKey: "some-key", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "explicit/some-key"}},
  2820. },
  2821. }
  2822. result := resolveSourceKeyConflicts(dataTo, explicit, secret)
  2823. Expect(result).To(HaveLen(2))
  2824. keys := make(map[string]string)
  2825. for _, d := range result {
  2826. keys[d.GetSecretKey()] = d.GetRemoteKey()
  2827. }
  2828. Expect(keys["other"]).To(Equal("dataTo/other"))
  2829. Expect(keys["some-key"]).To(Equal("explicit/some-key"))
  2830. })
  2831. })
  2832. Describe("validateRemoteKeyUniqueness", func() {
  2833. It("should pass for unique remote keys", func() {
  2834. entries := []v1alpha1.PushSecretData{
  2835. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "remote-a"}}},
  2836. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "remote-b"}}},
  2837. }
  2838. err := validateRemoteKeyUniqueness(entries)
  2839. Expect(err).ToNot(HaveOccurred())
  2840. })
  2841. It("should fail for duplicate remote keys", func() {
  2842. entries := []v1alpha1.PushSecretData{
  2843. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared"}}},
  2844. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared"}}},
  2845. }
  2846. err := validateRemoteKeyUniqueness(entries)
  2847. Expect(err).To(HaveOccurred())
  2848. Expect(err.Error()).To(ContainSubstring(testDuplicateRemoteKeyErr))
  2849. })
  2850. It("should pass for same remote key with different properties", func() {
  2851. entries := []v1alpha1.PushSecretData{
  2852. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared", Property: "field1"}}},
  2853. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared", Property: "field2"}}},
  2854. }
  2855. err := validateRemoteKeyUniqueness(entries)
  2856. Expect(err).ToNot(HaveOccurred())
  2857. })
  2858. It("should fail for same remote key and same property", func() {
  2859. entries := []v1alpha1.PushSecretData{
  2860. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared", Property: "field"}}},
  2861. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared", Property: "field"}}},
  2862. }
  2863. err := validateRemoteKeyUniqueness(entries)
  2864. Expect(err).To(HaveOccurred())
  2865. Expect(err.Error()).To(ContainSubstring(testDuplicateRemoteKeyErr))
  2866. })
  2867. })
  2868. Describe("mergeDataEntries", func() {
  2869. It("should merge valid entries", func() {
  2870. secret := &v1.Secret{Data: map[string][]byte{"a": []byte("v1"), "b": []byte("v2")}}
  2871. dataTo := []v1alpha1.PushSecretData{
  2872. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "a"}}},
  2873. }
  2874. explicit := []v1alpha1.PushSecretData{
  2875. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "b"}}},
  2876. }
  2877. result, err := mergeDataEntries(dataTo, explicit, secret)
  2878. Expect(err).ToNot(HaveOccurred())
  2879. Expect(result).To(HaveLen(2))
  2880. })
  2881. It("should override dataTo with explicit for same source key", func() {
  2882. secret := &v1.Secret{Data: map[string][]byte{"key": []byte("v1")}}
  2883. dataTo := []v1alpha1.PushSecretData{
  2884. {Match: v1alpha1.PushSecretMatch{SecretKey: "key", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "dataTo-path"}}},
  2885. }
  2886. explicit := []v1alpha1.PushSecretData{
  2887. {Match: v1alpha1.PushSecretMatch{SecretKey: "key", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "explicit-path"}}},
  2888. }
  2889. result, err := mergeDataEntries(dataTo, explicit, secret)
  2890. Expect(err).ToNot(HaveOccurred())
  2891. Expect(result).To(HaveLen(1))
  2892. Expect(result[0].GetRemoteKey()).To(Equal("explicit-path"))
  2893. })
  2894. It("should fail for remote key conflict after merge", func() {
  2895. secret := &v1.Secret{Data: map[string][]byte{"a": []byte("v1"), "b": []byte("v2")}}
  2896. dataTo := []v1alpha1.PushSecretData{
  2897. {Match: v1alpha1.PushSecretMatch{SecretKey: "a", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared"}}},
  2898. }
  2899. explicit := []v1alpha1.PushSecretData{
  2900. {Match: v1alpha1.PushSecretMatch{SecretKey: "b", RemoteRef: v1alpha1.PushSecretRemoteRef{RemoteKey: "shared"}}},
  2901. }
  2902. _, err := mergeDataEntries(dataTo, explicit, secret)
  2903. Expect(err).To(HaveOccurred())
  2904. Expect(err.Error()).To(ContainSubstring(testDuplicateRemoteKeyErr))
  2905. })
  2906. })
  2907. })