template.go 6.3 KB

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