externalsecret_controller_test.go 48 KB

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