pushsecret_controller_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 pushsecret
  13. import (
  14. "bytes"
  15. "context"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "time"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. v1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  26. v1beta1 "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. timeout = time.Second * 10
  33. interval = time.Millisecond * 250
  34. )
  35. type testCase struct {
  36. store v1beta1.GenericStore
  37. pushsecret *v1alpha1.PushSecret
  38. secret *v1.Secret
  39. assert func(pushsecret *v1alpha1.PushSecret, secret *v1.Secret) bool
  40. }
  41. func init() {
  42. fakeProvider = fake.New()
  43. v1beta1.ForceRegister(fakeProvider, &v1beta1.SecretStoreProvider{
  44. Fake: &v1beta1.FakeProvider{},
  45. })
  46. }
  47. type testTweaks func(*testCase)
  48. var _ = Describe("ExternalSecret controller", func() {
  49. const (
  50. PushSecretName = "test-es"
  51. PushSecretFQDN = "externalsecrets.external-secrets.io/test-es"
  52. PushSecretStore = "test-store"
  53. SecretName = "test-secret"
  54. PushSecretTargetSecretName = "test-secret"
  55. FakeManager = "fake.manager"
  56. expectedSecretVal = "SOMEVALUE was templated"
  57. targetPropObj = "{{ .targetProperty | toString | upper }} was templated"
  58. FooValue = "map-foo-value"
  59. BarValue = "map-bar-value"
  60. )
  61. var PushSecretNamespace string
  62. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  63. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  64. if t, err := strconv.Atoi(customTimeout); err == nil {
  65. timeout = time.Second * time.Duration(t)
  66. }
  67. }
  68. BeforeEach(func() {
  69. var err error
  70. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  71. Expect(err).ToNot(HaveOccurred())
  72. fakeProvider.Reset()
  73. })
  74. AfterEach(func() {
  75. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: PushSecretNamespace,
  78. },
  79. })).To(Succeed())
  80. k8sClient.Delete(context.Background(), &v1beta1.SecretStore{
  81. ObjectMeta: metav1.ObjectMeta{
  82. Name: PushSecretStore,
  83. Namespace: PushSecretNamespace,
  84. },
  85. })
  86. k8sClient.Delete(context.Background(), &v1.Secret{
  87. ObjectMeta: metav1.ObjectMeta{
  88. Name: SecretName,
  89. Namespace: PushSecretNamespace,
  90. },
  91. })
  92. })
  93. makeDefaultTestcase := func() *testCase {
  94. return &testCase{
  95. pushsecret: &v1alpha1.PushSecret{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Name: PushSecretName,
  98. Namespace: PushSecretNamespace,
  99. },
  100. Spec: v1alpha1.PushSecretSpec{
  101. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  102. {
  103. Name: PushSecretStore,
  104. Kind: "SecretStore",
  105. },
  106. },
  107. Selector: v1alpha1.PushSecretSelector{
  108. Secret: v1alpha1.PushSecretSecret{
  109. Name: SecretName,
  110. },
  111. },
  112. Data: []v1alpha1.PushSecretData{
  113. {
  114. Match: v1alpha1.PushSecretMatch{
  115. SecretKey: "key",
  116. RemoteRefs: []v1alpha1.PushSecretRemoteRefs{
  117. {
  118. RemoteKey: "path/to/key",
  119. },
  120. },
  121. },
  122. },
  123. },
  124. },
  125. },
  126. secret: &v1.Secret{
  127. ObjectMeta: metav1.ObjectMeta{
  128. Name: SecretName,
  129. Namespace: PushSecretNamespace,
  130. },
  131. Data: map[string][]byte{
  132. "key": []byte("value"),
  133. },
  134. },
  135. store: &v1beta1.SecretStore{
  136. ObjectMeta: metav1.ObjectMeta{
  137. Name: PushSecretStore,
  138. Namespace: PushSecretNamespace,
  139. },
  140. Spec: v1beta1.SecretStoreSpec{
  141. Provider: &v1beta1.SecretStoreProvider{
  142. Fake: &v1beta1.FakeProvider{
  143. Data: []v1beta1.FakeProviderData{},
  144. },
  145. },
  146. },
  147. },
  148. }
  149. }
  150. // if target Secret name is not specified it should use the ExternalSecret name.
  151. syncSuccessfully := func(tc *testCase) {
  152. fakeProvider.SetSecretFn = func() error {
  153. return nil
  154. }
  155. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  156. secretValue := secret.Data["key"]
  157. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRefs[0].RemoteKey].Value
  158. return bytes.Equal(secretValue, providerValue)
  159. }
  160. }
  161. // if target Secret name is not specified it should use the ExternalSecret name.
  162. failNoSecret := func(tc *testCase) {
  163. fakeProvider.SetSecretFn = func() error {
  164. return nil
  165. }
  166. tc.secret = nil
  167. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  168. return ps.Status.Conditions[0].Reason == v1alpha1.ReasonErrored
  169. }
  170. }
  171. // if target Secret name is not specified it should use the ExternalSecret name.
  172. failNoSecretStore := func(tc *testCase) {
  173. fakeProvider.SetSecretFn = func() error {
  174. return nil
  175. }
  176. tc.store = nil
  177. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  178. return ps.Status.Conditions[0].Reason == v1alpha1.ReasonErrored
  179. }
  180. }
  181. // if target Secret name is not specified it should use the ExternalSecret name.
  182. setSecretFail := func(tc *testCase) {
  183. fakeProvider.SetSecretFn = func() error {
  184. return fmt.Errorf("boom")
  185. }
  186. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  187. return ps.Status.Conditions[0].Reason == v1alpha1.ReasonErrored
  188. }
  189. }
  190. DescribeTable("When reconciling a PushSecret",
  191. func(tweaks ...testTweaks) {
  192. tc := makeDefaultTestcase()
  193. for _, tweak := range tweaks {
  194. tweak(tc)
  195. }
  196. ctx := context.Background()
  197. By("creating a secret store, secret and pushsecret")
  198. if tc.store != nil {
  199. Expect(k8sClient.Create(ctx, tc.store)).To(Succeed())
  200. }
  201. if tc.secret != nil {
  202. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  203. }
  204. if tc.pushsecret != nil {
  205. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  206. }
  207. time.Sleep(2 * time.Second)
  208. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  209. createdPS := &v1alpha1.PushSecret{}
  210. By("checking the pushSecret condition")
  211. Eventually(func() bool {
  212. err := k8sClient.Get(ctx, psKey, createdPS)
  213. if err != nil {
  214. return false
  215. }
  216. return tc.assert(createdPS, tc.secret)
  217. }, timeout, interval).Should(BeTrue())
  218. // this must be optional so we can test faulty es configuration
  219. },
  220. Entry("should work as we are not doing anything at all!", syncSuccessfully),
  221. Entry("should fail if Secret is not created", failNoSecret),
  222. Entry("should fail if SetSecret fails", setSecretFail),
  223. Entry("should fail if no valid SecretStore", failNoSecretStore),
  224. )
  225. })