externalsecret_controller_test.go 108 KB

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