externalsecret_controller_test.go 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  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. // // * fromString
  565. // // * template data
  566. // // * templateFrom
  567. // // * data
  568. // // * dataFrom
  569. syncWithTemplatePrecedence := func(tc *testCase) {
  570. const secretVal = "someValue"
  571. const tplStaticKey = "tplstatickey"
  572. const tplStaticVal = "tplstaticvalue"
  573. const tplFromCMName = "template-cm"
  574. const tplFromSecretName = "template-secret"
  575. const tplFromKey = "tpl-from-key"
  576. const tplFromSecKey = "tpl-from-sec-key"
  577. const tplFromVal = "tpl-from-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  578. const tplFromSecVal = "tpl-from-sec-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  579. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  580. ObjectMeta: metav1.ObjectMeta{
  581. Name: tplFromCMName,
  582. Namespace: ExternalSecretNamespace,
  583. },
  584. Data: map[string]string{
  585. tplFromKey: tplFromVal,
  586. },
  587. })).To(Succeed())
  588. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  589. ObjectMeta: metav1.ObjectMeta{
  590. Name: tplFromSecretName,
  591. Namespace: ExternalSecretNamespace,
  592. },
  593. Data: map[string][]byte{
  594. tplFromSecKey: []byte(tplFromSecVal),
  595. },
  596. })).To(Succeed())
  597. tc.externalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  598. Metadata: esv1beta1.ExternalSecretTemplateMetadata{},
  599. Type: v1.SecretTypeOpaque,
  600. TemplateFrom: []esv1beta1.TemplateFrom{
  601. {
  602. ConfigMap: &esv1beta1.TemplateRef{
  603. Name: tplFromCMName,
  604. Items: []esv1beta1.TemplateRefItem{
  605. {
  606. Key: tplFromKey,
  607. },
  608. },
  609. },
  610. },
  611. {
  612. Secret: &esv1beta1.TemplateRef{
  613. Name: tplFromSecretName,
  614. Items: []esv1beta1.TemplateRefItem{
  615. {
  616. Key: tplFromSecKey,
  617. },
  618. },
  619. },
  620. },
  621. },
  622. Data: map[string]string{
  623. // this should be the data value, not dataFrom
  624. targetProp: targetPropObj,
  625. // this should use the value from the map
  626. "bar": "value from map: {{ .bar | toString }}",
  627. // just a static value
  628. tplStaticKey: tplStaticVal,
  629. },
  630. }
  631. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  632. {
  633. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  634. Key: "datamap",
  635. },
  636. },
  637. }
  638. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  639. fakeProvider.WithGetSecretMap(map[string][]byte{
  640. "targetProperty": []byte(FooValue),
  641. "bar": []byte(BarValue),
  642. }, nil)
  643. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  644. // check values
  645. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  646. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  647. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  648. Expect(string(secret.Data[tplFromKey])).To(Equal("tpl-from-value: someValue // map-bar-value"))
  649. Expect(string(secret.Data[tplFromSecKey])).To(Equal("tpl-from-sec-value: someValue // map-bar-value"))
  650. }
  651. }
  652. syncTemplateFromKeysAndValues := func(tc *testCase) {
  653. const tplFromCMName = "template-cm"
  654. const tplFromSecretName = "template-secret"
  655. const tplFromKey = "tpl-from-key"
  656. const tplFromSecKey = "tpl-from-sec-key"
  657. const tplFromVal = "{{ .targetKey }}-cm: {{ .targetValue }}"
  658. const tplFromSecVal = "{{ .targetKey }}-sec: {{ .targetValue }}"
  659. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  660. ObjectMeta: metav1.ObjectMeta{
  661. Name: tplFromCMName,
  662. Namespace: ExternalSecretNamespace,
  663. },
  664. Data: map[string]string{
  665. tplFromKey: tplFromVal,
  666. },
  667. })).To(Succeed())
  668. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  669. ObjectMeta: metav1.ObjectMeta{
  670. Name: tplFromSecretName,
  671. Namespace: ExternalSecretNamespace,
  672. },
  673. Data: map[string][]byte{
  674. tplFromSecKey: []byte(tplFromSecVal),
  675. },
  676. })).To(Succeed())
  677. tc.externalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  678. Metadata: esv1beta1.ExternalSecretTemplateMetadata{},
  679. Type: v1.SecretTypeOpaque,
  680. TemplateFrom: []esv1beta1.TemplateFrom{
  681. {
  682. ConfigMap: &esv1beta1.TemplateRef{
  683. Name: tplFromCMName,
  684. Items: []esv1beta1.TemplateRefItem{
  685. {
  686. Key: tplFromKey,
  687. TemplateAs: esv1beta1.TemplateScopeKeysAndValues,
  688. },
  689. },
  690. },
  691. },
  692. {
  693. Secret: &esv1beta1.TemplateRef{
  694. Name: tplFromSecretName,
  695. Items: []esv1beta1.TemplateRefItem{
  696. {
  697. Key: tplFromSecKey,
  698. TemplateAs: esv1beta1.TemplateScopeKeysAndValues,
  699. },
  700. },
  701. },
  702. },
  703. },
  704. }
  705. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  706. {
  707. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  708. Key: "datamap",
  709. },
  710. },
  711. }
  712. fakeProvider.WithGetSecretMap(map[string][]byte{
  713. "targetKey": []byte(FooValue),
  714. "targetValue": []byte(BarValue),
  715. }, nil)
  716. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  717. // check values
  718. Expect(string(secret.Data["map-foo-value-cm"])).To(Equal(BarValue))
  719. Expect(string(secret.Data["map-foo-value-sec"])).To(Equal(BarValue))
  720. }
  721. }
  722. syncTemplateFromLiteral := func(tc *testCase) {
  723. tplDataVal := "{{ .targetKey }}-literal: {{ .targetValue }}"
  724. tplAnnotationsVal := "{{ .targetKey }}-annotations: {{ .targetValue }}"
  725. tplLabelsVal := "{{ .targetKey }}-labels: {{ .targetValue }}"
  726. tplComplexVal := `
  727. {{- range $k, $v := ( .complex | fromJson )}}
  728. {{ $k }}: {{ $v }}
  729. {{- end }}
  730. `
  731. tc.externalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  732. Metadata: esv1beta1.ExternalSecretTemplateMetadata{},
  733. Type: v1.SecretTypeOpaque,
  734. TemplateFrom: []esv1beta1.TemplateFrom{
  735. {
  736. Literal: &tplDataVal,
  737. },
  738. {
  739. Literal: &tplComplexVal,
  740. },
  741. {
  742. Target: esv1beta1.TemplateTargetAnnotations,
  743. Literal: &tplAnnotationsVal,
  744. },
  745. {
  746. Target: esv1beta1.TemplateTargetLabels,
  747. Literal: &tplLabelsVal,
  748. },
  749. },
  750. }
  751. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  752. {
  753. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  754. Key: "datamap",
  755. },
  756. },
  757. }
  758. fakeProvider.WithGetSecretMap(map[string][]byte{
  759. "targetKey": []byte(FooValue),
  760. "targetValue": []byte(BarValue),
  761. "complex": []byte("{\"nested\":\"json\",\"can\":\"be\",\"templated\":\"successfully\"}"),
  762. }, nil)
  763. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  764. // check values
  765. Expect(string(secret.Data["map-foo-value-literal"])).To(Equal(BarValue))
  766. Expect(string(secret.Data["nested"])).To(Equal("json"))
  767. Expect(string(secret.Data["can"])).To(Equal("be"))
  768. Expect(string(secret.Data["templated"])).To(Equal("successfully"))
  769. Expect(secret.ObjectMeta.Annotations["map-foo-value-annotations"]).To(Equal(BarValue))
  770. Expect(secret.ObjectMeta.Labels["map-foo-value-labels"]).To(Equal(BarValue))
  771. }
  772. }
  773. refreshWithTemplate := func(tc *testCase) {
  774. const secretVal = "someValue"
  775. const tplStaticKey = "tplstatickey"
  776. const tplStaticVal = "tplstaticvalue"
  777. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  778. tc.externalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  779. Metadata: esv1beta1.ExternalSecretTemplateMetadata{
  780. Labels: map[string]string{"foo": "bar"},
  781. Annotations: map[string]string{"foo": "bar"},
  782. },
  783. Type: v1.SecretTypeOpaque,
  784. Data: map[string]string{
  785. targetProp: targetPropObj,
  786. tplStaticKey: tplStaticVal,
  787. },
  788. }
  789. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  790. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  791. // check values
  792. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  793. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  794. // labels/annotations should be taken from the template
  795. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  796. // a secret will always have some extra annotations (i.e. hashmap check), so we only check for specific
  797. // source annotations
  798. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  799. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  800. }
  801. cleanEs := tc.externalSecret.DeepCopy()
  802. // now update ExternalSecret
  803. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  804. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  805. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  806. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  807. // wait for secret
  808. sec := &v1.Secret{}
  809. secretLookupKey := types.NamespacedName{
  810. Name: ExternalSecretTargetSecretName,
  811. Namespace: ExternalSecretNamespace,
  812. }
  813. Eventually(func() bool {
  814. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  815. if err != nil {
  816. return false
  817. }
  818. // ensure new data value exist
  819. return string(sec.Data["new"]) == "value"
  820. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  821. // also check labels/annotations have been updated
  822. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  823. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  824. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  825. }
  826. }
  827. }
  828. onlyMetadataFromTemplate := func(tc *testCase) {
  829. const secretVal = "someValue"
  830. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  831. tc.externalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  832. Metadata: esv1beta1.ExternalSecretTemplateMetadata{
  833. Labels: map[string]string{"foo": "bar"},
  834. Annotations: map[string]string{"foo": "bar"},
  835. },
  836. }
  837. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  838. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  839. // check values
  840. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  841. // labels/annotations should be taken from the template
  842. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  843. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  844. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  845. }
  846. }
  847. }
  848. // when the provider secret changes the Kind=Secret value
  849. // must change, too.
  850. refreshSecretValue := func(tc *testCase) {
  851. const targetProp = "targetProperty"
  852. const secretVal = "someValue"
  853. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  854. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  855. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  856. // check values
  857. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  858. // update provider secret
  859. newValue := "NEW VALUE"
  860. sec := &v1.Secret{}
  861. fakeProvider.WithGetSecret([]byte(newValue), nil)
  862. secretLookupKey := types.NamespacedName{
  863. Name: ExternalSecretTargetSecretName,
  864. Namespace: ExternalSecretNamespace,
  865. }
  866. Eventually(func() bool {
  867. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  868. if err != nil {
  869. return false
  870. }
  871. v := sec.Data[targetProp]
  872. return string(v) == newValue
  873. }, timeout, interval).Should(BeTrue())
  874. }
  875. }
  876. // when a provider secret was deleted it must be deleted from
  877. // the secret aswell
  878. refreshSecretValueMap := func(tc *testCase) {
  879. fakeProvider.WithGetSecretMap(map[string][]byte{
  880. "foo": []byte("1111"),
  881. "bar": []byte("2222"),
  882. }, nil)
  883. tc.externalSecret.Spec.Data = []esv1beta1.ExternalSecretData{}
  884. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  885. {
  886. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  887. Key: remoteKey,
  888. },
  889. },
  890. }
  891. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  892. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  893. // check values
  894. Expect(string(secret.Data["foo"])).To(Equal("1111"))
  895. Expect(string(secret.Data["bar"])).To(Equal("2222"))
  896. // update provider secret
  897. sec := &v1.Secret{}
  898. fakeProvider.WithGetSecretMap(map[string][]byte{
  899. "foo": []byte("1111"),
  900. }, nil)
  901. secretLookupKey := types.NamespacedName{
  902. Name: ExternalSecretTargetSecretName,
  903. Namespace: ExternalSecretNamespace,
  904. }
  905. Eventually(func() bool {
  906. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  907. if err != nil {
  908. return false
  909. }
  910. return string(sec.Data["foo"]) == "1111" &&
  911. sec.Data["bar"] == nil // must not be defined, it was deleted
  912. }, timeout, interval).Should(BeTrue())
  913. }
  914. }
  915. // when a provider secret was deleted it must be deleted from
  916. // the secret aswell when using a template
  917. refreshSecretValueMapTemplate := func(tc *testCase) {
  918. fakeProvider.WithGetSecretMap(map[string][]byte{
  919. "foo": []byte("1111"),
  920. "bar": []byte("2222"),
  921. }, nil)
  922. tc.externalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{}
  923. tc.externalSecret.Spec.Data = []esv1beta1.ExternalSecretData{}
  924. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  925. {
  926. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  927. Key: remoteKey,
  928. },
  929. },
  930. }
  931. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  932. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  933. // check values
  934. Expect(string(secret.Data["foo"])).To(Equal("1111"))
  935. Expect(string(secret.Data["bar"])).To(Equal("2222"))
  936. // update provider secret
  937. sec := &v1.Secret{}
  938. fakeProvider.WithGetSecretMap(map[string][]byte{
  939. "foo": []byte("1111"),
  940. }, nil)
  941. secretLookupKey := types.NamespacedName{
  942. Name: ExternalSecretTargetSecretName,
  943. Namespace: ExternalSecretNamespace,
  944. }
  945. Eventually(func() bool {
  946. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  947. if err != nil {
  948. return false
  949. }
  950. return string(sec.Data["foo"]) == "1111" &&
  951. sec.Data["bar"] == nil // must not be defined, it was deleted
  952. }, timeout, interval).Should(BeTrue())
  953. }
  954. }
  955. refreshintervalZero := func(tc *testCase) {
  956. const targetProp = "targetProperty"
  957. const secretVal = "someValue"
  958. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  959. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: 0}
  960. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  961. // check values
  962. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  963. // update provider secret
  964. newValue := "NEW VALUE"
  965. sec := &v1.Secret{}
  966. fakeProvider.WithGetSecret([]byte(newValue), nil)
  967. secretLookupKey := types.NamespacedName{
  968. Name: ExternalSecretTargetSecretName,
  969. Namespace: ExternalSecretNamespace,
  970. }
  971. Consistently(func() bool {
  972. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  973. if err != nil {
  974. return false
  975. }
  976. v := sec.Data[targetProp]
  977. return string(v) == secretVal
  978. }, time.Second*10, time.Second).Should(BeTrue())
  979. }
  980. }
  981. deleteSecretPolicy := func(tc *testCase) {
  982. expVal := []byte("1234")
  983. // set initial value
  984. fakeProvider.WithGetAllSecrets(map[string][]byte{
  985. "foo": expVal,
  986. "bar": expVal,
  987. }, nil)
  988. tc.externalSecret.Spec.Data = nil
  989. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  990. {
  991. Find: &esv1beta1.ExternalSecretFind{
  992. Tags: map[string]string{},
  993. },
  994. },
  995. }
  996. tc.externalSecret.Spec.Target.DeletionPolicy = esv1beta1.DeletionPolicyDelete
  997. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  998. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  999. Expect(secret.Data["foo"]).To(Equal(expVal))
  1000. // update provider secret
  1001. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1002. "foo": expVal,
  1003. }, nil)
  1004. sec := &v1.Secret{}
  1005. secretLookupKey := types.NamespacedName{
  1006. Name: ExternalSecretTargetSecretName,
  1007. Namespace: ExternalSecretNamespace,
  1008. }
  1009. Eventually(func() bool {
  1010. By("checking secret value for foo=1234 and bar=nil")
  1011. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1012. if err != nil {
  1013. return false
  1014. }
  1015. return bytes.Equal(sec.Data["foo"], expVal) && sec.Data["bar"] == nil
  1016. }, time.Second*10, time.Second).Should(BeTrue())
  1017. // return specific delete err to indicate deletion
  1018. fakeProvider.WithGetAllSecrets(map[string][]byte{}, esv1beta1.NoSecretErr)
  1019. Eventually(func() bool {
  1020. By("checking that secret has been deleted")
  1021. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1022. return apierrors.IsNotFound(err)
  1023. }, time.Second*10, time.Second).Should(BeTrue())
  1024. }
  1025. }
  1026. deleteSecretPolicyRetain := func(tc *testCase) {
  1027. expVal := []byte("1234")
  1028. // set initial value
  1029. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1030. "foo": expVal,
  1031. "bar": expVal,
  1032. }, nil)
  1033. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1034. {
  1035. Find: &esv1beta1.ExternalSecretFind{
  1036. Tags: map[string]string{},
  1037. },
  1038. },
  1039. }
  1040. tc.externalSecret.Spec.Target.DeletionPolicy = esv1beta1.DeletionPolicyRetain
  1041. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1042. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1043. Expect(secret.Data["foo"]).To(Equal(expVal))
  1044. sec := &v1.Secret{}
  1045. secretLookupKey := types.NamespacedName{
  1046. Name: ExternalSecretTargetSecretName,
  1047. Namespace: ExternalSecretNamespace,
  1048. }
  1049. // return specific delete err to indicate deletion
  1050. // however this should not trigger a delete
  1051. fakeProvider.WithGetAllSecrets(map[string][]byte{}, esv1beta1.NoSecretErr)
  1052. Consistently(func() bool {
  1053. By("checking that secret has not been deleted")
  1054. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1055. return apierrors.IsNotFound(err) && bytes.Equal(sec.Data["foo"], expVal)
  1056. }, time.Second*10, time.Second).Should(BeFalse())
  1057. }
  1058. }
  1059. // merge with existing secret using creationPolicy=Merge
  1060. // if provider secret gets deleted only the managed field should get deleted
  1061. deleteSecretPolicyMerge := func(tc *testCase) {
  1062. const secretVal = "someValue"
  1063. const existingKey = "some-existing-key"
  1064. existingVal := "some-existing-value"
  1065. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1066. tc.externalSecret.Spec.Target.CreationPolicy = esv1beta1.CreatePolicyMerge
  1067. tc.externalSecret.Spec.Target.DeletionPolicy = esv1beta1.DeletionPolicyMerge
  1068. // create secret beforehand
  1069. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  1070. ObjectMeta: metav1.ObjectMeta{
  1071. Name: ExternalSecretTargetSecretName,
  1072. Namespace: ExternalSecretNamespace,
  1073. },
  1074. Data: map[string][]byte{
  1075. existingKey: []byte(existingVal),
  1076. },
  1077. }, client.FieldOwner(FakeManager))).To(Succeed())
  1078. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1079. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1080. // check value
  1081. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  1082. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1083. sec := &v1.Secret{}
  1084. secretLookupKey := types.NamespacedName{
  1085. Name: ExternalSecretTargetSecretName,
  1086. Namespace: ExternalSecretNamespace,
  1087. }
  1088. // return specific delete err to indicate deletion
  1089. // however this should not trigger a delete
  1090. // instead expect that only the pre-existing value exists
  1091. fakeProvider.WithGetSecret(nil, esv1beta1.NoSecretErr)
  1092. Eventually(func() bool {
  1093. By("checking that secret has not been deleted and pre-existing key exists")
  1094. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1095. return !apierrors.IsNotFound(err) &&
  1096. len(sec.Data) == 1 &&
  1097. bytes.Equal(sec.Data[existingKey], []byte(existingVal))
  1098. }, time.Second*30, time.Second).Should(BeTrue())
  1099. }
  1100. }
  1101. // orphan the secret after the external secret has been deleted
  1102. createSecretPolicyOrphan := func(tc *testCase) {
  1103. const secretVal = "someValue"
  1104. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  1105. tc.externalSecret.Spec.Target.CreationPolicy = esv1beta1.CreatePolicyOrphan
  1106. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1107. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1108. // check value
  1109. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1110. sec := &v1.Secret{}
  1111. secretLookupKey := types.NamespacedName{
  1112. Name: ExternalSecretTargetSecretName,
  1113. Namespace: ExternalSecretNamespace,
  1114. }
  1115. err := k8sClient.Delete(context.Background(), tc.externalSecret)
  1116. Expect(err).ToNot(HaveOccurred())
  1117. Consistently(func() bool {
  1118. By("checking that secret has not been deleted")
  1119. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  1120. return !apierrors.IsNotFound(err)
  1121. }, time.Second*15, time.Second).Should(BeTrue())
  1122. }
  1123. }
  1124. // with rewrite all keys from a dataFrom operation
  1125. // should be put with new rewriting into the secret
  1126. syncAndRewriteWithDataFrom := func(tc *testCase) {
  1127. tc.externalSecret.Spec.Data = nil
  1128. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1129. {
  1130. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  1131. Key: remoteKey,
  1132. },
  1133. Rewrite: []esv1beta1.ExternalSecretRewrite{{
  1134. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  1135. Source: "(.*)",
  1136. Target: "new-$1",
  1137. },
  1138. }},
  1139. },
  1140. {
  1141. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  1142. Key: remoteKey,
  1143. },
  1144. Rewrite: []esv1beta1.ExternalSecretRewrite{{
  1145. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  1146. Source: "(.*)",
  1147. Target: "old-$1",
  1148. },
  1149. }},
  1150. },
  1151. }
  1152. fakeProvider.WithGetSecretMap(map[string][]byte{
  1153. "foo": []byte(FooValue),
  1154. "bar": []byte(BarValue),
  1155. }, nil)
  1156. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1157. // check values
  1158. Expect(string(secret.Data["new-foo"])).To(Equal(FooValue))
  1159. Expect(string(secret.Data["new-bar"])).To(Equal(BarValue))
  1160. Expect(string(secret.Data["old-foo"])).To(Equal(FooValue))
  1161. Expect(string(secret.Data["old-bar"])).To(Equal(BarValue))
  1162. }
  1163. }
  1164. // with rewrite keys from dataFrom
  1165. // should error if keys are not compliant
  1166. invalidExtractKeysErrCondition := func(tc *testCase) {
  1167. tc.externalSecret.Spec.Data = nil
  1168. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1169. {
  1170. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  1171. Key: remoteKey,
  1172. },
  1173. Rewrite: []esv1beta1.ExternalSecretRewrite{{
  1174. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  1175. Source: "(.*)",
  1176. Target: "$1",
  1177. },
  1178. }},
  1179. },
  1180. }
  1181. fakeProvider.WithGetSecretMap(map[string][]byte{
  1182. "foo/bar": []byte(FooValue),
  1183. "bar/foo": []byte(BarValue),
  1184. }, nil)
  1185. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1186. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1187. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1188. return false
  1189. }
  1190. return true
  1191. }
  1192. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1193. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1194. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1195. return false
  1196. }
  1197. return true
  1198. }
  1199. tc.checkExternalSecret = func(es *esv1beta1.ExternalSecret) {
  1200. Eventually(func() bool {
  1201. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1202. Expect(externalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1203. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1204. }, timeout, interval).Should(BeTrue())
  1205. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1206. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1207. }
  1208. }
  1209. // with rewrite keys from dataFrom
  1210. // should error if keys are not compliant
  1211. invalidFindKeysErrCondition := func(tc *testCase) {
  1212. tc.externalSecret.Spec.Data = nil
  1213. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1214. {
  1215. Find: &esv1beta1.ExternalSecretFind{
  1216. Name: &esv1beta1.FindName{
  1217. RegExp: ".*",
  1218. },
  1219. },
  1220. Rewrite: []esv1beta1.ExternalSecretRewrite{{
  1221. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  1222. Source: "(.*)",
  1223. Target: "$1",
  1224. },
  1225. }},
  1226. },
  1227. }
  1228. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1229. "foo/bar": []byte(FooValue),
  1230. "bar/foo": []byte(BarValue),
  1231. }, nil)
  1232. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1233. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1234. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1235. return false
  1236. }
  1237. return true
  1238. }
  1239. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1240. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1241. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1242. return false
  1243. }
  1244. return true
  1245. }
  1246. tc.checkExternalSecret = func(es *esv1beta1.ExternalSecret) {
  1247. Eventually(func() bool {
  1248. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1249. Expect(externalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1250. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1251. }, timeout, interval).Should(BeTrue())
  1252. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1253. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1254. }
  1255. }
  1256. // with dataFrom all properties from the specified secret
  1257. // should be put into the secret
  1258. syncWithDataFrom := func(tc *testCase) {
  1259. tc.externalSecret.Spec.Data = nil
  1260. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1261. {
  1262. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  1263. Key: remoteKey,
  1264. },
  1265. },
  1266. }
  1267. fakeProvider.WithGetSecretMap(map[string][]byte{
  1268. "foo": []byte(FooValue),
  1269. "bar": []byte(BarValue),
  1270. }, nil)
  1271. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1272. // check values
  1273. Expect(string(secret.Data["foo"])).To(Equal(FooValue))
  1274. Expect(string(secret.Data["bar"])).To(Equal(BarValue))
  1275. }
  1276. }
  1277. // with dataFrom.Find the change is on the called method GetAllSecrets
  1278. // all keys should be put into the secret
  1279. syncAndRewriteDataFromFind := func(tc *testCase) {
  1280. tc.externalSecret.Spec.Data = nil
  1281. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1282. {
  1283. Find: &esv1beta1.ExternalSecretFind{
  1284. Name: &esv1beta1.FindName{
  1285. RegExp: "foobar",
  1286. },
  1287. },
  1288. Rewrite: []esv1beta1.ExternalSecretRewrite{
  1289. {
  1290. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  1291. Source: "(.*)",
  1292. Target: "new-$1",
  1293. },
  1294. },
  1295. },
  1296. },
  1297. }
  1298. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1299. "foo": []byte(FooValue),
  1300. "bar": []byte(BarValue),
  1301. }, nil)
  1302. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1303. // check values
  1304. Expect(string(secret.Data["new-foo"])).To(Equal(FooValue))
  1305. Expect(string(secret.Data["new-bar"])).To(Equal(BarValue))
  1306. }
  1307. }
  1308. // with dataFrom.Find the change is on the called method GetAllSecrets
  1309. // all keys should be put into the secret
  1310. syncDataFromFind := func(tc *testCase) {
  1311. tc.externalSecret.Spec.Data = nil
  1312. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1313. {
  1314. Find: &esv1beta1.ExternalSecretFind{
  1315. Name: &esv1beta1.FindName{
  1316. RegExp: "foobar",
  1317. },
  1318. },
  1319. },
  1320. }
  1321. fakeProvider.WithGetAllSecrets(map[string][]byte{
  1322. "foo": []byte(FooValue),
  1323. "bar": []byte(BarValue),
  1324. }, nil)
  1325. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1326. // check values
  1327. Expect(string(secret.Data["foo"])).To(Equal(FooValue))
  1328. Expect(string(secret.Data["bar"])).To(Equal(BarValue))
  1329. }
  1330. }
  1331. // with dataFrom and using a template
  1332. // should be put into the secret
  1333. syncWithDataFromTemplate := func(tc *testCase) {
  1334. tc.externalSecret.Spec.Data = nil
  1335. tc.externalSecret.Spec.Target = esv1beta1.ExternalSecretTarget{
  1336. Name: ExternalSecretTargetSecretName,
  1337. Template: &esv1beta1.ExternalSecretTemplate{
  1338. Type: v1.SecretTypeTLS,
  1339. },
  1340. }
  1341. tc.externalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  1342. {
  1343. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  1344. Key: remoteKey,
  1345. },
  1346. },
  1347. }
  1348. fakeProvider.WithGetSecretMap(map[string][]byte{
  1349. "tls.crt": []byte(FooValue),
  1350. "tls.key": []byte(BarValue),
  1351. }, nil)
  1352. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1353. Expect(secret.Type).To(Equal(v1.SecretTypeTLS))
  1354. // check values
  1355. Expect(string(secret.Data["tls.crt"])).To(Equal(FooValue))
  1356. Expect(string(secret.Data["tls.key"])).To(Equal(BarValue))
  1357. }
  1358. }
  1359. // when a provider errors in a GetSecret call
  1360. // a error condition must be set.
  1361. providerErrCondition := func(tc *testCase) {
  1362. const secretVal = "foobar"
  1363. fakeProvider.WithGetSecret(nil, fmt.Errorf("boom"))
  1364. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  1365. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1366. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1367. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1368. return false
  1369. }
  1370. return true
  1371. }
  1372. tc.checkExternalSecret = func(es *esv1beta1.ExternalSecret) {
  1373. Eventually(func() bool {
  1374. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1375. Expect(externalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1376. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1377. }, timeout, interval).Should(BeTrue())
  1378. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1379. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1380. // es condition should reflect recovered provider error
  1381. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1382. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  1383. Eventually(func() bool {
  1384. err := k8sClient.Get(context.Background(), esKey, es)
  1385. if err != nil {
  1386. return false
  1387. }
  1388. // condition must now be true!
  1389. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1390. if cond == nil && cond.Status != v1.ConditionTrue {
  1391. return false
  1392. }
  1393. return true
  1394. }, timeout, interval).Should(BeTrue())
  1395. }
  1396. }
  1397. // When a ExternalSecret references an non-existing SecretStore
  1398. // a error condition must be set.
  1399. storeMissingErrCondition := func(tc *testCase) {
  1400. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  1401. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1402. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1403. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1404. return false
  1405. }
  1406. return true
  1407. }
  1408. tc.checkExternalSecret = func(es *esv1beta1.ExternalSecret) {
  1409. Eventually(func() bool {
  1410. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1411. Expect(externalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1412. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1413. }, timeout, interval).Should(BeTrue())
  1414. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1415. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1416. }
  1417. }
  1418. // when the provider constructor errors (e.g. invalid configuration)
  1419. // a SecretSyncedError status condition must be set
  1420. storeConstructErrCondition := func(tc *testCase) {
  1421. fakeProvider.WithNew(func(context.Context, esv1beta1.GenericStore, client.Client,
  1422. string) (esv1beta1.SecretsClient, error) {
  1423. return nil, fmt.Errorf("artificial constructor error")
  1424. })
  1425. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1426. // condition must be false
  1427. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1428. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1429. return false
  1430. }
  1431. return true
  1432. }
  1433. tc.checkExternalSecret = func(es *esv1beta1.ExternalSecret) {
  1434. Eventually(func() bool {
  1435. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  1436. Expect(externalSecretReconcileDuration.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metricDuration)).To(Succeed())
  1437. return metric.GetCounter().GetValue() >= 2.0 && metricDuration.GetGauge().GetValue() > 0.0
  1438. }, timeout, interval).Should(BeTrue())
  1439. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  1440. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1441. }
  1442. }
  1443. // when a SecretStore has a controller field set which we don't care about
  1444. // the externalSecret must not be touched
  1445. ignoreMismatchController := func(tc *testCase) {
  1446. tc.secretStore.GetSpec().Controller = "nop"
  1447. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1448. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1449. return cond == nil
  1450. }
  1451. tc.checkExternalSecret = func(es *esv1beta1.ExternalSecret) {
  1452. // Condition True and False should be 0, since the Condition was not created
  1453. Eventually(func() float64 {
  1454. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1beta1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric)).To(Succeed())
  1455. return metric.GetGauge().GetValue()
  1456. }, timeout, interval).Should(Equal(0.0))
  1457. Eventually(func() float64 {
  1458. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1beta1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric)).To(Succeed())
  1459. return metric.GetGauge().GetValue()
  1460. }, timeout, interval).Should(Equal(0.0))
  1461. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  1462. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1beta1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  1463. }
  1464. }
  1465. ignoreClusterSecretStoreWhenDisabled := func(tc *testCase) {
  1466. tc.externalSecret.Spec.SecretStoreRef.Kind = esv1beta1.ClusterSecretStoreKind
  1467. Expect(shouldSkipClusterSecretStore(
  1468. &Reconciler{
  1469. ClusterSecretStoreEnabled: false,
  1470. },
  1471. *tc.externalSecret,
  1472. )).To(BeTrue())
  1473. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1474. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1475. return cond == nil
  1476. }
  1477. }
  1478. // When the ownership is set to owner, and we delete a dependent child kind=secret
  1479. // it should be recreated without waiting for refresh interval
  1480. checkDeletion := func(tc *testCase) {
  1481. const secretVal = "someValue"
  1482. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1483. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  1484. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1485. // check values
  1486. oldUID := secret.UID
  1487. Expect(oldUID).NotTo(BeEmpty())
  1488. // delete the related config
  1489. Expect(k8sClient.Delete(context.TODO(), secret))
  1490. var newSecret v1.Secret
  1491. secretLookupKey := types.NamespacedName{
  1492. Name: ExternalSecretTargetSecretName,
  1493. Namespace: ExternalSecretNamespace,
  1494. }
  1495. Eventually(func() bool {
  1496. err := k8sClient.Get(context.Background(), secretLookupKey, &newSecret)
  1497. if err != nil {
  1498. return false
  1499. }
  1500. // new secret should be a new, recreated object with a different UID
  1501. return newSecret.UID != oldUID
  1502. }, timeout, interval).Should(BeTrue())
  1503. }
  1504. }
  1505. // Checks that secret annotation has been written based on the data
  1506. checkSecretDataHashAnnotation := func(tc *testCase) {
  1507. const secretVal = "someValue"
  1508. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1509. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1510. Expect(secret.Annotations[esv1beta1.AnnotationDataHash]).To(Equal("9d30b95ca81e156f9454b5ef3bfcc6ee"))
  1511. }
  1512. }
  1513. // When we amend the created kind=secret, refresh operation should be run again regardless of refresh interval
  1514. checkSecretDataHashAnnotationChange := func(tc *testCase) {
  1515. fakeData := map[string][]byte{
  1516. "targetProperty": []byte(FooValue),
  1517. }
  1518. fakeProvider.WithGetSecretMap(fakeData, nil)
  1519. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  1520. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1521. oldHash := secret.Annotations[esv1beta1.AnnotationDataHash]
  1522. oldResourceVersion := secret.ResourceVersion
  1523. Expect(oldHash).NotTo(BeEmpty())
  1524. cleanSecret := secret.DeepCopy()
  1525. secret.Data["new"] = []byte("value")
  1526. secret.ObjectMeta.Annotations[esv1beta1.AnnotationDataHash] = "thisiswronghash"
  1527. Expect(k8sClient.Patch(context.Background(), secret, client.MergeFrom(cleanSecret))).To(Succeed())
  1528. var refreshedSecret v1.Secret
  1529. secretLookupKey := types.NamespacedName{
  1530. Name: ExternalSecretTargetSecretName,
  1531. Namespace: ExternalSecretNamespace,
  1532. }
  1533. Eventually(func() bool {
  1534. err := k8sClient.Get(context.Background(), secretLookupKey, &refreshedSecret)
  1535. if err != nil {
  1536. return false
  1537. }
  1538. // refreshed secret should have a different generation (sign that it was updated), but since
  1539. // the secret source is the same (not changed), the hash should be reverted to an old value
  1540. return refreshedSecret.ResourceVersion != oldResourceVersion && refreshedSecret.Annotations[esv1beta1.AnnotationDataHash] == oldHash
  1541. }, timeout, interval).Should(BeTrue())
  1542. }
  1543. }
  1544. useClusterSecretStore := func(tc *testCase) {
  1545. tc.secretStore = &esv1beta1.ClusterSecretStore{
  1546. ObjectMeta: metav1.ObjectMeta{
  1547. Name: ExternalSecretStore,
  1548. },
  1549. Spec: esv1beta1.SecretStoreSpec{
  1550. Provider: &esv1beta1.SecretStoreProvider{
  1551. AWS: &esv1beta1.AWSProvider{
  1552. Service: esv1beta1.AWSServiceSecretsManager,
  1553. },
  1554. },
  1555. },
  1556. }
  1557. tc.externalSecret.Spec.SecretStoreRef.Kind = esv1beta1.ClusterSecretStoreKind
  1558. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  1559. }
  1560. // Secret is created when ClusterSecretStore has no conditions
  1561. noConditionsSecretCreated := func(tc *testCase) {
  1562. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1563. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1564. }
  1565. }
  1566. // Secret is not created when ClusterSecretStore has a single non-matching string condition
  1567. noSecretCreatedWhenNamespaceDoesntMatchStringCondition := func(tc *testCase) {
  1568. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1569. {
  1570. Namespaces: []string{"some-other-ns"},
  1571. },
  1572. }
  1573. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1574. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1575. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1576. return false
  1577. }
  1578. return true
  1579. }
  1580. }
  1581. // Secret is not created when ClusterSecretStore has a single non-matching string condition with multiple names
  1582. noSecretCreatedWhenNamespaceDoesntMatchStringConditionWithMultipleNames := func(tc *testCase) {
  1583. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1584. {
  1585. Namespaces: []string{"some-other-ns", "another-ns"},
  1586. },
  1587. }
  1588. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1589. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1590. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1591. return false
  1592. }
  1593. return true
  1594. }
  1595. }
  1596. // Secret is not created when ClusterSecretStore has a multiple non-matching string condition
  1597. noSecretCreatedWhenNamespaceDoesntMatchMultipleStringCondition := func(tc *testCase) {
  1598. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1599. {
  1600. Namespaces: []string{"some-other-ns"},
  1601. },
  1602. {
  1603. Namespaces: []string{"another-ns"},
  1604. },
  1605. }
  1606. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1607. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1608. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1609. return false
  1610. }
  1611. return true
  1612. }
  1613. }
  1614. // Secret is created when ClusterSecretStore has a single matching string condition
  1615. secretCreatedWhenNamespaceMatchesSingleStringCondition := func(tc *testCase) {
  1616. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1617. {
  1618. Namespaces: []string{ExternalSecretNamespace},
  1619. },
  1620. }
  1621. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1622. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1623. }
  1624. }
  1625. // Secret is created when ClusterSecretStore has a multiple string conditions, one matching
  1626. secretCreatedWhenNamespaceMatchesMultipleStringConditions := func(tc *testCase) {
  1627. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1628. {
  1629. Namespaces: []string{ExternalSecretNamespace, "some-other-ns"},
  1630. },
  1631. }
  1632. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1633. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1634. }
  1635. }
  1636. // Secret is not created when ClusterSecretStore has a single non-matching label condition
  1637. noSecretCreatedWhenNamespaceDoesntMatchLabelCondition := func(tc *testCase) {
  1638. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1639. {
  1640. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"some-label-key": "some-label-value"}},
  1641. },
  1642. }
  1643. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1644. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1645. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1646. return false
  1647. }
  1648. return true
  1649. }
  1650. }
  1651. // Secret is created when ClusterSecretStore has a single matching label condition
  1652. secretCreatedWhenNamespaceMatchOnlyLabelCondition := func(tc *testCase) {
  1653. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1654. {
  1655. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue}},
  1656. },
  1657. }
  1658. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1659. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1660. }
  1661. }
  1662. // Secret is not created when ClusterSecretStore has a partially matching label condition
  1663. noSecretCreatedWhenNamespacePartiallyMatchLabelCondition := func(tc *testCase) {
  1664. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1665. {
  1666. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue, "some-label-key": "some-label-value"}},
  1667. },
  1668. }
  1669. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1670. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1671. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1672. return false
  1673. }
  1674. return true
  1675. }
  1676. }
  1677. // Secret is created when ClusterSecretStore has at least one matching label condition
  1678. secretCreatedWhenNamespaceMatchOneLabelCondition := func(tc *testCase) {
  1679. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1680. {
  1681. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue}},
  1682. },
  1683. {
  1684. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"some-label-key": "some-label-value"}},
  1685. },
  1686. }
  1687. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1688. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1689. }
  1690. }
  1691. // Secret is created when ClusterSecretStore has multiple matching conditions
  1692. secretCreatedWhenNamespaceMatchMultipleConditions := func(tc *testCase) {
  1693. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1694. {
  1695. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{NamespaceLabelKey: NamespaceLabelValue}},
  1696. },
  1697. {
  1698. Namespaces: []string{ExternalSecretNamespace},
  1699. },
  1700. }
  1701. tc.checkSecret = func(es *esv1beta1.ExternalSecret, secret *v1.Secret) {
  1702. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  1703. }
  1704. }
  1705. // Secret is not created when ClusterSecretStore has multiple non-matching conditions
  1706. noSecretCreatedWhenNamespaceMatchMultipleNonMatchingConditions := func(tc *testCase) {
  1707. tc.secretStore.GetSpec().Conditions = []esv1beta1.ClusterSecretStoreCondition{
  1708. {
  1709. NamespaceSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"some-label-key": "some-label-value"}},
  1710. },
  1711. {
  1712. Namespaces: []string{"some-other-ns"},
  1713. },
  1714. }
  1715. tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
  1716. cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
  1717. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1beta1.ConditionReasonSecretSyncedError {
  1718. return false
  1719. }
  1720. return true
  1721. }
  1722. }
  1723. DescribeTable("When reconciling an ExternalSecret",
  1724. func(tweaks ...testTweaks) {
  1725. tc := makeDefaultTestcase()
  1726. for _, tweak := range tweaks {
  1727. tweak(tc)
  1728. }
  1729. ctx := context.Background()
  1730. By("creating a secret store and external secret")
  1731. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  1732. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  1733. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  1734. createdES := &esv1beta1.ExternalSecret{}
  1735. By("checking the es condition")
  1736. Eventually(func() bool {
  1737. err := k8sClient.Get(ctx, esKey, createdES)
  1738. if err != nil {
  1739. return false
  1740. }
  1741. return tc.checkCondition(createdES)
  1742. }, timeout, interval).Should(BeTrue())
  1743. tc.checkExternalSecret(createdES)
  1744. // this must be optional so we can test faulty es configuration
  1745. if tc.checkSecret != nil {
  1746. syncedSecret := &v1.Secret{}
  1747. secretLookupKey := types.NamespacedName{
  1748. Name: ExternalSecretTargetSecretName,
  1749. Namespace: ExternalSecretNamespace,
  1750. }
  1751. if createdES.Spec.Target.Name == "" {
  1752. secretLookupKey = types.NamespacedName{
  1753. Name: ExternalSecretName,
  1754. Namespace: ExternalSecretNamespace,
  1755. }
  1756. }
  1757. Eventually(func() bool {
  1758. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  1759. return err == nil
  1760. }, timeout, interval).Should(BeTrue())
  1761. tc.checkSecret(createdES, syncedSecret)
  1762. }
  1763. },
  1764. Entry("should recreate deleted secret", checkDeletion),
  1765. Entry("should create proper hash annotation for the external secret", checkSecretDataHashAnnotation),
  1766. Entry("should refresh when the hash annotation doesn't correspond to secret data", checkSecretDataHashAnnotationChange),
  1767. Entry("should use external secret name if target secret name isn't defined", syncWithoutTargetName),
  1768. Entry("should set the condition eventually", syncLabelsAnnotations),
  1769. Entry("should set prometheus counters", checkPrometheusCounters),
  1770. Entry("should merge with existing secret using creationPolicy=Merge", mergeWithSecret),
  1771. Entry("should error if secret doesn't exist when using creationPolicy=Merge", mergeWithSecretErr),
  1772. Entry("should not resolve conflicts with creationPolicy=Merge", mergeWithConflict),
  1773. Entry("should not update unchanged secret using creationPolicy=Merge", mergeWithSecretNoChange),
  1774. Entry("should not delete pre-existing secret with creationPolicy=Orphan", createSecretPolicyOrphan),
  1775. Entry("should sync with generatorRef", syncWithGeneratorRef),
  1776. Entry("should sync with multiple secret stores via sourceRef", syncWithMultipleSecretStores),
  1777. Entry("should sync with template", syncWithTemplate),
  1778. Entry("should sync with template engine v2", syncWithTemplateV2),
  1779. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  1780. Entry("should sync template from keys and values", syncTemplateFromKeysAndValues),
  1781. Entry("should sync template from literal", syncTemplateFromLiteral),
  1782. Entry("should refresh secret from template", refreshWithTemplate),
  1783. Entry("should be able to use only metadata from template", onlyMetadataFromTemplate),
  1784. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  1785. Entry("should refresh secret map when provider secret changes", refreshSecretValueMap),
  1786. Entry("should refresh secret map when provider secret changes when using a template", refreshSecretValueMapTemplate),
  1787. Entry("should not refresh secret value when provider secret changes but refreshInterval is zero", refreshintervalZero),
  1788. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  1789. Entry("should rewrite secret using dataFrom", syncAndRewriteWithDataFrom),
  1790. Entry("should not automatically convert from extract if rewrite is used", invalidExtractKeysErrCondition),
  1791. Entry("should fetch secret using dataFrom.find", syncDataFromFind),
  1792. Entry("should rewrite secret using dataFrom.find", syncAndRewriteDataFromFind),
  1793. Entry("should not automatically convert from find if rewrite is used", invalidFindKeysErrCondition),
  1794. Entry("should fetch secret using dataFrom and a template", syncWithDataFromTemplate),
  1795. Entry("should set error condition when provider errors", providerErrCondition),
  1796. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  1797. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  1798. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  1799. Entry("should not process cluster secret store when it is disabled", ignoreClusterSecretStoreWhenDisabled),
  1800. Entry("should eventually delete target secret with deletionPolicy=Delete", deleteSecretPolicy),
  1801. Entry("should not delete target secret with deletionPolicy=Retain", deleteSecretPolicyRetain),
  1802. Entry("should not delete pre-existing secret with deletionPolicy=Merge", deleteSecretPolicyMerge),
  1803. Entry("secret is created when there are no conditions for the cluster secret store", useClusterSecretStore, noConditionsSecretCreated),
  1804. Entry("secret is not created when the condition for the cluster secret store states a different namespace single string condition", useClusterSecretStore, noSecretCreatedWhenNamespaceDoesntMatchStringCondition),
  1805. 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),
  1806. Entry("secret is not created when the condition for the cluster secret store states a different namespace multiple string conditions", useClusterSecretStore, noSecretCreatedWhenNamespaceDoesntMatchMultipleStringCondition),
  1807. Entry("secret is created when the condition for the cluster secret store has only one matching namespace by string condition", useClusterSecretStore, secretCreatedWhenNamespaceMatchesSingleStringCondition),
  1808. Entry("secret is created when the condition for the cluster secret store has one matching namespace of multiple namespaces by string condition", useClusterSecretStore, secretCreatedWhenNamespaceMatchesMultipleStringConditions),
  1809. Entry("secret is not created when the condition for the cluster secret store states a non-matching label condition", useClusterSecretStore, noSecretCreatedWhenNamespaceDoesntMatchLabelCondition),
  1810. Entry("secret is created when the condition for the cluster secret store states a single matching label condition", useClusterSecretStore, secretCreatedWhenNamespaceMatchOnlyLabelCondition),
  1811. Entry("secret is not created when the condition for the cluster secret store states a partially-matching label condition", useClusterSecretStore, noSecretCreatedWhenNamespacePartiallyMatchLabelCondition),
  1812. Entry("secret is created when one of the label conditions for the cluster secret store matches", useClusterSecretStore, secretCreatedWhenNamespaceMatchOneLabelCondition),
  1813. Entry("secret is created when the namespaces matches multiple cluster secret store conditions", useClusterSecretStore, secretCreatedWhenNamespaceMatchMultipleConditions),
  1814. Entry("secret is not created when the namespaces doesn't match any of multiple cluster secret store conditions", useClusterSecretStore, noSecretCreatedWhenNamespaceMatchMultipleNonMatchingConditions),
  1815. )
  1816. })
  1817. var _ = Describe("ExternalSecret refresh logic", func() {
  1818. Context("secret refresh", func() {
  1819. It("should refresh when resource version does not match", func() {
  1820. Expect(shouldRefresh(esv1beta1.ExternalSecret{
  1821. Status: esv1beta1.ExternalSecretStatus{
  1822. SyncedResourceVersion: "some resource version",
  1823. },
  1824. })).To(BeTrue())
  1825. })
  1826. It("should refresh when labels change", func() {
  1827. es := esv1beta1.ExternalSecret{
  1828. ObjectMeta: metav1.ObjectMeta{
  1829. Generation: 1,
  1830. Labels: map[string]string{
  1831. "foo": "bar",
  1832. },
  1833. },
  1834. Spec: esv1beta1.ExternalSecretSpec{
  1835. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  1836. },
  1837. Status: esv1beta1.ExternalSecretStatus{
  1838. RefreshTime: metav1.Now(),
  1839. },
  1840. }
  1841. es.Status.SyncedResourceVersion = getResourceVersion(es)
  1842. // this should not refresh, rv matches object
  1843. Expect(shouldRefresh(es)).To(BeFalse())
  1844. // change labels without changing the syncedResourceVersion and expect refresh
  1845. es.ObjectMeta.Labels["new"] = "w00t"
  1846. Expect(shouldRefresh(es)).To(BeTrue())
  1847. })
  1848. It("should refresh when annotations change", func() {
  1849. es := esv1beta1.ExternalSecret{
  1850. ObjectMeta: metav1.ObjectMeta{
  1851. Generation: 1,
  1852. Annotations: map[string]string{
  1853. "foo": "bar",
  1854. },
  1855. },
  1856. Spec: esv1beta1.ExternalSecretSpec{
  1857. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  1858. },
  1859. Status: esv1beta1.ExternalSecretStatus{
  1860. RefreshTime: metav1.Now(),
  1861. },
  1862. }
  1863. es.Status.SyncedResourceVersion = getResourceVersion(es)
  1864. // this should not refresh, rv matches object
  1865. Expect(shouldRefresh(es)).To(BeFalse())
  1866. // change annotations without changing the syncedResourceVersion and expect refresh
  1867. es.ObjectMeta.Annotations["new"] = "w00t"
  1868. Expect(shouldRefresh(es)).To(BeTrue())
  1869. })
  1870. It("should refresh when generation has changed", func() {
  1871. es := esv1beta1.ExternalSecret{
  1872. ObjectMeta: metav1.ObjectMeta{
  1873. Generation: 1,
  1874. },
  1875. Spec: esv1beta1.ExternalSecretSpec{
  1876. RefreshInterval: &metav1.Duration{Duration: 0},
  1877. },
  1878. Status: esv1beta1.ExternalSecretStatus{
  1879. RefreshTime: metav1.Now(),
  1880. },
  1881. }
  1882. es.Status.SyncedResourceVersion = getResourceVersion(es)
  1883. Expect(shouldRefresh(es)).To(BeFalse())
  1884. // update gen -> refresh
  1885. es.ObjectMeta.Generation = 2
  1886. Expect(shouldRefresh(es)).To(BeTrue())
  1887. })
  1888. It("should skip refresh when refreshInterval is 0", func() {
  1889. es := esv1beta1.ExternalSecret{
  1890. ObjectMeta: metav1.ObjectMeta{
  1891. Generation: 1,
  1892. },
  1893. Spec: esv1beta1.ExternalSecretSpec{
  1894. RefreshInterval: &metav1.Duration{Duration: 0},
  1895. },
  1896. Status: esv1beta1.ExternalSecretStatus{},
  1897. }
  1898. // resource version matches
  1899. es.Status.SyncedResourceVersion = getResourceVersion(es)
  1900. Expect(shouldRefresh(es)).To(BeFalse())
  1901. })
  1902. It("should refresh when refresh interval has passed", func() {
  1903. es := esv1beta1.ExternalSecret{
  1904. ObjectMeta: metav1.ObjectMeta{
  1905. Generation: 1,
  1906. },
  1907. Spec: esv1beta1.ExternalSecretSpec{
  1908. RefreshInterval: &metav1.Duration{Duration: time.Second},
  1909. },
  1910. Status: esv1beta1.ExternalSecretStatus{
  1911. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  1912. },
  1913. }
  1914. // resource version matches
  1915. es.Status.SyncedResourceVersion = getResourceVersion(es)
  1916. Expect(shouldRefresh(es)).To(BeTrue())
  1917. })
  1918. It("should refresh when no refresh time was set", func() {
  1919. es := esv1beta1.ExternalSecret{
  1920. ObjectMeta: metav1.ObjectMeta{
  1921. Generation: 1,
  1922. },
  1923. Spec: esv1beta1.ExternalSecretSpec{
  1924. RefreshInterval: &metav1.Duration{Duration: time.Second},
  1925. },
  1926. Status: esv1beta1.ExternalSecretStatus{},
  1927. }
  1928. // resource version matches
  1929. es.Status.SyncedResourceVersion = getResourceVersion(es)
  1930. Expect(shouldRefresh(es)).To(BeTrue())
  1931. })
  1932. })
  1933. Context("objectmeta hash", func() {
  1934. It("should produce different hashes for different k/v pairs", func() {
  1935. h1 := hashMeta(metav1.ObjectMeta{
  1936. Generation: 1,
  1937. Annotations: map[string]string{
  1938. "foo": "bar",
  1939. },
  1940. })
  1941. h2 := hashMeta(metav1.ObjectMeta{
  1942. Generation: 1,
  1943. Annotations: map[string]string{
  1944. "foo": "bing",
  1945. },
  1946. })
  1947. Expect(h1).ToNot(Equal(h2))
  1948. })
  1949. It("should produce different hashes for different generations but same label/annotations", func() {
  1950. h1 := hashMeta(metav1.ObjectMeta{
  1951. Generation: 1,
  1952. Annotations: map[string]string{
  1953. "foo": "bar",
  1954. },
  1955. Labels: map[string]string{
  1956. "foo": "bar",
  1957. },
  1958. })
  1959. h2 := hashMeta(metav1.ObjectMeta{
  1960. Generation: 2,
  1961. Annotations: map[string]string{
  1962. "foo": "bar",
  1963. },
  1964. Labels: map[string]string{
  1965. "foo": "bar",
  1966. },
  1967. })
  1968. Expect(h1).To(Equal(h2))
  1969. })
  1970. It("should produce the same hash for the same k/v pairs", func() {
  1971. h1 := hashMeta(metav1.ObjectMeta{
  1972. Generation: 1,
  1973. })
  1974. h2 := hashMeta(metav1.ObjectMeta{
  1975. Generation: 1,
  1976. })
  1977. Expect(h1).To(Equal(h2))
  1978. h1 = hashMeta(metav1.ObjectMeta{
  1979. Generation: 1,
  1980. Annotations: map[string]string{
  1981. "foo": "bar",
  1982. },
  1983. })
  1984. h2 = hashMeta(metav1.ObjectMeta{
  1985. Generation: 1,
  1986. Annotations: map[string]string{
  1987. "foo": "bar",
  1988. },
  1989. })
  1990. Expect(h1).To(Equal(h2))
  1991. })
  1992. })
  1993. })
  1994. var _ = Describe("Controller Reconcile logic", func() {
  1995. Context("controller reconcile", func() {
  1996. It("should reconcile when resource is not synced", func() {
  1997. Expect(shouldReconcile(esv1beta1.ExternalSecret{
  1998. Status: esv1beta1.ExternalSecretStatus{
  1999. SyncedResourceVersion: "some resource version",
  2000. Conditions: []esv1beta1.ExternalSecretStatusCondition{{Reason: "NotASecretSynced"}},
  2001. },
  2002. })).To(BeTrue())
  2003. })
  2004. It("should reconcile when secret isn't immutable", func() {
  2005. Expect(shouldReconcile(esv1beta1.ExternalSecret{
  2006. Spec: esv1beta1.ExternalSecretSpec{
  2007. Target: esv1beta1.ExternalSecretTarget{
  2008. Immutable: false,
  2009. },
  2010. },
  2011. })).To(BeTrue())
  2012. })
  2013. It("should not reconcile if secret is immutable and has synced condition", func() {
  2014. Expect(shouldReconcile(esv1beta1.ExternalSecret{
  2015. Spec: esv1beta1.ExternalSecretSpec{
  2016. Target: esv1beta1.ExternalSecretTarget{
  2017. Immutable: true,
  2018. },
  2019. },
  2020. Status: esv1beta1.ExternalSecretStatus{
  2021. SyncedResourceVersion: "some resource version",
  2022. Conditions: []esv1beta1.ExternalSecretStatusCondition{{Reason: "SecretSynced"}},
  2023. },
  2024. })).To(BeFalse())
  2025. })
  2026. })
  2027. })
  2028. func externalSecretConditionShouldBe(name, ns string, ct esv1beta1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  2029. return Eventually(func() float64 {
  2030. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  2031. return metric.GetGauge().GetValue()
  2032. }, timeout, interval).Should(Equal(v))
  2033. }
  2034. func init() {
  2035. fakeProvider = fake.New()
  2036. esv1beta1.ForceRegister(fakeProvider, &esv1beta1.SecretStoreProvider{
  2037. AWS: &esv1beta1.AWSProvider{
  2038. Service: esv1beta1.AWSServiceSecretsManager,
  2039. },
  2040. })
  2041. }