pushsecret_controller_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. )
  35. BeforeEach(func() {
  36. client = new(fakes.Client)
  37. reconciler = &Reconciler{client, logr.Discard(), nil, nil, 0, ""}
  38. })
  39. Describe("#Reconcile", func() {
  40. var (
  41. statusWriter *fakes.StatusWriter
  42. )
  43. BeforeEach(func() {
  44. statusWriter = new(fakes.StatusWriter)
  45. client.StatusReturns(statusWriter)
  46. })
  47. It("succeeds", func() {
  48. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  49. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  50. Expect(err).NotTo(HaveOccurred())
  51. Expect(client.GetCallCount()).To(Equal(2))
  52. Expect(client.StatusCallCount()).To(Equal(1))
  53. _, gotNamespacedName, _ := client.GetArgsForCall(0)
  54. Expect(gotNamespacedName).To(Equal(namspacedName))
  55. Expect(statusWriter.PatchCallCount()).To(Equal(1))
  56. _, _, patch, _ := statusWriter.PatchArgsForCall(0)
  57. Expect(patch.Type()).To(Equal(types.MergePatchType))
  58. })
  59. When("an error returns in get", func() {
  60. BeforeEach(func() {
  61. client.GetReturns(errors.New("UnknownError"))
  62. })
  63. It("returns the error", func() {
  64. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  65. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  66. Expect(err).To(MatchError("get resource: UnknownError"))
  67. Expect(client.GetCallCount()).To(Equal(1))
  68. Expect(client.StatusCallCount()).To(Equal(0))
  69. })
  70. })
  71. When("an object is not found", func() {
  72. BeforeEach(func() {
  73. client.GetReturns(statusErrorNotFound{})
  74. })
  75. It("returns an empty result without error", func() {
  76. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  77. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  78. Expect(err).NotTo(HaveOccurred())
  79. })
  80. })
  81. })
  82. Describe("#GetPushSecretCondition", func() {
  83. It("returns nil for empty secret sink status", func() {
  84. pushSecretStatus := new(esapi.PushSecretStatus)
  85. pushSecretConditionType := new(esapi.PushSecretConditionType)
  86. Expect(GetPushSecretCondition(*pushSecretStatus, *pushSecretConditionType)).To(BeNil())
  87. })
  88. It("returns correct condition for secret sink status", func() {
  89. pushSecretStatusCondition := esapi.PushSecretStatusCondition{Type: esapi.PushSecretReady}
  90. pushSecretStatus := esapi.PushSecretStatus{Conditions: []esapi.PushSecretStatusCondition{pushSecretStatusCondition}}
  91. pushSecretConditionType := esapi.PushSecretReady
  92. Expect(GetPushSecretCondition(pushSecretStatus, pushSecretConditionType)).To(Equal(&pushSecretStatusCondition))
  93. })
  94. })
  95. Describe("#SetPushSecretCondition", func() {
  96. It("appends a condition", func() {
  97. pushSecret := esapi.PushSecret{}
  98. pushSecretStatusCondition := esapi.PushSecretStatusCondition{}
  99. pushSecretStatus := esapi.PushSecretStatus{Conditions: []esapi.PushSecretStatusCondition{pushSecretStatusCondition}}
  100. expected := esapi.PushSecret{Status: pushSecretStatus}
  101. Expect(SetPushSecretCondition(pushSecret, pushSecretStatusCondition)).To(Equal(expected))
  102. })
  103. It("changes an existing condition", func() {
  104. conditionStatusTrue := v1.ConditionTrue
  105. pushSecretWithCondition := esapi.PushSecret{Status: esapi.PushSecretStatus{Conditions: []esapi.PushSecretStatusCondition{
  106. {
  107. Status: conditionStatusTrue,
  108. Type: esapi.PushSecretReady,
  109. },
  110. }},
  111. }
  112. pushSecretStatusConditionTrue := esapi.PushSecretStatusCondition{Status: conditionStatusTrue,
  113. Type: esapi.PushSecretReady,
  114. Message: "Update status",
  115. }
  116. got := SetPushSecretCondition(pushSecretWithCondition, pushSecretStatusConditionTrue)
  117. Expect(len(got.Status.Conditions)).To(Equal(1))
  118. Expect(got.Status.Conditions[0]).To(Equal(pushSecretStatusConditionTrue))
  119. })
  120. })
  121. Describe("#GetSecret", func() {
  122. It("returns a secret if it exists", func() {
  123. sink := esapi.PushSecret{
  124. Spec: esapi.PushSecretSpec{
  125. Selector: esapi.PushSecretSelector{
  126. Secret: esapi.PushSecretSecret{
  127. Name: "foo",
  128. },
  129. },
  130. },
  131. }
  132. sink.Namespace = "foobar"
  133. _, err := reconciler.GetSecret(context.TODO(), sink)
  134. Expect(err).To(BeNil())
  135. _, name, _ := client.GetArgsForCall(0)
  136. Expect(name.Namespace).To(Equal("foobar"))
  137. Expect(name.Name).To(Equal("foo"))
  138. })
  139. It("returns an error if it doesn't exist", func() {
  140. client.GetReturns(errors.New("secret not found"))
  141. _, err := reconciler.GetSecret(context.TODO(), esapi.PushSecret{})
  142. Expect(err).To(HaveOccurred())
  143. })
  144. })
  145. Describe("#GetSecretStore", func() {
  146. sink := esapi.PushSecret{
  147. Spec: esapi.PushSecretSpec{
  148. SecretStoreRefs: []esapi.PushSecretStoreRef{
  149. {
  150. Name: "foo",
  151. },
  152. },
  153. },
  154. }
  155. sink.Namespace = "bar"
  156. clusterSink := esapi.PushSecret{
  157. Spec: esapi.PushSecretSpec{
  158. SecretStoreRefs: []esapi.PushSecretStoreRef{
  159. {
  160. Name: "foo",
  161. Kind: "ClusterSecretStore",
  162. },
  163. },
  164. },
  165. }
  166. It("returns a secretstore if it exists", func() {
  167. _, err := reconciler.GetSecretStores(context.TODO(), sink)
  168. Expect(err).To(BeNil())
  169. Expect(client.GetCallCount()).To(Equal(1))
  170. _, name, store := client.GetArgsForCall(0)
  171. Expect(name.Namespace).To(Equal("bar"))
  172. Expect(name.Name).To(Equal("foo"))
  173. Expect(store).To(BeAssignableToTypeOf(&v1beta1.SecretStore{}))
  174. })
  175. It("returns an error if it doesn't exist", func() {
  176. client.GetReturns(errors.New("secretstore not found"))
  177. _, err := reconciler.GetSecretStores(context.TODO(), sink)
  178. Expect(err).To(HaveOccurred())
  179. })
  180. It("returns a clustersecretstore if it exists", func() {
  181. _, err := reconciler.GetSecretStores(context.TODO(), clusterSink)
  182. Expect(err).To(BeNil())
  183. Expect(client.GetCallCount()).To(Equal(1))
  184. _, name, store := client.GetArgsForCall(0)
  185. Expect(store).To(BeAssignableToTypeOf(&v1beta1.ClusterSecretStore{}))
  186. Expect(name.Name).To(Equal("foo"))
  187. })
  188. })
  189. Describe("#SetSecretToProviders", func() {
  190. val := "supersecret"
  191. secret := &v1.Secret{
  192. Data: map[string][]byte{
  193. "foo": []byte(val),
  194. },
  195. }
  196. sink := esapi.PushSecret{
  197. Spec: esapi.PushSecretSpec{
  198. SecretStoreRefs: []esapi.PushSecretStoreRef{
  199. {
  200. Name: "foo",
  201. },
  202. },
  203. Data: []esapi.PushSecretData{
  204. {
  205. Match: []esapi.PushSecretMatch{
  206. {
  207. SecretKey: "foo",
  208. RemoteRefs: []esapi.PushSecretRemoteRefs{
  209. {
  210. RemoteKey: "bar",
  211. },
  212. },
  213. },
  214. },
  215. },
  216. },
  217. },
  218. }
  219. sink.Namespace = "bar"
  220. secretStore := v1beta1.SecretStore{}
  221. stores := make([]v1beta1.GenericStore, 0)
  222. stores = append(stores, &secretStore)
  223. It("gets the provider and client and then sets the secret", func() {
  224. Expect(reconciler.SetSecretToProviders(context.TODO(), []v1beta1.GenericStore{}, sink, secret)).To(BeNil())
  225. })
  226. It("returns an error if it can't get a provider", func() {
  227. err := reconciler.SetSecretToProviders(context.TODO(), stores, sink, secret)
  228. Expect(err).To(HaveOccurred())
  229. Expect(err.Error()).To(Equal(errGetProviderFailed))
  230. })
  231. It("returns an if it can't get a client", func() {
  232. specWithProvider := v1beta1.SecretStoreSpec{
  233. Provider: &v1beta1.SecretStoreProvider{
  234. Fake: &v1beta1.FakeProvider{},
  235. },
  236. }
  237. fakeProvider.WithNew(func(context.Context, v1beta1.GenericStore, kubeclient.Client,
  238. string) (v1beta1.SecretsClient, error) {
  239. return nil, fmt.Errorf("Something went wrong")
  240. })
  241. secretStore = v1beta1.SecretStore{
  242. Spec: specWithProvider,
  243. }
  244. stores[0] = &secretStore
  245. err := reconciler.SetSecretToProviders(context.TODO(), stores, sink, secret)
  246. Expect(err).To(HaveOccurred())
  247. Expect(err.Error()).To(Equal(errGetSecretsClientFailed))
  248. })
  249. It("returns an error if set secret fails", func() {
  250. specWithProvider := v1beta1.SecretStoreSpec{
  251. Provider: &v1beta1.SecretStoreProvider{
  252. Fake: &v1beta1.FakeProvider{},
  253. },
  254. }
  255. fakeProvider.Reset()
  256. fakeProvider.WithSetSecret(fmt.Errorf("something went wrong"))
  257. secretStore = v1beta1.SecretStore{
  258. Spec: specWithProvider,
  259. }
  260. stores[0] = &secretStore
  261. err := reconciler.SetSecretToProviders(context.TODO(), stores, sink, secret)
  262. Expect(err).To(HaveOccurred())
  263. Expect(err.Error()).To(Equal(fmt.Sprintf(errSetSecretFailed, "foo", "", "something went wrong")))
  264. })
  265. })
  266. })
  267. func init() {
  268. fakeProvider = fake.New()
  269. v1beta1.ForceRegister(fakeProvider, &v1beta1.SecretStoreProvider{
  270. Fake: &v1beta1.FakeProvider{},
  271. })
  272. }