externalsecret_controller_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 (not implemented)
  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. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  211. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{},
  212. Type: v1.SecretTypeOpaque,
  213. Data: map[string]string{
  214. // this should be the data value, not dataFrom
  215. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  216. // this should use the value from the map
  217. "bar": "value from map: {{ .bar | toString }}",
  218. // just a static value
  219. tplStaticKey: tplStaticVal,
  220. },
  221. }
  222. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  223. {
  224. Key: "datamap",
  225. },
  226. }
  227. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  228. fakeProvider.WithGetSecretMap(map[string][]byte{
  229. "targetProperty": []byte("map-foo-value"),
  230. "bar": []byte("map-bar-value"),
  231. }, nil)
  232. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  233. // check values
  234. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  235. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  236. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  237. }
  238. }
  239. refreshWithTemplate := func(tc *testCase) {
  240. const secretVal = "someValue"
  241. const expectedSecretVal = "SOMEVALUE was templated"
  242. const tplStaticKey = "tplstatickey"
  243. const tplStaticVal = "tplstaticvalue"
  244. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  245. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  246. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  247. Labels: map[string]string{"foo": "bar"},
  248. Annotations: map[string]string{"foo": "bar"},
  249. },
  250. Type: v1.SecretTypeOpaque,
  251. Data: map[string]string{
  252. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  253. tplStaticKey: tplStaticVal,
  254. },
  255. }
  256. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  257. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  258. // check values
  259. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  260. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  261. // labels/annotations should be taken from the template
  262. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  263. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  264. cleanEs := tc.externalSecret.DeepCopyObject()
  265. // now update ExternalSecret
  266. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  267. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  268. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  269. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  270. // wait for secret
  271. sec := &v1.Secret{}
  272. secretLookupKey := types.NamespacedName{
  273. Name: ExternalSecretTargetSecretName,
  274. Namespace: ExternalSecretNamespace,
  275. }
  276. Eventually(func() bool {
  277. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  278. if err != nil {
  279. return false
  280. }
  281. fmt.Fprintf(GinkgoWriter, "data: %#v\n", sec.Data)
  282. // ensure new data value exist
  283. return string(sec.Data["new"]) == "value"
  284. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  285. // also check labels/annotations have been updated
  286. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  287. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  288. }
  289. }
  290. // when the provider secret changes the Kind=Secret value
  291. // must change, too.
  292. refreshSecretValue := func(tc *testCase) {
  293. const targetProp = "targetProperty"
  294. const secretVal = "someValue"
  295. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  296. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  297. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  298. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  299. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  300. Eventually(func() bool {
  301. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  302. return metric.GetCounter().GetValue() == 1.0
  303. }, timeout, interval).Should(BeTrue())
  304. // check values
  305. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  306. // update provider secret
  307. newValue := "NEW VALUE"
  308. sec := &v1.Secret{}
  309. fakeProvider.WithGetSecret([]byte(newValue), nil)
  310. secretLookupKey := types.NamespacedName{
  311. Name: ExternalSecretTargetSecretName,
  312. Namespace: ExternalSecretNamespace,
  313. }
  314. Eventually(func() bool {
  315. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  316. if err != nil {
  317. return false
  318. }
  319. v := sec.Data[targetProp]
  320. return string(v) == newValue
  321. }, timeout, interval).Should(BeTrue())
  322. }
  323. }
  324. // with dataFrom all properties from the specified secret
  325. // should be put into the secret
  326. syncWithDataFrom := func(tc *testCase) {
  327. tc.externalSecret.Spec.Data = nil
  328. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  329. {
  330. Key: remoteKey,
  331. },
  332. }
  333. fakeProvider.WithGetSecretMap(map[string][]byte{
  334. "foo": []byte("map-foo-value"),
  335. "bar": []byte("map-bar-value"),
  336. }, nil)
  337. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  338. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  339. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  340. Eventually(func() bool {
  341. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  342. return metric.GetCounter().GetValue() == 1.0
  343. }, timeout, interval).Should(BeTrue())
  344. // check values
  345. Expect(string(secret.Data["foo"])).To(Equal("map-foo-value"))
  346. Expect(string(secret.Data["bar"])).To(Equal("map-bar-value"))
  347. }
  348. }
  349. // when a provider errors in a GetSecret call
  350. // a error condition must be set.
  351. providerErrCondition := func(tc *testCase) {
  352. const secretVal = "foobar"
  353. fakeProvider.WithGetSecret(nil, fmt.Errorf("boom"))
  354. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  355. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  356. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  357. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  358. return false
  359. }
  360. return true
  361. }
  362. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  363. Eventually(func() bool {
  364. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  365. return metric.GetCounter().GetValue() >= 2.0
  366. }, timeout, interval).Should(BeTrue())
  367. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  368. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  369. // es condition should reflect recovered provider error
  370. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  371. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  372. Eventually(func() bool {
  373. err := k8sClient.Get(context.Background(), esKey, es)
  374. if err != nil {
  375. return false
  376. }
  377. // condition must now be true!
  378. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  379. if cond == nil && cond.Status != v1.ConditionTrue {
  380. return false
  381. }
  382. return true
  383. }, timeout, interval).Should(BeTrue())
  384. }
  385. }
  386. // When a ExternalSecret references an non-existing SecretStore
  387. // a error condition must be set.
  388. storeMissingErrCondition := func(tc *testCase) {
  389. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  390. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  391. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  392. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  393. return false
  394. }
  395. return true
  396. }
  397. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  398. Eventually(func() bool {
  399. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  400. return metric.GetCounter().GetValue() >= 2.0
  401. }, timeout, interval).Should(BeTrue())
  402. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  403. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  404. }
  405. }
  406. // when the provider constructor errors (e.g. invalid configuration)
  407. // a SecretSyncedError status condition must be set
  408. storeConstructErrCondition := func(tc *testCase) {
  409. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  410. string) (provider.SecretsClient, error) {
  411. return nil, fmt.Errorf("artificial constructor error")
  412. })
  413. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  414. // condition must be false
  415. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  416. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  417. return false
  418. }
  419. return true
  420. }
  421. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  422. Eventually(func() bool {
  423. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  424. return metric.GetCounter().GetValue() >= 2.0
  425. }, timeout, interval).Should(BeTrue())
  426. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  427. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  428. }
  429. }
  430. // when a SecretStore has a controller field set which we don't care about
  431. // the externalSecret must not be touched
  432. ignoreMismatchController := func(tc *testCase) {
  433. tc.secretStore.Spec.Controller = "nop"
  434. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  435. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  436. return cond == nil
  437. }
  438. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  439. // Condition True and False should be 0, since the Condition was not created
  440. Eventually(func() float64 {
  441. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric)).To(Succeed())
  442. return metric.GetGauge().GetValue()
  443. }, timeout, interval).Should(Equal(0.0))
  444. Eventually(func() float64 {
  445. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric)).To(Succeed())
  446. return metric.GetGauge().GetValue()
  447. }, timeout, interval).Should(Equal(0.0))
  448. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  449. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  450. }
  451. }
  452. DescribeTable("When reconciling an ExternalSecret",
  453. func(tweaks ...testTweaks) {
  454. tc := makeDefaultTestcase()
  455. for _, tweak := range tweaks {
  456. tweak(tc)
  457. }
  458. ctx := context.Background()
  459. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  460. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  461. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  462. createdES := &esv1alpha1.ExternalSecret{}
  463. Eventually(func() bool {
  464. err := k8sClient.Get(ctx, esKey, createdES)
  465. if err != nil {
  466. return false
  467. }
  468. return tc.checkCondition(createdES)
  469. }, timeout, interval).Should(BeTrue())
  470. tc.checkExternalSecret(createdES)
  471. // this must be optional so we can test faulty es configuration
  472. if tc.checkSecret != nil {
  473. syncedSecret := &v1.Secret{}
  474. secretLookupKey := types.NamespacedName{
  475. Name: ExternalSecretTargetSecretName,
  476. Namespace: ExternalSecretNamespace,
  477. }
  478. Eventually(func() bool {
  479. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  480. return err == nil
  481. }, timeout, interval).Should(BeTrue())
  482. tc.checkSecret(createdES, syncedSecret)
  483. }
  484. },
  485. Entry("should set the condition eventually", syncLabelsAnnotations),
  486. Entry("should sync with template", syncWithTemplate),
  487. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  488. Entry("should refresh secret from template", refreshWithTemplate),
  489. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  490. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  491. Entry("should set error condition when provider errors", providerErrCondition),
  492. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  493. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  494. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  495. )
  496. })
  497. var _ = Describe("ExternalSecret refresh logic", func() {
  498. Context("secret refresh", func() {
  499. It("should refresh when resource version does not match", func() {
  500. Expect(shouldRefresh(esv1alpha1.ExternalSecret{
  501. Status: esv1alpha1.ExternalSecretStatus{
  502. SyncedResourceVersion: "some resource version",
  503. },
  504. })).To(BeTrue())
  505. })
  506. It("should refresh when labels change", func() {
  507. es := esv1alpha1.ExternalSecret{
  508. ObjectMeta: metav1.ObjectMeta{
  509. Generation: 1,
  510. Labels: map[string]string{
  511. "foo": "bar",
  512. },
  513. },
  514. Status: esv1alpha1.ExternalSecretStatus{},
  515. }
  516. es.Status.SyncedResourceVersion = getResourceVersion(es)
  517. // this should not refresh, rv matches object
  518. Expect(shouldRefresh(es)).To(BeFalse())
  519. // change labels without changing the syncedResourceVersion and expect refresh
  520. es.ObjectMeta.Labels["new"] = "w00t"
  521. Expect(shouldRefresh(es)).To(BeTrue())
  522. })
  523. It("should refresh when annotations change", func() {
  524. es := esv1alpha1.ExternalSecret{
  525. ObjectMeta: metav1.ObjectMeta{
  526. Generation: 1,
  527. Annotations: map[string]string{
  528. "foo": "bar",
  529. },
  530. },
  531. Status: esv1alpha1.ExternalSecretStatus{},
  532. }
  533. es.Status.SyncedResourceVersion = getResourceVersion(es)
  534. // this should not refresh, rv matches object
  535. Expect(shouldRefresh(es)).To(BeFalse())
  536. // change annotations without changing the syncedResourceVersion and expect refresh
  537. es.ObjectMeta.Annotations["new"] = "w00t"
  538. Expect(shouldRefresh(es)).To(BeTrue())
  539. })
  540. It("should refresh when generation has changed", func() {
  541. es := esv1alpha1.ExternalSecret{
  542. ObjectMeta: metav1.ObjectMeta{
  543. Generation: 1,
  544. },
  545. Status: esv1alpha1.ExternalSecretStatus{},
  546. }
  547. es.Status.SyncedResourceVersion = getResourceVersion(es)
  548. Expect(shouldRefresh(es)).To(BeFalse())
  549. // update gen -> refresh
  550. es.ObjectMeta.Generation = 2
  551. Expect(shouldRefresh(es)).To(BeTrue())
  552. })
  553. It("should skip refresh when refreshInterval is nil", func() {
  554. es := esv1alpha1.ExternalSecret{
  555. ObjectMeta: metav1.ObjectMeta{
  556. Generation: 1,
  557. },
  558. Spec: esv1alpha1.ExternalSecretSpec{
  559. RefreshInterval: nil,
  560. },
  561. Status: esv1alpha1.ExternalSecretStatus{},
  562. }
  563. // resource version matches
  564. es.Status.SyncedResourceVersion = getResourceVersion(es)
  565. Expect(shouldRefresh(es)).To(BeFalse())
  566. })
  567. It("should refresh when refresh interval has passed", func() {
  568. es := esv1alpha1.ExternalSecret{
  569. ObjectMeta: metav1.ObjectMeta{
  570. Generation: 1,
  571. },
  572. Spec: esv1alpha1.ExternalSecretSpec{
  573. RefreshInterval: &metav1.Duration{Duration: time.Second},
  574. },
  575. Status: esv1alpha1.ExternalSecretStatus{
  576. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  577. },
  578. }
  579. // resource version matches
  580. es.Status.SyncedResourceVersion = getResourceVersion(es)
  581. Expect(shouldRefresh(es)).To(BeTrue())
  582. })
  583. It("should refresh when no refresh time was set", func() {
  584. es := esv1alpha1.ExternalSecret{
  585. ObjectMeta: metav1.ObjectMeta{
  586. Generation: 1,
  587. },
  588. Spec: esv1alpha1.ExternalSecretSpec{
  589. RefreshInterval: &metav1.Duration{Duration: time.Second},
  590. },
  591. Status: esv1alpha1.ExternalSecretStatus{},
  592. }
  593. // resource version matches
  594. es.Status.SyncedResourceVersion = getResourceVersion(es)
  595. Expect(shouldRefresh(es)).To(BeTrue())
  596. })
  597. })
  598. Context("objectmeta hash", func() {
  599. It("should produce different hashes for different k/v pairs", func() {
  600. h1 := hashMeta(metav1.ObjectMeta{
  601. Generation: 1,
  602. Annotations: map[string]string{
  603. "foo": "bar",
  604. },
  605. })
  606. h2 := hashMeta(metav1.ObjectMeta{
  607. Generation: 1,
  608. Annotations: map[string]string{
  609. "foo": "bing",
  610. },
  611. })
  612. Expect(h1).ToNot(Equal(h2))
  613. })
  614. It("should produce different hashes for different generations but same label/annotations", func() {
  615. h1 := hashMeta(metav1.ObjectMeta{
  616. Generation: 1,
  617. Annotations: map[string]string{
  618. "foo": "bar",
  619. },
  620. Labels: map[string]string{
  621. "foo": "bar",
  622. },
  623. })
  624. h2 := hashMeta(metav1.ObjectMeta{
  625. Generation: 2,
  626. Annotations: map[string]string{
  627. "foo": "bar",
  628. },
  629. Labels: map[string]string{
  630. "foo": "bar",
  631. },
  632. })
  633. Expect(h1).To(Equal(h2))
  634. })
  635. It("should produce the same hash for the same k/v pairs", func() {
  636. h1 := hashMeta(metav1.ObjectMeta{
  637. Generation: 1,
  638. })
  639. h2 := hashMeta(metav1.ObjectMeta{
  640. Generation: 1,
  641. })
  642. Expect(h1).To(Equal(h2))
  643. h1 = hashMeta(metav1.ObjectMeta{
  644. Generation: 1,
  645. Annotations: map[string]string{
  646. "foo": "bar",
  647. },
  648. })
  649. h2 = hashMeta(metav1.ObjectMeta{
  650. Generation: 1,
  651. Annotations: map[string]string{
  652. "foo": "bar",
  653. },
  654. })
  655. Expect(h1).To(Equal(h2))
  656. })
  657. })
  658. })
  659. // CreateNamespace creates a new namespace in the cluster.
  660. func CreateNamespace(baseName string, c client.Client) (string, error) {
  661. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  662. ns := &v1.Namespace{
  663. ObjectMeta: metav1.ObjectMeta{
  664. GenerateName: genName,
  665. },
  666. }
  667. var err error
  668. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  669. err = c.Create(context.Background(), ns)
  670. if err != nil {
  671. return false, nil
  672. }
  673. return true, nil
  674. })
  675. if err != nil {
  676. return "", err
  677. }
  678. return ns.Name, nil
  679. }
  680. func externalSecretConditionShouldBe(name, ns string, ct esv1alpha1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  681. return Eventually(func() float64 {
  682. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  683. return metric.GetGauge().GetValue()
  684. }, timeout, interval).Should(Equal(v))
  685. }
  686. func init() {
  687. fakeProvider = fake.New()
  688. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  689. AWS: &esv1alpha1.AWSProvider{
  690. Service: esv1alpha1.AWSServiceSecretsManager,
  691. },
  692. })
  693. }