externalsecret_controller_test.go 96 KB

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