template.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. limitations under the License.
  10. */
  11. package template
  12. import (
  13. "context"
  14. "fmt"
  15. "time"
  16. "github.com/external-secrets/external-secrets-e2e/framework"
  17. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  18. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  19. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  20. "github.com/onsi/gomega"
  21. v1 "k8s.io/api/core/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. // nolint
  27. . "github.com/onsi/ginkgo/v2"
  28. )
  29. var _ = Describe("[template]", Label("template"), func() {
  30. f := framework.New("templating")
  31. prov := newProvider(f)
  32. fakeSecretClient := fake.New()
  33. DescribeTable("sync secrets", framework.TableFuncWithExternalSecret(f, prov),
  34. framework.Compose("template v1", f, genericExternalSecretTemplate, useTemplateV1),
  35. framework.Compose("template v2", f, genericExternalSecretTemplate, useTemplateV2),
  36. )
  37. DescribeTable("push secret", framework.TableFuncWithPushSecret(f, prov, fakeSecretClient),
  38. framework.Compose("template", f, genericPushSecretTemplate, useTemplateWithPushSecret),
  39. )
  40. })
  41. // useTemplateV1 specifies a test case which uses the template engine v1.
  42. func useTemplateV1(tc *framework.TestCase) {
  43. tc.ExternalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  44. EngineVersion: esv1beta1.TemplateEngineV1,
  45. Data: map[string]string{
  46. "tplv1": "executed: {{ .singlefoo | toString }}|{{ .singlebaz | toString }}",
  47. "other": `{{ .foo | toString }}|{{ .bar | toString }}`,
  48. },
  49. }
  50. tc.ExpectedSecret.Data = map[string][]byte{
  51. "tplv1": []byte(`executed: bar|bang`),
  52. "other": []byte(`barmap|bangmap`),
  53. }
  54. }
  55. // useTemplateV2 specifies a test case which uses the template engine v2.
  56. func useTemplateV2(tc *framework.TestCase) {
  57. tc.ExternalSecret.Spec.Target.Template = &esv1beta1.ExternalSecretTemplate{
  58. EngineVersion: esv1beta1.TemplateEngineV2,
  59. Data: map[string]string{
  60. "tplv2": "executed: {{ .singlefoo }}|{{ .singlebaz }}",
  61. "other": `{{ .foo }}|{{ .bar }}`,
  62. "sprig-str": `{{ .foo | upper }}`,
  63. "json-ex": `{{ $var := .singlejson | fromJson }}{{ $var.foo | toJson }}`,
  64. },
  65. }
  66. tc.ExpectedSecret.Data = map[string][]byte{
  67. "tplv2": []byte(`executed: bar|bang`),
  68. "other": []byte(`barmap|bangmap`),
  69. "sprig-str": []byte(`BARMAP`),
  70. "json-ex": []byte(`{"bar":"baz"}`),
  71. }
  72. }
  73. // This case uses template engine v1.
  74. func genericExternalSecretTemplate(f *framework.Framework) (string, func(*framework.TestCase)) {
  75. return "[template] should execute template v1", func(tc *framework.TestCase) {
  76. tc.ExpectedSecret = &v1.Secret{
  77. Type: v1.SecretTypeOpaque,
  78. }
  79. tc.ExternalSecret.Spec.Data = []esv1beta1.ExternalSecretData{
  80. {
  81. SecretKey: "singlefoo",
  82. RemoteRef: esv1beta1.ExternalSecretDataRemoteRef{
  83. Key: "foo",
  84. },
  85. },
  86. {
  87. SecretKey: "singlebaz",
  88. RemoteRef: esv1beta1.ExternalSecretDataRemoteRef{
  89. Key: "baz",
  90. },
  91. },
  92. {
  93. SecretKey: "singlejson",
  94. RemoteRef: esv1beta1.ExternalSecretDataRemoteRef{
  95. Key: "json",
  96. },
  97. },
  98. }
  99. tc.ExternalSecret.Spec.DataFrom = []esv1beta1.ExternalSecretDataFromRemoteRef{
  100. {
  101. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  102. Key: "map",
  103. },
  104. },
  105. }
  106. }
  107. }
  108. // This case uses template engine v1.
  109. func genericPushSecretTemplate(f *framework.Framework) (string, func(*framework.TestCase)) {
  110. return "[template] should execute template v1", func(tc *framework.TestCase) {
  111. secretKey1 := fmt.Sprintf("%s-%s", f.Namespace.Name, "one")
  112. tc.PushSecretSource = &v1.Secret{
  113. ObjectMeta: metav1.ObjectMeta{
  114. Name: secretKey1,
  115. Namespace: f.Namespace.Name,
  116. },
  117. Data: map[string][]byte{
  118. "singlefoo": []byte("bar"),
  119. },
  120. Type: v1.SecretTypeOpaque,
  121. }
  122. tc.PushSecret.Spec.Selector = esv1alpha1.PushSecretSelector{
  123. Secret: &esv1alpha1.PushSecretSecret{
  124. Name: secretKey1,
  125. },
  126. }
  127. tc.PushSecret.Spec.Data = []esv1alpha1.PushSecretData{
  128. {
  129. Match: esv1alpha1.PushSecretMatch{
  130. SecretKey: "singlefoo",
  131. RemoteRef: esv1alpha1.PushSecretRemoteRef{
  132. RemoteKey: "key",
  133. Property: "singlefoo",
  134. },
  135. },
  136. },
  137. }
  138. tc.VerifyPushSecretOutcome = func(sourcePs *esv1alpha1.PushSecret, pushClient esv1beta1.SecretsClient) {
  139. gomega.Eventually(func() bool {
  140. s := &esv1alpha1.PushSecret{}
  141. err := tc.Framework.CRClient.Get(context.Background(), types.NamespacedName{Name: tc.PushSecret.Name, Namespace: tc.PushSecret.Namespace}, s)
  142. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  143. for i := range s.Status.Conditions {
  144. c := s.Status.Conditions[i]
  145. if c.Type == esv1alpha1.PushSecretReady && c.Status == v1.ConditionTrue {
  146. return true
  147. }
  148. }
  149. return false
  150. }, time.Minute*1, time.Second*5).Should(gomega.BeTrue())
  151. // create an external secret that fetches the created remote secret
  152. // and check the value
  153. exampleOutput := "example-output"
  154. es := &esv1beta1.ExternalSecret{
  155. ObjectMeta: metav1.ObjectMeta{
  156. Name: "e2e-es",
  157. Namespace: f.Namespace.Name,
  158. },
  159. Spec: esv1beta1.ExternalSecretSpec{
  160. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  161. SecretStoreRef: esv1beta1.SecretStoreRef{
  162. Name: f.Namespace.Name,
  163. },
  164. Target: esv1beta1.ExternalSecretTarget{
  165. Name: exampleOutput,
  166. },
  167. Data: []esv1beta1.ExternalSecretData{
  168. {
  169. SecretKey: exampleOutput,
  170. RemoteRef: esv1beta1.ExternalSecretDataRemoteRef{
  171. Key: "key",
  172. },
  173. },
  174. },
  175. },
  176. }
  177. err := tc.Framework.CRClient.Create(context.Background(), es)
  178. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  179. outputSecret := &v1.Secret{}
  180. err = wait.PollImmediate(time.Second*5, time.Second*15, func() (bool, error) {
  181. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  182. Namespace: f.Namespace.Name,
  183. Name: exampleOutput,
  184. }, outputSecret)
  185. if apierrors.IsNotFound(err) {
  186. return false, nil
  187. }
  188. return true, nil
  189. })
  190. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  191. v, ok := outputSecret.Data[exampleOutput]
  192. gomega.Expect(ok).To(gomega.BeTrue())
  193. gomega.Expect(string(v)).To(gomega.Equal("executed: BAR"))
  194. }
  195. }
  196. }
  197. // useTemplateWithPushSecret specifies a test case which uses the template engine v1.
  198. func useTemplateWithPushSecret(tc *framework.TestCase) {
  199. tc.PushSecret.Spec.Template = &esv1beta1.ExternalSecretTemplate{
  200. EngineVersion: esv1beta1.TemplateEngineV2,
  201. Data: map[string]string{
  202. "singlefoo": "executed: {{ .singlefoo | upper }}",
  203. },
  204. }
  205. }