common_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 secretstore
  13. import (
  14. "context"
  15. "time"
  16. corev1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/types"
  19. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. )
  23. type testCase struct {
  24. store esapi.GenericStore
  25. assert func()
  26. }
  27. var _ = Describe("SecretStore reconcile", func() {
  28. var test *testCase
  29. BeforeEach(func() {
  30. test = makeDefaultTestcase()
  31. })
  32. AfterEach(func() {
  33. Expect(k8sClient.Delete(context.Background(), test.store)).ToNot(HaveOccurred())
  34. })
  35. // an invalid provider config should be reflected
  36. // in the store status condition
  37. invalidProvider := func(tc *testCase) {
  38. tc.assert = func() {
  39. Eventually(func() bool {
  40. ss := tc.store.Copy()
  41. err := k8sClient.Get(context.Background(), types.NamespacedName{
  42. Name: defaultStoreName,
  43. Namespace: ss.GetObjectMeta().Namespace,
  44. }, ss)
  45. if err != nil {
  46. return false
  47. }
  48. status := ss.GetStatus()
  49. if len(status.Conditions) != 1 {
  50. return false
  51. }
  52. return status.Conditions[0].Reason == esapi.ReasonInvalidProviderConfig &&
  53. hasEvent(tc.store.GetTypeMeta().Kind, ss.GetName(), esapi.ReasonInvalidProviderConfig)
  54. }).
  55. WithTimeout(time.Second * 10).
  56. WithPolling(time.Second).
  57. Should(BeTrue())
  58. }
  59. }
  60. // if controllerClass does not match the controller
  61. // should not touch this store
  62. ignoreControllerClass := func(tc *testCase) {
  63. spc := tc.store.GetSpec()
  64. spc.Controller = "something-else"
  65. tc.assert = func() {
  66. Consistently(func() bool {
  67. ss := tc.store.Copy()
  68. err := k8sClient.Get(context.Background(), types.NamespacedName{
  69. Name: defaultStoreName,
  70. Namespace: ss.GetObjectMeta().Namespace,
  71. }, ss)
  72. if err != nil {
  73. return true
  74. }
  75. return len(ss.GetStatus().Conditions) == 0
  76. }).
  77. WithTimeout(time.Second * 3).
  78. WithPolling(time.Millisecond * 500).
  79. Should(BeTrue())
  80. }
  81. }
  82. validProvider := func(tc *testCase) {
  83. spc := tc.store.GetSpec()
  84. spc.Provider.Vault = nil
  85. spc.Provider.Fake = &esapi.FakeProvider{
  86. Data: []esapi.FakeProviderData{},
  87. }
  88. tc.assert = func() {
  89. Eventually(func() bool {
  90. ss := tc.store.Copy()
  91. err := k8sClient.Get(context.Background(), types.NamespacedName{
  92. Name: defaultStoreName,
  93. Namespace: ss.GetNamespace(),
  94. }, ss)
  95. if err != nil {
  96. return false
  97. }
  98. if len(ss.GetStatus().Conditions) != 1 {
  99. return false
  100. }
  101. return ss.GetStatus().Conditions[0].Reason == esapi.ReasonStoreValid &&
  102. ss.GetStatus().Conditions[0].Type == esapi.SecretStoreReady &&
  103. ss.GetStatus().Conditions[0].Status == corev1.ConditionTrue &&
  104. hasEvent(tc.store.GetTypeMeta().Kind, ss.GetName(), esapi.ReasonStoreValid)
  105. }).
  106. WithTimeout(time.Second * 10).
  107. WithPolling(time.Second).
  108. Should(BeTrue())
  109. }
  110. }
  111. readWrite := func(tc *testCase) {
  112. spc := tc.store.GetSpec()
  113. spc.Provider.Vault = nil
  114. spc.Provider.Fake = &esapi.FakeProvider{
  115. Data: []esapi.FakeProviderData{},
  116. }
  117. tc.assert = func() {
  118. Eventually(func() bool {
  119. ss := tc.store.Copy()
  120. err := k8sClient.Get(context.Background(), types.NamespacedName{
  121. Name: defaultStoreName,
  122. Namespace: ss.GetNamespace(),
  123. }, ss)
  124. if err != nil {
  125. return false
  126. }
  127. if ss.GetStatus().Capabilities != esapi.SecretStoreReadWrite {
  128. return false
  129. }
  130. return true
  131. }).
  132. WithTimeout(time.Second * 10).
  133. WithPolling(time.Second).
  134. Should(BeTrue())
  135. }
  136. }
  137. DescribeTable("Controller Reconcile logic", func(muts ...func(tc *testCase)) {
  138. for _, mut := range muts {
  139. mut(test)
  140. }
  141. err := k8sClient.Create(context.Background(), test.store.Copy())
  142. Expect(err).ToNot(HaveOccurred())
  143. test.assert()
  144. },
  145. // namespaced store
  146. Entry("[namespace] invalid provider with secretStore should set InvalidStore condition", invalidProvider),
  147. Entry("[namespace] ignore stores with non-matching class", ignoreControllerClass),
  148. Entry("[namespace] valid provider has status=ready", validProvider),
  149. Entry("[namespace] valid provider has capabilities=ReadWrite", readWrite),
  150. // cluster store
  151. Entry("[cluster] invalid provider with secretStore should set InvalidStore condition", invalidProvider, useClusterStore),
  152. Entry("[cluster] ignore stores with non-matching class", ignoreControllerClass, useClusterStore),
  153. Entry("[cluster] valid provider has status=ready", validProvider, useClusterStore),
  154. Entry("[cluster] valid provider has capabilities=ReadWrite", readWrite, useClusterStore),
  155. )
  156. })
  157. const (
  158. defaultStoreName = "default-store"
  159. defaultControllerClass = "test-ctrl"
  160. )
  161. func makeDefaultTestcase() *testCase {
  162. return &testCase{
  163. assert: func() {
  164. // this is a noop by default
  165. },
  166. store: &esapi.SecretStore{
  167. TypeMeta: metav1.TypeMeta{
  168. Kind: esapi.SecretStoreKind,
  169. APIVersion: esapi.SecretStoreKindAPIVersion,
  170. },
  171. ObjectMeta: metav1.ObjectMeta{
  172. Name: defaultStoreName,
  173. Namespace: "default",
  174. },
  175. Spec: esapi.SecretStoreSpec{
  176. Controller: defaultControllerClass,
  177. // empty provider
  178. // a testCase mutator must fill in the concrete provider
  179. Provider: &esapi.SecretStoreProvider{
  180. Vault: &esapi.VaultProvider{
  181. Version: esapi.VaultKVStoreV1,
  182. },
  183. },
  184. },
  185. },
  186. }
  187. }
  188. func useClusterStore(tc *testCase) {
  189. spc := tc.store.GetSpec()
  190. meta := tc.store.GetObjectMeta()
  191. tc.store = &esapi.ClusterSecretStore{
  192. TypeMeta: metav1.TypeMeta{
  193. Kind: esapi.ClusterSecretStoreKind,
  194. APIVersion: esapi.ClusterSecretStoreKindAPIVersion,
  195. },
  196. ObjectMeta: metav1.ObjectMeta{
  197. Name: meta.Name,
  198. },
  199. Spec: *spc,
  200. }
  201. }
  202. func hasEvent(involvedKind, name, reason string) bool {
  203. el := &corev1.EventList{}
  204. err := k8sClient.List(context.Background(), el)
  205. if err != nil {
  206. return false
  207. }
  208. for i := range el.Items {
  209. ev := el.Items[i]
  210. if ev.InvolvedObject.Kind == involvedKind && ev.InvolvedObject.Name == name {
  211. if ev.Reason == reason {
  212. return true
  213. }
  214. }
  215. }
  216. return false
  217. }