pushsecret_controller_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. "context"
  15. "errors"
  16. "fmt"
  17. "time"
  18. "github.com/go-logr/logr"
  19. . "github.com/onsi/ginkgo/v2"
  20. . "github.com/onsi/gomega"
  21. v1 "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. ctrl "sigs.k8s.io/controller-runtime"
  24. kubeclient "sigs.k8s.io/controller-runtime/pkg/client"
  25. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  26. v1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. "github.com/external-secrets/external-secrets/pkg/controllers/pushsecret/internal/fakes"
  28. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  29. )
  30. var fakeProvider *fake.Client
  31. var _ = Describe("pushsecret", func() {
  32. var (
  33. reconciler *Reconciler
  34. client *fakes.Client
  35. recorder *fakes.FakeEventRecorder
  36. )
  37. BeforeEach(func() {
  38. client = new(fakes.Client)
  39. recorder = &fakes.FakeEventRecorder{}
  40. reconciler = &Reconciler{client, logr.Discard(), nil, recorder, 0, ""}
  41. })
  42. Describe("RefreshInterval", func() {
  43. var (
  44. statusWriter *fakes.StatusWriter
  45. )
  46. BeforeEach(func() {
  47. statusWriter = new(fakes.StatusWriter)
  48. client.StatusReturns(statusWriter)
  49. })
  50. It("Passes", func() {
  51. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  52. refreshInt := time.Duration(5000)
  53. reconciler.RequeueInterval = refreshInt
  54. result, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  55. Expect(result).To(Equal(ctrl.Result{RequeueAfter: refreshInt}))
  56. Expect(err).NotTo(HaveOccurred())
  57. })
  58. })
  59. Describe("#Reconcile", func() {
  60. var (
  61. statusWriter *fakes.StatusWriter
  62. )
  63. BeforeEach(func() {
  64. statusWriter = new(fakes.StatusWriter)
  65. client.StatusReturns(statusWriter)
  66. })
  67. It("succeeds", func() {
  68. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  69. result, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  70. Expect(result).To(Equal(ctrl.Result{RequeueAfter: 0, Requeue: false}))
  71. Expect(err).NotTo(HaveOccurred())
  72. Expect(client.GetCallCount()).To(Equal(2))
  73. Expect(client.StatusCallCount()).To(Equal(1))
  74. _, gotNamespacedName, _ := client.GetArgsForCall(0)
  75. Expect(gotNamespacedName).To(Equal(namspacedName))
  76. Expect(statusWriter.PatchCallCount()).To(Equal(1))
  77. _, _, patch, _ := statusWriter.PatchArgsForCall(0)
  78. Expect(patch.Type()).To(Equal(types.MergePatchType))
  79. Expect(recorder.EventCallCount()).To(Equal(1))
  80. _, _, reason, message := recorder.EventArgsForCall(0)
  81. Expect(reason).To(Equal(esapi.ReasonSynced))
  82. Expect(message).To(Equal("PushSecret synced successfully"))
  83. })
  84. When("an error returns in get", func() {
  85. BeforeEach(func() {
  86. client.GetReturns(errors.New("UnknownError"))
  87. })
  88. It("returns the error", func() {
  89. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  90. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  91. Expect(err).To(MatchError("get resource: UnknownError"))
  92. Expect(client.GetCallCount()).To(Equal(1))
  93. Expect(client.StatusCallCount()).To(Equal(0))
  94. _, _, reason, message := recorder.EventArgsForCall(0)
  95. Expect(reason).To(Equal(esapi.ReasonErrored))
  96. Expect(message).To(Equal("unable to get PushSecret"))
  97. })
  98. })
  99. When("an error returns in get secret", func() {
  100. BeforeEach(func() {
  101. client.GetStub = func(context context.Context, name types.NamespacedName, obj kubeclient.Object) error {
  102. switch obj.(type) {
  103. case *v1.Secret:
  104. return fmt.Errorf("GetSecretError")
  105. default:
  106. return nil
  107. }
  108. }
  109. })
  110. It("returns the error", func() {
  111. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  112. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  113. Expect(err).To(MatchError("GetSecretError"))
  114. _, _, reason, message := recorder.EventArgsForCall(0)
  115. Expect(reason).To(Equal(esapi.ReasonErrored))
  116. Expect(message).To(Equal(errFailedGetSecret))
  117. })
  118. })
  119. When("an error returns in get secret store", func() {
  120. BeforeEach(func() {
  121. client.GetStub = func(context context.Context, name types.NamespacedName, obj kubeclient.Object) error {
  122. switch v := obj.(type) {
  123. case *esapi.PushSecret:
  124. v.Spec.SecretStoreRefs = []esapi.PushSecretStoreRef{
  125. {Name: "a", Kind: "secretstore"},
  126. }
  127. }
  128. switch obj.(type) {
  129. case *v1beta1.SecretStore:
  130. return fmt.Errorf("BORK")
  131. default:
  132. return nil
  133. }
  134. }
  135. })
  136. It("returns the error", func() {
  137. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  138. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  139. Expect(err).To(MatchError("could not get SecretStore \"a\", BORK"))
  140. _, _, reason, message := recorder.EventArgsForCall(0)
  141. Expect(reason).To(Equal(esapi.ReasonErrored))
  142. Expect(message).To(Equal("could not get SecretStore \"a\", BORK"))
  143. })
  144. })
  145. When("an error returns in set secret to providers", func() {
  146. BeforeEach(func() {
  147. client.GetStub = func(context context.Context, name types.NamespacedName, obj kubeclient.Object) error {
  148. switch v := obj.(type) {
  149. case *esapi.PushSecret:
  150. v.Spec.SecretStoreRefs = []esapi.PushSecretStoreRef{
  151. {Name: "a", Kind: "secretstore"},
  152. }
  153. case *v1beta1.SecretStore:
  154. v.Kind = "PotatoStore"
  155. }
  156. switch obj.(type) {
  157. default:
  158. return nil
  159. }
  160. }
  161. })
  162. It("returns the error", func() {
  163. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  164. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  165. Expect(err).To(MatchError("could not start provider"))
  166. _, _, reason, message := recorder.EventArgsForCall(0)
  167. Expect(reason).To(Equal(esapi.ReasonErrored))
  168. Expect(message).To(Equal("set secret failed: could not start provider"))
  169. })
  170. })
  171. When("an object is not found", func() {
  172. BeforeEach(func() {
  173. client.GetReturns(statusErrorNotFound{})
  174. })
  175. It("returns an empty result without error", func() {
  176. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  177. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  178. Expect(err).NotTo(HaveOccurred())
  179. })
  180. })
  181. })
  182. Describe("#GetPushSecretCondition", func() {
  183. It("returns nil for empty secret sink status", func() {
  184. pushSecretStatus := new(esapi.PushSecretStatus)
  185. pushSecretConditionType := new(esapi.PushSecretConditionType)
  186. Expect(GetPushSecretCondition(*pushSecretStatus, *pushSecretConditionType)).To(BeNil())
  187. })
  188. It("returns correct condition for secret sink status", func() {
  189. pushSecretStatusCondition := esapi.PushSecretStatusCondition{Type: esapi.PushSecretReady}
  190. pushSecretStatus := esapi.PushSecretStatus{Conditions: []esapi.PushSecretStatusCondition{pushSecretStatusCondition}}
  191. pushSecretConditionType := esapi.PushSecretReady
  192. Expect(GetPushSecretCondition(pushSecretStatus, pushSecretConditionType)).To(Equal(&pushSecretStatusCondition))
  193. })
  194. })
  195. Describe("#SetPushSecretCondition", func() {
  196. It("appends a condition", func() {
  197. pushSecret := esapi.PushSecret{}
  198. pushSecretStatusCondition := esapi.PushSecretStatusCondition{}
  199. pushSecretStatus := esapi.PushSecretStatus{Conditions: []esapi.PushSecretStatusCondition{pushSecretStatusCondition}}
  200. expected := esapi.PushSecret{Status: pushSecretStatus}
  201. Expect(SetPushSecretCondition(pushSecret, pushSecretStatusCondition)).To(Equal(expected))
  202. })
  203. It("changes an existing condition", func() {
  204. conditionStatusTrue := v1.ConditionTrue
  205. pushSecretWithCondition := esapi.PushSecret{Status: esapi.PushSecretStatus{Conditions: []esapi.PushSecretStatusCondition{
  206. {
  207. Status: conditionStatusTrue,
  208. Type: esapi.PushSecretReady,
  209. },
  210. }},
  211. }
  212. pushSecretStatusConditionTrue := esapi.PushSecretStatusCondition{Status: conditionStatusTrue,
  213. Type: esapi.PushSecretReady,
  214. Message: "Update status",
  215. }
  216. got := SetPushSecretCondition(pushSecretWithCondition, pushSecretStatusConditionTrue)
  217. Expect(len(got.Status.Conditions)).To(Equal(1))
  218. Expect(got.Status.Conditions[0]).To(Equal(pushSecretStatusConditionTrue))
  219. })
  220. })
  221. Describe("#GetSecret", func() {
  222. It("returns a secret if it exists", func() {
  223. sink := esapi.PushSecret{
  224. Spec: esapi.PushSecretSpec{
  225. Selector: esapi.PushSecretSelector{
  226. Secret: esapi.PushSecretSecret{
  227. Name: "foo",
  228. },
  229. },
  230. },
  231. }
  232. sink.Namespace = "foobar"
  233. _, err := reconciler.GetSecret(context.TODO(), sink)
  234. Expect(err).To(BeNil())
  235. _, name, _ := client.GetArgsForCall(0)
  236. Expect(name.Namespace).To(Equal("foobar"))
  237. Expect(name.Name).To(Equal("foo"))
  238. })
  239. It("returns an error if it doesn't exist", func() {
  240. client.GetReturns(errors.New("secret not found"))
  241. _, err := reconciler.GetSecret(context.TODO(), esapi.PushSecret{})
  242. Expect(err).To(HaveOccurred())
  243. })
  244. })
  245. Describe("#GetSecretStore", func() {
  246. sink := esapi.PushSecret{
  247. Spec: esapi.PushSecretSpec{
  248. SecretStoreRefs: []esapi.PushSecretStoreRef{
  249. {
  250. Name: "foo",
  251. },
  252. },
  253. },
  254. }
  255. sink.Namespace = "bar"
  256. clusterSink := esapi.PushSecret{
  257. Spec: esapi.PushSecretSpec{
  258. SecretStoreRefs: []esapi.PushSecretStoreRef{
  259. {
  260. Name: "foo",
  261. Kind: "ClusterSecretStore",
  262. },
  263. },
  264. },
  265. }
  266. It("returns a secretstore if it exists", func() {
  267. _, err := reconciler.GetSecretStores(context.TODO(), sink)
  268. Expect(err).To(BeNil())
  269. Expect(client.GetCallCount()).To(Equal(1))
  270. _, name, store := client.GetArgsForCall(0)
  271. Expect(name.Namespace).To(Equal("bar"))
  272. Expect(name.Name).To(Equal("foo"))
  273. Expect(store).To(BeAssignableToTypeOf(&v1beta1.SecretStore{}))
  274. })
  275. It("returns an error if it doesn't exist", func() {
  276. client.GetReturns(errors.New("secretstore not found"))
  277. _, err := reconciler.GetSecretStores(context.TODO(), sink)
  278. Expect(err).To(HaveOccurred())
  279. })
  280. It("returns a clustersecretstore if it exists", func() {
  281. _, err := reconciler.GetSecretStores(context.TODO(), clusterSink)
  282. Expect(err).To(BeNil())
  283. Expect(client.GetCallCount()).To(Equal(1))
  284. _, name, store := client.GetArgsForCall(0)
  285. Expect(store).To(BeAssignableToTypeOf(&v1beta1.ClusterSecretStore{}))
  286. Expect(name.Name).To(Equal("foo"))
  287. })
  288. })
  289. Describe("#SetSecretToProviders", func() {
  290. val := "supersecret"
  291. secret := &v1.Secret{
  292. Data: map[string][]byte{
  293. "foo": []byte(val),
  294. },
  295. }
  296. sink := esapi.PushSecret{
  297. Spec: esapi.PushSecretSpec{
  298. SecretStoreRefs: []esapi.PushSecretStoreRef{
  299. {
  300. Name: "foo",
  301. },
  302. },
  303. Data: []esapi.PushSecretData{
  304. {
  305. Match: []esapi.PushSecretMatch{
  306. {
  307. SecretKey: "foo",
  308. RemoteRefs: []esapi.PushSecretRemoteRefs{
  309. {
  310. RemoteKey: "bar",
  311. },
  312. },
  313. },
  314. },
  315. },
  316. },
  317. },
  318. }
  319. sink.Namespace = "bar"
  320. secretStore := v1beta1.SecretStore{}
  321. stores := make([]v1beta1.GenericStore, 0)
  322. stores = append(stores, &secretStore)
  323. It("gets the provider and client and then sets the secret", func() {
  324. Expect(reconciler.SetSecretToProviders(context.TODO(), []v1beta1.GenericStore{}, sink, secret)).To(BeNil())
  325. })
  326. It("returns an error if it can't get a provider", func() {
  327. err := reconciler.SetSecretToProviders(context.TODO(), stores, sink, secret)
  328. Expect(err).To(HaveOccurred())
  329. Expect(err.Error()).To(Equal(errGetProviderFailed))
  330. })
  331. It("returns an if it can't get a client", func() {
  332. specWithProvider := v1beta1.SecretStoreSpec{
  333. Provider: &v1beta1.SecretStoreProvider{
  334. Fake: &v1beta1.FakeProvider{},
  335. },
  336. }
  337. fakeProvider.WithNew(func(context.Context, v1beta1.GenericStore, kubeclient.Client,
  338. string) (v1beta1.SecretsClient, error) {
  339. return nil, fmt.Errorf("Something went wrong")
  340. })
  341. secretStore = v1beta1.SecretStore{
  342. Spec: specWithProvider,
  343. }
  344. stores[0] = &secretStore
  345. err := reconciler.SetSecretToProviders(context.TODO(), stores, sink, secret)
  346. Expect(err).To(HaveOccurred())
  347. Expect(err.Error()).To(Equal(errGetSecretsClientFailed))
  348. })
  349. It("returns an error if set secret fails", func() {
  350. specWithProvider := v1beta1.SecretStoreSpec{
  351. Provider: &v1beta1.SecretStoreProvider{
  352. Fake: &v1beta1.FakeProvider{},
  353. },
  354. }
  355. fakeProvider.Reset()
  356. fakeProvider.WithSetSecret(fmt.Errorf("something went wrong"))
  357. secretStore = v1beta1.SecretStore{
  358. Spec: specWithProvider,
  359. }
  360. stores[0] = &secretStore
  361. err := reconciler.SetSecretToProviders(context.TODO(), stores, sink, secret)
  362. Expect(err).To(HaveOccurred())
  363. Expect(err.Error()).To(Equal(fmt.Sprintf(errSetSecretFailed, "foo", "", "something went wrong")))
  364. })
  365. })
  366. // Secrets should not be updated when refreshInterval is not equals to zero
  367. // RequeueInterval shouldn't be updated if spec.refreshInterval is zero
  368. // Checking if requeue interval is zero does an error occur
  369. // requeue interval not zero
  370. //
  371. //Describe("#")
  372. })
  373. func init() {
  374. fakeProvider = fake.New()
  375. v1beta1.ForceRegister(fakeProvider, &v1beta1.SecretStoreProvider{
  376. Fake: &v1beta1.FakeProvider{},
  377. })
  378. }