externalsecret_controller_test.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. "context"
  15. "fmt"
  16. "os"
  17. "strconv"
  18. "time"
  19. . "github.com/onsi/ginkgo"
  20. . "github.com/onsi/ginkgo/extensions/table"
  21. . "github.com/onsi/gomega"
  22. dto "github.com/prometheus/client_model/go"
  23. v1 "k8s.io/api/core/v1"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/apimachinery/pkg/util/wait"
  27. "sigs.k8s.io/controller-runtime/pkg/client"
  28. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  29. "github.com/external-secrets/external-secrets/pkg/provider"
  30. "github.com/external-secrets/external-secrets/pkg/provider/fake"
  31. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  32. )
  33. var (
  34. fakeProvider *fake.Client
  35. metric dto.Metric
  36. timeout = time.Second * 10
  37. interval = time.Millisecond * 250
  38. )
  39. type testCase struct {
  40. secretStore *esv1alpha1.SecretStore
  41. externalSecret *esv1alpha1.ExternalSecret
  42. // checkCondition should return true if the externalSecret
  43. // has the expected condition
  44. checkCondition func(*esv1alpha1.ExternalSecret) bool
  45. // checkExternalSecret is called after the condition has been verified
  46. // use this to verify the externalSecret
  47. checkExternalSecret func(*esv1alpha1.ExternalSecret)
  48. // optional. use this to test the secret value
  49. checkSecret func(*esv1alpha1.ExternalSecret, *v1.Secret)
  50. }
  51. type testTweaks func(*testCase)
  52. var _ = Describe("Kind=secret existence logic", func() {
  53. type testCase struct {
  54. Name string
  55. Input v1.Secret
  56. ExpectedOutput bool
  57. }
  58. tests := []testCase{
  59. {
  60. Name: "Should not be valid in case of missing uid",
  61. Input: v1.Secret{},
  62. ExpectedOutput: false,
  63. },
  64. {
  65. Name: "A nil annotation should not be valid",
  66. Input: v1.Secret{
  67. ObjectMeta: metav1.ObjectMeta{
  68. UID: "xxx",
  69. Annotations: map[string]string{},
  70. },
  71. },
  72. ExpectedOutput: false,
  73. },
  74. {
  75. Name: "A nil annotation should not be valid",
  76. Input: v1.Secret{
  77. ObjectMeta: metav1.ObjectMeta{
  78. UID: "xxx",
  79. Annotations: map[string]string{},
  80. },
  81. },
  82. ExpectedOutput: false,
  83. },
  84. {
  85. Name: "An invalid annotation hash should not be valid",
  86. Input: v1.Secret{
  87. ObjectMeta: metav1.ObjectMeta{
  88. UID: "xxx",
  89. Annotations: map[string]string{
  90. esv1alpha1.AnnotationDataHash: "xxxxxx",
  91. },
  92. },
  93. },
  94. ExpectedOutput: false,
  95. },
  96. {
  97. Name: "A valid config map should return true",
  98. Input: v1.Secret{
  99. ObjectMeta: metav1.ObjectMeta{
  100. UID: "xxx",
  101. Annotations: map[string]string{
  102. esv1alpha1.AnnotationDataHash: "caa0155759a6a9b3b6ada5a6883ee2bb",
  103. },
  104. },
  105. Data: map[string][]byte{
  106. "foo": []byte("value1"),
  107. "bar": []byte("value2"),
  108. },
  109. },
  110. ExpectedOutput: true,
  111. },
  112. }
  113. for _, tt := range tests {
  114. It(tt.Name, func() {
  115. Expect(isSecretValid(tt.Input)).To(BeEquivalentTo(tt.ExpectedOutput))
  116. })
  117. }
  118. })
  119. var _ = Describe("ExternalSecret controller", func() {
  120. const (
  121. ExternalSecretName = "test-es"
  122. ExternalSecretStore = "test-store"
  123. ExternalSecretTargetSecretName = "test-secret"
  124. FakeManager = "fake.manager"
  125. expectedSecretVal = "SOMEVALUE was templated"
  126. targetPropObj = "{{ .targetProperty | toString | upper }} was templated"
  127. FooValue = "map-foo-value"
  128. BarValue = "map-bar-value"
  129. )
  130. var ExternalSecretNamespace string
  131. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  132. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  133. if t, err := strconv.Atoi(customTimeout); err == nil {
  134. timeout = time.Second * time.Duration(t)
  135. }
  136. }
  137. BeforeEach(func() {
  138. var err error
  139. ExternalSecretNamespace, err = CreateNamespace("test-ns", k8sClient)
  140. Expect(err).ToNot(HaveOccurred())
  141. metric.Reset()
  142. syncCallsTotal.Reset()
  143. syncCallsError.Reset()
  144. externalSecretCondition.Reset()
  145. })
  146. AfterEach(func() {
  147. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  148. ObjectMeta: metav1.ObjectMeta{
  149. Name: ExternalSecretNamespace,
  150. },
  151. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  152. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  153. ObjectMeta: metav1.ObjectMeta{
  154. Name: ExternalSecretStore,
  155. Namespace: ExternalSecretNamespace,
  156. },
  157. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  158. })
  159. const targetProp = "targetProperty"
  160. const remoteKey = "barz"
  161. const remoteProperty = "bang"
  162. makeDefaultTestcase := func() *testCase {
  163. return &testCase{
  164. // default condition: es should be ready
  165. checkCondition: func(es *esv1alpha1.ExternalSecret) bool {
  166. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  167. if cond == nil || cond.Status != v1.ConditionTrue {
  168. return false
  169. }
  170. return true
  171. },
  172. checkExternalSecret: func(es *esv1alpha1.ExternalSecret) {},
  173. secretStore: &esv1alpha1.SecretStore{
  174. ObjectMeta: metav1.ObjectMeta{
  175. Name: ExternalSecretStore,
  176. Namespace: ExternalSecretNamespace,
  177. },
  178. Spec: esv1alpha1.SecretStoreSpec{
  179. Provider: &esv1alpha1.SecretStoreProvider{
  180. AWS: &esv1alpha1.AWSProvider{
  181. Service: esv1alpha1.AWSServiceSecretsManager,
  182. },
  183. },
  184. },
  185. },
  186. externalSecret: &esv1alpha1.ExternalSecret{
  187. ObjectMeta: metav1.ObjectMeta{
  188. Name: ExternalSecretName,
  189. Namespace: ExternalSecretNamespace,
  190. },
  191. Spec: esv1alpha1.ExternalSecretSpec{
  192. SecretStoreRef: esv1alpha1.SecretStoreRef{
  193. Name: ExternalSecretStore,
  194. },
  195. Target: esv1alpha1.ExternalSecretTarget{
  196. Name: ExternalSecretTargetSecretName,
  197. },
  198. Data: []esv1alpha1.ExternalSecretData{
  199. {
  200. SecretKey: targetProp,
  201. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  202. Extract: esv1alpha1.ExternalSecretExtract{
  203. Key: remoteKey,
  204. Property: remoteProperty,
  205. },
  206. },
  207. },
  208. },
  209. },
  210. },
  211. }
  212. }
  213. // if target Secret name is not specified it should use the ExternalSecret name.
  214. syncWithoutTargetName := func(tc *testCase) {
  215. tc.externalSecret.Spec.Target.Name = ""
  216. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  217. // check secret name
  218. Expect(secret.ObjectMeta.Name).To(Equal(ExternalSecretName))
  219. }
  220. }
  221. // labels and annotations from the Kind=ExternalSecret
  222. // should be copied over to the Kind=Secret
  223. syncLabelsAnnotations := func(tc *testCase) {
  224. const secretVal = "someValue"
  225. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  226. "fooobar": "bazz",
  227. }
  228. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  229. "hihihih": "hehehe",
  230. }
  231. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  232. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  233. // check value
  234. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  235. // check labels & annotations
  236. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  237. for k, v := range es.ObjectMeta.Annotations {
  238. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  239. }
  240. // ownerRef must not not be set!
  241. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeTrue())
  242. }
  243. }
  244. checkPrometheusCounters := func(tc *testCase) {
  245. const secretVal = "someValue"
  246. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  247. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  248. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  249. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  250. Eventually(func() bool {
  251. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  252. return metric.GetCounter().GetValue() == 1.0
  253. }, timeout, interval).Should(BeTrue())
  254. }
  255. }
  256. // merge with existing secret using creationPolicy=Merge
  257. // it should NOT have a ownerReference
  258. // metadata.managedFields with the correct owner should be added to the secret
  259. mergeWithSecret := func(tc *testCase) {
  260. const secretVal = "someValue"
  261. const existingKey = "pre-existing-key"
  262. existingVal := "pre-existing-value"
  263. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  264. // create secret beforehand
  265. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  266. ObjectMeta: metav1.ObjectMeta{
  267. Name: ExternalSecretTargetSecretName,
  268. Namespace: ExternalSecretNamespace,
  269. },
  270. Data: map[string][]byte{
  271. existingKey: []byte(existingVal),
  272. },
  273. }, client.FieldOwner(FakeManager))).To(Succeed())
  274. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  275. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  276. // check value
  277. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  278. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  279. // check labels & annotations
  280. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  281. for k, v := range es.ObjectMeta.Annotations {
  282. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  283. }
  284. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  285. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  286. Expect(hasFieldOwnership(
  287. secret.ObjectMeta,
  288. "external-secrets",
  289. fmt.Sprintf("{\"f:data\":{\"f:targetProperty\":{}},\"f:immutable\":{},\"f:metadata\":{\"f:annotations\":{\"f:%s\":{}}}}", esv1alpha1.AnnotationDataHash)),
  290. ).To(BeTrue())
  291. Expect(hasFieldOwnership(secret.ObjectMeta, FakeManager, "{\"f:data\":{\".\":{},\"f:pre-existing-key\":{}},\"f:type\":{}}")).To(BeTrue())
  292. }
  293. }
  294. // should not merge with secret if it doesn't exist
  295. mergeWithSecretErr := func(tc *testCase) {
  296. const secretVal = "someValue"
  297. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  298. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  299. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  300. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  301. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  302. return false
  303. }
  304. return true
  305. }
  306. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  307. Eventually(func() bool {
  308. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  309. return metric.GetCounter().GetValue() >= 2.0
  310. }, timeout, interval).Should(BeTrue())
  311. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  312. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  313. }
  314. }
  315. // controller should force ownership
  316. mergeWithConflict := func(tc *testCase) {
  317. const secretVal = "someValue"
  318. // this should confict
  319. const existingKey = targetProp
  320. existingVal := "pre-existing-value"
  321. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  322. // create secret beforehand
  323. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  324. ObjectMeta: metav1.ObjectMeta{
  325. Name: ExternalSecretTargetSecretName,
  326. Namespace: ExternalSecretNamespace,
  327. },
  328. Data: map[string][]byte{
  329. existingKey: []byte(existingVal),
  330. },
  331. }, client.FieldOwner(FakeManager))).To(Succeed())
  332. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  333. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  334. // check that value stays the same
  335. Expect(string(secret.Data[existingKey])).To(Equal(secretVal))
  336. // check owner/managedFields
  337. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  338. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  339. Expect(hasFieldOwnership(secret.ObjectMeta, "external-secrets", "{\"f:data\":{\"f:targetProperty\":{}},\"f:immutable\":{},\"f:metadata\":{\"f:annotations\":{\"f:reconcile.external-secrets.io/data-hash\":{}}}}")).To(BeTrue())
  340. }
  341. }
  342. // when using a template it should be used as a blueprint
  343. // to construct a new secret: labels, annotations and type
  344. syncWithTemplate := func(tc *testCase) {
  345. const secretVal = "someValue"
  346. const tplStaticKey = "tplstatickey"
  347. const tplStaticVal = "tplstaticvalue"
  348. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  349. "fooobar": "bazz",
  350. }
  351. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  352. "hihihih": "hehehe",
  353. }
  354. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  355. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  356. Labels: map[string]string{
  357. "foos": "ball",
  358. },
  359. Annotations: map[string]string{
  360. "hihi": "ga",
  361. },
  362. },
  363. Type: v1.SecretTypeOpaque,
  364. Data: map[string]string{
  365. targetProp: targetPropObj,
  366. tplStaticKey: tplStaticVal,
  367. },
  368. }
  369. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  370. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  371. // check values
  372. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  373. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  374. // labels/annotations should be taken from the template
  375. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  376. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  377. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  378. }
  379. }
  380. }
  381. // secret should be synced with correct value precedence:
  382. // * template
  383. // * templateFrom
  384. // * data
  385. // * dataFrom
  386. syncWithTemplatePrecedence := func(tc *testCase) {
  387. const secretVal = "someValue"
  388. const tplStaticKey = "tplstatickey"
  389. const tplStaticVal = "tplstaticvalue"
  390. const tplFromCMName = "template-cm"
  391. const tplFromSecretName = "template-secret"
  392. const tplFromKey = "tpl-from-key"
  393. const tplFromSecKey = "tpl-from-sec-key"
  394. const tplFromVal = "tpl-from-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  395. const tplFromSecVal = "tpl-from-sec-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  396. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  397. ObjectMeta: metav1.ObjectMeta{
  398. Name: tplFromCMName,
  399. Namespace: ExternalSecretNamespace,
  400. },
  401. Data: map[string]string{
  402. tplFromKey: tplFromVal,
  403. },
  404. })).To(Succeed())
  405. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  406. ObjectMeta: metav1.ObjectMeta{
  407. Name: tplFromSecretName,
  408. Namespace: ExternalSecretNamespace,
  409. },
  410. Data: map[string][]byte{
  411. tplFromSecKey: []byte(tplFromSecVal),
  412. },
  413. })).To(Succeed())
  414. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  415. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{},
  416. Type: v1.SecretTypeOpaque,
  417. TemplateFrom: []esv1alpha1.TemplateFrom{
  418. {
  419. ConfigMap: &esv1alpha1.TemplateRef{
  420. Name: tplFromCMName,
  421. Items: []esv1alpha1.TemplateRefItem{
  422. {
  423. Key: tplFromKey,
  424. },
  425. },
  426. },
  427. },
  428. {
  429. Secret: &esv1alpha1.TemplateRef{
  430. Name: tplFromSecretName,
  431. Items: []esv1alpha1.TemplateRefItem{
  432. {
  433. Key: tplFromSecKey,
  434. },
  435. },
  436. },
  437. },
  438. },
  439. Data: map[string]string{
  440. // this should be the data value, not dataFrom
  441. targetProp: targetPropObj,
  442. // this should use the value from the map
  443. "bar": "value from map: {{ .bar | toString }}",
  444. // just a static value
  445. tplStaticKey: tplStaticVal,
  446. },
  447. }
  448. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  449. {
  450. Extract: esv1alpha1.ExternalSecretExtract{
  451. Key: "datamap",
  452. },
  453. },
  454. }
  455. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  456. fakeProvider.WithGetSecretMap(map[string][]byte{
  457. "targetProperty": []byte(FooValue),
  458. "bar": []byte(BarValue),
  459. }, nil)
  460. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  461. // check values
  462. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  463. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  464. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  465. Expect(string(secret.Data[tplFromKey])).To(Equal("tpl-from-value: someValue // map-bar-value"))
  466. Expect(string(secret.Data[tplFromSecKey])).To(Equal("tpl-from-sec-value: someValue // map-bar-value"))
  467. }
  468. }
  469. refreshWithTemplate := func(tc *testCase) {
  470. const secretVal = "someValue"
  471. const tplStaticKey = "tplstatickey"
  472. const tplStaticVal = "tplstaticvalue"
  473. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  474. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  475. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  476. Labels: map[string]string{"foo": "bar"},
  477. Annotations: map[string]string{"foo": "bar"},
  478. },
  479. Type: v1.SecretTypeOpaque,
  480. Data: map[string]string{
  481. targetProp: targetPropObj,
  482. tplStaticKey: tplStaticVal,
  483. },
  484. }
  485. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  486. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  487. // check values
  488. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  489. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  490. // labels/annotations should be taken from the template
  491. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  492. // a secret will always have some extra annotations (i.e. hashmap check), so we only check for specific
  493. // source annotations
  494. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  495. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  496. }
  497. cleanEs := tc.externalSecret.DeepCopy()
  498. // now update ExternalSecret
  499. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  500. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  501. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  502. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  503. // wait for secret
  504. sec := &v1.Secret{}
  505. secretLookupKey := types.NamespacedName{
  506. Name: ExternalSecretTargetSecretName,
  507. Namespace: ExternalSecretNamespace,
  508. }
  509. Eventually(func() bool {
  510. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  511. if err != nil {
  512. return false
  513. }
  514. // ensure new data value exist
  515. return string(sec.Data["new"]) == "value"
  516. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  517. // also check labels/annotations have been updated
  518. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  519. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  520. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  521. }
  522. }
  523. }
  524. onlyMetadataFromTemplate := func(tc *testCase) {
  525. const secretVal = "someValue"
  526. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  527. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  528. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  529. Labels: map[string]string{"foo": "bar"},
  530. Annotations: map[string]string{"foo": "bar"},
  531. },
  532. }
  533. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  534. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  535. // check values
  536. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  537. // labels/annotations should be taken from the template
  538. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  539. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  540. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  541. }
  542. }
  543. }
  544. // when the provider secret changes the Kind=Secret value
  545. // must change, too.
  546. refreshSecretValue := func(tc *testCase) {
  547. const targetProp = "targetProperty"
  548. const secretVal = "someValue"
  549. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  550. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  551. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  552. // check values
  553. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  554. // update provider secret
  555. newValue := "NEW VALUE"
  556. sec := &v1.Secret{}
  557. fakeProvider.WithGetSecret([]byte(newValue), nil)
  558. secretLookupKey := types.NamespacedName{
  559. Name: ExternalSecretTargetSecretName,
  560. Namespace: ExternalSecretNamespace,
  561. }
  562. Eventually(func() bool {
  563. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  564. if err != nil {
  565. return false
  566. }
  567. v := sec.Data[targetProp]
  568. return string(v) == newValue
  569. }, timeout, interval).Should(BeTrue())
  570. }
  571. }
  572. refreshintervalZero := func(tc *testCase) {
  573. const targetProp = "targetProperty"
  574. const secretVal = "someValue"
  575. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  576. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: 0}
  577. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  578. // check values
  579. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  580. // update provider secret
  581. newValue := "NEW VALUE"
  582. sec := &v1.Secret{}
  583. fakeProvider.WithGetSecret([]byte(newValue), nil)
  584. secretLookupKey := types.NamespacedName{
  585. Name: ExternalSecretTargetSecretName,
  586. Namespace: ExternalSecretNamespace,
  587. }
  588. Consistently(func() bool {
  589. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  590. if err != nil {
  591. return false
  592. }
  593. v := sec.Data[targetProp]
  594. return string(v) == secretVal
  595. }, time.Second*10, time.Second).Should(BeTrue())
  596. }
  597. }
  598. // with dataFrom all properties from the specified secret
  599. // should be put into the secret
  600. syncWithDataFrom := func(tc *testCase) {
  601. tc.externalSecret.Spec.Data = nil
  602. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  603. {
  604. Extract: esv1alpha1.ExternalSecretExtract{
  605. Key: remoteKey,
  606. },
  607. },
  608. }
  609. fakeProvider.WithGetSecretMap(map[string][]byte{
  610. "foo": []byte(FooValue),
  611. "bar": []byte(BarValue),
  612. }, nil)
  613. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  614. // check values
  615. Expect(string(secret.Data["foo"])).To(Equal(FooValue))
  616. Expect(string(secret.Data["bar"])).To(Equal(BarValue))
  617. }
  618. }
  619. // with dataFrom and using a template
  620. // should be put into the secret
  621. syncWithDataFromTemplate := func(tc *testCase) {
  622. tc.externalSecret.Spec.Data = nil
  623. tc.externalSecret.Spec.Target = esv1alpha1.ExternalSecretTarget{
  624. Name: ExternalSecretTargetSecretName,
  625. Template: &esv1alpha1.ExternalSecretTemplate{
  626. Type: v1.SecretTypeTLS,
  627. },
  628. }
  629. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  630. {
  631. Extract: esv1alpha1.ExternalSecretExtract{
  632. Key: remoteKey,
  633. },
  634. },
  635. }
  636. fakeProvider.WithGetSecretMap(map[string][]byte{
  637. "tls.crt": []byte(FooValue),
  638. "tls.key": []byte(BarValue),
  639. }, nil)
  640. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  641. Expect(secret.Type).To(Equal(v1.SecretTypeTLS))
  642. // check values
  643. Expect(string(secret.Data["tls.crt"])).To(Equal(FooValue))
  644. Expect(string(secret.Data["tls.key"])).To(Equal(BarValue))
  645. }
  646. }
  647. // when a provider errors in a GetSecret call
  648. // a error condition must be set.
  649. providerErrCondition := func(tc *testCase) {
  650. const secretVal = "foobar"
  651. fakeProvider.WithGetSecret(nil, fmt.Errorf("boom"))
  652. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  653. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  654. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  655. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  656. return false
  657. }
  658. return true
  659. }
  660. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  661. Eventually(func() bool {
  662. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  663. return metric.GetCounter().GetValue() >= 2.0
  664. }, timeout, interval).Should(BeTrue())
  665. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  666. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  667. // es condition should reflect recovered provider error
  668. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  669. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  670. Eventually(func() bool {
  671. err := k8sClient.Get(context.Background(), esKey, es)
  672. if err != nil {
  673. return false
  674. }
  675. // condition must now be true!
  676. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  677. if cond == nil && cond.Status != v1.ConditionTrue {
  678. return false
  679. }
  680. return true
  681. }, timeout, interval).Should(BeTrue())
  682. }
  683. }
  684. // When a ExternalSecret references an non-existing SecretStore
  685. // a error condition must be set.
  686. storeMissingErrCondition := func(tc *testCase) {
  687. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  688. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  689. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  690. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  691. return false
  692. }
  693. return true
  694. }
  695. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  696. Eventually(func() bool {
  697. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  698. return metric.GetCounter().GetValue() >= 2.0
  699. }, timeout, interval).Should(BeTrue())
  700. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  701. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  702. }
  703. }
  704. // when the provider constructor errors (e.g. invalid configuration)
  705. // a SecretSyncedError status condition must be set
  706. storeConstructErrCondition := func(tc *testCase) {
  707. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  708. string) (provider.SecretsClient, error) {
  709. return nil, fmt.Errorf("artificial constructor error")
  710. })
  711. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  712. // condition must be false
  713. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  714. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  715. return false
  716. }
  717. return true
  718. }
  719. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  720. Eventually(func() bool {
  721. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  722. return metric.GetCounter().GetValue() >= 2.0
  723. }, timeout, interval).Should(BeTrue())
  724. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  725. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  726. }
  727. }
  728. // when a SecretStore has a controller field set which we don't care about
  729. // the externalSecret must not be touched
  730. ignoreMismatchController := func(tc *testCase) {
  731. tc.secretStore.Spec.Controller = "nop"
  732. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  733. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  734. return cond == nil
  735. }
  736. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  737. // Condition True and False should be 0, since the Condition was not created
  738. Eventually(func() float64 {
  739. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric)).To(Succeed())
  740. return metric.GetGauge().GetValue()
  741. }, timeout, interval).Should(Equal(0.0))
  742. Eventually(func() float64 {
  743. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric)).To(Succeed())
  744. return metric.GetGauge().GetValue()
  745. }, timeout, interval).Should(Equal(0.0))
  746. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  747. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  748. }
  749. }
  750. // When the ownership is set to owner, and we delete a dependent child kind=secret
  751. // it should be recreated without waiting for refresh interval
  752. checkDeletion := func(tc *testCase) {
  753. const secretVal = "someValue"
  754. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  755. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  756. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  757. // check values
  758. oldUID := secret.UID
  759. Expect(oldUID).NotTo(BeEmpty())
  760. // delete the related config
  761. Expect(k8sClient.Delete(context.TODO(), secret))
  762. var newSecret v1.Secret
  763. secretLookupKey := types.NamespacedName{
  764. Name: ExternalSecretTargetSecretName,
  765. Namespace: ExternalSecretNamespace,
  766. }
  767. Eventually(func() bool {
  768. err := k8sClient.Get(context.Background(), secretLookupKey, &newSecret)
  769. if err != nil {
  770. return false
  771. }
  772. // new secret should be a new, recreated object with a different UID
  773. return newSecret.UID != oldUID
  774. }, timeout, interval).Should(BeTrue())
  775. }
  776. }
  777. // Checks that secret annotation has been written based on the data
  778. checkSecretDataHashAnnotation := func(tc *testCase) {
  779. const secretVal = "someValue"
  780. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  781. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  782. Expect(secret.Annotations[esv1alpha1.AnnotationDataHash]).To(Equal("9d30b95ca81e156f9454b5ef3bfcc6ee"))
  783. }
  784. }
  785. // When we amend the created kind=secret, refresh operation should be run again regardless of refresh interval
  786. checkSecretDataHashAnnotationChange := func(tc *testCase) {
  787. fakeData := map[string][]byte{
  788. "targetProperty": []byte(FooValue),
  789. }
  790. fakeProvider.WithGetSecretMap(fakeData, nil)
  791. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Minute * 10}
  792. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  793. oldHash := secret.Annotations[esv1alpha1.AnnotationDataHash]
  794. oldResourceVersion := secret.ResourceVersion
  795. Expect(oldHash).NotTo(BeEmpty())
  796. cleanSecret := secret.DeepCopy()
  797. secret.Data["new"] = []byte("value")
  798. secret.ObjectMeta.Annotations[esv1alpha1.AnnotationDataHash] = "thisiswronghash"
  799. Expect(k8sClient.Patch(context.Background(), secret, client.MergeFrom(cleanSecret))).To(Succeed())
  800. var refreshedSecret v1.Secret
  801. secretLookupKey := types.NamespacedName{
  802. Name: ExternalSecretTargetSecretName,
  803. Namespace: ExternalSecretNamespace,
  804. }
  805. Eventually(func() bool {
  806. err := k8sClient.Get(context.Background(), secretLookupKey, &refreshedSecret)
  807. if err != nil {
  808. return false
  809. }
  810. // refreshed secret should have a different generation (sign that it was updated), but since
  811. // the secret source is the same (not changed), the hash should be reverted to an old value
  812. return refreshedSecret.ResourceVersion != oldResourceVersion && refreshedSecret.Annotations[esv1alpha1.AnnotationDataHash] == oldHash
  813. }, timeout, interval).Should(BeTrue())
  814. }
  815. }
  816. DescribeTable("When reconciling an ExternalSecret",
  817. func(tweaks ...testTweaks) {
  818. tc := makeDefaultTestcase()
  819. for _, tweak := range tweaks {
  820. tweak(tc)
  821. }
  822. ctx := context.Background()
  823. By("creating a secret store and external secret")
  824. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  825. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  826. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  827. createdES := &esv1alpha1.ExternalSecret{}
  828. By("checking the es condition")
  829. Eventually(func() bool {
  830. err := k8sClient.Get(ctx, esKey, createdES)
  831. if err != nil {
  832. return false
  833. }
  834. return tc.checkCondition(createdES)
  835. }, timeout, interval).Should(BeTrue())
  836. tc.checkExternalSecret(createdES)
  837. // this must be optional so we can test faulty es configuration
  838. if tc.checkSecret != nil {
  839. syncedSecret := &v1.Secret{}
  840. secretLookupKey := types.NamespacedName{
  841. Name: ExternalSecretTargetSecretName,
  842. Namespace: ExternalSecretNamespace,
  843. }
  844. if createdES.Spec.Target.Name == "" {
  845. secretLookupKey = types.NamespacedName{
  846. Name: ExternalSecretName,
  847. Namespace: ExternalSecretNamespace,
  848. }
  849. }
  850. Eventually(func() bool {
  851. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  852. return err == nil
  853. }, timeout, interval).Should(BeTrue())
  854. tc.checkSecret(createdES, syncedSecret)
  855. }
  856. },
  857. Entry("should recreate deleted secret", checkDeletion),
  858. Entry("should create proper hash annotation for the external secret", checkSecretDataHashAnnotation),
  859. Entry("should refresh when the hash annotation doesn't correspond to secret data", checkSecretDataHashAnnotationChange),
  860. Entry("should use external secret name if target secret name isn't defined", syncWithoutTargetName),
  861. Entry("should set the condition eventually", syncLabelsAnnotations),
  862. Entry("should set prometheus counters", checkPrometheusCounters),
  863. Entry("should merge with existing secret using creationPolicy=Merge", mergeWithSecret),
  864. Entry("should error if secret doesn't exist when using creationPolicy=Merge", mergeWithSecretErr),
  865. Entry("should not resolve conflicts with creationPolicy=Merge", mergeWithConflict),
  866. Entry("should sync with template", syncWithTemplate),
  867. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  868. Entry("should refresh secret from template", refreshWithTemplate),
  869. Entry("should be able to use only metadata from template", onlyMetadataFromTemplate),
  870. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  871. Entry("should not refresh secret value when provider secret changes but refreshInterval is zero", refreshintervalZero),
  872. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  873. Entry("should fetch secret using dataFrom and a template", syncWithDataFromTemplate),
  874. Entry("should set error condition when provider errors", providerErrCondition),
  875. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  876. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  877. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  878. )
  879. })
  880. var _ = Describe("ExternalSecret refresh logic", func() {
  881. Context("secret refresh", func() {
  882. It("should refresh when resource version does not match", func() {
  883. Expect(shouldRefresh(esv1alpha1.ExternalSecret{
  884. Status: esv1alpha1.ExternalSecretStatus{
  885. SyncedResourceVersion: "some resource version",
  886. },
  887. })).To(BeTrue())
  888. })
  889. It("should refresh when labels change", func() {
  890. es := esv1alpha1.ExternalSecret{
  891. ObjectMeta: metav1.ObjectMeta{
  892. Generation: 1,
  893. Labels: map[string]string{
  894. "foo": "bar",
  895. },
  896. },
  897. Spec: esv1alpha1.ExternalSecretSpec{
  898. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  899. },
  900. Status: esv1alpha1.ExternalSecretStatus{
  901. RefreshTime: metav1.Now(),
  902. },
  903. }
  904. es.Status.SyncedResourceVersion = getResourceVersion(es)
  905. // this should not refresh, rv matches object
  906. Expect(shouldRefresh(es)).To(BeFalse())
  907. // change labels without changing the syncedResourceVersion and expect refresh
  908. es.ObjectMeta.Labels["new"] = "w00t"
  909. Expect(shouldRefresh(es)).To(BeTrue())
  910. })
  911. It("should refresh when annotations change", func() {
  912. es := esv1alpha1.ExternalSecret{
  913. ObjectMeta: metav1.ObjectMeta{
  914. Generation: 1,
  915. Annotations: map[string]string{
  916. "foo": "bar",
  917. },
  918. },
  919. Spec: esv1alpha1.ExternalSecretSpec{
  920. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  921. },
  922. Status: esv1alpha1.ExternalSecretStatus{
  923. RefreshTime: metav1.Now(),
  924. },
  925. }
  926. es.Status.SyncedResourceVersion = getResourceVersion(es)
  927. // this should not refresh, rv matches object
  928. Expect(shouldRefresh(es)).To(BeFalse())
  929. // change annotations without changing the syncedResourceVersion and expect refresh
  930. es.ObjectMeta.Annotations["new"] = "w00t"
  931. Expect(shouldRefresh(es)).To(BeTrue())
  932. })
  933. It("should refresh when generation has changed", func() {
  934. es := esv1alpha1.ExternalSecret{
  935. ObjectMeta: metav1.ObjectMeta{
  936. Generation: 1,
  937. },
  938. Spec: esv1alpha1.ExternalSecretSpec{
  939. RefreshInterval: &metav1.Duration{Duration: 0},
  940. },
  941. Status: esv1alpha1.ExternalSecretStatus{
  942. RefreshTime: metav1.Now(),
  943. },
  944. }
  945. es.Status.SyncedResourceVersion = getResourceVersion(es)
  946. Expect(shouldRefresh(es)).To(BeFalse())
  947. // update gen -> refresh
  948. es.ObjectMeta.Generation = 2
  949. Expect(shouldRefresh(es)).To(BeTrue())
  950. })
  951. It("should skip refresh when refreshInterval is 0", func() {
  952. es := esv1alpha1.ExternalSecret{
  953. ObjectMeta: metav1.ObjectMeta{
  954. Generation: 1,
  955. },
  956. Spec: esv1alpha1.ExternalSecretSpec{
  957. RefreshInterval: &metav1.Duration{Duration: 0},
  958. },
  959. Status: esv1alpha1.ExternalSecretStatus{},
  960. }
  961. // resource version matches
  962. es.Status.SyncedResourceVersion = getResourceVersion(es)
  963. Expect(shouldRefresh(es)).To(BeFalse())
  964. })
  965. It("should refresh when refresh interval has passed", func() {
  966. es := esv1alpha1.ExternalSecret{
  967. ObjectMeta: metav1.ObjectMeta{
  968. Generation: 1,
  969. },
  970. Spec: esv1alpha1.ExternalSecretSpec{
  971. RefreshInterval: &metav1.Duration{Duration: time.Second},
  972. },
  973. Status: esv1alpha1.ExternalSecretStatus{
  974. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  975. },
  976. }
  977. // resource version matches
  978. es.Status.SyncedResourceVersion = getResourceVersion(es)
  979. Expect(shouldRefresh(es)).To(BeTrue())
  980. })
  981. It("should refresh when no refresh time was set", func() {
  982. es := esv1alpha1.ExternalSecret{
  983. ObjectMeta: metav1.ObjectMeta{
  984. Generation: 1,
  985. },
  986. Spec: esv1alpha1.ExternalSecretSpec{
  987. RefreshInterval: &metav1.Duration{Duration: time.Second},
  988. },
  989. Status: esv1alpha1.ExternalSecretStatus{},
  990. }
  991. // resource version matches
  992. es.Status.SyncedResourceVersion = getResourceVersion(es)
  993. Expect(shouldRefresh(es)).To(BeTrue())
  994. })
  995. })
  996. Context("objectmeta hash", func() {
  997. It("should produce different hashes for different k/v pairs", func() {
  998. h1 := hashMeta(metav1.ObjectMeta{
  999. Generation: 1,
  1000. Annotations: map[string]string{
  1001. "foo": "bar",
  1002. },
  1003. })
  1004. h2 := hashMeta(metav1.ObjectMeta{
  1005. Generation: 1,
  1006. Annotations: map[string]string{
  1007. "foo": "bing",
  1008. },
  1009. })
  1010. Expect(h1).ToNot(Equal(h2))
  1011. })
  1012. It("should produce different hashes for different generations but same label/annotations", func() {
  1013. h1 := hashMeta(metav1.ObjectMeta{
  1014. Generation: 1,
  1015. Annotations: map[string]string{
  1016. "foo": "bar",
  1017. },
  1018. Labels: map[string]string{
  1019. "foo": "bar",
  1020. },
  1021. })
  1022. h2 := hashMeta(metav1.ObjectMeta{
  1023. Generation: 2,
  1024. Annotations: map[string]string{
  1025. "foo": "bar",
  1026. },
  1027. Labels: map[string]string{
  1028. "foo": "bar",
  1029. },
  1030. })
  1031. Expect(h1).To(Equal(h2))
  1032. })
  1033. It("should produce the same hash for the same k/v pairs", func() {
  1034. h1 := hashMeta(metav1.ObjectMeta{
  1035. Generation: 1,
  1036. })
  1037. h2 := hashMeta(metav1.ObjectMeta{
  1038. Generation: 1,
  1039. })
  1040. Expect(h1).To(Equal(h2))
  1041. h1 = hashMeta(metav1.ObjectMeta{
  1042. Generation: 1,
  1043. Annotations: map[string]string{
  1044. "foo": "bar",
  1045. },
  1046. })
  1047. h2 = hashMeta(metav1.ObjectMeta{
  1048. Generation: 1,
  1049. Annotations: map[string]string{
  1050. "foo": "bar",
  1051. },
  1052. })
  1053. Expect(h1).To(Equal(h2))
  1054. })
  1055. })
  1056. })
  1057. var _ = Describe("Controller Reconcile logic", func() {
  1058. Context("controller reconcile", func() {
  1059. It("should reconcile when resource is not synced", func() {
  1060. Expect(shouldReconcile(esv1alpha1.ExternalSecret{
  1061. Status: esv1alpha1.ExternalSecretStatus{
  1062. SyncedResourceVersion: "some resource version",
  1063. Conditions: []esv1alpha1.ExternalSecretStatusCondition{{Reason: "NotASecretSynced"}},
  1064. },
  1065. })).To(BeTrue())
  1066. })
  1067. It("should reconcile when secret isn't immutable", func() {
  1068. Expect(shouldReconcile(esv1alpha1.ExternalSecret{
  1069. Spec: esv1alpha1.ExternalSecretSpec{
  1070. Target: esv1alpha1.ExternalSecretTarget{
  1071. Immutable: false,
  1072. },
  1073. },
  1074. })).To(BeTrue())
  1075. })
  1076. It("should not reconcile if secret is immutable and has synced condition", func() {
  1077. Expect(shouldReconcile(esv1alpha1.ExternalSecret{
  1078. Spec: esv1alpha1.ExternalSecretSpec{
  1079. Target: esv1alpha1.ExternalSecretTarget{
  1080. Immutable: true,
  1081. },
  1082. },
  1083. Status: esv1alpha1.ExternalSecretStatus{
  1084. SyncedResourceVersion: "some resource version",
  1085. Conditions: []esv1alpha1.ExternalSecretStatusCondition{{Reason: "SecretSynced"}},
  1086. },
  1087. })).To(BeFalse())
  1088. })
  1089. })
  1090. })
  1091. // CreateNamespace creates a new namespace in the cluster.
  1092. func CreateNamespace(baseName string, c client.Client) (string, error) {
  1093. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  1094. ns := &v1.Namespace{
  1095. ObjectMeta: metav1.ObjectMeta{
  1096. GenerateName: genName,
  1097. },
  1098. }
  1099. var err error
  1100. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  1101. err = c.Create(context.Background(), ns)
  1102. if err != nil {
  1103. return false, nil
  1104. }
  1105. return true, nil
  1106. })
  1107. if err != nil {
  1108. return "", err
  1109. }
  1110. return ns.Name, nil
  1111. }
  1112. func hasOwnerRef(meta metav1.ObjectMeta, kind, name string) bool {
  1113. for _, ref := range meta.OwnerReferences {
  1114. if ref.Kind == kind && ref.Name == name {
  1115. return true
  1116. }
  1117. }
  1118. return false
  1119. }
  1120. func hasFieldOwnership(meta metav1.ObjectMeta, mgr, rawFields string) bool {
  1121. for _, ref := range meta.ManagedFields {
  1122. if ref.Manager == mgr && string(ref.FieldsV1.Raw) == rawFields {
  1123. return true
  1124. }
  1125. }
  1126. return false
  1127. }
  1128. func externalSecretConditionShouldBe(name, ns string, ct esv1alpha1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  1129. return Eventually(func() float64 {
  1130. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  1131. return metric.GetGauge().GetValue()
  1132. }, timeout, interval).Should(Equal(v))
  1133. }
  1134. func init() {
  1135. fakeProvider = fake.New()
  1136. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  1137. AWS: &esv1alpha1.AWSProvider{
  1138. Service: esv1alpha1.AWSServiceSecretsManager,
  1139. },
  1140. })
  1141. }