common_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/v1"
  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. // an unknown store validation result should be reflected
  138. // in the store status condition
  139. validationUnknown := func(tc *testCase) {
  140. spc := tc.store.GetSpec()
  141. spc.Provider.Vault = nil
  142. validationResultUnknown := esapi.ValidationResultUnknown
  143. spc.Provider.Fake = &esapi.FakeProvider{
  144. Data: []esapi.FakeProviderData{},
  145. ValidationResult: &validationResultUnknown,
  146. }
  147. tc.assert = func() {
  148. Eventually(func() bool {
  149. ss := tc.store.Copy()
  150. err := k8sClient.Get(context.Background(), types.NamespacedName{
  151. Name: defaultStoreName,
  152. Namespace: ss.GetNamespace(),
  153. }, ss)
  154. if err != nil {
  155. return false
  156. }
  157. if len(ss.GetStatus().Conditions) != 1 {
  158. return false
  159. }
  160. return ss.GetStatus().Conditions[0].Reason == esapi.ReasonValidationUnknown &&
  161. ss.GetStatus().Conditions[0].Type == esapi.SecretStoreReady &&
  162. ss.GetStatus().Conditions[0].Status == corev1.ConditionTrue &&
  163. hasEvent(tc.store.GetTypeMeta().Kind, ss.GetName(), esapi.ReasonValidationUnknown)
  164. }).
  165. WithTimeout(time.Second * 5).
  166. WithPolling(time.Second).
  167. Should(BeTrue())
  168. }
  169. }
  170. DescribeTable("Controller Reconcile logic", func(muts ...func(tc *testCase)) {
  171. for _, mut := range muts {
  172. mut(test)
  173. }
  174. err := k8sClient.Create(context.Background(), test.store.Copy())
  175. Expect(err).ToNot(HaveOccurred())
  176. test.assert()
  177. },
  178. // namespaced store
  179. Entry("[namespace] invalid provider with secretStore should set InvalidStore condition", invalidProvider),
  180. Entry("[namespace] ignore stores with non-matching class", ignoreControllerClass),
  181. Entry("[namespace] valid provider has status=ready", validProvider),
  182. Entry("[namespace] valid provider has capabilities=ReadWrite", readWrite),
  183. Entry("[namespace] validation unknown status should set ValidationUnknown condition", validationUnknown),
  184. // cluster store
  185. Entry("[cluster] invalid provider with secretStore should set InvalidStore condition", invalidProvider, useClusterStore),
  186. Entry("[cluster] ignore stores with non-matching class", ignoreControllerClass, useClusterStore),
  187. Entry("[cluster] valid provider has status=ready", validProvider, useClusterStore),
  188. Entry("[cluster] valid provider has capabilities=ReadWrite", readWrite, useClusterStore),
  189. Entry("[cluster] validation unknown status should set ValidationUnknown condition", validationUnknown, useClusterStore),
  190. )
  191. })
  192. const (
  193. defaultStoreName = "default-store"
  194. defaultControllerClass = "test-ctrl"
  195. )
  196. func makeDefaultTestcase() *testCase {
  197. return &testCase{
  198. assert: func() {
  199. // this is a noop by default
  200. },
  201. store: &esapi.SecretStore{
  202. TypeMeta: metav1.TypeMeta{
  203. Kind: esapi.SecretStoreKind,
  204. APIVersion: esapi.SecretStoreKindAPIVersion,
  205. },
  206. ObjectMeta: metav1.ObjectMeta{
  207. Name: defaultStoreName,
  208. Namespace: "default",
  209. },
  210. Spec: esapi.SecretStoreSpec{
  211. Controller: defaultControllerClass,
  212. // empty provider
  213. // a testCase mutator must fill in the concrete provider
  214. Provider: &esapi.SecretStoreProvider{
  215. Vault: &esapi.VaultProvider{
  216. Version: esapi.VaultKVStoreV1,
  217. },
  218. },
  219. },
  220. },
  221. }
  222. }
  223. func useClusterStore(tc *testCase) {
  224. spc := tc.store.GetSpec()
  225. meta := tc.store.GetObjectMeta()
  226. tc.store = &esapi.ClusterSecretStore{
  227. TypeMeta: metav1.TypeMeta{
  228. Kind: esapi.ClusterSecretStoreKind,
  229. APIVersion: esapi.ClusterSecretStoreKindAPIVersion,
  230. },
  231. ObjectMeta: metav1.ObjectMeta{
  232. Name: meta.Name,
  233. },
  234. Spec: *spc,
  235. }
  236. }
  237. func hasEvent(involvedKind, name, reason string) bool {
  238. el := &corev1.EventList{}
  239. err := k8sClient.List(context.Background(), el)
  240. if err != nil {
  241. return false
  242. }
  243. for i := range el.Items {
  244. ev := el.Items[i]
  245. if ev.InvolvedObject.Kind == involvedKind && ev.InvolvedObject.Name == name {
  246. if ev.Reason == reason {
  247. return true
  248. }
  249. }
  250. }
  251. return false
  252. }