externalsecret_controller_test.go 76 KB

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