externalsecret_controller_test.go 65 KB

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