pushsecret_controller_test.go 13 KB

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