externalsecret_controller_test.go 49 KB

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