externalsecret_controller_test.go 76 KB

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