externalsecret_controller_test.go 96 KB

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