externalsecret_controller_test.go 85 KB

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