externalsecret_controller_test.go 86 KB

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