template.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package template
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/external-secrets/external-secrets-e2e/framework"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. "github.com/external-secrets/external-secrets/runtime/testing/fake"
  22. // nolint
  23. . "github.com/onsi/gomega"
  24. v1 "k8s.io/api/core/v1"
  25. apierrors "k8s.io/apimachinery/pkg/api/errors"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "k8s.io/apimachinery/pkg/types"
  28. "k8s.io/apimachinery/pkg/util/wait"
  29. // nolint
  30. . "github.com/onsi/ginkgo/v2"
  31. )
  32. var _ = Describe("[template]", Label("template"), func() {
  33. f := framework.New("templating")
  34. prov := newProvider(f)
  35. fakeSecretClient := fake.New()
  36. DescribeTable("sync secrets", framework.TableFuncWithExternalSecret(f, prov),
  37. framework.Compose("template v2", f, genericExternalSecretTemplate, useTemplateV2),
  38. )
  39. DescribeTable("push secret", framework.TableFuncWithPushSecret(f, prov, fakeSecretClient),
  40. framework.Compose("template", f, genericPushSecretTemplate, useTemplateWithPushSecret),
  41. )
  42. })
  43. // useTemplateV2 specifies a test case which uses the template engine v2.
  44. func useTemplateV2(tc *framework.TestCase) {
  45. tc.ExternalSecret.Spec.Target.Template = &esv1.ExternalSecretTemplate{
  46. EngineVersion: esv1.TemplateEngineV2,
  47. Data: map[string]string{
  48. "tplv2": "executed: {{ .singlefoo }}|{{ .singlebaz }}",
  49. "other": `{{ .foo }}|{{ .bar }}`,
  50. "sprig-str": `{{ .foo | upper }}`,
  51. "json-ex": `{{ $var := .singlejson | fromJson }}{{ $var.foo | toJson }}`,
  52. },
  53. }
  54. tc.ExpectedSecret.Data = map[string][]byte{
  55. "tplv2": []byte(`executed: bar|bang`),
  56. "other": []byte(`barmap|bangmap`),
  57. "sprig-str": []byte(`BARMAP`),
  58. "json-ex": []byte(`{"bar":"baz"}`),
  59. }
  60. }
  61. // This case uses template engine v1.
  62. func genericExternalSecretTemplate(f *framework.Framework) (string, func(*framework.TestCase)) {
  63. return "[template] should execute template v1", func(tc *framework.TestCase) {
  64. tc.ExpectedSecret = &v1.Secret{
  65. Type: v1.SecretTypeOpaque,
  66. }
  67. tc.ExternalSecret.Spec.Data = []esv1.ExternalSecretData{
  68. {
  69. SecretKey: "singlefoo",
  70. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  71. Key: "foo",
  72. },
  73. },
  74. {
  75. SecretKey: "singlebaz",
  76. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  77. Key: "baz",
  78. },
  79. },
  80. {
  81. SecretKey: "singlejson",
  82. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  83. Key: "json",
  84. },
  85. },
  86. }
  87. tc.ExternalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
  88. {
  89. Extract: &esv1.ExternalSecretDataRemoteRef{
  90. Key: "map",
  91. },
  92. },
  93. }
  94. }
  95. }
  96. // This case uses template engine v1.
  97. func genericPushSecretTemplate(f *framework.Framework) (string, func(*framework.TestCase)) {
  98. return "[template] should execute template v1", func(tc *framework.TestCase) {
  99. secretKey1 := fmt.Sprintf("%s-%s", f.Namespace.Name, "one")
  100. tc.PushSecretSource = &v1.Secret{
  101. ObjectMeta: metav1.ObjectMeta{
  102. Name: secretKey1,
  103. Namespace: f.Namespace.Name,
  104. },
  105. Data: map[string][]byte{
  106. "singlefoo": []byte("bar"),
  107. },
  108. Type: v1.SecretTypeOpaque,
  109. }
  110. tc.PushSecret.Spec.Selector = esv1alpha1.PushSecretSelector{
  111. Secret: &esv1alpha1.PushSecretSecret{
  112. Name: secretKey1,
  113. },
  114. }
  115. tc.PushSecret.Spec.Data = []esv1alpha1.PushSecretData{
  116. {
  117. Match: esv1alpha1.PushSecretMatch{
  118. SecretKey: "singlefoo",
  119. RemoteRef: esv1alpha1.PushSecretRemoteRef{
  120. RemoteKey: "key",
  121. Property: "singlefoo",
  122. },
  123. },
  124. },
  125. }
  126. tc.VerifyPushSecretOutcome = func(sourcePs *esv1alpha1.PushSecret, pushClient esv1.SecretsClient) {
  127. Eventually(func() bool {
  128. s := &esv1alpha1.PushSecret{}
  129. err := tc.Framework.CRClient.Get(GinkgoT().Context(), types.NamespacedName{Name: tc.PushSecret.Name, Namespace: tc.PushSecret.Namespace}, s)
  130. Expect(err).ToNot(HaveOccurred())
  131. for i := range s.Status.Conditions {
  132. c := s.Status.Conditions[i]
  133. if c.Type == esv1alpha1.PushSecretReady && c.Status == v1.ConditionTrue {
  134. return true
  135. }
  136. }
  137. return false
  138. }, time.Minute*1, time.Second*5).Should(BeTrue())
  139. // create an external secret that fetches the created remote secret
  140. // and check the value
  141. exampleOutput := "example-output"
  142. es := &esv1.ExternalSecret{
  143. ObjectMeta: metav1.ObjectMeta{
  144. Name: "e2e-es",
  145. Namespace: f.Namespace.Name,
  146. },
  147. Spec: esv1.ExternalSecretSpec{
  148. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  149. SecretStoreRef: esv1.SecretStoreRef{
  150. Name: f.Namespace.Name,
  151. },
  152. Target: esv1.ExternalSecretTarget{
  153. Name: exampleOutput,
  154. },
  155. Data: []esv1.ExternalSecretData{
  156. {
  157. SecretKey: exampleOutput,
  158. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  159. Key: "key",
  160. },
  161. },
  162. },
  163. },
  164. }
  165. err := tc.Framework.CRClient.Create(GinkgoT().Context(), es)
  166. Expect(err).ToNot(HaveOccurred())
  167. outputSecret := &v1.Secret{}
  168. err = wait.PollUntilContextTimeout(GinkgoT().Context(), time.Second*5, time.Second*15, true, func(ctx context.Context) (bool, error) {
  169. err := f.CRClient.Get(ctx, types.NamespacedName{
  170. Namespace: f.Namespace.Name,
  171. Name: exampleOutput,
  172. }, outputSecret)
  173. if apierrors.IsNotFound(err) {
  174. return false, nil
  175. }
  176. return true, nil
  177. })
  178. Expect(err).ToNot(HaveOccurred())
  179. v, ok := outputSecret.Data[exampleOutput]
  180. Expect(ok).To(BeTrue())
  181. Expect(string(v)).To(Equal("executed: BAR"))
  182. }
  183. }
  184. }
  185. // useTemplateWithPushSecret specifies a test case which uses the template engine v1.
  186. func useTemplateWithPushSecret(tc *framework.TestCase) {
  187. tc.PushSecret.Spec.Template = &esv1.ExternalSecretTemplate{
  188. EngineVersion: esv1.TemplateEngineV2,
  189. Data: map[string]string{
  190. "singlefoo": "executed: {{ .singlefoo | upper }}",
  191. },
  192. }
  193. }