externalsecret_controller_test.go 87 KB

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