externalsecret_controller_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. "time"
  17. . "github.com/onsi/ginkgo"
  18. . "github.com/onsi/ginkgo/extensions/table"
  19. . "github.com/onsi/gomega"
  20. dto "github.com/prometheus/client_model/go"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. "github.com/external-secrets/external-secrets/pkg/provider"
  28. "github.com/external-secrets/external-secrets/pkg/provider/fake"
  29. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  30. )
  31. var (
  32. fakeProvider *fake.Client
  33. metric dto.Metric
  34. timeout = time.Second * 10
  35. interval = time.Millisecond * 250
  36. )
  37. type testCase struct {
  38. secretStore *esv1alpha1.SecretStore
  39. externalSecret *esv1alpha1.ExternalSecret
  40. // checkCondition should return true if the externalSecret
  41. // has the expected condition
  42. checkCondition func(*esv1alpha1.ExternalSecret) bool
  43. // checkExternalSecret is called after the condition has been verified
  44. // use this to verify the externalSecret
  45. checkExternalSecret func(*esv1alpha1.ExternalSecret)
  46. // optional. use this to test the secret value
  47. checkSecret func(*esv1alpha1.ExternalSecret, *v1.Secret)
  48. }
  49. type testTweaks func(*testCase)
  50. var _ = Describe("ExternalSecret controller", func() {
  51. const (
  52. ExternalSecretName = "test-es"
  53. ExternalSecretStore = "test-store"
  54. ExternalSecretTargetSecretName = "test-secret"
  55. )
  56. var ExternalSecretNamespace string
  57. BeforeEach(func() {
  58. var err error
  59. ExternalSecretNamespace, err = CreateNamespace("test-ns", k8sClient)
  60. Expect(err).ToNot(HaveOccurred())
  61. metric.Reset()
  62. syncCallsTotal.Reset()
  63. syncCallsError.Reset()
  64. externalSecretCondition.Reset()
  65. })
  66. AfterEach(func() {
  67. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  68. ObjectMeta: metav1.ObjectMeta{
  69. Name: ExternalSecretNamespace,
  70. },
  71. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  72. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  73. ObjectMeta: metav1.ObjectMeta{
  74. Name: ExternalSecretStore,
  75. Namespace: ExternalSecretNamespace,
  76. },
  77. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  78. })
  79. const targetProp = "targetProperty"
  80. const remoteKey = "barz"
  81. const remoteProperty = "bang"
  82. makeDefaultTestcase := func() *testCase {
  83. return &testCase{
  84. // default condition: es should be ready
  85. checkCondition: func(es *esv1alpha1.ExternalSecret) bool {
  86. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  87. if cond == nil || cond.Status != v1.ConditionTrue {
  88. return false
  89. }
  90. return true
  91. },
  92. checkExternalSecret: func(es *esv1alpha1.ExternalSecret) {},
  93. secretStore: &esv1alpha1.SecretStore{
  94. ObjectMeta: metav1.ObjectMeta{
  95. Name: ExternalSecretStore,
  96. Namespace: ExternalSecretNamespace,
  97. },
  98. Spec: esv1alpha1.SecretStoreSpec{
  99. Provider: &esv1alpha1.SecretStoreProvider{
  100. AWS: &esv1alpha1.AWSProvider{
  101. Service: esv1alpha1.AWSServiceSecretsManager,
  102. },
  103. },
  104. },
  105. },
  106. externalSecret: &esv1alpha1.ExternalSecret{
  107. ObjectMeta: metav1.ObjectMeta{
  108. Name: ExternalSecretName,
  109. Namespace: ExternalSecretNamespace,
  110. },
  111. Spec: esv1alpha1.ExternalSecretSpec{
  112. SecretStoreRef: esv1alpha1.SecretStoreRef{
  113. Name: ExternalSecretStore,
  114. },
  115. Target: esv1alpha1.ExternalSecretTarget{
  116. Name: ExternalSecretTargetSecretName,
  117. },
  118. Data: []esv1alpha1.ExternalSecretData{
  119. {
  120. SecretKey: targetProp,
  121. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  122. Key: remoteKey,
  123. Property: remoteProperty,
  124. },
  125. },
  126. },
  127. },
  128. },
  129. }
  130. }
  131. // labels and annotations from the Kind=ExternalSecret
  132. // should be copied over to the Kind=Secret
  133. syncLabelsAnnotations := func(tc *testCase) {
  134. const secretVal = "someValue"
  135. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  136. "fooobar": "bazz",
  137. }
  138. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  139. "hihihih": "hehehe",
  140. }
  141. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  142. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  143. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  144. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  145. Eventually(func() bool {
  146. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  147. return metric.GetCounter().GetValue() == 1.0
  148. }, timeout, interval).Should(BeTrue())
  149. // check value
  150. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  151. // check labels & annotations
  152. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  153. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.ObjectMeta.Annotations))
  154. }
  155. }
  156. // when using a template it should be used as a blueprint
  157. // to construct a new secret: labels, annotations and type
  158. syncWithTemplate := func(tc *testCase) {
  159. const secretVal = "someValue"
  160. const expectedSecretVal = "SOMEVALUE was templated"
  161. const tplStaticKey = "tplstatickey"
  162. const tplStaticVal = "tplstaticvalue"
  163. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  164. "fooobar": "bazz",
  165. }
  166. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  167. "hihihih": "hehehe",
  168. }
  169. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  170. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  171. Labels: map[string]string{
  172. "foos": "ball",
  173. },
  174. Annotations: map[string]string{
  175. "hihi": "ga",
  176. },
  177. },
  178. Type: v1.SecretTypeOpaque,
  179. Data: map[string]string{
  180. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  181. tplStaticKey: tplStaticVal,
  182. },
  183. }
  184. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  185. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  186. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  187. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  188. Eventually(func() bool {
  189. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  190. return metric.GetCounter().GetValue() == 1.0
  191. }, timeout, interval).Should(BeTrue())
  192. // check values
  193. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  194. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  195. // labels/annotations should be taken from the template
  196. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  197. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  198. }
  199. }
  200. // secret should be synced with correct value precedence:
  201. // * template
  202. // * templateFrom
  203. // * data
  204. // * dataFrom
  205. syncWithTemplatePrecedence := func(tc *testCase) {
  206. const secretVal = "someValue"
  207. const expectedSecretVal = "SOMEVALUE was templated"
  208. const tplStaticKey = "tplstatickey"
  209. const tplStaticVal = "tplstaticvalue"
  210. const tplFromCMName = "template-cm"
  211. const tplFromKey = "tpl-from-key"
  212. const tplFromVal = "tpl-from-value"
  213. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  214. ObjectMeta: metav1.ObjectMeta{
  215. Name: "template-cm",
  216. Namespace: ExternalSecretNamespace,
  217. },
  218. Data: map[string]string{
  219. tplFromKey: tplFromVal,
  220. },
  221. })).To(Succeed())
  222. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  223. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{},
  224. Type: v1.SecretTypeOpaque,
  225. TemplateFrom: []esv1alpha1.TemplateFrom{
  226. {
  227. ConfigMap: &esv1alpha1.TemplateRef{
  228. Name: tplFromCMName,
  229. Items: []esv1alpha1.TemplateRefItem{
  230. {
  231. Key: tplFromKey,
  232. },
  233. },
  234. },
  235. },
  236. },
  237. Data: map[string]string{
  238. // this should be the data value, not dataFrom
  239. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  240. // this should use the value from the map
  241. "bar": "value from map: {{ .bar | toString }}",
  242. // just a static value
  243. tplStaticKey: tplStaticVal,
  244. },
  245. }
  246. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  247. {
  248. Key: "datamap",
  249. },
  250. }
  251. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  252. fakeProvider.WithGetSecretMap(map[string][]byte{
  253. "targetProperty": []byte("map-foo-value"),
  254. "bar": []byte("map-bar-value"),
  255. }, nil)
  256. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  257. // check values
  258. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  259. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  260. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  261. Expect(string(secret.Data[tplFromKey])).To(Equal(tplFromVal))
  262. }
  263. }
  264. refreshWithTemplate := func(tc *testCase) {
  265. const secretVal = "someValue"
  266. const expectedSecretVal = "SOMEVALUE was templated"
  267. const tplStaticKey = "tplstatickey"
  268. const tplStaticVal = "tplstaticvalue"
  269. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  270. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  271. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  272. Labels: map[string]string{"foo": "bar"},
  273. Annotations: map[string]string{"foo": "bar"},
  274. },
  275. Type: v1.SecretTypeOpaque,
  276. Data: map[string]string{
  277. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  278. tplStaticKey: tplStaticVal,
  279. },
  280. }
  281. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  282. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  283. // check values
  284. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  285. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  286. // labels/annotations should be taken from the template
  287. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  288. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  289. cleanEs := tc.externalSecret.DeepCopy()
  290. // now update ExternalSecret
  291. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  292. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  293. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  294. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  295. // wait for secret
  296. sec := &v1.Secret{}
  297. secretLookupKey := types.NamespacedName{
  298. Name: ExternalSecretTargetSecretName,
  299. Namespace: ExternalSecretNamespace,
  300. }
  301. Eventually(func() bool {
  302. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  303. if err != nil {
  304. return false
  305. }
  306. fmt.Fprintf(GinkgoWriter, "data: %#v\n", sec.Data)
  307. // ensure new data value exist
  308. return string(sec.Data["new"]) == "value"
  309. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  310. // also check labels/annotations have been updated
  311. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  312. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  313. }
  314. }
  315. // when the provider secret changes the Kind=Secret value
  316. // must change, too.
  317. refreshSecretValue := func(tc *testCase) {
  318. const targetProp = "targetProperty"
  319. const secretVal = "someValue"
  320. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  321. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  322. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  323. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  324. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  325. Eventually(func() bool {
  326. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  327. return metric.GetCounter().GetValue() == 1.0
  328. }, timeout, interval).Should(BeTrue())
  329. // check values
  330. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  331. // update provider secret
  332. newValue := "NEW VALUE"
  333. sec := &v1.Secret{}
  334. fakeProvider.WithGetSecret([]byte(newValue), nil)
  335. secretLookupKey := types.NamespacedName{
  336. Name: ExternalSecretTargetSecretName,
  337. Namespace: ExternalSecretNamespace,
  338. }
  339. Eventually(func() bool {
  340. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  341. if err != nil {
  342. return false
  343. }
  344. v := sec.Data[targetProp]
  345. return string(v) == newValue
  346. }, timeout, interval).Should(BeTrue())
  347. }
  348. }
  349. // with dataFrom all properties from the specified secret
  350. // should be put into the secret
  351. syncWithDataFrom := func(tc *testCase) {
  352. tc.externalSecret.Spec.Data = nil
  353. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  354. {
  355. Key: remoteKey,
  356. },
  357. }
  358. fakeProvider.WithGetSecretMap(map[string][]byte{
  359. "foo": []byte("map-foo-value"),
  360. "bar": []byte("map-bar-value"),
  361. }, nil)
  362. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  363. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  364. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  365. Eventually(func() bool {
  366. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  367. return metric.GetCounter().GetValue() == 1.0
  368. }, timeout, interval).Should(BeTrue())
  369. // check values
  370. Expect(string(secret.Data["foo"])).To(Equal("map-foo-value"))
  371. Expect(string(secret.Data["bar"])).To(Equal("map-bar-value"))
  372. }
  373. }
  374. // when a provider errors in a GetSecret call
  375. // a error condition must be set.
  376. providerErrCondition := func(tc *testCase) {
  377. const secretVal = "foobar"
  378. fakeProvider.WithGetSecret(nil, fmt.Errorf("boom"))
  379. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  380. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  381. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  382. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  383. return false
  384. }
  385. return true
  386. }
  387. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  388. Eventually(func() bool {
  389. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  390. return metric.GetCounter().GetValue() >= 2.0
  391. }, timeout, interval).Should(BeTrue())
  392. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  393. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  394. // es condition should reflect recovered provider error
  395. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  396. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  397. Eventually(func() bool {
  398. err := k8sClient.Get(context.Background(), esKey, es)
  399. if err != nil {
  400. return false
  401. }
  402. // condition must now be true!
  403. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  404. if cond == nil && cond.Status != v1.ConditionTrue {
  405. return false
  406. }
  407. return true
  408. }, timeout, interval).Should(BeTrue())
  409. }
  410. }
  411. // When a ExternalSecret references an non-existing SecretStore
  412. // a error condition must be set.
  413. storeMissingErrCondition := func(tc *testCase) {
  414. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  415. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  416. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  417. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  418. return false
  419. }
  420. return true
  421. }
  422. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  423. Eventually(func() bool {
  424. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  425. return metric.GetCounter().GetValue() >= 2.0
  426. }, timeout, interval).Should(BeTrue())
  427. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  428. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  429. }
  430. }
  431. // when the provider constructor errors (e.g. invalid configuration)
  432. // a SecretSyncedError status condition must be set
  433. storeConstructErrCondition := func(tc *testCase) {
  434. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  435. string) (provider.SecretsClient, error) {
  436. return nil, fmt.Errorf("artificial constructor error")
  437. })
  438. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  439. // condition must be false
  440. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  441. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  442. return false
  443. }
  444. return true
  445. }
  446. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  447. Eventually(func() bool {
  448. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  449. return metric.GetCounter().GetValue() >= 2.0
  450. }, timeout, interval).Should(BeTrue())
  451. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  452. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  453. }
  454. }
  455. // when a SecretStore has a controller field set which we don't care about
  456. // the externalSecret must not be touched
  457. ignoreMismatchController := func(tc *testCase) {
  458. tc.secretStore.Spec.Controller = "nop"
  459. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  460. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  461. return cond == nil
  462. }
  463. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  464. // Condition True and False should be 0, since the Condition was not created
  465. Eventually(func() float64 {
  466. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric)).To(Succeed())
  467. return metric.GetGauge().GetValue()
  468. }, timeout, interval).Should(Equal(0.0))
  469. Eventually(func() float64 {
  470. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric)).To(Succeed())
  471. return metric.GetGauge().GetValue()
  472. }, timeout, interval).Should(Equal(0.0))
  473. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  474. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  475. }
  476. }
  477. DescribeTable("When reconciling an ExternalSecret",
  478. func(tweaks ...testTweaks) {
  479. tc := makeDefaultTestcase()
  480. for _, tweak := range tweaks {
  481. tweak(tc)
  482. }
  483. ctx := context.Background()
  484. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  485. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  486. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  487. createdES := &esv1alpha1.ExternalSecret{}
  488. Eventually(func() bool {
  489. err := k8sClient.Get(ctx, esKey, createdES)
  490. if err != nil {
  491. return false
  492. }
  493. return tc.checkCondition(createdES)
  494. }, timeout, interval).Should(BeTrue())
  495. tc.checkExternalSecret(createdES)
  496. // this must be optional so we can test faulty es configuration
  497. if tc.checkSecret != nil {
  498. syncedSecret := &v1.Secret{}
  499. secretLookupKey := types.NamespacedName{
  500. Name: ExternalSecretTargetSecretName,
  501. Namespace: ExternalSecretNamespace,
  502. }
  503. Eventually(func() bool {
  504. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  505. return err == nil
  506. }, timeout, interval).Should(BeTrue())
  507. tc.checkSecret(createdES, syncedSecret)
  508. }
  509. },
  510. Entry("should set the condition eventually", syncLabelsAnnotations),
  511. Entry("should sync with template", syncWithTemplate),
  512. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  513. Entry("should refresh secret from template", refreshWithTemplate),
  514. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  515. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  516. Entry("should set error condition when provider errors", providerErrCondition),
  517. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  518. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  519. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  520. )
  521. })
  522. var _ = Describe("ExternalSecret refresh logic", func() {
  523. Context("secret refresh", func() {
  524. It("should refresh when resource version does not match", func() {
  525. Expect(shouldRefresh(esv1alpha1.ExternalSecret{
  526. Status: esv1alpha1.ExternalSecretStatus{
  527. SyncedResourceVersion: "some resource version",
  528. },
  529. })).To(BeTrue())
  530. })
  531. It("should refresh when labels change", func() {
  532. es := esv1alpha1.ExternalSecret{
  533. ObjectMeta: metav1.ObjectMeta{
  534. Generation: 1,
  535. Labels: map[string]string{
  536. "foo": "bar",
  537. },
  538. },
  539. Status: esv1alpha1.ExternalSecretStatus{},
  540. }
  541. es.Status.SyncedResourceVersion = getResourceVersion(es)
  542. // this should not refresh, rv matches object
  543. Expect(shouldRefresh(es)).To(BeFalse())
  544. // change labels without changing the syncedResourceVersion and expect refresh
  545. es.ObjectMeta.Labels["new"] = "w00t"
  546. Expect(shouldRefresh(es)).To(BeTrue())
  547. })
  548. It("should refresh when annotations change", func() {
  549. es := esv1alpha1.ExternalSecret{
  550. ObjectMeta: metav1.ObjectMeta{
  551. Generation: 1,
  552. Annotations: map[string]string{
  553. "foo": "bar",
  554. },
  555. },
  556. Status: esv1alpha1.ExternalSecretStatus{},
  557. }
  558. es.Status.SyncedResourceVersion = getResourceVersion(es)
  559. // this should not refresh, rv matches object
  560. Expect(shouldRefresh(es)).To(BeFalse())
  561. // change annotations without changing the syncedResourceVersion and expect refresh
  562. es.ObjectMeta.Annotations["new"] = "w00t"
  563. Expect(shouldRefresh(es)).To(BeTrue())
  564. })
  565. It("should refresh when generation has changed", func() {
  566. es := esv1alpha1.ExternalSecret{
  567. ObjectMeta: metav1.ObjectMeta{
  568. Generation: 1,
  569. },
  570. Status: esv1alpha1.ExternalSecretStatus{},
  571. }
  572. es.Status.SyncedResourceVersion = getResourceVersion(es)
  573. Expect(shouldRefresh(es)).To(BeFalse())
  574. // update gen -> refresh
  575. es.ObjectMeta.Generation = 2
  576. Expect(shouldRefresh(es)).To(BeTrue())
  577. })
  578. It("should skip refresh when refreshInterval is nil", func() {
  579. es := esv1alpha1.ExternalSecret{
  580. ObjectMeta: metav1.ObjectMeta{
  581. Generation: 1,
  582. },
  583. Spec: esv1alpha1.ExternalSecretSpec{
  584. RefreshInterval: nil,
  585. },
  586. Status: esv1alpha1.ExternalSecretStatus{},
  587. }
  588. // resource version matches
  589. es.Status.SyncedResourceVersion = getResourceVersion(es)
  590. Expect(shouldRefresh(es)).To(BeFalse())
  591. })
  592. It("should refresh when refresh interval has passed", func() {
  593. es := esv1alpha1.ExternalSecret{
  594. ObjectMeta: metav1.ObjectMeta{
  595. Generation: 1,
  596. },
  597. Spec: esv1alpha1.ExternalSecretSpec{
  598. RefreshInterval: &metav1.Duration{Duration: time.Second},
  599. },
  600. Status: esv1alpha1.ExternalSecretStatus{
  601. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  602. },
  603. }
  604. // resource version matches
  605. es.Status.SyncedResourceVersion = getResourceVersion(es)
  606. Expect(shouldRefresh(es)).To(BeTrue())
  607. })
  608. It("should refresh when no refresh time was set", func() {
  609. es := esv1alpha1.ExternalSecret{
  610. ObjectMeta: metav1.ObjectMeta{
  611. Generation: 1,
  612. },
  613. Spec: esv1alpha1.ExternalSecretSpec{
  614. RefreshInterval: &metav1.Duration{Duration: time.Second},
  615. },
  616. Status: esv1alpha1.ExternalSecretStatus{},
  617. }
  618. // resource version matches
  619. es.Status.SyncedResourceVersion = getResourceVersion(es)
  620. Expect(shouldRefresh(es)).To(BeTrue())
  621. })
  622. })
  623. Context("objectmeta hash", func() {
  624. It("should produce different hashes for different k/v pairs", func() {
  625. h1 := hashMeta(metav1.ObjectMeta{
  626. Generation: 1,
  627. Annotations: map[string]string{
  628. "foo": "bar",
  629. },
  630. })
  631. h2 := hashMeta(metav1.ObjectMeta{
  632. Generation: 1,
  633. Annotations: map[string]string{
  634. "foo": "bing",
  635. },
  636. })
  637. Expect(h1).ToNot(Equal(h2))
  638. })
  639. It("should produce different hashes for different generations but same label/annotations", func() {
  640. h1 := hashMeta(metav1.ObjectMeta{
  641. Generation: 1,
  642. Annotations: map[string]string{
  643. "foo": "bar",
  644. },
  645. Labels: map[string]string{
  646. "foo": "bar",
  647. },
  648. })
  649. h2 := hashMeta(metav1.ObjectMeta{
  650. Generation: 2,
  651. Annotations: map[string]string{
  652. "foo": "bar",
  653. },
  654. Labels: map[string]string{
  655. "foo": "bar",
  656. },
  657. })
  658. Expect(h1).To(Equal(h2))
  659. })
  660. It("should produce the same hash for the same k/v pairs", func() {
  661. h1 := hashMeta(metav1.ObjectMeta{
  662. Generation: 1,
  663. })
  664. h2 := hashMeta(metav1.ObjectMeta{
  665. Generation: 1,
  666. })
  667. Expect(h1).To(Equal(h2))
  668. h1 = hashMeta(metav1.ObjectMeta{
  669. Generation: 1,
  670. Annotations: map[string]string{
  671. "foo": "bar",
  672. },
  673. })
  674. h2 = hashMeta(metav1.ObjectMeta{
  675. Generation: 1,
  676. Annotations: map[string]string{
  677. "foo": "bar",
  678. },
  679. })
  680. Expect(h1).To(Equal(h2))
  681. })
  682. })
  683. })
  684. // CreateNamespace creates a new namespace in the cluster.
  685. func CreateNamespace(baseName string, c client.Client) (string, error) {
  686. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  687. ns := &v1.Namespace{
  688. ObjectMeta: metav1.ObjectMeta{
  689. GenerateName: genName,
  690. },
  691. }
  692. var err error
  693. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  694. err = c.Create(context.Background(), ns)
  695. if err != nil {
  696. return false, nil
  697. }
  698. return true, nil
  699. })
  700. if err != nil {
  701. return "", err
  702. }
  703. return ns.Name, nil
  704. }
  705. func externalSecretConditionShouldBe(name, ns string, ct esv1alpha1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  706. return Eventually(func() float64 {
  707. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  708. return metric.GetGauge().GetValue()
  709. }, timeout, interval).Should(Equal(v))
  710. }
  711. func init() {
  712. fakeProvider = fake.New()
  713. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  714. AWS: &esv1alpha1.AWSProvider{
  715. Service: esv1alpha1.AWSServiceSecretsManager,
  716. },
  717. })
  718. }