externalsecret_controller_test.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. // ownerRef must not not be set!
  155. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeTrue())
  156. }
  157. }
  158. // merge with existing secret using creationPolicy=Merge
  159. // it should NOT have a ownerReference
  160. // metadata.managedFields with the correct owner should be added to the secret
  161. mergeWithSecret := func(tc *testCase) {
  162. const secretVal = "someValue"
  163. const existingKey = "pre-existing-key"
  164. existingVal := "pre-existing-value"
  165. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  166. // create secret beforehand
  167. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  168. ObjectMeta: metav1.ObjectMeta{
  169. Name: "test-secret",
  170. Namespace: ExternalSecretNamespace,
  171. },
  172. Data: map[string][]byte{
  173. existingKey: []byte(existingVal),
  174. },
  175. }, client.FieldOwner("fake.manager"))).To(Succeed())
  176. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  177. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  178. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  179. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  180. Eventually(func() bool {
  181. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  182. return metric.GetCounter().GetValue() == 1.0
  183. }, timeout, interval).Should(BeTrue())
  184. // check value
  185. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  186. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  187. // check labels & annotations
  188. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  189. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.ObjectMeta.Annotations))
  190. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  191. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  192. Expect(hasFieldOwnership(secret.ObjectMeta, "external-secrets", "{\"f:data\":{\"f:targetProperty\":{}},\"f:immutable\":{}}")).To(BeTrue())
  193. Expect(hasFieldOwnership(secret.ObjectMeta, "fake.manager", "{\"f:data\":{\".\":{},\"f:pre-existing-key\":{}},\"f:type\":{}}")).To(BeTrue())
  194. }
  195. }
  196. // should not merge with secret if it doesn't exist
  197. mergeWithSecretErr := func(tc *testCase) {
  198. const secretVal = "someValue"
  199. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  200. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  201. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  202. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  203. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  204. return false
  205. }
  206. return true
  207. }
  208. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  209. Eventually(func() bool {
  210. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  211. return metric.GetCounter().GetValue() >= 2.0
  212. }, timeout, interval).Should(BeTrue())
  213. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  214. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  215. }
  216. }
  217. // controller should not force override but
  218. // return an error on conflict
  219. mergeWithConflict := func(tc *testCase) {
  220. const secretVal = "someValue"
  221. // this should confict
  222. const existingKey = targetProp
  223. existingVal := "pre-existing-value"
  224. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  225. // create secret beforehand
  226. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  227. ObjectMeta: metav1.ObjectMeta{
  228. Name: "test-secret",
  229. Namespace: ExternalSecretNamespace,
  230. },
  231. Data: map[string][]byte{
  232. existingKey: []byte(existingVal),
  233. },
  234. }, client.FieldOwner("fake.manager"))).To(Succeed())
  235. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  236. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  237. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  238. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  239. return false
  240. }
  241. return true
  242. }
  243. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  244. // check that value stays the same
  245. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  246. Expect(string(secret.Data[targetProp])).ToNot(Equal(secretVal))
  247. // check owner/managedFields
  248. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  249. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(1))
  250. Expect(hasFieldOwnership(secret.ObjectMeta, "fake.manager", "{\"f:data\":{\".\":{},\"f:targetProperty\":{}},\"f:type\":{}}")).To(BeTrue())
  251. }
  252. }
  253. // when using a template it should be used as a blueprint
  254. // to construct a new secret: labels, annotations and type
  255. syncWithTemplate := func(tc *testCase) {
  256. const secretVal = "someValue"
  257. const expectedSecretVal = "SOMEVALUE was templated"
  258. const tplStaticKey = "tplstatickey"
  259. const tplStaticVal = "tplstaticvalue"
  260. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  261. "fooobar": "bazz",
  262. }
  263. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  264. "hihihih": "hehehe",
  265. }
  266. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  267. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  268. Labels: map[string]string{
  269. "foos": "ball",
  270. },
  271. Annotations: map[string]string{
  272. "hihi": "ga",
  273. },
  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. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  284. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  285. Eventually(func() bool {
  286. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  287. return metric.GetCounter().GetValue() == 1.0
  288. }, timeout, interval).Should(BeTrue())
  289. // check values
  290. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  291. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  292. // labels/annotations should be taken from the template
  293. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  294. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  295. }
  296. }
  297. // secret should be synced with correct value precedence:
  298. // * template
  299. // * templateFrom
  300. // * data
  301. // * dataFrom
  302. syncWithTemplatePrecedence := func(tc *testCase) {
  303. const secretVal = "someValue"
  304. const expectedSecretVal = "SOMEVALUE was templated"
  305. const tplStaticKey = "tplstatickey"
  306. const tplStaticVal = "tplstaticvalue"
  307. const tplFromCMName = "template-cm"
  308. const tplFromKey = "tpl-from-key"
  309. const tplFromVal = "tpl-from-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  310. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  311. ObjectMeta: metav1.ObjectMeta{
  312. Name: "template-cm",
  313. Namespace: ExternalSecretNamespace,
  314. },
  315. Data: map[string]string{
  316. tplFromKey: tplFromVal,
  317. },
  318. })).To(Succeed())
  319. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  320. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{},
  321. Type: v1.SecretTypeOpaque,
  322. TemplateFrom: []esv1alpha1.TemplateFrom{
  323. {
  324. ConfigMap: &esv1alpha1.TemplateRef{
  325. Name: tplFromCMName,
  326. Items: []esv1alpha1.TemplateRefItem{
  327. {
  328. Key: tplFromKey,
  329. },
  330. },
  331. },
  332. },
  333. },
  334. Data: map[string]string{
  335. // this should be the data value, not dataFrom
  336. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  337. // this should use the value from the map
  338. "bar": "value from map: {{ .bar | toString }}",
  339. // just a static value
  340. tplStaticKey: tplStaticVal,
  341. },
  342. }
  343. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  344. {
  345. Key: "datamap",
  346. },
  347. }
  348. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  349. fakeProvider.WithGetSecretMap(map[string][]byte{
  350. "targetProperty": []byte("map-foo-value"),
  351. "bar": []byte("map-bar-value"),
  352. }, nil)
  353. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  354. // check values
  355. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  356. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  357. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  358. Expect(string(secret.Data[tplFromKey])).To(Equal("tpl-from-value: someValue // map-bar-value"))
  359. }
  360. }
  361. refreshWithTemplate := func(tc *testCase) {
  362. const secretVal = "someValue"
  363. const expectedSecretVal = "SOMEVALUE was templated"
  364. const tplStaticKey = "tplstatickey"
  365. const tplStaticVal = "tplstaticvalue"
  366. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  367. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  368. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  369. Labels: map[string]string{"foo": "bar"},
  370. Annotations: map[string]string{"foo": "bar"},
  371. },
  372. Type: v1.SecretTypeOpaque,
  373. Data: map[string]string{
  374. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  375. tplStaticKey: tplStaticVal,
  376. },
  377. }
  378. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  379. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  380. // check values
  381. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  382. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  383. // labels/annotations should be taken from the template
  384. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  385. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  386. cleanEs := tc.externalSecret.DeepCopy()
  387. // now update ExternalSecret
  388. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  389. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  390. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  391. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  392. // wait for secret
  393. sec := &v1.Secret{}
  394. secretLookupKey := types.NamespacedName{
  395. Name: ExternalSecretTargetSecretName,
  396. Namespace: ExternalSecretNamespace,
  397. }
  398. Eventually(func() bool {
  399. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  400. if err != nil {
  401. return false
  402. }
  403. // ensure new data value exist
  404. return string(sec.Data["new"]) == "value"
  405. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  406. // also check labels/annotations have been updated
  407. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  408. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  409. }
  410. }
  411. // when the provider secret changes the Kind=Secret value
  412. // must change, too.
  413. refreshSecretValue := func(tc *testCase) {
  414. const targetProp = "targetProperty"
  415. const secretVal = "someValue"
  416. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  417. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  418. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  419. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  420. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  421. Eventually(func() bool {
  422. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  423. return metric.GetCounter().GetValue() == 1.0
  424. }, timeout, interval).Should(BeTrue())
  425. // check values
  426. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  427. // update provider secret
  428. newValue := "NEW VALUE"
  429. sec := &v1.Secret{}
  430. fakeProvider.WithGetSecret([]byte(newValue), nil)
  431. secretLookupKey := types.NamespacedName{
  432. Name: ExternalSecretTargetSecretName,
  433. Namespace: ExternalSecretNamespace,
  434. }
  435. Eventually(func() bool {
  436. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  437. if err != nil {
  438. return false
  439. }
  440. v := sec.Data[targetProp]
  441. return string(v) == newValue
  442. }, timeout, interval).Should(BeTrue())
  443. }
  444. }
  445. refreshintervalZero := func(tc *testCase) {
  446. const targetProp = "targetProperty"
  447. const secretVal = "someValue"
  448. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  449. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: 0}
  450. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  451. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  452. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  453. Eventually(func() bool {
  454. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  455. return metric.GetCounter().GetValue() == 1.0
  456. }, timeout, interval).Should(BeTrue())
  457. // check values
  458. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  459. // update provider secret
  460. newValue := "NEW VALUE"
  461. sec := &v1.Secret{}
  462. fakeProvider.WithGetSecret([]byte(newValue), nil)
  463. secretLookupKey := types.NamespacedName{
  464. Name: ExternalSecretTargetSecretName,
  465. Namespace: ExternalSecretNamespace,
  466. }
  467. Consistently(func() bool {
  468. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  469. if err != nil {
  470. return false
  471. }
  472. v := sec.Data[targetProp]
  473. return string(v) == secretVal
  474. }, time.Second*10, time.Second).Should(BeTrue())
  475. }
  476. }
  477. // with dataFrom all properties from the specified secret
  478. // should be put into the secret
  479. syncWithDataFrom := func(tc *testCase) {
  480. tc.externalSecret.Spec.Data = nil
  481. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  482. {
  483. Key: remoteKey,
  484. },
  485. }
  486. fakeProvider.WithGetSecretMap(map[string][]byte{
  487. "foo": []byte("map-foo-value"),
  488. "bar": []byte("map-bar-value"),
  489. }, nil)
  490. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  491. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  492. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  493. Eventually(func() bool {
  494. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  495. return metric.GetCounter().GetValue() == 1.0
  496. }, timeout, interval).Should(BeTrue())
  497. // check values
  498. Expect(string(secret.Data["foo"])).To(Equal("map-foo-value"))
  499. Expect(string(secret.Data["bar"])).To(Equal("map-bar-value"))
  500. }
  501. }
  502. // when a provider errors in a GetSecret call
  503. // a error condition must be set.
  504. providerErrCondition := func(tc *testCase) {
  505. const secretVal = "foobar"
  506. fakeProvider.WithGetSecret(nil, fmt.Errorf("boom"))
  507. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  508. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  509. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  510. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  511. return false
  512. }
  513. return true
  514. }
  515. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  516. Eventually(func() bool {
  517. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  518. return metric.GetCounter().GetValue() >= 2.0
  519. }, timeout, interval).Should(BeTrue())
  520. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  521. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  522. // es condition should reflect recovered provider error
  523. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  524. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  525. Eventually(func() bool {
  526. err := k8sClient.Get(context.Background(), esKey, es)
  527. if err != nil {
  528. return false
  529. }
  530. // condition must now be true!
  531. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  532. if cond == nil && cond.Status != v1.ConditionTrue {
  533. return false
  534. }
  535. return true
  536. }, timeout, interval).Should(BeTrue())
  537. }
  538. }
  539. // When a ExternalSecret references an non-existing SecretStore
  540. // a error condition must be set.
  541. storeMissingErrCondition := func(tc *testCase) {
  542. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  543. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  544. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  545. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  546. return false
  547. }
  548. return true
  549. }
  550. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  551. Eventually(func() bool {
  552. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  553. return metric.GetCounter().GetValue() >= 2.0
  554. }, timeout, interval).Should(BeTrue())
  555. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  556. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  557. }
  558. }
  559. // when the provider constructor errors (e.g. invalid configuration)
  560. // a SecretSyncedError status condition must be set
  561. storeConstructErrCondition := func(tc *testCase) {
  562. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  563. string) (provider.SecretsClient, error) {
  564. return nil, fmt.Errorf("artificial constructor error")
  565. })
  566. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  567. // condition must be false
  568. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  569. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  570. return false
  571. }
  572. return true
  573. }
  574. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  575. Eventually(func() bool {
  576. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  577. return metric.GetCounter().GetValue() >= 2.0
  578. }, timeout, interval).Should(BeTrue())
  579. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  580. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  581. }
  582. }
  583. // when a SecretStore has a controller field set which we don't care about
  584. // the externalSecret must not be touched
  585. ignoreMismatchController := func(tc *testCase) {
  586. tc.secretStore.Spec.Controller = "nop"
  587. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  588. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  589. return cond == nil
  590. }
  591. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  592. // Condition True and False should be 0, since the Condition was not created
  593. Eventually(func() float64 {
  594. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric)).To(Succeed())
  595. return metric.GetGauge().GetValue()
  596. }, timeout, interval).Should(Equal(0.0))
  597. Eventually(func() float64 {
  598. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric)).To(Succeed())
  599. return metric.GetGauge().GetValue()
  600. }, timeout, interval).Should(Equal(0.0))
  601. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  602. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  603. }
  604. }
  605. DescribeTable("When reconciling an ExternalSecret",
  606. func(tweaks ...testTweaks) {
  607. tc := makeDefaultTestcase()
  608. for _, tweak := range tweaks {
  609. tweak(tc)
  610. }
  611. ctx := context.Background()
  612. By("creating a secret store and external secret")
  613. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  614. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  615. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  616. createdES := &esv1alpha1.ExternalSecret{}
  617. By("checking the es condition")
  618. Eventually(func() bool {
  619. err := k8sClient.Get(ctx, esKey, createdES)
  620. if err != nil {
  621. return false
  622. }
  623. return tc.checkCondition(createdES)
  624. }, timeout, interval).Should(BeTrue())
  625. tc.checkExternalSecret(createdES)
  626. // this must be optional so we can test faulty es configuration
  627. if tc.checkSecret != nil {
  628. syncedSecret := &v1.Secret{}
  629. secretLookupKey := types.NamespacedName{
  630. Name: ExternalSecretTargetSecretName,
  631. Namespace: ExternalSecretNamespace,
  632. }
  633. Eventually(func() bool {
  634. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  635. return err == nil
  636. }, timeout, interval).Should(BeTrue())
  637. tc.checkSecret(createdES, syncedSecret)
  638. }
  639. },
  640. Entry("should set the condition eventually", syncLabelsAnnotations),
  641. Entry("should merge with existing secret using creationPolicy=Merge", mergeWithSecret),
  642. Entry("should error if sceret doesn't exist when using creationPolicy=Merge", mergeWithSecretErr),
  643. Entry("should not resolve conflicts with creationPolicy=Merge", mergeWithConflict),
  644. Entry("should sync with template", syncWithTemplate),
  645. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  646. Entry("should refresh secret from template", refreshWithTemplate),
  647. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  648. Entry("should not refresh secret value when provider secret changes but refreshInterval is zero", refreshintervalZero),
  649. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  650. Entry("should set error condition when provider errors", providerErrCondition),
  651. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  652. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  653. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  654. )
  655. })
  656. var _ = Describe("ExternalSecret refresh logic", func() {
  657. Context("secret refresh", func() {
  658. It("should refresh when resource version does not match", func() {
  659. Expect(shouldRefresh(esv1alpha1.ExternalSecret{
  660. Status: esv1alpha1.ExternalSecretStatus{
  661. SyncedResourceVersion: "some resource version",
  662. },
  663. })).To(BeTrue())
  664. })
  665. It("should refresh when labels change", func() {
  666. es := esv1alpha1.ExternalSecret{
  667. ObjectMeta: metav1.ObjectMeta{
  668. Generation: 1,
  669. Labels: map[string]string{
  670. "foo": "bar",
  671. },
  672. },
  673. Spec: esv1alpha1.ExternalSecretSpec{
  674. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  675. },
  676. Status: esv1alpha1.ExternalSecretStatus{
  677. RefreshTime: metav1.Now(),
  678. },
  679. }
  680. es.Status.SyncedResourceVersion = getResourceVersion(es)
  681. // this should not refresh, rv matches object
  682. Expect(shouldRefresh(es)).To(BeFalse())
  683. // change labels without changing the syncedResourceVersion and expect refresh
  684. es.ObjectMeta.Labels["new"] = "w00t"
  685. Expect(shouldRefresh(es)).To(BeTrue())
  686. })
  687. It("should refresh when annotations change", func() {
  688. es := esv1alpha1.ExternalSecret{
  689. ObjectMeta: metav1.ObjectMeta{
  690. Generation: 1,
  691. Annotations: map[string]string{
  692. "foo": "bar",
  693. },
  694. },
  695. Spec: esv1alpha1.ExternalSecretSpec{
  696. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  697. },
  698. Status: esv1alpha1.ExternalSecretStatus{
  699. RefreshTime: metav1.Now(),
  700. },
  701. }
  702. es.Status.SyncedResourceVersion = getResourceVersion(es)
  703. // this should not refresh, rv matches object
  704. Expect(shouldRefresh(es)).To(BeFalse())
  705. // change annotations without changing the syncedResourceVersion and expect refresh
  706. es.ObjectMeta.Annotations["new"] = "w00t"
  707. Expect(shouldRefresh(es)).To(BeTrue())
  708. })
  709. It("should refresh when generation has changed", func() {
  710. es := esv1alpha1.ExternalSecret{
  711. ObjectMeta: metav1.ObjectMeta{
  712. Generation: 1,
  713. },
  714. Spec: esv1alpha1.ExternalSecretSpec{
  715. RefreshInterval: &metav1.Duration{Duration: 0},
  716. },
  717. Status: esv1alpha1.ExternalSecretStatus{
  718. RefreshTime: metav1.Now(),
  719. },
  720. }
  721. es.Status.SyncedResourceVersion = getResourceVersion(es)
  722. Expect(shouldRefresh(es)).To(BeFalse())
  723. // update gen -> refresh
  724. es.ObjectMeta.Generation = 2
  725. Expect(shouldRefresh(es)).To(BeTrue())
  726. })
  727. It("should skip refresh when refreshInterval is 0", func() {
  728. es := esv1alpha1.ExternalSecret{
  729. ObjectMeta: metav1.ObjectMeta{
  730. Generation: 1,
  731. },
  732. Spec: esv1alpha1.ExternalSecretSpec{
  733. RefreshInterval: &metav1.Duration{Duration: 0},
  734. },
  735. Status: esv1alpha1.ExternalSecretStatus{},
  736. }
  737. // resource version matches
  738. es.Status.SyncedResourceVersion = getResourceVersion(es)
  739. Expect(shouldRefresh(es)).To(BeFalse())
  740. })
  741. It("should refresh when refresh interval has passed", func() {
  742. es := esv1alpha1.ExternalSecret{
  743. ObjectMeta: metav1.ObjectMeta{
  744. Generation: 1,
  745. },
  746. Spec: esv1alpha1.ExternalSecretSpec{
  747. RefreshInterval: &metav1.Duration{Duration: time.Second},
  748. },
  749. Status: esv1alpha1.ExternalSecretStatus{
  750. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  751. },
  752. }
  753. // resource version matches
  754. es.Status.SyncedResourceVersion = getResourceVersion(es)
  755. Expect(shouldRefresh(es)).To(BeTrue())
  756. })
  757. It("should refresh when no refresh time was set", func() {
  758. es := esv1alpha1.ExternalSecret{
  759. ObjectMeta: metav1.ObjectMeta{
  760. Generation: 1,
  761. },
  762. Spec: esv1alpha1.ExternalSecretSpec{
  763. RefreshInterval: &metav1.Duration{Duration: time.Second},
  764. },
  765. Status: esv1alpha1.ExternalSecretStatus{},
  766. }
  767. // resource version matches
  768. es.Status.SyncedResourceVersion = getResourceVersion(es)
  769. Expect(shouldRefresh(es)).To(BeTrue())
  770. })
  771. })
  772. Context("objectmeta hash", func() {
  773. It("should produce different hashes for different k/v pairs", func() {
  774. h1 := hashMeta(metav1.ObjectMeta{
  775. Generation: 1,
  776. Annotations: map[string]string{
  777. "foo": "bar",
  778. },
  779. })
  780. h2 := hashMeta(metav1.ObjectMeta{
  781. Generation: 1,
  782. Annotations: map[string]string{
  783. "foo": "bing",
  784. },
  785. })
  786. Expect(h1).ToNot(Equal(h2))
  787. })
  788. It("should produce different hashes for different generations but same label/annotations", func() {
  789. h1 := hashMeta(metav1.ObjectMeta{
  790. Generation: 1,
  791. Annotations: map[string]string{
  792. "foo": "bar",
  793. },
  794. Labels: map[string]string{
  795. "foo": "bar",
  796. },
  797. })
  798. h2 := hashMeta(metav1.ObjectMeta{
  799. Generation: 2,
  800. Annotations: map[string]string{
  801. "foo": "bar",
  802. },
  803. Labels: map[string]string{
  804. "foo": "bar",
  805. },
  806. })
  807. Expect(h1).To(Equal(h2))
  808. })
  809. It("should produce the same hash for the same k/v pairs", func() {
  810. h1 := hashMeta(metav1.ObjectMeta{
  811. Generation: 1,
  812. })
  813. h2 := hashMeta(metav1.ObjectMeta{
  814. Generation: 1,
  815. })
  816. Expect(h1).To(Equal(h2))
  817. h1 = hashMeta(metav1.ObjectMeta{
  818. Generation: 1,
  819. Annotations: map[string]string{
  820. "foo": "bar",
  821. },
  822. })
  823. h2 = hashMeta(metav1.ObjectMeta{
  824. Generation: 1,
  825. Annotations: map[string]string{
  826. "foo": "bar",
  827. },
  828. })
  829. Expect(h1).To(Equal(h2))
  830. })
  831. })
  832. })
  833. var _ = Describe("Controller Reconcile logic", func() {
  834. Context("controller reconcile", func() {
  835. It("should reconcile when resource is not synced", func() {
  836. Expect(shouldReconcile(esv1alpha1.ExternalSecret{
  837. Status: esv1alpha1.ExternalSecretStatus{
  838. SyncedResourceVersion: "some resource version",
  839. Conditions: []esv1alpha1.ExternalSecretStatusCondition{{Reason: "NotASecretSynced"}},
  840. },
  841. })).To(BeTrue())
  842. })
  843. It("should reconcile when secret isn't immutable", func() {
  844. Expect(shouldReconcile(esv1alpha1.ExternalSecret{
  845. Spec: esv1alpha1.ExternalSecretSpec{
  846. Target: esv1alpha1.ExternalSecretTarget{
  847. Immutable: false,
  848. },
  849. },
  850. })).To(BeTrue())
  851. })
  852. It("should not reconcile if secret is immutable and has synced condition", func() {
  853. Expect(shouldReconcile(esv1alpha1.ExternalSecret{
  854. Spec: esv1alpha1.ExternalSecretSpec{
  855. Target: esv1alpha1.ExternalSecretTarget{
  856. Immutable: true,
  857. },
  858. },
  859. Status: esv1alpha1.ExternalSecretStatus{
  860. SyncedResourceVersion: "some resource version",
  861. Conditions: []esv1alpha1.ExternalSecretStatusCondition{{Reason: "SecretSynced"}},
  862. },
  863. })).To(BeFalse())
  864. })
  865. })
  866. })
  867. // CreateNamespace creates a new namespace in the cluster.
  868. func CreateNamespace(baseName string, c client.Client) (string, error) {
  869. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  870. ns := &v1.Namespace{
  871. ObjectMeta: metav1.ObjectMeta{
  872. GenerateName: genName,
  873. },
  874. }
  875. var err error
  876. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  877. err = c.Create(context.Background(), ns)
  878. if err != nil {
  879. return false, nil
  880. }
  881. return true, nil
  882. })
  883. if err != nil {
  884. return "", err
  885. }
  886. return ns.Name, nil
  887. }
  888. func hasOwnerRef(meta metav1.ObjectMeta, kind, name string) bool {
  889. for _, ref := range meta.OwnerReferences {
  890. if ref.Kind == kind && ref.Name == name {
  891. return true
  892. }
  893. }
  894. return false
  895. }
  896. func hasFieldOwnership(meta metav1.ObjectMeta, mgr, rawFields string) bool {
  897. for _, ref := range meta.ManagedFields {
  898. if ref.Manager == mgr && string(ref.FieldsV1.Raw) == rawFields {
  899. return true
  900. }
  901. }
  902. return false
  903. }
  904. func externalSecretConditionShouldBe(name, ns string, ct esv1alpha1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  905. return Eventually(func() float64 {
  906. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  907. return metric.GetGauge().GetValue()
  908. }, timeout, interval).Should(Equal(v))
  909. }
  910. func init() {
  911. fakeProvider = fake.New()
  912. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  913. AWS: &esv1alpha1.AWSProvider{
  914. Service: esv1alpha1.AWSServiceSecretsManager,
  915. },
  916. })
  917. }