externalsecret_controller_test.go 93 KB

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