externalsecret_controller_test.go 115 KB

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