externalsecret_controller_test.go 92 KB

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