externalsecret_controller_test.go 107 KB

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