externalsecret_controller_test.go 96 KB

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