externalsecret_controller_test.go 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668
  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 externalsecret
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "os"
  21. "strconv"
  22. "time"
  23. "github.com/google/go-cmp/cmp"
  24. "github.com/google/go-cmp/cmp/cmpopts"
  25. "github.com/onsi/gomega/format"
  26. "github.com/prometheus/client_golang/prometheus"
  27. dto "github.com/prometheus/client_model/go"
  28. v1 "k8s.io/api/core/v1"
  29. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  30. apierrors "k8s.io/apimachinery/pkg/api/errors"
  31. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  32. "k8s.io/apimachinery/pkg/types"
  33. "sigs.k8s.io/controller-runtime/pkg/client"
  34. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  35. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  36. ctest "github.com/external-secrets/external-secrets/pkg/controllers/commontest"
  37. "github.com/external-secrets/external-secrets/pkg/controllers/externalsecret/esmetrics"
  38. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  39. ctrlutil "github.com/external-secrets/external-secrets/pkg/controllers/util"
  40. "github.com/external-secrets/external-secrets/runtime/esutils"
  41. "github.com/external-secrets/external-secrets/runtime/testing/fake"
  42. . "github.com/onsi/ginkgo/v2"
  43. . "github.com/onsi/gomega"
  44. )
  45. const (
  46. labelKey = "label-key"
  47. labelValue = "label-value"
  48. annotationKey = "annotation-key"
  49. annotationValue = "annotation-value"
  50. existingLabelKey = "existing-label-key"
  51. existingLabelValue = "existing-label-value"
  52. nullByteSecretVal = "A\x00B"
  53. )
  54. var (
  55. fakeProvider *fake.Client
  56. metric dto.Metric
  57. metricDuration dto.Metric
  58. timeout = time.Second * 10
  59. interval = time.Millisecond * 250
  60. )
  61. var (
  62. testSyncCallsTotal *prometheus.CounterVec
  63. testSyncCallsError *prometheus.CounterVec
  64. testExternalSecretCondition *prometheus.GaugeVec
  65. testExternalSecretReconcileDuration *prometheus.GaugeVec
  66. )
  67. type testCase struct {
  68. secretStore esv1.GenericStore
  69. externalSecret *esv1.ExternalSecret
  70. targetSecretName string
  71. // checkCondition should return true if the externalSecret
  72. // has the expected condition
  73. checkCondition func(*esv1.ExternalSecret) bool
  74. // checkExternalSecret is called after the condition has been verified
  75. // use this to verify the externalSecret
  76. checkExternalSecret func(*esv1.ExternalSecret)
  77. // optional. use this to test the secret value
  78. checkSecret func(*esv1.ExternalSecret, *v1.Secret)
  79. }
  80. func makeExternalSecret(policy esv1.ExternalSecretCreationPolicy) *esv1.ExternalSecret {
  81. return &esv1.ExternalSecret{
  82. Spec: esv1.ExternalSecretSpec{
  83. Target: esv1.ExternalSecretTarget{
  84. CreationPolicy: policy,
  85. },
  86. },
  87. }
  88. }
  89. type testTweaks func(*testCase)
  90. var _ = Describe("Kind=secret existence logic", func() {
  91. validData := map[string][]byte{
  92. "foo": []byte("value1"),
  93. "bar": []byte("value2"),
  94. }
  95. type testCase struct {
  96. Name string
  97. Input *v1.Secret
  98. ExternalSecret *esv1.ExternalSecret
  99. ExpectedOutput bool
  100. }
  101. tests := []testCase{
  102. {
  103. Name: "Should not be valid in case of missing uid",
  104. Input: &v1.Secret{},
  105. ExternalSecret: &esv1.ExternalSecret{},
  106. ExpectedOutput: false,
  107. },
  108. {
  109. Name: "A nil annotation should not be valid",
  110. Input: &v1.Secret{
  111. ObjectMeta: metav1.ObjectMeta{
  112. UID: "xxx",
  113. Labels: map[string]string{
  114. esv1.LabelManaged: esv1.LabelManagedValue,
  115. },
  116. Annotations: map[string]string{},
  117. },
  118. },
  119. ExternalSecret: makeExternalSecret(esv1.CreatePolicyOwner),
  120. ExpectedOutput: false,
  121. },
  122. {
  123. Name: "An invalid annotation hash should not be valid",
  124. Input: &v1.Secret{
  125. ObjectMeta: metav1.ObjectMeta{
  126. UID: "xxx",
  127. Labels: map[string]string{
  128. esv1.LabelManaged: esv1.LabelManagedValue,
  129. },
  130. Annotations: map[string]string{
  131. esv1.AnnotationDataHash: "xxxxxx",
  132. },
  133. },
  134. },
  135. ExternalSecret: makeExternalSecret(esv1.CreatePolicyOwner),
  136. ExpectedOutput: false,
  137. },
  138. {
  139. Name: "A valid secret should return true",
  140. Input: &v1.Secret{
  141. ObjectMeta: metav1.ObjectMeta{
  142. UID: "xxx",
  143. Labels: map[string]string{
  144. esv1.LabelManaged: esv1.LabelManagedValue,
  145. },
  146. Annotations: map[string]string{
  147. esv1.AnnotationDataHash: esutils.ObjectHash(validData),
  148. },
  149. },
  150. Data: validData,
  151. },
  152. ExternalSecret: makeExternalSecret(esv1.CreatePolicyOwner),
  153. ExpectedOutput: true,
  154. },
  155. {
  156. Name: "Ignore Annotations if creation policy is Orphan",
  157. Input: &v1.Secret{
  158. ObjectMeta: metav1.ObjectMeta{
  159. UID: "xxx",
  160. Labels: map[string]string{
  161. esv1.LabelManaged: esv1.LabelManagedValue,
  162. },
  163. Annotations: map[string]string{
  164. esv1.AnnotationDataHash: "xxxxxx",
  165. },
  166. },
  167. },
  168. ExternalSecret: makeExternalSecret(esv1.CreatePolicyOrphan),
  169. ExpectedOutput: true,
  170. },
  171. {
  172. Name: "Ignore missing UID Secret if creation policy is Orphan",
  173. Input: &v1.Secret{},
  174. ExternalSecret: makeExternalSecret(esv1.CreatePolicyOrphan),
  175. ExpectedOutput: true,
  176. },
  177. {
  178. Name: "Missing UID Secret is invalid with creation policy CreateOrMerge",
  179. Input: &v1.Secret{},
  180. ExternalSecret: makeExternalSecret(esv1.CreatePolicyCreateOrMerge),
  181. ExpectedOutput: false,
  182. },
  183. {
  184. Name: "A valid secret with creation policy CreateOrMerge should return true",
  185. Input: &v1.Secret{
  186. ObjectMeta: metav1.ObjectMeta{
  187. UID: "xxx",
  188. Labels: map[string]string{
  189. esv1.LabelManaged: esv1.LabelManagedValue,
  190. },
  191. Annotations: map[string]string{
  192. esv1.AnnotationDataHash: esutils.ObjectHash(validData),
  193. },
  194. },
  195. Data: validData,
  196. },
  197. ExternalSecret: makeExternalSecret(esv1.CreatePolicyCreateOrMerge),
  198. ExpectedOutput: true,
  199. },
  200. }
  201. for _, tt := range tests {
  202. It(tt.Name, func() {
  203. Expect(isSecretValid(tt.Input, tt.ExternalSecret)).To(BeEquivalentTo(tt.ExpectedOutput))
  204. })
  205. }
  206. })
  207. var _ = Describe("ExternalSecret controller", Serial, func() {
  208. const (
  209. ExternalSecretName = "test-es"
  210. ExternalSecretFQDN = "externalsecrets.external-secrets.io/test-es"
  211. ExternalSecretStore = "test-store"
  212. ExternalSecretTargetSecretName = "test-secret"
  213. FakeManager = "fake.manager"
  214. expectedSecretVal = "SOMEVALUE was templated"
  215. targetPropObj = "{{ .targetProperty | toString | upper }} was templated"
  216. FooValue = "map-foo-value"
  217. BarValue = "map-bar-value"
  218. NamespaceLabelKey = "css-test-label-key"
  219. NamespaceLabelValue = "css-test-label-value"
  220. )
  221. var ExternalSecretNamespace string
  222. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  223. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  224. if t, err := strconv.Atoi(customTimeout); err == nil {
  225. timeout = time.Second * time.Duration(t)
  226. }
  227. }
  228. BeforeEach(func() {
  229. var err error
  230. ExternalSecretNamespace, err = ctest.CreateNamespaceWithLabels("test-ns", k8sClient, map[string]string{NamespaceLabelKey: NamespaceLabelValue})
  231. Expect(err).ToNot(HaveOccurred())
  232. metric.Reset()
  233. testSyncCallsTotal.Reset()
  234. testSyncCallsError.Reset()
  235. testExternalSecretCondition.Reset()
  236. testExternalSecretReconcileDuration.Reset()
  237. fakeProvider.Reset()
  238. })
  239. AfterEach(
  240. func() {
  241. secretStore := &esv1.SecretStore{}
  242. secretStoreLookupKey := types.NamespacedName{
  243. Name: ExternalSecretStore,
  244. Namespace: ExternalSecretNamespace,
  245. }
  246. if err := k8sClient.Get(context.Background(), secretStoreLookupKey, secretStore); err == nil {
  247. Expect(k8sClient.Delete(context.Background(), secretStore)).To(Succeed())
  248. }
  249. clusterSecretStore := &esv1.ClusterSecretStore{}
  250. clusterSecretStoreLookupKey := types.NamespacedName{
  251. Name: ExternalSecretStore,
  252. }
  253. if err := k8sClient.Get(context.Background(), clusterSecretStoreLookupKey, clusterSecretStore); err == nil {
  254. Expect(k8sClient.Delete(context.Background(), clusterSecretStore)).To(Succeed())
  255. }
  256. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  257. ObjectMeta: metav1.ObjectMeta{
  258. Name: ExternalSecretNamespace,
  259. },
  260. })).To(Succeed())
  261. },
  262. )
  263. const (
  264. secretVal = "some-value"
  265. targetProp = "targetProperty"
  266. remoteKey = "barz"
  267. remoteProperty = "bang"
  268. existingKey = "pre-existing-key"
  269. existingVal = "pre-existing-value"
  270. )
  271. makeDefaultTestcase := func() *testCase {
  272. return &testCase{
  273. // default condition: es should be ready
  274. targetSecretName: ExternalSecretTargetSecretName,
  275. checkCondition: func(es *esv1.ExternalSecret) bool {
  276. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  277. if cond == nil || cond.Status != v1.ConditionTrue {
  278. return false
  279. }
  280. return true
  281. },
  282. checkExternalSecret: func(_ *esv1.ExternalSecret) {
  283. // noop by default
  284. },
  285. secretStore: &esv1.SecretStore{
  286. ObjectMeta: metav1.ObjectMeta{
  287. Name: ExternalSecretStore,
  288. Namespace: ExternalSecretNamespace,
  289. },
  290. Spec: esv1.SecretStoreSpec{
  291. Provider: &esv1.SecretStoreProvider{
  292. AWS: &esv1.AWSProvider{
  293. Service: esv1.AWSServiceSecretsManager,
  294. },
  295. },
  296. },
  297. },
  298. externalSecret: &esv1.ExternalSecret{
  299. ObjectMeta: metav1.ObjectMeta{
  300. Name: ExternalSecretName,
  301. Namespace: ExternalSecretNamespace,
  302. },
  303. Spec: esv1.ExternalSecretSpec{
  304. SecretStoreRef: esv1.SecretStoreRef{
  305. Name: ExternalSecretStore,
  306. },
  307. Target: esv1.ExternalSecretTarget{
  308. Name: ExternalSecretTargetSecretName,
  309. },
  310. Data: []esv1.ExternalSecretData{
  311. {
  312. SecretKey: targetProp,
  313. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  314. Key: remoteKey,
  315. Property: remoteProperty,
  316. },
  317. },
  318. },
  319. },
  320. },
  321. }
  322. }
  323. // if target Secret name is not specified it should use the ExternalSecret name.
  324. syncWithoutTargetName := func(tc *testCase) {
  325. tc.externalSecret.Spec.Target.Name = ""
  326. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  327. // check secret name
  328. Expect(secret.ObjectMeta.Name).To(Equal(ExternalSecretName))
  329. // check binding secret on external secret
  330. Expect(es.Status.Binding.Name).To(Equal(secret.ObjectMeta.Name))
  331. }
  332. }
  333. // if target Secret name is not specified it should use the ExternalSecret name.
  334. syncBigNames := func(tc *testCase) {
  335. tc.targetSecretName = "this-is-a-very-big-secret-name-that-wouldnt-be-generated-due-to-label-limits"
  336. tc.externalSecret.Spec.Target.Name = "this-is-a-very-big-secret-name-that-wouldnt-be-generated-due-to-label-limits"
  337. tc.checkSecret = func(es *esv1.ExternalSecret, _ *v1.Secret) {
  338. // check binding secret on external secret
  339. Expect(es.Status.Binding.Name).To(Equal(tc.externalSecret.Spec.Target.Name))
  340. }
  341. }
  342. // the secret name is reflected on the external secret's status as the binding secret
  343. syncBindingSecret := func(tc *testCase) {
  344. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  345. // check binding secret on external secret
  346. Expect(es.Status.Binding.Name).To(Equal(secret.ObjectMeta.Name))
  347. }
  348. }
  349. // their is no binding secret when a secret is not synced
  350. skipBindingSecret := func(tc *testCase) {
  351. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyNone
  352. tc.checkExternalSecret = func(es *esv1.ExternalSecret) {
  353. // check binding secret is not set
  354. Expect(es.Status.Binding.Name).To(BeEmpty())
  355. }
  356. }
  357. // labels and annotations from the Kind=ExternalSecret
  358. // should be copied over to the Kind=Secret
  359. syncLabelsAnnotations := func(tc *testCase) {
  360. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  361. labelKey: labelValue,
  362. }
  363. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  364. annotationKey: annotationValue,
  365. }
  366. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  367. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  368. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(labelKey, labelValue))
  369. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(annotationKey, annotationValue))
  370. // ownerRef must not be set!
  371. Expect(ctest.HasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeTrue())
  372. }
  373. }
  374. // labels and annotations from the ExternalSecret
  375. // should be merged to the Secret if exists
  376. mergeLabelsAnnotations := func(tc *testCase) {
  377. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  378. labelKey: labelValue,
  379. }
  380. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  381. annotationKey: annotationValue,
  382. }
  383. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  384. // Create a secret owned by another entity to test if the pre-existing metadata is preserved
  385. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  386. ObjectMeta: metav1.ObjectMeta{
  387. Name: ExternalSecretTargetSecretName,
  388. Namespace: ExternalSecretNamespace,
  389. Labels: map[string]string{
  390. existingLabelKey: existingLabelValue,
  391. },
  392. Annotations: map[string]string{
  393. "existing-annotation-key": "existing-annotation-value",
  394. },
  395. },
  396. }, client.FieldOwner(FakeManager))).To(Succeed())
  397. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  398. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(labelKey, labelValue))
  399. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(existingLabelKey, existingLabelValue))
  400. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(annotationKey, annotationValue))
  401. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue("existing-annotation-key", "existing-annotation-value"))
  402. }
  403. }
  404. removeOutdatedLabelsAnnotations := func(tc *testCase) {
  405. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  406. labelKey: labelValue,
  407. }
  408. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  409. annotationKey: annotationValue,
  410. }
  411. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  412. // Create a secret owned by the operator to test if the outdated pre-existing metadata is removed
  413. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  414. ObjectMeta: metav1.ObjectMeta{
  415. Name: ExternalSecretTargetSecretName,
  416. Namespace: ExternalSecretNamespace,
  417. Labels: map[string]string{
  418. existingLabelKey: existingLabelValue,
  419. },
  420. Annotations: map[string]string{
  421. "existing-annotation-key": "existing-annotation-value",
  422. },
  423. },
  424. }, client.FieldOwner(ExternalSecretFQDN))).To(Succeed())
  425. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  426. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(labelKey, labelValue))
  427. Expect(secret.ObjectMeta.Labels).NotTo(HaveKeyWithValue(existingLabelKey, existingLabelValue))
  428. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(annotationKey, annotationValue))
  429. Expect(secret.ObjectMeta.Annotations).NotTo(HaveKeyWithValue("existing-annotation-key", "existing-annotation-value"))
  430. }
  431. }
  432. checkPrometheusCounters := func(tc *testCase) {
  433. const secretVal = "someValue"
  434. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  435. tc.checkSecret = func(_ *esv1.ExternalSecret, _ *v1.Secret) {
  436. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  437. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  438. Eventually(func() bool {
  439. Expect(testSyncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  440. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  441. // three reconciliations: initial sync, status update, secret update
  442. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  443. }, timeout, interval).Should(BeTrue())
  444. }
  445. }
  446. // merge with existing secret using creationPolicy=Merge
  447. // it should NOT have a ownerReference
  448. // metadata.managedFields with the correct owner should be added to the secret
  449. mergeWithSecret := func(tc *testCase) {
  450. const secretVal = "someValue"
  451. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  452. tc.externalSecret.Labels = map[string]string{
  453. "es-label-key": "es-label-value",
  454. }
  455. tc.externalSecret.Annotations = map[string]string{
  456. "es-annotation-key": "es-annotation-value",
  457. }
  458. // create secret beforehand
  459. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  460. ObjectMeta: metav1.ObjectMeta{
  461. Name: ExternalSecretTargetSecretName,
  462. Namespace: ExternalSecretNamespace,
  463. Labels: map[string]string{
  464. existingLabelKey: existingLabelValue,
  465. },
  466. Annotations: map[string]string{
  467. "existing-annotation-key": "existing-annotation-value",
  468. },
  469. },
  470. Data: map[string][]byte{
  471. existingKey: []byte(existingVal),
  472. },
  473. }, client.FieldOwner(FakeManager))).To(Succeed())
  474. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  475. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  476. // check value
  477. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  478. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  479. Expect(secret.ObjectMeta.Labels).To(HaveLen(3))
  480. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(existingLabelKey, existingLabelValue))
  481. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue("es-label-key", "es-label-value"))
  482. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(esv1.LabelManaged, esv1.LabelManagedValue))
  483. Expect(secret.ObjectMeta.Annotations).To(HaveLen(3))
  484. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue("existing-annotation-key", "existing-annotation-value"))
  485. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue("es-annotation-key", "es-annotation-value"))
  486. Expect(secret.ObjectMeta.Annotations).To(HaveKey(esv1.AnnotationDataHash))
  487. Expect(ctest.HasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretFQDN)).To(BeFalse())
  488. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  489. oldCharactersAroundMismatchToInclude := format.CharactersAroundMismatchToInclude
  490. format.CharactersAroundMismatchToInclude = 10
  491. Expect(ctest.FirstManagedFieldForManager(secret.ObjectMeta, ExternalSecretFQDN)).To(
  492. Equal(
  493. fmt.Sprintf(
  494. `{"f:data":{"f:targetProperty":{}},"f:metadata":{"f:annotations":{"f:es-annotation-key":{},"f:%s":{}},"f:labels":{"f:es-label-key":{},"f:%s":{}}}}`,
  495. esv1.AnnotationDataHash,
  496. esv1.LabelManaged,
  497. ),
  498. ),
  499. )
  500. Expect(ctest.FirstManagedFieldForManager(secret.ObjectMeta, FakeManager)).To(
  501. Equal(`{"f:data":{".":{},"f:pre-existing-key":{}},"f:metadata":{"f:annotations":{".":{},"f:existing-annotation-key":{}},"f:labels":{".":{},"f:existing-label-key":{}}},"f:type":{}}`),
  502. )
  503. format.CharactersAroundMismatchToInclude = oldCharactersAroundMismatchToInclude
  504. }
  505. }
  506. mergeWithSecretUpdate := func(tc *testCase) {
  507. const secretVal = "someValue"
  508. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  509. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Hour}
  510. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  511. ObjectMeta: metav1.ObjectMeta{
  512. Name: ExternalSecretTargetSecretName,
  513. Namespace: ExternalSecretNamespace,
  514. },
  515. Data: map[string][]byte{
  516. existingKey: []byte(existingVal),
  517. },
  518. }, client.FieldOwner(FakeManager))).To(Succeed())
  519. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  520. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  521. // Overwrite the secret value to check if the change kicks reconciliation and overwrites it again
  522. Expect(k8sClient.Update(context.Background(), &v1.Secret{
  523. ObjectMeta: metav1.ObjectMeta{
  524. Name: ExternalSecretTargetSecretName,
  525. Namespace: ExternalSecretNamespace,
  526. },
  527. Data: map[string][]byte{
  528. existingKey: []byte("differentValue"),
  529. },
  530. }, client.FieldOwner(FakeManager))).To(Succeed())
  531. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  532. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  533. }
  534. }
  535. // should not update if no changes
  536. mergeWithSecretNoChange := func(tc *testCase) {
  537. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  538. // create secret beforehand
  539. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  540. ObjectMeta: metav1.ObjectMeta{
  541. Name: ExternalSecretTargetSecretName,
  542. Namespace: ExternalSecretNamespace,
  543. },
  544. Data: map[string][]byte{
  545. existingKey: []byte(existingVal),
  546. },
  547. }, client.FieldOwner(FakeManager))).To(Succeed())
  548. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  549. oldResourceVersion := secret.ResourceVersion
  550. cleanSecret := secret.DeepCopy()
  551. Expect(k8sClient.Patch(context.Background(), secret, client.MergeFrom(cleanSecret))).To(Succeed())
  552. newSecret := &v1.Secret{}
  553. Eventually(func() bool {
  554. secretLookupKey := types.NamespacedName{
  555. Name: ExternalSecretTargetSecretName,
  556. Namespace: ExternalSecretNamespace,
  557. }
  558. err := k8sClient.Get(context.Background(), secretLookupKey, newSecret)
  559. if err != nil {
  560. return false
  561. }
  562. return oldResourceVersion == newSecret.ResourceVersion
  563. }, timeout, interval).Should(Equal(true))
  564. }
  565. }
  566. // should not merge with secret if it doesn't exist
  567. mergeWithSecretErr := func(tc *testCase) {
  568. const secretVal = "someValue"
  569. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  570. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  571. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  572. expected := []esv1.ExternalSecretStatusCondition{
  573. {
  574. Type: esv1.ExternalSecretReady,
  575. Status: v1.ConditionTrue,
  576. Reason: esv1.ConditionReasonSecretMissing,
  577. Message: msgMissing,
  578. },
  579. }
  580. opts := cmpopts.IgnoreFields(esv1.ExternalSecretStatusCondition{}, "LastTransitionTime")
  581. if diff := cmp.Diff(expected, es.Status.Conditions, opts); diff != "" {
  582. GinkgoLogr.Info("(-got, +want)\n%s", "diff", diff)
  583. return false
  584. }
  585. return true
  586. }
  587. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  588. Eventually(func() bool {
  589. Expect(testSyncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  590. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  591. return metric.GetCounter().GetValue() == 0 && metricDuration.GetGauge().GetValue() > 0.0
  592. }, timeout, interval).Should(BeTrue())
  593. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  594. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  595. }
  596. }
  597. // controller should force ownership
  598. mergeWithConflict := func(tc *testCase) {
  599. const secretVal = "someValue"
  600. // this should confict
  601. const existingKey = targetProp
  602. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  603. // create secret beforehand
  604. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  605. ObjectMeta: metav1.ObjectMeta{
  606. Name: ExternalSecretTargetSecretName,
  607. Namespace: ExternalSecretNamespace,
  608. },
  609. Data: map[string][]byte{
  610. existingKey: []byte(existingVal),
  611. },
  612. }, client.FieldOwner(FakeManager))).To(Succeed())
  613. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  614. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  615. // check that value stays the same
  616. Expect(string(secret.Data[existingKey])).To(Equal(secretVal))
  617. // check owner/managedFields
  618. Expect(ctest.HasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretFQDN)).To(BeFalse())
  619. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  620. oldCharactersAroundMismatchToInclude := format.CharactersAroundMismatchToInclude
  621. format.CharactersAroundMismatchToInclude = 10
  622. Expect(ctest.FirstManagedFieldForManager(secret.ObjectMeta, ExternalSecretFQDN)).To(
  623. Equal(fmt.Sprintf(`{"f:data":{"f:targetProperty":{}},"f:metadata":{"f:annotations":{".":{},"f:%s":{}},"f:labels":{".":{},"f:%s":{}}}}`, esv1.AnnotationDataHash, esv1.LabelManaged)),
  624. )
  625. format.CharactersAroundMismatchToInclude = oldCharactersAroundMismatchToInclude
  626. }
  627. }
  628. syncWithGeneratorRef := func(tc *testCase) {
  629. const secretKey = "somekey"
  630. const secretVal = "someValue"
  631. Expect(k8sClient.Create(context.Background(), &genv1alpha1.Fake{
  632. ObjectMeta: metav1.ObjectMeta{
  633. Name: "mytestfake",
  634. Namespace: ExternalSecretNamespace,
  635. },
  636. Spec: genv1alpha1.FakeSpec{
  637. Data: map[string]string{
  638. secretKey: secretVal,
  639. },
  640. },
  641. })).To(Succeed())
  642. // reset secretStoreRef
  643. tc.externalSecret.Spec.SecretStoreRef = esv1.SecretStoreRef{}
  644. tc.externalSecret.Spec.Data = nil
  645. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  646. {
  647. SourceRef: &esv1.StoreGeneratorSourceRef{
  648. GeneratorRef: &esv1.GeneratorRef{
  649. APIVersion: genv1alpha1.Group + "/" + genv1alpha1.Version,
  650. Kind: "Fake",
  651. Name: "mytestfake",
  652. },
  653. },
  654. },
  655. }
  656. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  657. // check values
  658. Expect(string(secret.Data[secretKey])).To(Equal(secretVal))
  659. }
  660. }
  661. syncWithClusterGeneratorRef := func(tc *testCase) {
  662. const secretKey = "somekey2"
  663. const secretVal = "someValue2"
  664. Expect(k8sClient.Create(context.Background(), &genv1alpha1.ClusterGenerator{
  665. ObjectMeta: metav1.ObjectMeta{
  666. Name: "mytestfake",
  667. },
  668. Spec: genv1alpha1.ClusterGeneratorSpec{
  669. Kind: "Fake",
  670. Generator: genv1alpha1.GeneratorSpec{
  671. FakeSpec: &genv1alpha1.FakeSpec{
  672. Data: map[string]string{
  673. secretKey: secretVal,
  674. },
  675. },
  676. },
  677. },
  678. })).To(Succeed())
  679. // reset secretStoreRef
  680. tc.externalSecret.Spec.SecretStoreRef = esv1.SecretStoreRef{}
  681. tc.externalSecret.Spec.Data = nil
  682. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  683. {
  684. SourceRef: &esv1.StoreGeneratorSourceRef{
  685. GeneratorRef: &esv1.GeneratorRef{
  686. APIVersion: genv1alpha1.Group + "/" + genv1alpha1.Version,
  687. Kind: "ClusterGenerator",
  688. Name: "mytestfake",
  689. },
  690. },
  691. },
  692. }
  693. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  694. // check values
  695. Expect(string(secret.Data[secretKey])).To(Equal(secretVal))
  696. }
  697. }
  698. deleteOrphanedSecrets := func(tc *testCase) {
  699. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  700. cleanEs := es.DeepCopy()
  701. oldSecret := v1.Secret{}
  702. oldSecretName := types.NamespacedName{
  703. Name: "test-secret",
  704. Namespace: secret.Namespace,
  705. }
  706. newSecret := v1.Secret{}
  707. secretName := types.NamespacedName{
  708. Name: "new-foo",
  709. Namespace: secret.Namespace,
  710. }
  711. Eventually(func() bool {
  712. err := k8sClient.Get(context.Background(), oldSecretName, &oldSecret)
  713. return err == nil
  714. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  715. es.Spec.Target.Name = "new-foo"
  716. Expect(k8sClient.Patch(context.Background(), es, client.MergeFrom(cleanEs))).To(Succeed())
  717. Eventually(func() bool {
  718. err := k8sClient.Get(context.Background(), secretName, &newSecret)
  719. return err == nil
  720. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  721. Eventually(func() bool {
  722. err := k8sClient.Get(context.Background(), oldSecretName, &oldSecret)
  723. return apierrors.IsNotFound(err)
  724. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  725. }
  726. }
  727. ignoreMismatchControllerForGeneratorRef := func(_ *testCase) {
  728. const secretKey = "somekey"
  729. const secretVal = "someValue"
  730. fakeGenerator := &genv1alpha1.Fake{
  731. ObjectMeta: metav1.ObjectMeta{
  732. Name: "mytestfake2",
  733. Namespace: ExternalSecretNamespace,
  734. },
  735. Spec: genv1alpha1.FakeSpec{
  736. Data: map[string]string{
  737. secretKey: secretVal,
  738. },
  739. Controller: "fakeControllerClass",
  740. },
  741. }
  742. fakeGeneratorJSON, _ := json.Marshal(fakeGenerator)
  743. Expect(shouldSkipGenerator(
  744. &Reconciler{
  745. ControllerClass: "default",
  746. },
  747. &apiextensions.JSON{Raw: fakeGeneratorJSON},
  748. )).To(BeTrue())
  749. }
  750. syncWithMultipleSecretStores := func(tc *testCase) {
  751. Expect(k8sClient.Create(context.Background(), &esv1.SecretStore{
  752. ObjectMeta: metav1.ObjectMeta{
  753. Name: "foo",
  754. Namespace: ExternalSecretNamespace,
  755. },
  756. Spec: esv1.SecretStoreSpec{
  757. Provider: &esv1.SecretStoreProvider{
  758. Fake: &esv1.FakeProvider{
  759. Data: []esv1.FakeProviderData{
  760. {
  761. Key: "foo",
  762. Version: "",
  763. Value: `{"foo":"bar","foo2":"bar2"}`,
  764. },
  765. },
  766. },
  767. },
  768. },
  769. })).To(Succeed())
  770. Expect(k8sClient.Create(context.Background(), &esv1.SecretStore{
  771. ObjectMeta: metav1.ObjectMeta{
  772. Name: "baz",
  773. Namespace: ExternalSecretNamespace,
  774. },
  775. Spec: esv1.SecretStoreSpec{
  776. Provider: &esv1.SecretStoreProvider{
  777. Fake: &esv1.FakeProvider{
  778. Data: []esv1.FakeProviderData{
  779. {
  780. Key: "baz",
  781. Version: "",
  782. Value: `{"baz":"bang","baz2":"bang2"}`,
  783. },
  784. },
  785. },
  786. },
  787. },
  788. })).To(Succeed())
  789. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  790. {
  791. Extract: &esv1.ExternalSecretDataRemoteRef{
  792. Key: "foo",
  793. },
  794. SourceRef: &esv1.StoreGeneratorSourceRef{
  795. SecretStoreRef: &esv1.SecretStoreRef{
  796. Name: "foo",
  797. Kind: esv1.SecretStoreKind,
  798. },
  799. },
  800. },
  801. {
  802. Extract: &esv1.ExternalSecretDataRemoteRef{
  803. Key: "baz",
  804. },
  805. SourceRef: &esv1.StoreGeneratorSourceRef{
  806. SecretStoreRef: &esv1.SecretStoreRef{
  807. Name: "baz",
  808. Kind: esv1.SecretStoreKind,
  809. },
  810. },
  811. },
  812. }
  813. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  814. // check values
  815. Expect(string(secret.Data["foo"])).To(Equal("bar"))
  816. Expect(string(secret.Data["foo2"])).To(Equal("bar2"))
  817. Expect(string(secret.Data["baz"])).To(Equal("bang"))
  818. Expect(string(secret.Data["baz2"])).To(Equal("bang2"))
  819. }
  820. }
  821. // when using a template it should be used as a blueprint
  822. // to construct a new secret: labels, annotations, finalizers and type
  823. syncWithTemplate := func(tc *testCase) {
  824. const secretVal = "someValue"
  825. const tplStaticKey = "tplstatickey"
  826. const tplStaticVal = "tplstaticvalue"
  827. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  828. "fooobar": "bazz",
  829. }
  830. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  831. "hihihih": "hehehe",
  832. }
  833. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  834. Metadata: esv1.ExternalSecretTemplateMetadata{
  835. Labels: map[string]string{
  836. "foos": "ball",
  837. },
  838. Annotations: map[string]string{
  839. "hihi": "ga",
  840. },
  841. Finalizers: []string{
  842. "example.com/finalizer",
  843. },
  844. },
  845. Type: v1.SecretTypeOpaque,
  846. EngineVersion: esv1.TemplateEngineV2,
  847. Data: map[string]string{
  848. targetProp: targetPropObj,
  849. tplStaticKey: tplStaticVal,
  850. },
  851. }
  852. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  853. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  854. // check values
  855. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  856. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  857. // labels/annotations/finalizers should be taken from the template
  858. for k, v := range es.Spec.Target.Template.Metadata.Labels {
  859. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(k, v))
  860. }
  861. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  862. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  863. }
  864. for _, v := range es.Spec.Target.Template.Metadata.Finalizers {
  865. Expect(secret.ObjectMeta.Finalizers).To(ContainElement(v))
  866. }
  867. }
  868. }
  869. // when using a v2 template it should use the v2 engine version
  870. syncWithTemplateV2 := func(tc *testCase) {
  871. const secretVal = "someValue"
  872. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  873. Type: v1.SecretTypeOpaque,
  874. // it should default to v2 for beta11
  875. // EngineVersion: esv1.TemplateEngineV2,
  876. Data: map[string]string{
  877. targetProp: "{{ .targetProperty | upper }} was templated",
  878. },
  879. }
  880. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  881. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  882. // check values
  883. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  884. }
  885. }
  886. // // secret should be synced with correct value precedence:
  887. // // * fromString
  888. // // * template data
  889. // // * templateFrom
  890. // // * data
  891. // // * dataFrom
  892. syncWithTemplatePrecedence := func(tc *testCase) {
  893. const secretVal = "someValue"
  894. const tplStaticKey = "tplstatickey"
  895. const tplStaticVal = "tplstaticvalue"
  896. const tplFromCMName = "template-cm"
  897. const tplFromSecretName = "template-secret"
  898. const tplFromKey = "tpl-from-key"
  899. const tplFromSecKey = "tpl-from-sec-key"
  900. const tplFromVal = "tpl-from-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  901. const tplFromSecVal = "tpl-from-sec-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  902. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  903. ObjectMeta: metav1.ObjectMeta{
  904. Name: tplFromCMName,
  905. Namespace: ExternalSecretNamespace,
  906. },
  907. Data: map[string]string{
  908. tplFromKey: tplFromVal,
  909. },
  910. })).To(Succeed())
  911. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  912. ObjectMeta: metav1.ObjectMeta{
  913. Name: tplFromSecretName,
  914. Namespace: ExternalSecretNamespace,
  915. },
  916. Data: map[string][]byte{
  917. tplFromSecKey: []byte(tplFromSecVal),
  918. },
  919. })).To(Succeed())
  920. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  921. Metadata: esv1.ExternalSecretTemplateMetadata{},
  922. Type: v1.SecretTypeOpaque,
  923. TemplateFrom: []esv1.TemplateFrom{
  924. {
  925. ConfigMap: &esv1.TemplateRef{
  926. Name: tplFromCMName,
  927. Items: []esv1.TemplateRefItem{
  928. {
  929. Key: tplFromKey,
  930. },
  931. },
  932. },
  933. },
  934. {
  935. Secret: &esv1.TemplateRef{
  936. Name: tplFromSecretName,
  937. Items: []esv1.TemplateRefItem{
  938. {
  939. Key: tplFromSecKey,
  940. },
  941. },
  942. },
  943. },
  944. },
  945. Data: map[string]string{
  946. // this should be the data value, not dataFrom
  947. targetProp: targetPropObj,
  948. // this should use the value from the map
  949. "bar": "value from map: {{ .bar | toString }}",
  950. // just a static value
  951. tplStaticKey: tplStaticVal,
  952. },
  953. }
  954. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  955. {
  956. Extract: &esv1.ExternalSecretDataRemoteRef{
  957. Key: "datamap",
  958. },
  959. },
  960. }
  961. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  962. fakeProvider.WithGetSecretMap(map[string][]byte{
  963. "targetProperty": []byte(FooValue),
  964. "bar": []byte(BarValue),
  965. }, nil)
  966. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  967. // check values
  968. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  969. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  970. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  971. Expect(string(secret.Data[tplFromKey])).To(Equal("tpl-from-value: someValue // map-bar-value"))
  972. Expect(string(secret.Data[tplFromSecKey])).To(Equal("tpl-from-sec-value: someValue // map-bar-value"))
  973. }
  974. }
  975. syncTemplateFromKeysAndValues := func(tc *testCase) {
  976. const tplFromCMName = "template-cm"
  977. const tplFromSecretName = "template-secret"
  978. const tplFromKey = "tpl-from-key"
  979. const tplFromSecKey = "tpl-from-sec-key"
  980. const tplFromVal = "{{ .targetKey }}-cm: {{ .targetValue }}"
  981. const tplFromSecVal = "{{ .targetKey }}-sec: {{ .targetValue }}"
  982. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  983. ObjectMeta: metav1.ObjectMeta{
  984. Name: tplFromCMName,
  985. Namespace: ExternalSecretNamespace,
  986. },
  987. Data: map[string]string{
  988. tplFromKey: tplFromVal,
  989. },
  990. })).To(Succeed())
  991. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  992. ObjectMeta: metav1.ObjectMeta{
  993. Name: tplFromSecretName,
  994. Namespace: ExternalSecretNamespace,
  995. },
  996. Data: map[string][]byte{
  997. tplFromSecKey: []byte(tplFromSecVal),
  998. },
  999. })).To(Succeed())
  1000. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  1001. Metadata: esv1.ExternalSecretTemplateMetadata{},
  1002. Type: v1.SecretTypeOpaque,
  1003. TemplateFrom: []esv1.TemplateFrom{
  1004. {
  1005. ConfigMap: &esv1.TemplateRef{
  1006. Name: tplFromCMName,
  1007. Items: []esv1.TemplateRefItem{
  1008. {
  1009. Key: tplFromKey,
  1010. TemplateAs: esv1.TemplateScopeKeysAndValues,
  1011. },
  1012. },
  1013. },
  1014. },
  1015. {
  1016. Secret: &esv1.TemplateRef{
  1017. Name: tplFromSecretName,
  1018. Items: []esv1.TemplateRefItem{
  1019. {
  1020. Key: tplFromSecKey,
  1021. TemplateAs: esv1.TemplateScopeKeysAndValues,
  1022. },
  1023. },
  1024. },
  1025. },
  1026. },
  1027. }
  1028. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1029. {
  1030. Extract: &esv1.ExternalSecretDataRemoteRef{
  1031. Key: "datamap",
  1032. },
  1033. },
  1034. }
  1035. fakeProvider.WithGetSecretMap(map[string][]byte{
  1036. "targetKey": []byte(FooValue),
  1037. "targetValue": []byte(BarValue),
  1038. }, nil)
  1039. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1040. // check values
  1041. Expect(string(secret.Data["map-foo-value-cm"])).To(Equal(BarValue))
  1042. Expect(string(secret.Data["map-foo-value-sec"])).To(Equal(BarValue))
  1043. }
  1044. }
  1045. syncTemplateFromLiteral := func(tc *testCase) {
  1046. tplDataVal := "{{ .targetKey }}-literal: {{ .targetValue }}"
  1047. tplAnnotationsVal := "{{ .targetKey }}-annotations: {{ .targetValue }}"
  1048. tplLabelsVal := "{{ .targetKey }}-labels: {{ .targetValue }}"
  1049. tplComplexVal := `
  1050. {{- range $k, $v := ( .complex | fromJson )}}
  1051. {{ $k }}: {{ $v }}
  1052. {{- end }}
  1053. `
  1054. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  1055. Metadata: esv1.ExternalSecretTemplateMetadata{},
  1056. Type: v1.SecretTypeOpaque,
  1057. TemplateFrom: []esv1.TemplateFrom{
  1058. {
  1059. Literal: &tplDataVal,
  1060. },
  1061. {
  1062. Literal: &tplComplexVal,
  1063. },
  1064. {
  1065. Target: esv1.TemplateTargetAnnotations,
  1066. Literal: &tplAnnotationsVal,
  1067. },
  1068. {
  1069. Target: esv1.TemplateTargetLabels,
  1070. Literal: &tplLabelsVal,
  1071. },
  1072. },
  1073. }
  1074. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1075. {
  1076. Extract: &esv1.ExternalSecretDataRemoteRef{
  1077. Key: "datamap",
  1078. },
  1079. },
  1080. }
  1081. fakeProvider.WithGetSecretMap(map[string][]byte{
  1082. "targetKey": []byte(FooValue),
  1083. "targetValue": []byte(BarValue),
  1084. "complex": []byte("{\"nested\":\"json\",\"can\":\"be\",\"templated\":\"successfully\"}"),
  1085. }, nil)
  1086. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1087. // check values
  1088. Expect(string(secret.Data["map-foo-value-literal"])).To(Equal(BarValue))
  1089. Expect(string(secret.Data["nested"])).To(Equal("json"))
  1090. Expect(string(secret.Data["can"])).To(Equal("be"))
  1091. Expect(string(secret.Data["templated"])).To(Equal("successfully"))
  1092. Expect(secret.ObjectMeta.Annotations["map-foo-value-annotations"]).To(Equal(BarValue))
  1093. Expect(secret.ObjectMeta.Labels["map-foo-value-labels"]).To(Equal(BarValue))
  1094. }
  1095. }
  1096. refreshWithTemplate := func(tc *testCase) {
  1097. const secretVal = "someValue"
  1098. const tplStaticKey = "tplstatickey"
  1099. const tplStaticVal = "tplstaticvalue"
  1100. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1101. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  1102. Metadata: esv1.ExternalSecretTemplateMetadata{
  1103. Labels: map[string]string{"foo": "bar"},
  1104. Annotations: map[string]string{"foo": "bar"},
  1105. Finalizers: []string{"example.com/finalizer"},
  1106. },
  1107. Type: v1.SecretTypeOpaque,
  1108. Data: map[string]string{
  1109. targetProp: targetPropObj,
  1110. tplStaticKey: tplStaticVal,
  1111. },
  1112. }
  1113. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1114. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  1115. // check values
  1116. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  1117. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  1118. // labels/annotations/finalizers should be taken from the template
  1119. for k, v := range es.Spec.Target.Template.Metadata.Labels {
  1120. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(k, v))
  1121. }
  1122. // a secret will always have some extra annotations (i.e. hashmap check), so we only check for specific
  1123. // source annotations
  1124. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  1125. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  1126. }
  1127. for _, v := range es.Spec.Target.Template.Metadata.Finalizers {
  1128. Expect(secret.ObjectMeta.Finalizers).To(ContainElement(v))
  1129. }
  1130. cleanEs := tc.externalSecret.DeepCopy()
  1131. // now update ExternalSecret
  1132. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  1133. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  1134. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  1135. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  1136. // wait for secret
  1137. sec := &v1.Secret{}
  1138. secretLookupKey := types.NamespacedName{
  1139. Name: ExternalSecretTargetSecretName,
  1140. Namespace: ExternalSecretNamespace,
  1141. }
  1142. Eventually(func() bool {
  1143. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1144. if err != nil {
  1145. return false
  1146. }
  1147. // ensure new data value exist
  1148. return string(sec.Data["new"]) == "value"
  1149. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  1150. // also check labels/annotations have been updated
  1151. for k, v := range es.Spec.Target.Template.Metadata.Labels {
  1152. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(k, v))
  1153. }
  1154. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  1155. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  1156. }
  1157. }
  1158. }
  1159. onlyMetadataFromTemplate := func(tc *testCase) {
  1160. const secretVal = "someValue"
  1161. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1162. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  1163. Metadata: esv1.ExternalSecretTemplateMetadata{
  1164. Labels: map[string]string{"foo": "bar"},
  1165. Annotations: map[string]string{"foo": "bar"},
  1166. Finalizers: []string{"example.com/finalizer"},
  1167. },
  1168. }
  1169. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1170. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  1171. // check values
  1172. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1173. // labels/annotations/finalizers should be taken from the template
  1174. for k, v := range es.Spec.Target.Template.Metadata.Labels {
  1175. Expect(secret.ObjectMeta.Labels).To(HaveKeyWithValue(k, v))
  1176. }
  1177. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  1178. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  1179. }
  1180. for _, v := range es.Spec.Target.Template.Metadata.Finalizers {
  1181. Expect(secret.ObjectMeta.Finalizers).To(ContainElement(v))
  1182. }
  1183. }
  1184. }
  1185. // when the provider secret changes the Kind=Secret value
  1186. // must change, too.
  1187. refreshSecretValue := func(tc *testCase) {
  1188. const targetProp = "targetProperty"
  1189. const secretVal = "someValue"
  1190. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1191. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1192. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1193. // check values
  1194. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1195. // update provider secret
  1196. newValue := "NEW VALUE"
  1197. sec := &v1.Secret{}
  1198. fakeProvider.WithGetSecret([]byte(newValue), nil)
  1199. secretLookupKey := types.NamespacedName{
  1200. Name: ExternalSecretTargetSecretName,
  1201. Namespace: ExternalSecretNamespace,
  1202. }
  1203. Eventually(func() bool {
  1204. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1205. if err != nil {
  1206. return false
  1207. }
  1208. v := sec.Data[targetProp]
  1209. return string(v) == newValue
  1210. }, timeout, interval).Should(BeTrue())
  1211. }
  1212. }
  1213. // when a provider secret was deleted it must be deleted from
  1214. // the secret aswell
  1215. refreshSecretValueMap := func(tc *testCase) {
  1216. fakeProvider.WithGetSecretMap(map[string][]byte{
  1217. "foo": []byte("1111"),
  1218. "bar": []byte("2222"),
  1219. }, nil)
  1220. tc.externalSecret.Spec.Data = []esv1.ExternalSecretData{}
  1221. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1222. {
  1223. Extract: &esv1.ExternalSecretDataRemoteRef{
  1224. Key: remoteKey,
  1225. },
  1226. },
  1227. }
  1228. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1229. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1230. // check values
  1231. Expect(string(secret.Data["foo"])).To(Equal("1111"))
  1232. Expect(string(secret.Data["bar"])).To(Equal("2222"))
  1233. // update provider secret
  1234. sec := &v1.Secret{}
  1235. fakeProvider.WithGetSecretMap(map[string][]byte{
  1236. "foo": []byte("1111"),
  1237. }, nil)
  1238. secretLookupKey := types.NamespacedName{
  1239. Name: ExternalSecretTargetSecretName,
  1240. Namespace: ExternalSecretNamespace,
  1241. }
  1242. Eventually(func() bool {
  1243. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1244. if err != nil {
  1245. return false
  1246. }
  1247. return string(sec.Data["foo"]) == "1111" &&
  1248. sec.Data["bar"] == nil // must not be defined, it was deleted
  1249. }, timeout, interval).Should(BeTrue())
  1250. }
  1251. }
  1252. // when a provider secret was deleted it must be deleted from
  1253. // the secret aswell when using a template
  1254. refreshSecretValueMapTemplate := func(tc *testCase) {
  1255. fakeProvider.WithGetSecretMap(map[string][]byte{
  1256. "foo": []byte("1111"),
  1257. "bar": []byte("2222"),
  1258. }, nil)
  1259. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{}
  1260. tc.externalSecret.Spec.Data = []esv1.ExternalSecretData{}
  1261. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1262. {
  1263. Extract: &esv1.ExternalSecretDataRemoteRef{
  1264. Key: remoteKey,
  1265. },
  1266. },
  1267. }
  1268. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1269. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1270. // check values
  1271. Expect(string(secret.Data["foo"])).To(Equal("1111"))
  1272. Expect(string(secret.Data["bar"])).To(Equal("2222"))
  1273. // update provider secret
  1274. sec := &v1.Secret{}
  1275. fakeProvider.WithGetSecretMap(map[string][]byte{
  1276. "foo": []byte("1111"),
  1277. }, nil)
  1278. secretLookupKey := types.NamespacedName{
  1279. Name: ExternalSecretTargetSecretName,
  1280. Namespace: ExternalSecretNamespace,
  1281. }
  1282. Eventually(func() bool {
  1283. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1284. if err != nil {
  1285. return false
  1286. }
  1287. return string(sec.Data["foo"]) == "1111" &&
  1288. sec.Data["bar"] == nil // must not be defined, it was deleted
  1289. }, timeout, interval).Should(BeTrue())
  1290. }
  1291. }
  1292. refreshintervalZero := func(tc *testCase) {
  1293. const targetProp = "targetProperty"
  1294. const secretVal = "someValue"
  1295. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1296. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: 0}
  1297. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1298. // check values
  1299. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1300. // update provider secret
  1301. newValue := "NEW VALUE"
  1302. sec := &v1.Secret{}
  1303. fakeProvider.WithGetSecret([]byte(newValue), nil)
  1304. secretLookupKey := types.NamespacedName{
  1305. Name: ExternalSecretTargetSecretName,
  1306. Namespace: ExternalSecretNamespace,
  1307. }
  1308. Consistently(func() bool {
  1309. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1310. if err != nil {
  1311. return false
  1312. }
  1313. v := sec.Data[targetProp]
  1314. return string(v) == secretVal
  1315. }, time.Second*10, time.Second).Should(BeTrue())
  1316. }
  1317. }
  1318. deletionPolicyDelete := func(tc *testCase) {
  1319. expVal := []byte("1234")
  1320. // set initial value
  1321. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1322. "foo": expVal,
  1323. "bar": expVal,
  1324. }, nil)
  1325. tc.externalSecret.Spec.Data = nil
  1326. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1327. {
  1328. Find: &esv1.ExternalSecretFind{
  1329. Tags: map[string]string{},
  1330. },
  1331. },
  1332. }
  1333. tc.externalSecret.Spec.Target.DeletionPolicy = esv1.DeletionPolicyDelete
  1334. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1335. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1336. Expect(secret.Data["foo"]).To(Equal(expVal))
  1337. // update provider secret
  1338. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1339. "foo": expVal,
  1340. }, nil)
  1341. sec := &v1.Secret{}
  1342. secretLookupKey := types.NamespacedName{
  1343. Name: ExternalSecretTargetSecretName,
  1344. Namespace: ExternalSecretNamespace,
  1345. }
  1346. Eventually(func() bool {
  1347. By("checking secret value for foo=1234 and bar=nil")
  1348. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1349. if err != nil {
  1350. return false
  1351. }
  1352. return bytes.Equal(sec.Data["foo"], expVal) && sec.Data["bar"] == nil
  1353. }, time.Second*10, time.Second).Should(BeTrue())
  1354. // return specific delete err to indicate deletion
  1355. fakeProvider.WithGetAllSecrets(map[string][]byte{}, esv1.NoSecretErr)
  1356. Eventually(func() bool {
  1357. By("checking that secret has been deleted")
  1358. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1359. return apierrors.IsNotFound(err)
  1360. }, time.Second*10, time.Second).Should(BeTrue())
  1361. }
  1362. }
  1363. deletionPolicyRetain := func(tc *testCase) {
  1364. expVal := []byte("1234")
  1365. // set initial value
  1366. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1367. "foo": expVal,
  1368. "bar": expVal,
  1369. }, nil)
  1370. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1371. {
  1372. Find: &esv1.ExternalSecretFind{
  1373. Tags: map[string]string{},
  1374. },
  1375. },
  1376. }
  1377. tc.externalSecret.Spec.Target.DeletionPolicy = esv1.DeletionPolicyRetain
  1378. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1379. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1380. Expect(secret.Data["foo"]).To(Equal(expVal))
  1381. sec := &v1.Secret{}
  1382. secretLookupKey := types.NamespacedName{
  1383. Name: ExternalSecretTargetSecretName,
  1384. Namespace: ExternalSecretNamespace,
  1385. }
  1386. // return specific delete err to indicate deletion
  1387. // however this should not trigger a delete
  1388. fakeProvider.WithGetAllSecrets(map[string][]byte{}, esv1.NoSecretErr)
  1389. Consistently(func() bool {
  1390. By("checking that secret has not been deleted")
  1391. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1392. if err != nil {
  1393. GinkgoLogr.Error(err, "failed getting a secret")
  1394. return false
  1395. }
  1396. if got := sec.Data["foo"]; !bytes.Equal(got, expVal) {
  1397. GinkgoLogr.Info("received an unexpected secret value", "got", got, "expected", expVal)
  1398. return false
  1399. }
  1400. return true
  1401. }, time.Second*10, time.Second).Should(BeTrue())
  1402. }
  1403. }
  1404. deletionPolicyRetainEmptyData := func(tc *testCase) {
  1405. // set initial value
  1406. fakeProvider.WithGetAllSecrets(make(map[string][]byte), nil)
  1407. tc.externalSecret.Spec.Data = make([]esv1.ExternalSecretData, 0)
  1408. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1409. {
  1410. Find: &esv1.ExternalSecretFind{
  1411. Tags: map[string]string{
  1412. "non-existing-key": "non-existing-value",
  1413. },
  1414. },
  1415. },
  1416. }
  1417. tc.externalSecret.Spec.Target.DeletionPolicy = esv1.DeletionPolicyRetain
  1418. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1419. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1420. expected := []esv1.ExternalSecretStatusCondition{
  1421. {
  1422. Type: esv1.ExternalSecretReady,
  1423. Status: v1.ConditionTrue,
  1424. Reason: esv1.ConditionReasonSecretSynced,
  1425. Message: msgSyncedRetain,
  1426. },
  1427. }
  1428. opts := cmpopts.IgnoreFields(esv1.ExternalSecretStatusCondition{}, "LastTransitionTime")
  1429. if diff := cmp.Diff(expected, es.Status.Conditions, opts); diff != "" {
  1430. GinkgoLogr.Info("(-got, +want)\n%s", "diff", diff)
  1431. return false
  1432. }
  1433. return true
  1434. }
  1435. }
  1436. // merge with existing secret using creationPolicy=Merge
  1437. // if provider secret gets deleted only the managed field should get deleted
  1438. deletionPolicyMerge := func(tc *testCase) {
  1439. const secretVal = "someValue"
  1440. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1441. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  1442. tc.externalSecret.Spec.Target.DeletionPolicy = esv1.DeletionPolicyMerge
  1443. // create secret beforehand
  1444. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  1445. ObjectMeta: metav1.ObjectMeta{
  1446. Name: ExternalSecretTargetSecretName,
  1447. Namespace: ExternalSecretNamespace,
  1448. },
  1449. Data: map[string][]byte{
  1450. existingKey: []byte(existingVal),
  1451. },
  1452. }, client.FieldOwner(FakeManager))).To(Succeed())
  1453. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1454. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1455. // check value
  1456. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  1457. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1458. sec := &v1.Secret{}
  1459. secretLookupKey := types.NamespacedName{
  1460. Name: ExternalSecretTargetSecretName,
  1461. Namespace: ExternalSecretNamespace,
  1462. }
  1463. // return specific delete err to indicate deletion
  1464. // however this should not trigger a delete
  1465. // instead expect that only the pre-existing value exists
  1466. fakeProvider.WithGetSecret(nil, esv1.NoSecretErr)
  1467. Eventually(func() bool {
  1468. By("checking that secret has not been deleted and pre-existing key exists")
  1469. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1470. return !apierrors.IsNotFound(err) &&
  1471. len(sec.Data) == 1 &&
  1472. bytes.Equal(sec.Data[existingKey], []byte(existingVal))
  1473. }, time.Second*30, time.Second).Should(BeTrue())
  1474. }
  1475. }
  1476. // orphan the secret after the external secret has been deleted
  1477. createSecretPolicyOrphan := func(tc *testCase) {
  1478. const secretVal = "someValue"
  1479. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1480. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  1481. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1482. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1483. // check value
  1484. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1485. sec := &v1.Secret{}
  1486. secretLookupKey := types.NamespacedName{
  1487. Name: ExternalSecretTargetSecretName,
  1488. Namespace: ExternalSecretNamespace,
  1489. }
  1490. err := k8sClient.Delete(context.Background(), tc.externalSecret)
  1491. Expect(err).ToNot(HaveOccurred())
  1492. Consistently(func() bool {
  1493. By("checking that secret has not been deleted")
  1494. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1495. return !apierrors.IsNotFound(err)
  1496. }, time.Second*15, time.Second).Should(BeTrue())
  1497. }
  1498. }
  1499. // with rewrite all keys from a dataFrom operation
  1500. // should be put with new rewriting into the secret
  1501. syncAndRewriteWithDataFrom := func(tc *testCase) {
  1502. tc.externalSecret.Spec.Data = nil
  1503. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1504. {
  1505. Extract: &esv1.ExternalSecretDataRemoteRef{
  1506. Key: remoteKey,
  1507. },
  1508. Rewrite: []esv1.ExternalSecretRewrite{{
  1509. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1510. Source: "(.*)",
  1511. Target: "new-$1",
  1512. },
  1513. }},
  1514. },
  1515. {
  1516. Extract: &esv1.ExternalSecretDataRemoteRef{
  1517. Key: remoteKey,
  1518. },
  1519. Rewrite: []esv1.ExternalSecretRewrite{{
  1520. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1521. Source: "(.*)",
  1522. Target: "old-$1",
  1523. },
  1524. }},
  1525. },
  1526. }
  1527. fakeProvider.WithGetSecretMap(map[string][]byte{
  1528. "foo": []byte(FooValue),
  1529. "bar": []byte(BarValue),
  1530. }, nil)
  1531. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1532. // check values
  1533. Expect(string(secret.Data["new-foo"])).To(Equal(FooValue))
  1534. Expect(string(secret.Data["new-bar"])).To(Equal(BarValue))
  1535. Expect(string(secret.Data["old-foo"])).To(Equal(FooValue))
  1536. Expect(string(secret.Data["old-bar"])).To(Equal(BarValue))
  1537. }
  1538. }
  1539. // with rewrite keys from dataFrom
  1540. // should error if keys are not compliant
  1541. invalidExtractKeysErrCondition := func(tc *testCase) {
  1542. tc.externalSecret.Spec.Data = nil
  1543. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1544. {
  1545. Extract: &esv1.ExternalSecretDataRemoteRef{
  1546. Key: remoteKey,
  1547. },
  1548. Rewrite: []esv1.ExternalSecretRewrite{{
  1549. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1550. Source: "(.*)",
  1551. Target: "$1",
  1552. },
  1553. }},
  1554. },
  1555. }
  1556. fakeProvider.WithGetSecretMap(map[string][]byte{
  1557. "foo/bar": []byte(FooValue),
  1558. "bar/foo": []byte(BarValue),
  1559. }, nil)
  1560. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1561. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1562. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1563. return false
  1564. }
  1565. return true
  1566. }
  1567. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1568. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1569. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1570. return false
  1571. }
  1572. return true
  1573. }
  1574. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  1575. Eventually(func() bool {
  1576. Expect(testSyncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1577. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1578. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1579. }, timeout, interval).Should(BeTrue())
  1580. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1581. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1582. }
  1583. }
  1584. // with rewrite keys from dataFrom
  1585. // should error if keys are not compliant
  1586. invalidFindKeysErrCondition := func(tc *testCase) {
  1587. tc.externalSecret.Spec.Data = nil
  1588. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1589. {
  1590. Find: &esv1.ExternalSecretFind{
  1591. Name: &esv1.FindName{
  1592. RegExp: ".*",
  1593. },
  1594. },
  1595. Rewrite: []esv1.ExternalSecretRewrite{{
  1596. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1597. Source: "(.*)",
  1598. Target: "$1",
  1599. },
  1600. }},
  1601. },
  1602. }
  1603. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1604. "foo/bar": []byte(FooValue),
  1605. "bar/foo": []byte(BarValue),
  1606. }, nil)
  1607. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1608. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1609. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1610. return false
  1611. }
  1612. return true
  1613. }
  1614. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1615. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1616. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1617. return false
  1618. }
  1619. return true
  1620. }
  1621. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  1622. Eventually(func() bool {
  1623. Expect(testSyncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1624. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1625. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1626. }, timeout, interval).Should(BeTrue())
  1627. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1628. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1629. }
  1630. }
  1631. // with dataFrom all properties from the specified secret
  1632. // should be put into the secret
  1633. syncWithDataFrom := func(tc *testCase) {
  1634. tc.externalSecret.Spec.Data = nil
  1635. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1636. {
  1637. Extract: &esv1.ExternalSecretDataRemoteRef{
  1638. Key: remoteKey,
  1639. },
  1640. },
  1641. }
  1642. fakeProvider.WithGetSecretMap(map[string][]byte{
  1643. "foo": []byte(FooValue),
  1644. "bar": []byte(BarValue),
  1645. }, nil)
  1646. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1647. // check values
  1648. Expect(string(secret.Data["foo"])).To(Equal(FooValue))
  1649. Expect(string(secret.Data["bar"])).To(Equal(BarValue))
  1650. }
  1651. }
  1652. // with dataFrom.Find the change is on the called method GetAllSecrets
  1653. // all keys should be put into the secret
  1654. syncAndRewriteDataFromFind := func(tc *testCase) {
  1655. tc.externalSecret.Spec.Data = nil
  1656. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1657. {
  1658. Find: &esv1.ExternalSecretFind{
  1659. Name: &esv1.FindName{
  1660. RegExp: "foobar",
  1661. },
  1662. },
  1663. Rewrite: []esv1.ExternalSecretRewrite{
  1664. {
  1665. Regexp: &esv1.ExternalSecretRewriteRegexp{
  1666. Source: "(.*)",
  1667. Target: "new-$1",
  1668. },
  1669. },
  1670. },
  1671. },
  1672. }
  1673. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1674. "foo": []byte(FooValue),
  1675. "bar": []byte(BarValue),
  1676. }, nil)
  1677. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1678. // check values
  1679. Expect(string(secret.Data["new-foo"])).To(Equal(FooValue))
  1680. Expect(string(secret.Data["new-bar"])).To(Equal(BarValue))
  1681. }
  1682. }
  1683. // with dataFrom.Find the change is on the called method GetAllSecrets
  1684. // all keys should be put into the secret
  1685. syncDataFromFind := func(tc *testCase) {
  1686. tc.externalSecret.Spec.Data = nil
  1687. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1688. {
  1689. Find: &esv1.ExternalSecretFind{
  1690. Name: &esv1.FindName{
  1691. RegExp: "foobar",
  1692. },
  1693. },
  1694. },
  1695. }
  1696. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1697. "foo": []byte(FooValue),
  1698. "bar": []byte(BarValue),
  1699. }, nil)
  1700. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1701. // check values
  1702. Expect(string(secret.Data["foo"])).To(Equal(FooValue))
  1703. Expect(string(secret.Data["bar"])).To(Equal(BarValue))
  1704. }
  1705. }
  1706. // with dataFrom and using a template
  1707. // should be put into the secret
  1708. syncWithDataFromTemplate := func(tc *testCase) {
  1709. tc.externalSecret.Spec.Data = nil
  1710. tc.externalSecret.Spec.Target = esv1.ExternalSecretTarget{
  1711. Name: ExternalSecretTargetSecretName,
  1712. Template: &esv1.ExternalSecretTemplate{
  1713. Type: v1.SecretTypeTLS,
  1714. },
  1715. }
  1716. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  1717. {
  1718. Extract: &esv1.ExternalSecretDataRemoteRef{
  1719. Key: remoteKey,
  1720. },
  1721. },
  1722. }
  1723. fakeProvider.WithGetSecretMap(map[string][]byte{
  1724. "tls.crt": []byte(FooValue),
  1725. "tls.key": []byte(BarValue),
  1726. }, nil)
  1727. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1728. Expect(secret.Type).To(Equal(v1.SecretTypeTLS))
  1729. // check values
  1730. Expect(string(secret.Data["tls.crt"])).To(Equal(FooValue))
  1731. Expect(string(secret.Data["tls.key"])).To(Equal(BarValue))
  1732. }
  1733. }
  1734. // when a provider errors in a GetSecret call
  1735. // a error condition must be set.
  1736. providerErrCondition := func(tc *testCase) {
  1737. const secretVal = "foobar"
  1738. fakeProvider.WithGetSecret(nil, errors.New("boom"))
  1739. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  1740. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1741. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1742. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1743. return false
  1744. }
  1745. return true
  1746. }
  1747. tc.checkExternalSecret = func(es *esv1.ExternalSecret) {
  1748. Eventually(func() bool {
  1749. Expect(testSyncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1750. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1751. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1752. }, timeout, interval).Should(BeTrue())
  1753. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1754. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1755. // es condition should reflect recovered provider error
  1756. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1757. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  1758. Eventually(func() bool {
  1759. err := k8sClient.Get(context.Background(), esKey, es)
  1760. if err != nil {
  1761. return false
  1762. }
  1763. // condition must now be true!
  1764. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1765. if cond == nil || cond.Status != v1.ConditionTrue {
  1766. return false
  1767. }
  1768. return true
  1769. }, timeout, interval).Should(BeTrue())
  1770. }
  1771. }
  1772. // When a ExternalSecret references an non-existing SecretStore
  1773. // a error condition must be set.
  1774. storeMissingErrCondition := func(tc *testCase) {
  1775. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  1776. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1777. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1778. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1779. return false
  1780. }
  1781. return true
  1782. }
  1783. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  1784. Eventually(func() bool {
  1785. Expect(testSyncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1786. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1787. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1788. }, timeout, interval).Should(BeTrue())
  1789. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1790. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1791. }
  1792. }
  1793. // when the provider constructor errors (e.g. invalid configuration)
  1794. // a SecretSyncedError status condition must be set
  1795. storeConstructErrCondition := func(tc *testCase) {
  1796. fakeProvider.WithNew(func(context.Context, esv1.GenericStore, client.Client,
  1797. string) (esv1.SecretsClient, error) {
  1798. return nil, errors.New("artificial constructor error")
  1799. })
  1800. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1801. // condition must be false
  1802. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1803. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  1804. return false
  1805. }
  1806. return true
  1807. }
  1808. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  1809. Eventually(func() bool {
  1810. Expect(testSyncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1811. Expect(testExternalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1812. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1813. }, timeout, interval).Should(BeTrue())
  1814. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1815. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1816. }
  1817. }
  1818. // when a SecretStore has a controller field set which we don't care about
  1819. // the externalSecret must not be touched
  1820. ignoreMismatchController := func(tc *testCase) {
  1821. tc.secretStore.GetSpec().Controller = "nop"
  1822. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1823. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1824. return cond == nil
  1825. }
  1826. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  1827. // Condition True and False should be 0, since the Condition was not created
  1828. Eventually(func() float64 {
  1829. Expect(
  1830. testExternalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric),
  1831. ).To(Succeed())
  1832. return metric.GetGauge().GetValue()
  1833. }, timeout, interval).Should(Equal(0.0))
  1834. Eventually(func() float64 {
  1835. Expect(
  1836. testExternalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric),
  1837. ).To(Succeed())
  1838. return metric.GetGauge().GetValue()
  1839. }, timeout, interval).Should(Equal(0.0))
  1840. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  1841. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1842. }
  1843. }
  1844. ignoreClusterSecretStoreWhenDisabled := func(tc *testCase) {
  1845. tc.externalSecret.Spec.SecretStoreRef.Kind = esv1.ClusterSecretStoreKind
  1846. Expect(shouldSkipClusterSecretStore(
  1847. &Reconciler{
  1848. ClusterSecretStoreEnabled: false,
  1849. },
  1850. tc.externalSecret,
  1851. )).To(BeTrue())
  1852. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  1853. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  1854. return cond == nil
  1855. }
  1856. }
  1857. // When the ownership is set to owner, and we delete a dependent child kind=secret
  1858. // it should be recreated without waiting for refresh interval
  1859. checkDeletion := func(tc *testCase) {
  1860. const secretVal = "someValue"
  1861. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1862. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  1863. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1864. // check values
  1865. oldUID := secret.UID
  1866. Expect(oldUID).NotTo(BeEmpty())
  1867. // delete the related config
  1868. Expect(k8sClient.Delete(context.TODO(), secret))
  1869. var newSecret v1.Secret
  1870. secretLookupKey := types.NamespacedName{
  1871. Name: ExternalSecretTargetSecretName,
  1872. Namespace: ExternalSecretNamespace,
  1873. }
  1874. Eventually(func() bool {
  1875. err := k8sClient.Get(context.Background(), secretLookupKey, &newSecret)
  1876. if err != nil {
  1877. return false
  1878. }
  1879. // new secret should be a new, recreated object with a different UID
  1880. return newSecret.UID != oldUID
  1881. }, timeout, interval).Should(BeTrue())
  1882. }
  1883. }
  1884. // Checks that secret annotation has been written based on the data
  1885. checkSecretDataHashAnnotation := func(tc *testCase) {
  1886. const secretVal = "someValue"
  1887. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1888. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1889. expectedHash := esutils.ObjectHash(map[string][]byte{
  1890. targetProp: []byte(secretVal),
  1891. })
  1892. Expect(secret.Annotations[esv1.AnnotationDataHash]).To(Equal(expectedHash))
  1893. }
  1894. }
  1895. // Checks that secret annotation has been written based on the all the data for merge keys
  1896. checkMergeSecretDataHashAnnotation := func(tc *testCase) {
  1897. const secretVal = "someValue"
  1898. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyMerge
  1899. // create secret beforehand
  1900. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  1901. ObjectMeta: metav1.ObjectMeta{
  1902. Name: ExternalSecretTargetSecretName,
  1903. Namespace: ExternalSecretNamespace,
  1904. },
  1905. Data: map[string][]byte{
  1906. existingKey: []byte(existingVal),
  1907. },
  1908. }, client.FieldOwner(FakeManager))).To(Succeed())
  1909. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1910. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1911. expectedHash := esutils.ObjectHash(map[string][]byte{
  1912. existingKey: []byte(existingVal),
  1913. targetProp: []byte(secretVal),
  1914. })
  1915. Expect(secret.Annotations[esv1.AnnotationDataHash]).To(Equal(expectedHash))
  1916. }
  1917. }
  1918. // When we amend the created kind=secret, refresh operation should be run again regardless of refresh interval
  1919. checkSecretDataHashAnnotationChange := func(tc *testCase) {
  1920. fakeData := map[string][]byte{
  1921. "targetProperty": []byte(FooValue),
  1922. }
  1923. fakeProvider.WithGetSecretMap(fakeData, nil)
  1924. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  1925. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1926. oldHash := secret.Annotations[esv1.AnnotationDataHash]
  1927. oldResourceVersion := secret.ResourceVersion
  1928. Expect(oldHash).NotTo(BeEmpty())
  1929. cleanSecret := secret.DeepCopy()
  1930. secret.Data["new"] = []byte("value")
  1931. secret.ObjectMeta.Annotations[esv1.AnnotationDataHash] = "thisiswronghash"
  1932. Expect(k8sClient.Patch(context.Background(), secret, client.MergeFrom(cleanSecret))).To(Succeed())
  1933. var refreshedSecret v1.Secret
  1934. secretLookupKey := types.NamespacedName{
  1935. Name: ExternalSecretTargetSecretName,
  1936. Namespace: ExternalSecretNamespace,
  1937. }
  1938. Eventually(func() bool {
  1939. err := k8sClient.Get(context.Background(), secretLookupKey, &refreshedSecret)
  1940. if err != nil {
  1941. return false
  1942. }
  1943. // refreshed secret should have a different generation (sign that it was updated), but since
  1944. // the secret source is the same (not changed), the hash should be reverted to an old value
  1945. return refreshedSecret.ResourceVersion != oldResourceVersion && refreshedSecret.Annotations[esv1.AnnotationDataHash] == oldHash
  1946. }, timeout, interval).Should(BeTrue())
  1947. }
  1948. }
  1949. // When we update the template, remaining keys should not be preserved
  1950. templateShouldRewrite := func(tc *testCase) {
  1951. const secretVal = "someValue"
  1952. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1953. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  1954. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  1955. Data: map[string]string{
  1956. "key": `{{.targetProperty}}-foo`,
  1957. },
  1958. }
  1959. tc.checkSecret = func(es *esv1.ExternalSecret, secret *v1.Secret) {
  1960. Expect(secret.Data["key"]).To(Equal([]byte("someValue-foo")))
  1961. newEs := es.DeepCopy()
  1962. newEs.Spec.Target.Template.Data = map[string]string{
  1963. "new": "foo",
  1964. }
  1965. Expect(k8sClient.Patch(context.Background(), newEs, client.MergeFrom(es))).To(Succeed())
  1966. var refreshedSecret v1.Secret
  1967. secretLookupKey := types.NamespacedName{
  1968. Name: ExternalSecretTargetSecretName,
  1969. Namespace: ExternalSecretNamespace,
  1970. }
  1971. Eventually(func() bool {
  1972. err := k8sClient.Get(context.Background(), secretLookupKey, &refreshedSecret)
  1973. if err != nil {
  1974. return false
  1975. }
  1976. // ensure new data value exist
  1977. return string(refreshedSecret.Data["new"]) == "foo"
  1978. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  1979. }
  1980. }
  1981. // When we update the template, remaining keys should not be preserved
  1982. templateShouldMerge := func(tc *testCase) {
  1983. const secretVal = "someValue"
  1984. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1985. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  1986. tc.externalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  1987. MergePolicy: esv1.MergePolicyMerge,
  1988. Data: map[string]string{
  1989. "key": `{{.targetProperty}}-foo`,
  1990. },
  1991. }
  1992. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  1993. Expect(secret.Data["key"]).To(Equal([]byte("someValue-foo")))
  1994. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1995. }
  1996. }
  1997. failWhenFetchedSecretContainsNullBytes := func(tc *testCase) {
  1998. tc.externalSecret.Spec.Data[0].RemoteRef.NullBytePolicy = esv1.ExternalSecretNullBytePolicyFail
  1999. fakeProvider.WithGetSecret([]byte(nullByteSecretVal), nil)
  2000. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2001. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2002. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2003. return false
  2004. }
  2005. return true
  2006. }
  2007. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  2008. secretLookupKey := types.NamespacedName{
  2009. Name: tc.targetSecretName,
  2010. Namespace: ExternalSecretNamespace,
  2011. }
  2012. Consistently(func() bool {
  2013. err := k8sClient.Get(context.Background(), secretLookupKey, &v1.Secret{})
  2014. return apierrors.IsNotFound(err)
  2015. }, time.Second, interval).Should(BeTrue())
  2016. }
  2017. }
  2018. failWhenExtractedSecretContainsNullBytes := func(tc *testCase) {
  2019. tc.externalSecret.Spec.Data = nil
  2020. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  2021. {
  2022. Extract: &esv1.ExternalSecretDataRemoteRef{
  2023. Key: remoteKey,
  2024. NullBytePolicy: esv1.ExternalSecretNullBytePolicyFail,
  2025. },
  2026. },
  2027. }
  2028. fakeProvider.WithGetSecretMap(map[string][]byte{
  2029. "payload": []byte(nullByteSecretVal),
  2030. }, nil)
  2031. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2032. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2033. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2034. return false
  2035. }
  2036. return true
  2037. }
  2038. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  2039. secretLookupKey := types.NamespacedName{
  2040. Name: tc.targetSecretName,
  2041. Namespace: ExternalSecretNamespace,
  2042. }
  2043. Consistently(func() bool {
  2044. err := k8sClient.Get(context.Background(), secretLookupKey, &v1.Secret{})
  2045. return apierrors.IsNotFound(err)
  2046. }, time.Second, interval).Should(BeTrue())
  2047. }
  2048. }
  2049. failWhenFoundSecretContainsNullBytes := func(tc *testCase) {
  2050. tc.externalSecret.Spec.Data = nil
  2051. tc.externalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  2052. {
  2053. Find: &esv1.ExternalSecretFind{
  2054. Name: &esv1.FindName{
  2055. RegExp: "foobar",
  2056. },
  2057. NullBytePolicy: esv1.ExternalSecretNullBytePolicyFail,
  2058. },
  2059. },
  2060. }
  2061. fakeProvider.WithGetAllSecrets(map[string][]byte{
  2062. "payload": []byte(nullByteSecretVal),
  2063. }, nil)
  2064. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2065. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2066. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2067. return false
  2068. }
  2069. return true
  2070. }
  2071. tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
  2072. secretLookupKey := types.NamespacedName{
  2073. Name: tc.targetSecretName,
  2074. Namespace: ExternalSecretNamespace,
  2075. }
  2076. Consistently(func() bool {
  2077. err := k8sClient.Get(context.Background(), secretLookupKey, &v1.Secret{})
  2078. return apierrors.IsNotFound(err)
  2079. }, time.Second, interval).Should(BeTrue())
  2080. }
  2081. }
  2082. useClusterSecretStore := func(tc *testCase) {
  2083. tc.secretStore = &esv1.ClusterSecretStore{
  2084. ObjectMeta: metav1.ObjectMeta{
  2085. Name: ExternalSecretStore,
  2086. },
  2087. Spec: esv1.SecretStoreSpec{
  2088. Provider: &esv1.SecretStoreProvider{
  2089. AWS: &esv1.AWSProvider{
  2090. Service: esv1.AWSServiceSecretsManager,
  2091. },
  2092. },
  2093. },
  2094. }
  2095. tc.externalSecret.Spec.SecretStoreRef.Kind = esv1.ClusterSecretStoreKind
  2096. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  2097. }
  2098. // Secret is created when ClusterSecretStore has no conditions
  2099. noConditionsSecretCreated := func(tc *testCase) {
  2100. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  2101. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  2102. }
  2103. }
  2104. // Secret is not created when ClusterSecretStore has a single non-matching string condition
  2105. noSecretCreatedWhenNamespaceDoesntMatchStringCondition := func(tc *testCase) {
  2106. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2107. {
  2108. Namespaces: []string{"some-other-ns"},
  2109. },
  2110. }
  2111. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2112. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2113. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2114. return false
  2115. }
  2116. return true
  2117. }
  2118. }
  2119. // Secret is not created when ClusterSecretStore has a single non-matching string condition with multiple names
  2120. noSecretCreatedWhenNamespaceDoesntMatchStringConditionWithMultipleNames := func(tc *testCase) {
  2121. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2122. {
  2123. Namespaces: []string{"some-other-ns", "another-ns"},
  2124. },
  2125. }
  2126. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2127. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2128. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2129. return false
  2130. }
  2131. return true
  2132. }
  2133. }
  2134. // Secret is not created when ClusterSecretStore has a multiple non-matching string condition
  2135. noSecretCreatedWhenNamespaceDoesntMatchMultipleStringCondition := func(tc *testCase) {
  2136. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2137. {
  2138. Namespaces: []string{"some-other-ns"},
  2139. },
  2140. {
  2141. Namespaces: []string{"another-ns"},
  2142. },
  2143. }
  2144. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2145. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2146. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2147. return false
  2148. }
  2149. return true
  2150. }
  2151. }
  2152. // Secret is created when ClusterSecretStore has a single matching string condition
  2153. secretCreatedWhenNamespaceMatchesSingleStringCondition := func(tc *testCase) {
  2154. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2155. {
  2156. Namespaces: []string{ExternalSecretNamespace},
  2157. },
  2158. }
  2159. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  2160. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  2161. }
  2162. }
  2163. // Secret is created when ClusterSecretStore has a multiple string conditions, one matching
  2164. secretCreatedWhenNamespaceMatchesMultipleStringConditions := func(tc *testCase) {
  2165. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2166. {
  2167. Namespaces: []string{ExternalSecretNamespace, "some-other-ns"},
  2168. },
  2169. }
  2170. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  2171. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  2172. }
  2173. }
  2174. // Secret is not created when ClusterSecretStore has a single non-matching label condition
  2175. noSecretCreatedWhenNamespaceDoesntMatchLabelCondition := func(tc *testCase) {
  2176. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2177. {
  2178. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"some-label-key": "some-label-value"}},
  2179. },
  2180. }
  2181. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2182. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2183. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2184. return false
  2185. }
  2186. return true
  2187. }
  2188. }
  2189. // Secret is created when ClusterSecretStore has a single matching label condition
  2190. secretCreatedWhenNamespaceMatchOnlyLabelCondition := func(tc *testCase) {
  2191. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2192. {
  2193. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue}},
  2194. },
  2195. }
  2196. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  2197. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  2198. }
  2199. }
  2200. // Secret is not created when ClusterSecretStore has a partially matching label condition
  2201. noSecretCreatedWhenNamespacePartiallyMatchLabelCondition := func(tc *testCase) {
  2202. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2203. {
  2204. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue, "some-label-key": "some-label-value"}},
  2205. },
  2206. }
  2207. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2208. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2209. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2210. return false
  2211. }
  2212. return true
  2213. }
  2214. }
  2215. // Secret is created when ClusterSecretStore has at least one matching label condition
  2216. secretCreatedWhenNamespaceMatchOneLabelCondition := func(tc *testCase) {
  2217. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2218. {
  2219. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue}},
  2220. },
  2221. {
  2222. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"some-label-key": "some-label-value"}},
  2223. },
  2224. }
  2225. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  2226. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  2227. }
  2228. }
  2229. // Secret is created when ClusterSecretStore has multiple matching conditions
  2230. secretCreatedWhenNamespaceMatchMultipleConditions := func(tc *testCase) {
  2231. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2232. {
  2233. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue}},
  2234. },
  2235. {
  2236. Namespaces: []string{ExternalSecretNamespace},
  2237. },
  2238. }
  2239. tc.checkSecret = func(_ *esv1.ExternalSecret, secret *v1.Secret) {
  2240. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  2241. }
  2242. }
  2243. // Secret is not created when ClusterSecretStore has multiple non-matching conditions
  2244. noSecretCreatedWhenNamespaceMatchMultipleNonMatchingConditions := func(tc *testCase) {
  2245. tc.secretStore.GetSpec().Conditions = []esv1.ClusterSecretStoreCondition{
  2246. {
  2247. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"some-label-key": "some-label-value"}},
  2248. },
  2249. {
  2250. Namespaces: []string{"some-other-ns"},
  2251. },
  2252. }
  2253. tc.checkCondition = func(es *esv1.ExternalSecret) bool {
  2254. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2255. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
  2256. return false
  2257. }
  2258. return true
  2259. }
  2260. }
  2261. DescribeTable(
  2262. "When reconciling an ExternalSecret",
  2263. func(tweaks ...testTweaks) {
  2264. tc := makeDefaultTestcase()
  2265. for _, tweak := range tweaks {
  2266. tweak(tc)
  2267. }
  2268. ctx := context.Background()
  2269. By("creating a secret store and external secret")
  2270. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2271. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  2272. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  2273. createdES := &esv1.ExternalSecret{}
  2274. By("checking the es condition")
  2275. Eventually(func() bool {
  2276. err := k8sClient.Get(ctx, esKey, createdES)
  2277. if err != nil {
  2278. return false
  2279. }
  2280. return tc.checkCondition(createdES)
  2281. }, timeout, interval).Should(BeTrue())
  2282. tc.checkExternalSecret(createdES)
  2283. // this must be optional so we can test faulty es configuration
  2284. if tc.checkSecret != nil {
  2285. syncedSecret := &v1.Secret{}
  2286. secretLookupKey := types.NamespacedName{
  2287. Name: tc.targetSecretName,
  2288. Namespace: ExternalSecretNamespace,
  2289. }
  2290. if createdES.Spec.Target.Name == "" {
  2291. secretLookupKey = types.NamespacedName{
  2292. Name: ExternalSecretName,
  2293. Namespace: ExternalSecretNamespace,
  2294. }
  2295. }
  2296. Eventually(func() bool {
  2297. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  2298. return err == nil
  2299. }, timeout, interval).Should(BeTrue())
  2300. tc.checkSecret(createdES, syncedSecret)
  2301. }
  2302. },
  2303. Entry("should recreate deleted secret", checkDeletion),
  2304. Entry("should create proper hash annotation for the external secret", checkSecretDataHashAnnotation),
  2305. Entry("should create proper hash annotation for the external secret with creationPolicy=Merge", checkMergeSecretDataHashAnnotation),
  2306. Entry("es deletes orphaned secrets", deleteOrphanedSecrets),
  2307. Entry("should refresh when the hash annotation doesn't correspond to secret data", checkSecretDataHashAnnotationChange),
  2308. Entry("should use external secret name if target secret name isn't defined", syncWithoutTargetName),
  2309. Entry("should sync to target secrets with naming bigger than 63 characters", syncBigNames),
  2310. Entry("should expose the secret as a provisioned service binding secret", syncBindingSecret),
  2311. Entry("should not expose a provisioned service when no secret is synced", skipBindingSecret),
  2312. Entry("should set labels and annotations from the ExternalSecret", syncLabelsAnnotations),
  2313. Entry("should merge labels and annotations to the ones owned by other entity", mergeLabelsAnnotations),
  2314. Entry("should removed outdated labels and annotations", removeOutdatedLabelsAnnotations),
  2315. Entry("should set prometheus counters", checkPrometheusCounters),
  2316. Entry("should merge with existing secret using creationPolicy=Merge", mergeWithSecret),
  2317. Entry("should kick reconciliation when secret changes using creationPolicy=Merge", mergeWithSecretUpdate),
  2318. Entry("should error if secret doesn't exist when using creationPolicy=Merge", mergeWithSecretErr),
  2319. Entry("should not resolve conflicts with creationPolicy=Merge", mergeWithConflict),
  2320. Entry("should not update unchanged secret using creationPolicy=Merge", mergeWithSecretNoChange),
  2321. Entry("should not delete pre-existing secret with creationPolicy=Orphan", createSecretPolicyOrphan),
  2322. Entry("should sync cluster generator ref", syncWithClusterGeneratorRef),
  2323. Entry("should sync with generatorRef", syncWithGeneratorRef),
  2324. Entry("should not process generatorRef with mismatching controller field", ignoreMismatchControllerForGeneratorRef),
  2325. Entry("should sync with multiple secret stores via sourceRef", syncWithMultipleSecretStores),
  2326. Entry("should sync with template", syncWithTemplate),
  2327. Entry("should sync with template engine v2", syncWithTemplateV2),
  2328. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  2329. Entry("should sync template from keys and values", syncTemplateFromKeysAndValues),
  2330. Entry("should sync template from literal", syncTemplateFromLiteral),
  2331. Entry("should fail when fetched secret data contains null bytes and remoteRef.nullBytePolicy=Fail", failWhenFetchedSecretContainsNullBytes),
  2332. Entry("should fail when extracted secret data contains null bytes and extract.nullBytePolicy=Fail", failWhenExtractedSecretContainsNullBytes),
  2333. Entry("should fail when found secret data contains null bytes and find.nullBytePolicy=Fail", failWhenFoundSecretContainsNullBytes),
  2334. Entry("should update template if ExternalSecret is updated", templateShouldRewrite),
  2335. Entry("should keep data with templates if MergePolicy=Merge", templateShouldMerge),
  2336. Entry("should refresh secret from template", refreshWithTemplate),
  2337. Entry("should be able to use only metadata from template", onlyMetadataFromTemplate),
  2338. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  2339. Entry("should refresh secret map when provider secret changes", refreshSecretValueMap),
  2340. Entry("should refresh secret map when provider secret changes when using a template", refreshSecretValueMapTemplate),
  2341. Entry("should not refresh secret value when provider secret changes but refreshInterval is zero", refreshintervalZero),
  2342. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  2343. Entry("should rewrite secret using dataFrom", syncAndRewriteWithDataFrom),
  2344. Entry("should not automatically convert from extract if rewrite is used", invalidExtractKeysErrCondition),
  2345. Entry("should fetch secret using dataFrom.find", syncDataFromFind),
  2346. Entry("should rewrite secret using dataFrom.find", syncAndRewriteDataFromFind),
  2347. Entry("should not automatically convert from find if rewrite is used", invalidFindKeysErrCondition),
  2348. Entry("should fetch secret using dataFrom and a template", syncWithDataFromTemplate),
  2349. Entry("should set error condition when provider errors", providerErrCondition),
  2350. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  2351. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  2352. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  2353. Entry("should not process cluster secret store when it is disabled", ignoreClusterSecretStoreWhenDisabled),
  2354. Entry("should eventually delete target secret with deletionPolicy=Delete", deletionPolicyDelete),
  2355. Entry("should not delete target secret with deletionPolicy=Retain", deletionPolicyRetain),
  2356. Entry("should update the status properly even if the deletionPolicy is Retain and the data is empty", deletionPolicyRetainEmptyData),
  2357. Entry("should not delete pre-existing secret with deletionPolicy=Merge", deletionPolicyMerge),
  2358. Entry("secret is created when there are no conditions for the cluster secret store", useClusterSecretStore, noConditionsSecretCreated),
  2359. Entry(
  2360. "secret is not created when the condition for the cluster secret store states a different namespace single string condition",
  2361. useClusterSecretStore,
  2362. noSecretCreatedWhenNamespaceDoesntMatchStringCondition,
  2363. ),
  2364. Entry(
  2365. "secret is not created when the condition for the cluster secret store states a different namespace single string condition with multiple names",
  2366. useClusterSecretStore,
  2367. noSecretCreatedWhenNamespaceDoesntMatchStringConditionWithMultipleNames,
  2368. ),
  2369. Entry(
  2370. "secret is not created when the condition for the cluster secret store states a different namespace multiple string conditions",
  2371. useClusterSecretStore,
  2372. noSecretCreatedWhenNamespaceDoesntMatchMultipleStringCondition,
  2373. ),
  2374. Entry(
  2375. "secret is created when the condition for the cluster secret store has only one matching namespace by string condition",
  2376. useClusterSecretStore,
  2377. secretCreatedWhenNamespaceMatchesSingleStringCondition,
  2378. ),
  2379. Entry(
  2380. "secret is created when the condition for the cluster secret store has one matching namespace of multiple namespaces by string condition",
  2381. useClusterSecretStore,
  2382. secretCreatedWhenNamespaceMatchesMultipleStringConditions,
  2383. ),
  2384. Entry(
  2385. "secret is not created when the condition for the cluster secret store states a non-matching label condition",
  2386. useClusterSecretStore,
  2387. noSecretCreatedWhenNamespaceDoesntMatchLabelCondition,
  2388. ),
  2389. Entry("secret is created when the condition for the cluster secret store states a single matching label condition", useClusterSecretStore, secretCreatedWhenNamespaceMatchOnlyLabelCondition),
  2390. Entry(
  2391. "secret is not created when the condition for the cluster secret store states a partially-matching label condition",
  2392. useClusterSecretStore,
  2393. noSecretCreatedWhenNamespacePartiallyMatchLabelCondition,
  2394. ),
  2395. Entry("secret is created when one of the label conditions for the cluster secret store matches", useClusterSecretStore, secretCreatedWhenNamespaceMatchOneLabelCondition),
  2396. Entry("secret is created when the namespaces matches multiple cluster secret store conditions", useClusterSecretStore, secretCreatedWhenNamespaceMatchMultipleConditions),
  2397. Entry(
  2398. "secret is not created when the namespaces doesn't match any of multiple cluster secret store conditions",
  2399. useClusterSecretStore,
  2400. noSecretCreatedWhenNamespaceMatchMultipleNonMatchingConditions,
  2401. ),
  2402. )
  2403. // Regression coverage for issue #6640: refreshPolicy CreatedOnce tracks its
  2404. // "once" on the ExternalSecret's own status, not on whether the target Secret
  2405. // already exists. Recreating the ExternalSecret object therefore re-syncs and
  2406. // overwrites the existing target Secret unless target.immutable is set.
  2407. Context("RefreshPolicy=CreatedOnce recreation (issue #6640)", func() {
  2408. It("overwrites an existing target Secret when the ExternalSecret is recreated without immutable", func() {
  2409. ctx := context.Background()
  2410. const firstVal = "created-once-first"
  2411. const secondVal = "created-once-second"
  2412. tc := makeDefaultTestcase()
  2413. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2414. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  2415. fakeProvider.WithGetSecret([]byte(firstVal), nil)
  2416. By("creating the secret store and external secret")
  2417. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2418. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2419. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2420. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  2421. By("waiting for the target secret to be created with the first value")
  2422. Eventually(func() string {
  2423. sec := &v1.Secret{}
  2424. if err := k8sClient.Get(ctx, secretKey, sec); err != nil {
  2425. return ""
  2426. }
  2427. return string(sec.Data[targetProp])
  2428. }, timeout, interval).Should(Equal(firstVal))
  2429. By("changing the provider value; CreatedOnce must not refresh the live ExternalSecret")
  2430. fakeProvider.WithGetSecret([]byte(secondVal), nil)
  2431. Consistently(func() string {
  2432. sec := &v1.Secret{}
  2433. if err := k8sClient.Get(ctx, secretKey, sec); err != nil {
  2434. return ""
  2435. }
  2436. return string(sec.Data[targetProp])
  2437. }, time.Second*3, interval).Should(Equal(firstVal))
  2438. By("deleting the ExternalSecret; creationPolicy=Orphan keeps the target secret")
  2439. Expect(k8sClient.Delete(ctx, tc.externalSecret)).To(Succeed())
  2440. Eventually(func() bool {
  2441. return apierrors.IsNotFound(k8sClient.Get(ctx, esKey, &esv1.ExternalSecret{}))
  2442. }, timeout, interval).Should(BeTrue())
  2443. sec := &v1.Secret{}
  2444. Expect(k8sClient.Get(ctx, secretKey, sec)).To(Succeed())
  2445. Expect(string(sec.Data[targetProp])).To(Equal(firstVal))
  2446. By("recreating an identical ExternalSecret")
  2447. recreated := makeDefaultTestcase().externalSecret
  2448. recreated.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2449. recreated.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  2450. Expect(k8sClient.Create(ctx, recreated)).To(Succeed())
  2451. By("observing the pre-existing target secret is overwritten with the new value")
  2452. Eventually(func() string {
  2453. s := &v1.Secret{}
  2454. if err := k8sClient.Get(ctx, secretKey, s); err != nil {
  2455. return ""
  2456. }
  2457. return string(s.Data[targetProp])
  2458. }, timeout, interval).Should(Equal(secondVal))
  2459. })
  2460. It("preserves an existing target Secret when the ExternalSecret is recreated with target.immutable", func() {
  2461. ctx := context.Background()
  2462. const firstVal = "immutable-first"
  2463. const secondVal = "immutable-second"
  2464. tc := makeDefaultTestcase()
  2465. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2466. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  2467. tc.externalSecret.Spec.Target.Immutable = true
  2468. fakeProvider.WithGetSecret([]byte(firstVal), nil)
  2469. By("creating the secret store and immutable external secret")
  2470. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2471. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2472. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2473. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  2474. By("waiting for the immutable target secret to be created")
  2475. Eventually(func() string {
  2476. sec := &v1.Secret{}
  2477. if err := k8sClient.Get(ctx, secretKey, sec); err != nil {
  2478. return ""
  2479. }
  2480. return string(sec.Data[targetProp])
  2481. }, timeout, interval).Should(Equal(firstVal))
  2482. sec := &v1.Secret{}
  2483. Expect(k8sClient.Get(ctx, secretKey, sec)).To(Succeed())
  2484. Expect(sec.Immutable).ToNot(BeNil())
  2485. Expect(*sec.Immutable).To(BeTrue())
  2486. By("changing the provider value and deleting the ExternalSecret")
  2487. fakeProvider.WithGetSecret([]byte(secondVal), nil)
  2488. Expect(k8sClient.Delete(ctx, tc.externalSecret)).To(Succeed())
  2489. Eventually(func() bool {
  2490. return apierrors.IsNotFound(k8sClient.Get(ctx, esKey, &esv1.ExternalSecret{}))
  2491. }, timeout, interval).Should(BeTrue())
  2492. By("recreating the identical immutable ExternalSecret")
  2493. recreated := makeDefaultTestcase().externalSecret
  2494. recreated.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2495. recreated.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  2496. recreated.Spec.Target.Immutable = true
  2497. Expect(k8sClient.Create(ctx, recreated)).To(Succeed())
  2498. By("waiting for the recreated ExternalSecret to reconcile to Ready")
  2499. Eventually(func() bool {
  2500. es := &esv1.ExternalSecret{}
  2501. if err := k8sClient.Get(ctx, esKey, es); err != nil {
  2502. return false
  2503. }
  2504. cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
  2505. return cond != nil && cond.Status == v1.ConditionTrue
  2506. }, timeout, interval).Should(BeTrue())
  2507. By("confirming the pre-existing secret data is preserved despite the changed provider value")
  2508. Consistently(func() string {
  2509. s := &v1.Secret{}
  2510. if err := k8sClient.Get(ctx, secretKey, s); err != nil {
  2511. return ""
  2512. }
  2513. return string(s.Data[targetProp])
  2514. }, time.Second*5, interval).Should(Equal(firstVal))
  2515. })
  2516. It("recreates a deleted target Secret even with target.immutable set (creationPolicy Owner)", func() {
  2517. // immutable only blocks overwriting an EXISTING Secret's data; a
  2518. // deleted Secret no longer exists, so the controller creates a fresh
  2519. // one on the next reconcile (the by-label Secret watch enqueues it).
  2520. ctx := context.Background()
  2521. const val = "immutable-recreate"
  2522. tc := makeDefaultTestcase()
  2523. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2524. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyOwner
  2525. tc.externalSecret.Spec.Target.Immutable = true
  2526. fakeProvider.WithGetSecret([]byte(val), nil)
  2527. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2528. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2529. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2530. By("waiting for the immutable target secret to be created")
  2531. secret := &v1.Secret{}
  2532. Eventually(func() bool {
  2533. if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
  2534. return false
  2535. }
  2536. return secret.UID != "" && string(secret.Data[targetProp]) == val
  2537. }, timeout, interval).Should(BeTrue())
  2538. Expect(secret.Immutable).ToNot(BeNil())
  2539. Expect(*secret.Immutable).To(BeTrue())
  2540. oldUID := secret.UID
  2541. By("deleting the target secret")
  2542. Expect(k8sClient.Delete(ctx, secret)).To(Succeed())
  2543. By("observing the controller recreates it as a new object")
  2544. Eventually(func() bool {
  2545. recreated := &v1.Secret{}
  2546. if err := k8sClient.Get(ctx, secretKey, recreated); err != nil {
  2547. return false
  2548. }
  2549. return recreated.UID != "" && recreated.UID != oldUID
  2550. }, timeout, interval).Should(BeTrue())
  2551. })
  2552. It("does not recreate a deleted target Secret with creationPolicy Orphan and refreshPolicy CreatedOnce", func() {
  2553. // With Orphan the controller treats a missing Secret as valid, and
  2554. // CreatedOnce never triggers another refresh, so a deleted Secret is
  2555. // not recreated. immutable is irrelevant to this outcome.
  2556. ctx := context.Background()
  2557. const val = "orphan-created-once"
  2558. tc := makeDefaultTestcase()
  2559. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2560. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  2561. tc.externalSecret.Spec.Target.Immutable = true
  2562. fakeProvider.WithGetSecret([]byte(val), nil)
  2563. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2564. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2565. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2566. By("waiting for the target secret to be created")
  2567. secret := &v1.Secret{}
  2568. Eventually(func() bool {
  2569. if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
  2570. return false
  2571. }
  2572. return secret.UID != "" && string(secret.Data[targetProp]) == val
  2573. }, timeout, interval).Should(BeTrue())
  2574. By("deleting the target secret")
  2575. Expect(k8sClient.Delete(ctx, secret)).To(Succeed())
  2576. Eventually(func() bool {
  2577. return apierrors.IsNotFound(k8sClient.Get(ctx, secretKey, &v1.Secret{}))
  2578. }, timeout, interval).Should(BeTrue())
  2579. By("confirming the controller leaves it deleted")
  2580. Consistently(func() bool {
  2581. return apierrors.IsNotFound(k8sClient.Get(ctx, secretKey, &v1.Secret{}))
  2582. }, time.Second*5, interval).Should(BeTrue())
  2583. })
  2584. It("with Orphan + Periodic + immutable: creates, recreates on delete, and keeps the Secret if the ExternalSecret is deleted", func() {
  2585. // Orphan keeps the Secret when the ExternalSecret goes away (no
  2586. // ownerReference), Periodic re-triggers a sync so a deleted Secret is
  2587. // recreated at the next interval, and immutable stops the periodic
  2588. // re-sync from rewriting the live value once it exists.
  2589. ctx := context.Background()
  2590. const val = "orphan-periodic-immutable"
  2591. tc := makeDefaultTestcase()
  2592. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyPeriodic
  2593. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  2594. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyOrphan
  2595. tc.externalSecret.Spec.Target.Immutable = true
  2596. fakeProvider.WithGetSecret([]byte(val), nil)
  2597. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2598. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2599. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2600. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  2601. By("creating and populating an immutable target secret")
  2602. secret := &v1.Secret{}
  2603. Eventually(func() bool {
  2604. if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
  2605. return false
  2606. }
  2607. return secret.UID != "" && string(secret.Data[targetProp]) == val
  2608. }, timeout, interval).Should(BeTrue())
  2609. Expect(secret.Immutable).ToNot(BeNil())
  2610. Expect(*secret.Immutable).To(BeTrue())
  2611. oldUID := secret.UID
  2612. By("recreating and repopulating it after the target secret is deleted")
  2613. Expect(k8sClient.Delete(ctx, secret)).To(Succeed())
  2614. Eventually(func() bool {
  2615. recreated := &v1.Secret{}
  2616. if err := k8sClient.Get(ctx, secretKey, recreated); err != nil {
  2617. return false
  2618. }
  2619. return recreated.UID != "" && recreated.UID != oldUID &&
  2620. string(recreated.Data[targetProp]) == val &&
  2621. recreated.Immutable != nil && *recreated.Immutable
  2622. }, timeout, interval).Should(BeTrue())
  2623. By("keeping the target secret after the ExternalSecret is deleted")
  2624. Expect(k8sClient.Delete(ctx, tc.externalSecret)).To(Succeed())
  2625. Eventually(func() bool {
  2626. return apierrors.IsNotFound(k8sClient.Get(ctx, esKey, &esv1.ExternalSecret{}))
  2627. }, timeout, interval).Should(BeTrue())
  2628. Consistently(func() bool {
  2629. return k8sClient.Get(ctx, secretKey, &v1.Secret{}) == nil
  2630. }, time.Second*5, interval).Should(BeTrue())
  2631. })
  2632. })
  2633. Context("creationPolicy=CreateOrMerge", func() {
  2634. It("merges into an existing Secret and preserves foreign keys", func() {
  2635. ctx := context.Background()
  2636. const foreignKey = "owned-elsewhere"
  2637. const foreignVal = "keepme"
  2638. const val = "from-provider"
  2639. // a Secret created by another party, with a key ESO does not manage
  2640. Expect(k8sClient.Create(ctx, &v1.Secret{
  2641. ObjectMeta: metav1.ObjectMeta{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace},
  2642. Data: map[string][]byte{foreignKey: []byte(foreignVal)},
  2643. })).To(Succeed())
  2644. tc := makeDefaultTestcase()
  2645. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyCreateOrMerge
  2646. fakeProvider.WithGetSecret([]byte(val), nil)
  2647. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2648. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2649. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2650. By("merging the provider key while keeping the foreign key")
  2651. Eventually(func() bool {
  2652. s := &v1.Secret{}
  2653. if err := k8sClient.Get(ctx, secretKey, s); err != nil {
  2654. return false
  2655. }
  2656. return string(s.Data[targetProp]) == val && string(s.Data[foreignKey]) == foreignVal
  2657. }, timeout, interval).Should(BeTrue())
  2658. })
  2659. It("creates when missing, recreates a deleted target immediately under CreatedOnce, and retains on ES delete", func() {
  2660. ctx := context.Background()
  2661. const val = "createormerge-val"
  2662. tc := makeDefaultTestcase()
  2663. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2664. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyCreateOrMerge
  2665. // no RefreshInterval: recreation must not depend on it
  2666. fakeProvider.WithGetSecret([]byte(val), nil)
  2667. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2668. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2669. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2670. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  2671. By("creating and populating the target secret")
  2672. secret := &v1.Secret{}
  2673. Eventually(func() bool {
  2674. if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
  2675. return false
  2676. }
  2677. return secret.UID != "" && string(secret.Data[targetProp]) == val
  2678. }, timeout, interval).Should(BeTrue())
  2679. oldUID := secret.UID
  2680. By("recreating it immediately after the target secret is deleted")
  2681. Expect(k8sClient.Delete(ctx, secret)).To(Succeed())
  2682. Eventually(func() bool {
  2683. r := &v1.Secret{}
  2684. if err := k8sClient.Get(ctx, secretKey, r); err != nil {
  2685. return false
  2686. }
  2687. return r.UID != "" && r.UID != oldUID && string(r.Data[targetProp]) == val
  2688. }, timeout, interval).Should(BeTrue())
  2689. By("keeping the target secret after the ExternalSecret is deleted")
  2690. Expect(k8sClient.Delete(ctx, tc.externalSecret)).To(Succeed())
  2691. Eventually(func() bool {
  2692. return apierrors.IsNotFound(k8sClient.Get(ctx, esKey, &esv1.ExternalSecret{}))
  2693. }, timeout, interval).Should(BeTrue())
  2694. Consistently(func() bool {
  2695. return k8sClient.Get(ctx, secretKey, &v1.Secret{}) == nil
  2696. }, time.Second*5, interval).Should(BeTrue())
  2697. })
  2698. It("with immutable, creates and freezes the Secret and recreates it after deletion", func() {
  2699. ctx := context.Background()
  2700. const val = "createormerge-immutable"
  2701. tc := makeDefaultTestcase()
  2702. tc.externalSecret.Spec.RefreshPolicy = esv1.RefreshPolicyCreatedOnce
  2703. tc.externalSecret.Spec.Target.CreationPolicy = esv1.CreatePolicyCreateOrMerge
  2704. tc.externalSecret.Spec.Target.Immutable = true
  2705. fakeProvider.WithGetSecret([]byte(val), nil)
  2706. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  2707. Expect(k8sClient.Create(ctx, tc.externalSecret)).To(Succeed())
  2708. secretKey := types.NamespacedName{Name: ExternalSecretTargetSecretName, Namespace: ExternalSecretNamespace}
  2709. By("creating a populated, immutable target secret")
  2710. secret := &v1.Secret{}
  2711. Eventually(func() bool {
  2712. if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
  2713. return false
  2714. }
  2715. return secret.UID != "" && string(secret.Data[targetProp]) == val
  2716. }, timeout, interval).Should(BeTrue())
  2717. Expect(secret.Immutable).ToNot(BeNil())
  2718. Expect(*secret.Immutable).To(BeTrue())
  2719. oldUID := secret.UID
  2720. By("recreating a fresh immutable secret after deletion")
  2721. Expect(k8sClient.Delete(ctx, secret)).To(Succeed())
  2722. Eventually(func() bool {
  2723. r := &v1.Secret{}
  2724. if err := k8sClient.Get(ctx, secretKey, r); err != nil {
  2725. return false
  2726. }
  2727. return r.UID != "" && r.UID != oldUID && string(r.Data[targetProp]) == val &&
  2728. r.Immutable != nil && *r.Immutable
  2729. }, timeout, interval).Should(BeTrue())
  2730. })
  2731. })
  2732. })
  2733. var _ = Describe("ExternalSecret refresh logic", func() {
  2734. Context("secret refresh", func() {
  2735. It("should refresh when resource version does not match", func() {
  2736. Expect(shouldRefresh(&esv1.ExternalSecret{
  2737. Spec: esv1.ExternalSecretSpec{
  2738. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  2739. },
  2740. Status: esv1.ExternalSecretStatus{
  2741. SyncedResourceVersion: "some resource version",
  2742. },
  2743. })).To(BeTrue())
  2744. })
  2745. It("should refresh when labels change", func() {
  2746. es := &esv1.ExternalSecret{
  2747. ObjectMeta: metav1.ObjectMeta{
  2748. Generation: 1,
  2749. Labels: map[string]string{
  2750. "foo": "bar",
  2751. },
  2752. },
  2753. Spec: esv1.ExternalSecretSpec{
  2754. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  2755. },
  2756. Status: esv1.ExternalSecretStatus{
  2757. RefreshTime: metav1.Now(),
  2758. },
  2759. }
  2760. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  2761. // this should not refresh, rv matches object
  2762. Expect(shouldRefresh(es)).To(BeFalse())
  2763. // change labels without changing the syncedResourceVersion and expect refresh
  2764. es.ObjectMeta.Labels["new"] = "w00t"
  2765. Expect(shouldRefresh(es)).To(BeTrue())
  2766. })
  2767. It("should refresh when annotations change", func() {
  2768. es := &esv1.ExternalSecret{
  2769. ObjectMeta: metav1.ObjectMeta{
  2770. Generation: 1,
  2771. Annotations: map[string]string{
  2772. "foo": "bar",
  2773. },
  2774. },
  2775. Spec: esv1.ExternalSecretSpec{
  2776. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  2777. },
  2778. Status: esv1.ExternalSecretStatus{
  2779. RefreshTime: metav1.Now(),
  2780. },
  2781. }
  2782. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  2783. // this should not refresh, rv matches object
  2784. Expect(shouldRefresh(es)).To(BeFalse())
  2785. // change annotations without changing the syncedResourceVersion and expect refresh
  2786. es.ObjectMeta.Annotations["new"] = "w00t"
  2787. Expect(shouldRefresh(es)).To(BeTrue())
  2788. })
  2789. It("should refresh when generation has changed", func() {
  2790. es := &esv1.ExternalSecret{
  2791. ObjectMeta: metav1.ObjectMeta{
  2792. Generation: 1,
  2793. },
  2794. Spec: esv1.ExternalSecretSpec{
  2795. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  2796. },
  2797. Status: esv1.ExternalSecretStatus{
  2798. RefreshTime: metav1.Now(),
  2799. },
  2800. }
  2801. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  2802. Expect(shouldRefresh(es)).To(BeFalse())
  2803. // update gen -> refresh
  2804. es.ObjectMeta.Generation = 2
  2805. Expect(shouldRefresh(es)).To(BeTrue())
  2806. })
  2807. It("should skip refresh when refreshInterval is 0", func() {
  2808. es := &esv1.ExternalSecret{
  2809. ObjectMeta: metav1.ObjectMeta{
  2810. Generation: 1,
  2811. },
  2812. Spec: esv1.ExternalSecretSpec{
  2813. RefreshInterval: &metav1.Duration{Duration: 0},
  2814. },
  2815. Status: esv1.ExternalSecretStatus{},
  2816. }
  2817. // resource version matches
  2818. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  2819. Expect(shouldRefresh(es)).To(BeFalse())
  2820. })
  2821. It("should refresh when refresh interval has passed", func() {
  2822. es := &esv1.ExternalSecret{
  2823. ObjectMeta: metav1.ObjectMeta{
  2824. Generation: 1,
  2825. },
  2826. Spec: esv1.ExternalSecretSpec{
  2827. RefreshInterval: &metav1.Duration{Duration: time.Second},
  2828. },
  2829. Status: esv1.ExternalSecretStatus{
  2830. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  2831. },
  2832. }
  2833. // resource version matches
  2834. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  2835. Expect(shouldRefresh(es)).To(BeTrue())
  2836. })
  2837. It("should refresh when no refresh time was set", func() {
  2838. es := &esv1.ExternalSecret{
  2839. ObjectMeta: metav1.ObjectMeta{
  2840. Generation: 1,
  2841. },
  2842. Spec: esv1.ExternalSecretSpec{
  2843. RefreshInterval: &metav1.Duration{Duration: time.Second},
  2844. },
  2845. Status: esv1.ExternalSecretStatus{},
  2846. }
  2847. // resource version matches
  2848. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  2849. Expect(shouldRefresh(es)).To(BeTrue())
  2850. })
  2851. })
  2852. Context("objectmeta hash", func() {
  2853. It("should produce different hashes for different k/v pairs", func() {
  2854. h1 := ctrlutil.HashMeta(metav1.ObjectMeta{
  2855. Generation: 1,
  2856. Annotations: map[string]string{
  2857. "foo": "bar",
  2858. },
  2859. })
  2860. h2 := ctrlutil.HashMeta(metav1.ObjectMeta{
  2861. Generation: 1,
  2862. Annotations: map[string]string{
  2863. "foo": "bing",
  2864. },
  2865. })
  2866. Expect(h1).ToNot(Equal(h2))
  2867. })
  2868. It("should produce different hashes for different generations but same label/annotations", func() {
  2869. h1 := ctrlutil.HashMeta(metav1.ObjectMeta{
  2870. Generation: 1,
  2871. Annotations: map[string]string{
  2872. "foo": "bar",
  2873. },
  2874. Labels: map[string]string{
  2875. "foo": "bar",
  2876. },
  2877. })
  2878. h2 := ctrlutil.HashMeta(metav1.ObjectMeta{
  2879. Generation: 2,
  2880. Annotations: map[string]string{
  2881. "foo": "bar",
  2882. },
  2883. Labels: map[string]string{
  2884. "foo": "bar",
  2885. },
  2886. })
  2887. Expect(h1).To(Equal(h2))
  2888. })
  2889. It("should produce the same hash for the same k/v pairs", func() {
  2890. h1 := ctrlutil.HashMeta(metav1.ObjectMeta{
  2891. Generation: 1,
  2892. })
  2893. h2 := ctrlutil.HashMeta(metav1.ObjectMeta{
  2894. Generation: 1,
  2895. })
  2896. Expect(h1).To(Equal(h2))
  2897. h1 = ctrlutil.HashMeta(metav1.ObjectMeta{
  2898. Generation: 1,
  2899. Annotations: map[string]string{
  2900. "foo": "bar",
  2901. },
  2902. })
  2903. h2 = ctrlutil.HashMeta(metav1.ObjectMeta{
  2904. Generation: 1,
  2905. Annotations: map[string]string{
  2906. "foo": "bar",
  2907. },
  2908. })
  2909. Expect(h1).To(Equal(h2))
  2910. })
  2911. })
  2912. })
  2913. var _ = Describe("ExternalSecret update predicate", func() {
  2914. It("should ignore status-only updates", func() {
  2915. oldES := &esv1.ExternalSecret{
  2916. ObjectMeta: metav1.ObjectMeta{
  2917. Name: "foo",
  2918. Namespace: "default",
  2919. Generation: 1,
  2920. },
  2921. Status: esv1.ExternalSecretStatus{
  2922. RefreshTime: metav1.Now(),
  2923. },
  2924. }
  2925. newES := oldES.DeepCopy()
  2926. newES.Status.RefreshTime = metav1.NewTime(time.Now().Add(time.Minute))
  2927. Expect(shouldEnqueueExternalSecretUpdate(oldES, newES)).To(BeFalse())
  2928. })
  2929. It("should enqueue when generation changes", func() {
  2930. oldES := &esv1.ExternalSecret{
  2931. ObjectMeta: metav1.ObjectMeta{
  2932. Name: "foo",
  2933. Namespace: "default",
  2934. Generation: 1,
  2935. },
  2936. }
  2937. newES := oldES.DeepCopy()
  2938. newES.Generation = 2
  2939. Expect(shouldEnqueueExternalSecretUpdate(oldES, newES)).To(BeTrue())
  2940. })
  2941. It("should enqueue when labels change", func() {
  2942. oldES := &esv1.ExternalSecret{
  2943. ObjectMeta: metav1.ObjectMeta{
  2944. Name: "foo",
  2945. Namespace: "default",
  2946. Generation: 1,
  2947. Labels: map[string]string{
  2948. "app": "a",
  2949. },
  2950. },
  2951. }
  2952. newES := oldES.DeepCopy()
  2953. newES.Labels["app"] = "b"
  2954. Expect(shouldEnqueueExternalSecretUpdate(oldES, newES)).To(BeTrue())
  2955. })
  2956. It("should enqueue when annotations change", func() {
  2957. oldES := &esv1.ExternalSecret{
  2958. ObjectMeta: metav1.ObjectMeta{
  2959. Name: "foo",
  2960. Namespace: "default",
  2961. Generation: 1,
  2962. Annotations: map[string]string{
  2963. "note": "a",
  2964. },
  2965. },
  2966. }
  2967. newES := oldES.DeepCopy()
  2968. newES.Annotations["note"] = "b"
  2969. Expect(shouldEnqueueExternalSecretUpdate(oldES, newES)).To(BeTrue())
  2970. })
  2971. It("should enqueue when deletion timestamp changes", func() {
  2972. oldES := &esv1.ExternalSecret{
  2973. ObjectMeta: metav1.ObjectMeta{
  2974. Name: "foo",
  2975. Namespace: "default",
  2976. Generation: 1,
  2977. },
  2978. }
  2979. newES := oldES.DeepCopy()
  2980. now := metav1.Now()
  2981. newES.DeletionTimestamp = &now
  2982. Expect(shouldEnqueueExternalSecretUpdate(oldES, newES)).To(BeTrue())
  2983. })
  2984. It("should enqueue when finalizers change", func() {
  2985. oldES := &esv1.ExternalSecret{
  2986. ObjectMeta: metav1.ObjectMeta{
  2987. Name: "foo",
  2988. Namespace: "default",
  2989. Generation: 1,
  2990. Finalizers: []string{"external-secrets.io/finalizer"},
  2991. },
  2992. }
  2993. newES := oldES.DeepCopy()
  2994. newES.Finalizers = nil
  2995. Expect(shouldEnqueueExternalSecretUpdate(oldES, newES)).To(BeTrue())
  2996. })
  2997. })
  2998. var _ = Describe("ExternalSecret refresh policy", func() {
  2999. Context("RefreshPolicy=CreatedOnce", func() {
  3000. It("should refresh when SyncedResourceVersion is empty", func() {
  3001. es := &esv1.ExternalSecret{
  3002. Spec: esv1.ExternalSecretSpec{
  3003. RefreshPolicy: esv1.RefreshPolicyCreatedOnce,
  3004. },
  3005. Status: esv1.ExternalSecretStatus{
  3006. SyncedResourceVersion: "",
  3007. },
  3008. }
  3009. Expect(shouldRefresh(es)).To(BeTrue())
  3010. })
  3011. It("should refresh when RefreshTime is zero", func() {
  3012. es := &esv1.ExternalSecret{
  3013. Spec: esv1.ExternalSecretSpec{
  3014. RefreshPolicy: esv1.RefreshPolicyCreatedOnce,
  3015. },
  3016. Status: esv1.ExternalSecretStatus{
  3017. SyncedResourceVersion: "some-version",
  3018. RefreshTime: metav1.Time{},
  3019. },
  3020. }
  3021. Expect(shouldRefresh(es)).To(BeTrue())
  3022. })
  3023. It("should not refresh when already synced", func() {
  3024. es := &esv1.ExternalSecret{
  3025. Spec: esv1.ExternalSecretSpec{
  3026. RefreshPolicy: esv1.RefreshPolicyCreatedOnce,
  3027. },
  3028. Status: esv1.ExternalSecretStatus{
  3029. SyncedResourceVersion: "some-version",
  3030. RefreshTime: metav1.Now(),
  3031. },
  3032. }
  3033. Expect(shouldRefresh(es)).To(BeFalse())
  3034. })
  3035. })
  3036. Context("RefreshPolicy=OnChange", func() {
  3037. It("should refresh when SyncedResourceVersion is empty", func() {
  3038. es := &esv1.ExternalSecret{
  3039. Spec: esv1.ExternalSecretSpec{
  3040. RefreshPolicy: esv1.RefreshPolicyOnChange,
  3041. },
  3042. Status: esv1.ExternalSecretStatus{
  3043. SyncedResourceVersion: "",
  3044. },
  3045. }
  3046. Expect(shouldRefresh(es)).To(BeTrue())
  3047. })
  3048. It("should refresh when RefreshTime is zero", func() {
  3049. es := &esv1.ExternalSecret{
  3050. Spec: esv1.ExternalSecretSpec{
  3051. RefreshPolicy: esv1.RefreshPolicyOnChange,
  3052. },
  3053. Status: esv1.ExternalSecretStatus{
  3054. RefreshTime: metav1.Time{},
  3055. },
  3056. }
  3057. Expect(shouldRefresh(es)).To(BeTrue())
  3058. })
  3059. It("should refresh when resource version changes", func() {
  3060. es := &esv1.ExternalSecret{
  3061. ObjectMeta: metav1.ObjectMeta{
  3062. Generation: 1,
  3063. Annotations: map[string]string{
  3064. "foo": "bar",
  3065. },
  3066. },
  3067. Spec: esv1.ExternalSecretSpec{
  3068. RefreshPolicy: esv1.RefreshPolicyOnChange,
  3069. },
  3070. Status: esv1.ExternalSecretStatus{
  3071. RefreshTime: metav1.Now(),
  3072. SyncedResourceVersion: "old-version",
  3073. },
  3074. }
  3075. // The temp annotation is added in the shouldRefresh function
  3076. Expect(shouldRefresh(es)).To(BeTrue())
  3077. })
  3078. It("should not refresh when resource version matches", func() {
  3079. es := &esv1.ExternalSecret{
  3080. ObjectMeta: metav1.ObjectMeta{
  3081. Generation: 1,
  3082. },
  3083. Spec: esv1.ExternalSecretSpec{
  3084. RefreshPolicy: esv1.RefreshPolicyOnChange,
  3085. },
  3086. Status: esv1.ExternalSecretStatus{
  3087. RefreshTime: metav1.Now(),
  3088. },
  3089. }
  3090. // Set the synced resource version to match the current resource version
  3091. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3092. Expect(shouldRefresh(es)).To(BeFalse())
  3093. })
  3094. It("should refresh when annotations change", func() {
  3095. es := &esv1.ExternalSecret{
  3096. ObjectMeta: metav1.ObjectMeta{
  3097. Generation: 1,
  3098. Annotations: map[string]string{
  3099. "foo": "bar",
  3100. },
  3101. },
  3102. Spec: esv1.ExternalSecretSpec{
  3103. RefreshPolicy: esv1.RefreshPolicyOnChange,
  3104. },
  3105. Status: esv1.ExternalSecretStatus{
  3106. RefreshTime: metav1.Now(),
  3107. },
  3108. }
  3109. // Set the synced resource version to match the current resource version
  3110. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3111. Expect(shouldRefresh(es)).To(BeFalse())
  3112. es.Annotations["foo"] = "bar1"
  3113. Expect(shouldRefresh(es)).To(BeTrue())
  3114. })
  3115. It("should refresh when spec changes", func() {
  3116. es := &esv1.ExternalSecret{
  3117. ObjectMeta: metav1.ObjectMeta{
  3118. Generation: 1,
  3119. },
  3120. Spec: esv1.ExternalSecretSpec{
  3121. RefreshPolicy: esv1.RefreshPolicyOnChange,
  3122. Data: []esv1.ExternalSecretData{
  3123. {
  3124. SecretKey: "key1",
  3125. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  3126. Key: "remote-key1",
  3127. },
  3128. },
  3129. },
  3130. },
  3131. Status: esv1.ExternalSecretStatus{
  3132. RefreshTime: metav1.Now(),
  3133. },
  3134. }
  3135. // Set the synced resource version to match the current resource version
  3136. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3137. // Initially should not refresh
  3138. Expect(shouldRefresh(es)).To(BeFalse())
  3139. // Update the spec by adding a new data item
  3140. es.ObjectMeta.Generation = 2
  3141. es.Spec.Data = append(es.Spec.Data, esv1.ExternalSecretData{
  3142. SecretKey: "key2",
  3143. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  3144. Key: "remote-key2",
  3145. },
  3146. })
  3147. // The temp annotation is added in the shouldRefresh function to track spec hash
  3148. Expect(shouldRefresh(es)).To(BeTrue())
  3149. })
  3150. })
  3151. Context("Default refresh policy (Periodic)", func() {
  3152. It("should behave like Periodic when RefreshPolicy is not set", func() {
  3153. es := &esv1.ExternalSecret{
  3154. ObjectMeta: metav1.ObjectMeta{
  3155. Generation: 1,
  3156. },
  3157. Spec: esv1.ExternalSecretSpec{
  3158. // No RefreshPolicy set, should default to Periodic behavior
  3159. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  3160. },
  3161. Status: esv1.ExternalSecretStatus{
  3162. RefreshTime: metav1.Now(),
  3163. },
  3164. }
  3165. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3166. Expect(shouldRefresh(es)).To(BeFalse())
  3167. // When refresh interval has passed
  3168. es.Status.RefreshTime = metav1.NewTime(metav1.Now().Add(-time.Minute * 2))
  3169. Expect(shouldRefresh(es)).To(BeTrue())
  3170. })
  3171. })
  3172. Context("RefreshPolicy=Periodic", func() {
  3173. It("should not refresh when refreshInterval is 0 and already synced", func() {
  3174. es := &esv1.ExternalSecret{
  3175. ObjectMeta: metav1.ObjectMeta{
  3176. Generation: 1,
  3177. },
  3178. Spec: esv1.ExternalSecretSpec{
  3179. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3180. RefreshInterval: &metav1.Duration{Duration: 0},
  3181. },
  3182. Status: esv1.ExternalSecretStatus{
  3183. SyncedResourceVersion: "some-version",
  3184. },
  3185. }
  3186. // Set the synced resource version to match the current resource version
  3187. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3188. Expect(shouldRefresh(es)).To(BeFalse())
  3189. })
  3190. It("should refresh when resource version changes", func() {
  3191. es := &esv1.ExternalSecret{
  3192. ObjectMeta: metav1.ObjectMeta{
  3193. Generation: 2,
  3194. },
  3195. Spec: esv1.ExternalSecretSpec{
  3196. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3197. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  3198. },
  3199. Status: esv1.ExternalSecretStatus{
  3200. RefreshTime: metav1.Now(),
  3201. SyncedResourceVersion: "old-version",
  3202. },
  3203. }
  3204. Expect(shouldRefresh(es)).To(BeTrue())
  3205. })
  3206. It("should refresh when refresh interval has passed", func() {
  3207. es := &esv1.ExternalSecret{
  3208. ObjectMeta: metav1.ObjectMeta{
  3209. Generation: 1,
  3210. },
  3211. Spec: esv1.ExternalSecretSpec{
  3212. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3213. RefreshInterval: &metav1.Duration{Duration: time.Second},
  3214. },
  3215. Status: esv1.ExternalSecretStatus{
  3216. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  3217. },
  3218. }
  3219. // Resource version matches
  3220. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3221. Expect(shouldRefresh(es)).To(BeTrue())
  3222. })
  3223. It("should refresh when no refresh time was set", func() {
  3224. es := &esv1.ExternalSecret{
  3225. ObjectMeta: metav1.ObjectMeta{
  3226. Generation: 1,
  3227. },
  3228. Spec: esv1.ExternalSecretSpec{
  3229. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3230. RefreshInterval: &metav1.Duration{Duration: time.Second},
  3231. },
  3232. Status: esv1.ExternalSecretStatus{},
  3233. }
  3234. // Resource version matches
  3235. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3236. Expect(shouldRefresh(es)).To(BeTrue())
  3237. })
  3238. It("should refresh when refresh time is in the future", func() {
  3239. es := &esv1.ExternalSecret{
  3240. ObjectMeta: metav1.ObjectMeta{
  3241. Generation: 1,
  3242. },
  3243. Spec: esv1.ExternalSecretSpec{
  3244. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3245. RefreshInterval: &metav1.Duration{Duration: time.Second},
  3246. },
  3247. Status: esv1.ExternalSecretStatus{
  3248. RefreshTime: metav1.NewTime(time.Now().Add(time.Hour)), // Future time
  3249. },
  3250. }
  3251. // Resource version matches
  3252. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3253. Expect(shouldRefresh(es)).To(BeTrue())
  3254. })
  3255. It("should refresh when refreshInterval not 0 and spec changes", func() {
  3256. es := &esv1.ExternalSecret{
  3257. ObjectMeta: metav1.ObjectMeta{
  3258. Generation: 1,
  3259. },
  3260. Spec: esv1.ExternalSecretSpec{
  3261. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3262. RefreshInterval: &metav1.Duration{Duration: 10 * time.Second},
  3263. Data: []esv1.ExternalSecretData{
  3264. {
  3265. SecretKey: "key1",
  3266. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  3267. Key: "remote-key1",
  3268. },
  3269. },
  3270. },
  3271. },
  3272. Status: esv1.ExternalSecretStatus{
  3273. SyncedResourceVersion: "some-version",
  3274. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  3275. },
  3276. }
  3277. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3278. Expect(shouldRefresh(es)).To(BeFalse())
  3279. // Update the spec by adding a new data item
  3280. es.ObjectMeta.Generation = 2
  3281. es.Spec.Data = append(es.Spec.Data, esv1.ExternalSecretData{
  3282. SecretKey: "key2",
  3283. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  3284. Key: "remote-key2",
  3285. },
  3286. })
  3287. Expect(shouldRefresh(es)).To(BeTrue())
  3288. })
  3289. It("should not refresh when refreshInterval is 0 even if spec changes", func() {
  3290. es := &esv1.ExternalSecret{
  3291. ObjectMeta: metav1.ObjectMeta{
  3292. Generation: 1,
  3293. },
  3294. Spec: esv1.ExternalSecretSpec{
  3295. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3296. RefreshInterval: &metav1.Duration{Duration: 0},
  3297. Data: []esv1.ExternalSecretData{
  3298. {
  3299. SecretKey: "key1",
  3300. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  3301. Key: "remote-key1",
  3302. },
  3303. },
  3304. },
  3305. },
  3306. Status: esv1.ExternalSecretStatus{
  3307. SyncedResourceVersion: "some-version",
  3308. RefreshTime: metav1.Now(),
  3309. },
  3310. }
  3311. // Set the synced resource version to match the current resource version
  3312. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3313. Expect(shouldRefresh(es)).To(BeFalse())
  3314. // Update the spec by adding a new data item
  3315. es.ObjectMeta.Generation = 2
  3316. es.Spec.Data = append(es.Spec.Data, esv1.ExternalSecretData{
  3317. SecretKey: "key2",
  3318. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  3319. Key: "remote-key2",
  3320. },
  3321. })
  3322. // Should still not refresh because interval is 0
  3323. Expect(shouldRefresh(es)).To(BeFalse())
  3324. })
  3325. It("should not refresh when refreshInterval is 0 even if labels or annotations change", func() {
  3326. es := &esv1.ExternalSecret{
  3327. ObjectMeta: metav1.ObjectMeta{
  3328. Generation: 1,
  3329. Labels: map[string]string{
  3330. "original-label": "value",
  3331. },
  3332. Annotations: map[string]string{
  3333. "original-annotation": "value",
  3334. },
  3335. },
  3336. Spec: esv1.ExternalSecretSpec{
  3337. RefreshPolicy: esv1.RefreshPolicyPeriodic,
  3338. RefreshInterval: &metav1.Duration{Duration: 0},
  3339. },
  3340. Status: esv1.ExternalSecretStatus{
  3341. SyncedResourceVersion: "some-version",
  3342. RefreshTime: metav1.Now(),
  3343. },
  3344. }
  3345. // Set the synced resource version to match the current resource version
  3346. es.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(es.ObjectMeta)
  3347. Expect(shouldRefresh(es)).To(BeFalse())
  3348. // Update labels and annotations
  3349. es.ObjectMeta.Labels["new-label"] = "new-value"
  3350. es.ObjectMeta.Annotations["new-annotation"] = "new-value"
  3351. // Should still not refresh because interval is 0
  3352. Expect(shouldRefresh(es)).To(BeFalse())
  3353. })
  3354. })
  3355. })
  3356. func externalSecretConditionShouldBe(name, ns string, ct esv1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  3357. return Eventually(func() float64 {
  3358. Expect(testExternalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  3359. return metric.GetGauge().GetValue()
  3360. }, timeout, interval).Should(Equal(v))
  3361. }
  3362. func init() {
  3363. fakeProvider = fake.New()
  3364. esv1.ForceRegister(fakeProvider, &esv1.SecretStoreProvider{
  3365. AWS: &esv1.AWSProvider{
  3366. Service: esv1.AWSServiceSecretsManager,
  3367. },
  3368. }, esv1.MaintenanceStatusMaintained)
  3369. ctrlmetrics.SetUpLabelNames(false)
  3370. esmetrics.SetUpMetrics()
  3371. testSyncCallsTotal = esmetrics.GetCounterVec(esmetrics.SyncCallsKey)
  3372. testSyncCallsError = esmetrics.GetCounterVec(esmetrics.SyncCallsErrorKey)
  3373. testExternalSecretCondition = esmetrics.GetGaugeVec(esmetrics.ExternalSecretStatusConditionKey)
  3374. testExternalSecretReconcileDuration = esmetrics.GetGaugeVec(esmetrics.ExternalSecretReconcileDurationKey)
  3375. }