pushsecret_controller_test.go 92 KB

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