externalsecret_controller_test.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. )
  126. var ExternalSecretNamespace string
  127. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  128. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  129. if t, err := strconv.Atoi(customTimeout); err == nil {
  130. timeout = time.Second * time.Duration(t)
  131. }
  132. }
  133. BeforeEach(func() {
  134. var err error
  135. ExternalSecretNamespace, err = CreateNamespace("test-ns", k8sClient)
  136. Expect(err).ToNot(HaveOccurred())
  137. metric.Reset()
  138. syncCallsTotal.Reset()
  139. syncCallsError.Reset()
  140. externalSecretCondition.Reset()
  141. })
  142. AfterEach(func() {
  143. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  144. ObjectMeta: metav1.ObjectMeta{
  145. Name: ExternalSecretNamespace,
  146. },
  147. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  148. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  149. ObjectMeta: metav1.ObjectMeta{
  150. Name: ExternalSecretStore,
  151. Namespace: ExternalSecretNamespace,
  152. },
  153. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  154. })
  155. const targetProp = "targetProperty"
  156. const remoteKey = "barz"
  157. const remoteProperty = "bang"
  158. makeDefaultTestcase := func() *testCase {
  159. return &testCase{
  160. // default condition: es should be ready
  161. checkCondition: func(es *esv1alpha1.ExternalSecret) bool {
  162. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  163. if cond == nil || cond.Status != v1.ConditionTrue {
  164. return false
  165. }
  166. return true
  167. },
  168. checkExternalSecret: func(es *esv1alpha1.ExternalSecret) {},
  169. secretStore: &esv1alpha1.SecretStore{
  170. ObjectMeta: metav1.ObjectMeta{
  171. Name: ExternalSecretStore,
  172. Namespace: ExternalSecretNamespace,
  173. },
  174. Spec: esv1alpha1.SecretStoreSpec{
  175. Provider: &esv1alpha1.SecretStoreProvider{
  176. AWS: &esv1alpha1.AWSProvider{
  177. Service: esv1alpha1.AWSServiceSecretsManager,
  178. },
  179. },
  180. },
  181. },
  182. externalSecret: &esv1alpha1.ExternalSecret{
  183. ObjectMeta: metav1.ObjectMeta{
  184. Name: ExternalSecretName,
  185. Namespace: ExternalSecretNamespace,
  186. },
  187. Spec: esv1alpha1.ExternalSecretSpec{
  188. SecretStoreRef: esv1alpha1.SecretStoreRef{
  189. Name: ExternalSecretStore,
  190. },
  191. Target: esv1alpha1.ExternalSecretTarget{
  192. Name: ExternalSecretTargetSecretName,
  193. },
  194. Data: []esv1alpha1.ExternalSecretData{
  195. {
  196. SecretKey: targetProp,
  197. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  198. Key: remoteKey,
  199. Property: remoteProperty,
  200. },
  201. },
  202. },
  203. },
  204. },
  205. }
  206. }
  207. // if target Secret name is not specified it should use the ExternalSecret name.
  208. syncWithoutTargetName := func(tc *testCase) {
  209. tc.externalSecret.Spec.Target.Name = ""
  210. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  211. // check secret name
  212. Expect(secret.ObjectMeta.Name).To(Equal(ExternalSecretName))
  213. }
  214. }
  215. // labels and annotations from the Kind=ExternalSecret
  216. // should be copied over to the Kind=Secret
  217. syncLabelsAnnotations := func(tc *testCase) {
  218. const secretVal = "someValue"
  219. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  220. "fooobar": "bazz",
  221. }
  222. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  223. "hihihih": "hehehe",
  224. }
  225. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  226. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  227. // check value
  228. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  229. // check labels & annotations
  230. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  231. for k, v := range es.ObjectMeta.Annotations {
  232. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  233. }
  234. // ownerRef must not not be set!
  235. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeTrue())
  236. }
  237. }
  238. checkPrometheusCounters := func(tc *testCase) {
  239. const secretVal = "someValue"
  240. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  241. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  242. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  243. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  244. Eventually(func() bool {
  245. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  246. return metric.GetCounter().GetValue() == 1.0
  247. }, timeout, interval).Should(BeTrue())
  248. }
  249. }
  250. // merge with existing secret using creationPolicy=Merge
  251. // it should NOT have a ownerReference
  252. // metadata.managedFields with the correct owner should be added to the secret
  253. mergeWithSecret := func(tc *testCase) {
  254. const secretVal = "someValue"
  255. const existingKey = "pre-existing-key"
  256. existingVal := "pre-existing-value"
  257. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  258. // create secret beforehand
  259. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  260. ObjectMeta: metav1.ObjectMeta{
  261. Name: ExternalSecretTargetSecretName,
  262. Namespace: ExternalSecretNamespace,
  263. },
  264. Data: map[string][]byte{
  265. existingKey: []byte(existingVal),
  266. },
  267. }, client.FieldOwner(FakeManager))).To(Succeed())
  268. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  269. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  270. // check value
  271. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  272. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  273. // check labels & annotations
  274. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  275. for k, v := range es.ObjectMeta.Annotations {
  276. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  277. }
  278. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  279. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  280. Expect(hasFieldOwnership(
  281. secret.ObjectMeta,
  282. "external-secrets",
  283. fmt.Sprintf("{\"f:data\":{\"f:targetProperty\":{}},\"f:metadata\":{\"f:annotations\":{\"f:%s\":{}}}}", esv1alpha1.AnnotationDataHash)),
  284. ).To(BeTrue())
  285. Expect(hasFieldOwnership(secret.ObjectMeta, FakeManager, "{\"f:data\":{\".\":{},\"f:pre-existing-key\":{}},\"f:type\":{}}")).To(BeTrue())
  286. }
  287. }
  288. // should not merge with secret if it doesn't exist
  289. mergeWithSecretErr := func(tc *testCase) {
  290. const secretVal = "someValue"
  291. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  292. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  293. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  294. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  295. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  296. return false
  297. }
  298. return true
  299. }
  300. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  301. Eventually(func() bool {
  302. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  303. return metric.GetCounter().GetValue() >= 2.0
  304. }, timeout, interval).Should(BeTrue())
  305. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  306. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  307. }
  308. }
  309. // controller should not force override but
  310. // return an error on conflict
  311. mergeWithConflict := func(tc *testCase) {
  312. const secretVal = "someValue"
  313. // this should confict
  314. const existingKey = targetProp
  315. existingVal := "pre-existing-value"
  316. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  317. // create secret beforehand
  318. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  319. ObjectMeta: metav1.ObjectMeta{
  320. Name: ExternalSecretTargetSecretName,
  321. Namespace: ExternalSecretNamespace,
  322. },
  323. Data: map[string][]byte{
  324. existingKey: []byte(existingVal),
  325. },
  326. }, client.FieldOwner(FakeManager))).To(Succeed())
  327. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  328. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  329. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  330. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  331. return false
  332. }
  333. return true
  334. }
  335. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  336. // check that value stays the same
  337. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  338. Expect(string(secret.Data[targetProp])).ToNot(Equal(secretVal))
  339. // check owner/managedFields
  340. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  341. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(1))
  342. Expect(hasFieldOwnership(secret.ObjectMeta, FakeManager, "{\"f:data\":{\".\":{},\"f:targetProperty\":{}},\"f:type\":{}}")).To(BeTrue())
  343. }
  344. }
  345. // when using a template it should be used as a blueprint
  346. // to construct a new secret: labels, annotations and type
  347. syncWithTemplate := func(tc *testCase) {
  348. const secretVal = "someValue"
  349. const expectedSecretVal = "SOMEVALUE was templated"
  350. const tplStaticKey = "tplstatickey"
  351. const tplStaticVal = "tplstaticvalue"
  352. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  353. "fooobar": "bazz",
  354. }
  355. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  356. "hihihih": "hehehe",
  357. }
  358. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  359. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  360. Labels: map[string]string{
  361. "foos": "ball",
  362. },
  363. Annotations: map[string]string{
  364. "hihi": "ga",
  365. },
  366. },
  367. Type: v1.SecretTypeOpaque,
  368. Data: map[string]string{
  369. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  370. tplStaticKey: tplStaticVal,
  371. },
  372. }
  373. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  374. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  375. // check values
  376. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  377. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  378. // labels/annotations should be taken from the template
  379. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  380. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  381. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  382. }
  383. }
  384. }
  385. // secret should be synced with correct value precedence:
  386. // * template
  387. // * templateFrom
  388. // * data
  389. // * dataFrom
  390. syncWithTemplatePrecedence := func(tc *testCase) {
  391. const secretVal = "someValue"
  392. const expectedSecretVal = "SOMEVALUE was templated"
  393. const tplStaticKey = "tplstatickey"
  394. const tplStaticVal = "tplstaticvalue"
  395. const tplFromCMName = "template-cm"
  396. const tplFromSecretName = "template-secret"
  397. const tplFromKey = "tpl-from-key"
  398. const tplFromSecKey = "tpl-from-sec-key"
  399. const tplFromVal = "tpl-from-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  400. const tplFromSecVal = "tpl-from-sec-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  401. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  402. ObjectMeta: metav1.ObjectMeta{
  403. Name: tplFromCMName,
  404. Namespace: ExternalSecretNamespace,
  405. },
  406. Data: map[string]string{
  407. tplFromKey: tplFromVal,
  408. },
  409. })).To(Succeed())
  410. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  411. ObjectMeta: metav1.ObjectMeta{
  412. Name: tplFromSecretName,
  413. Namespace: ExternalSecretNamespace,
  414. },
  415. Data: map[string][]byte{
  416. tplFromSecKey: []byte(tplFromSecVal),
  417. },
  418. })).To(Succeed())
  419. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  420. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{},
  421. Type: v1.SecretTypeOpaque,
  422. TemplateFrom: []esv1alpha1.TemplateFrom{
  423. {
  424. ConfigMap: &esv1alpha1.TemplateRef{
  425. Name: tplFromCMName,
  426. Items: []esv1alpha1.TemplateRefItem{
  427. {
  428. Key: tplFromKey,
  429. },
  430. },
  431. },
  432. },
  433. {
  434. Secret: &esv1alpha1.TemplateRef{
  435. Name: tplFromSecretName,
  436. Items: []esv1alpha1.TemplateRefItem{
  437. {
  438. Key: tplFromSecKey,
  439. },
  440. },
  441. },
  442. },
  443. },
  444. Data: map[string]string{
  445. // this should be the data value, not dataFrom
  446. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  447. // this should use the value from the map
  448. "bar": "value from map: {{ .bar | toString }}",
  449. // just a static value
  450. tplStaticKey: tplStaticVal,
  451. },
  452. }
  453. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  454. {
  455. Key: "datamap",
  456. },
  457. }
  458. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  459. fakeProvider.WithGetSecretMap(map[string][]byte{
  460. "targetProperty": []byte("map-foo-value"),
  461. "bar": []byte("map-bar-value"),
  462. }, nil)
  463. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  464. // check values
  465. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  466. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  467. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  468. Expect(string(secret.Data[tplFromKey])).To(Equal("tpl-from-value: someValue // map-bar-value"))
  469. Expect(string(secret.Data[tplFromSecKey])).To(Equal("tpl-from-sec-value: someValue // map-bar-value"))
  470. }
  471. }
  472. refreshWithTemplate := func(tc *testCase) {
  473. const secretVal = "someValue"
  474. const expectedSecretVal = "SOMEVALUE was templated"
  475. const tplStaticKey = "tplstatickey"
  476. const tplStaticVal = "tplstaticvalue"
  477. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  478. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  479. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  480. Labels: map[string]string{"foo": "bar"},
  481. Annotations: map[string]string{"foo": "bar"},
  482. },
  483. Type: v1.SecretTypeOpaque,
  484. Data: map[string]string{
  485. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  486. tplStaticKey: tplStaticVal,
  487. },
  488. }
  489. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  490. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  491. // check values
  492. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  493. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  494. // labels/annotations should be taken from the template
  495. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  496. // a secret will always have some extra annotations (i.e. hashmap check), so we only check for specific
  497. // source annotations
  498. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  499. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  500. }
  501. cleanEs := tc.externalSecret.DeepCopy()
  502. // now update ExternalSecret
  503. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  504. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  505. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  506. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  507. // wait for secret
  508. sec := &v1.Secret{}
  509. secretLookupKey := types.NamespacedName{
  510. Name: ExternalSecretTargetSecretName,
  511. Namespace: ExternalSecretNamespace,
  512. }
  513. Eventually(func() bool {
  514. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  515. if err != nil {
  516. return false
  517. }
  518. // ensure new data value exist
  519. return string(sec.Data["new"]) == "value"
  520. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  521. // also check labels/annotations have been updated
  522. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  523. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  524. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  525. }
  526. }
  527. }
  528. onlyMetadataFromTemplate := func(tc *testCase) {
  529. const secretVal = "someValue"
  530. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  531. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  532. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  533. Labels: map[string]string{"foo": "bar"},
  534. Annotations: map[string]string{"foo": "bar"},
  535. },
  536. }
  537. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  538. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  539. // check values
  540. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  541. // labels/annotations should be taken from the template
  542. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  543. for k, v := range es.Spec.Target.Template.Metadata.Annotations {
  544. Expect(secret.ObjectMeta.Annotations).To(HaveKeyWithValue(k, v))
  545. }
  546. }
  547. }
  548. // when the provider secret changes the Kind=Secret value
  549. // must change, too.
  550. refreshSecretValue := func(tc *testCase) {
  551. const targetProp = "targetProperty"
  552. const secretVal = "someValue"
  553. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  554. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  555. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  556. // check values
  557. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  558. // update provider secret
  559. newValue := "NEW VALUE"
  560. sec := &v1.Secret{}
  561. fakeProvider.WithGetSecret([]byte(newValue), nil)
  562. secretLookupKey := types.NamespacedName{
  563. Name: ExternalSecretTargetSecretName,
  564. Namespace: ExternalSecretNamespace,
  565. }
  566. Eventually(func() bool {
  567. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  568. if err != nil {
  569. return false
  570. }
  571. v := sec.Data[targetProp]
  572. return string(v) == newValue
  573. }, timeout, interval).Should(BeTrue())
  574. }
  575. }
  576. refreshintervalZero := func(tc *testCase) {
  577. const targetProp = "targetProperty"
  578. const secretVal = "someValue"
  579. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  580. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: 0}
  581. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  582. // check values
  583. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  584. // update provider secret
  585. newValue := "NEW VALUE"
  586. sec := &v1.Secret{}
  587. fakeProvider.WithGetSecret([]byte(newValue), nil)
  588. secretLookupKey := types.NamespacedName{
  589. Name: ExternalSecretTargetSecretName,
  590. Namespace: ExternalSecretNamespace,
  591. }
  592. Consistently(func() bool {
  593. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  594. if err != nil {
  595. return false
  596. }
  597. v := sec.Data[targetProp]
  598. return string(v) == secretVal
  599. }, time.Second*10, time.Second).Should(BeTrue())
  600. }
  601. }
  602. // with dataFrom all properties from the specified secret
  603. // should be put into the secret
  604. syncWithDataFrom := func(tc *testCase) {
  605. tc.externalSecret.Spec.Data = nil
  606. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  607. {
  608. Key: remoteKey,
  609. },
  610. }
  611. fakeProvider.WithGetSecretMap(map[string][]byte{
  612. "foo": []byte("map-foo-value"),
  613. "bar": []byte("map-bar-value"),
  614. }, nil)
  615. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  616. // check values
  617. Expect(string(secret.Data["foo"])).To(Equal("map-foo-value"))
  618. Expect(string(secret.Data["bar"])).To(Equal("map-bar-value"))
  619. }
  620. }
  621. // with dataFrom and using a template
  622. // should be put into the secret
  623. syncWithDataFromTemplate := func(tc *testCase) {
  624. tc.externalSecret.Spec.Data = nil
  625. tc.externalSecret.Spec.Target = esv1alpha1.ExternalSecretTarget{
  626. Name: ExternalSecretTargetSecretName,
  627. Template: &esv1alpha1.ExternalSecretTemplate{
  628. Type: v1.SecretTypeTLS,
  629. },
  630. }
  631. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  632. {
  633. Key: remoteKey,
  634. },
  635. }
  636. fakeProvider.WithGetSecretMap(map[string][]byte{
  637. "tls.crt": []byte("map-foo-value"),
  638. "tls.key": []byte("map-bar-value"),
  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("map-foo-value"))
  644. Expect(string(secret.Data["tls.key"])).To(Equal("map-bar-value"))
  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("map-foo-value"),
  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. // CreateNamespace creates a new namespace in the cluster.
  1058. func CreateNamespace(baseName string, c client.Client) (string, error) {
  1059. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  1060. ns := &v1.Namespace{
  1061. ObjectMeta: metav1.ObjectMeta{
  1062. GenerateName: genName,
  1063. },
  1064. }
  1065. var err error
  1066. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  1067. err = c.Create(context.Background(), ns)
  1068. if err != nil {
  1069. return false, nil
  1070. }
  1071. return true, nil
  1072. })
  1073. if err != nil {
  1074. return "", err
  1075. }
  1076. return ns.Name, nil
  1077. }
  1078. func hasOwnerRef(meta metav1.ObjectMeta, kind, name string) bool {
  1079. for _, ref := range meta.OwnerReferences {
  1080. if ref.Kind == kind && ref.Name == name {
  1081. return true
  1082. }
  1083. }
  1084. return false
  1085. }
  1086. func hasFieldOwnership(meta metav1.ObjectMeta, mgr, rawFields string) bool {
  1087. for _, ref := range meta.ManagedFields {
  1088. if ref.Manager == mgr && string(ref.FieldsV1.Raw) == rawFields {
  1089. return true
  1090. }
  1091. }
  1092. return false
  1093. }
  1094. func externalSecretConditionShouldBe(name, ns string, ct esv1alpha1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  1095. return Eventually(func() float64 {
  1096. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  1097. return metric.GetGauge().GetValue()
  1098. }, timeout, interval).Should(Equal(v))
  1099. }
  1100. func init() {
  1101. fakeProvider = fake.New()
  1102. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  1103. AWS: &esv1alpha1.AWSProvider{
  1104. Service: esv1alpha1.AWSServiceSecretsManager,
  1105. },
  1106. })
  1107. }