pushsecret_controller_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. "bytes"
  15. "context"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "time"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. v1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. v1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  28. ctest "github.com/external-secrets/external-secrets/pkg/controllers/commontest"
  29. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  30. )
  31. var (
  32. fakeProvider *fake.Client
  33. timeout = time.Second * 10
  34. interval = time.Millisecond * 250
  35. )
  36. type testCase struct {
  37. store v1beta1.GenericStore
  38. pushsecret *v1alpha1.PushSecret
  39. secret *v1.Secret
  40. assert func(pushsecret *v1alpha1.PushSecret, secret *v1.Secret) bool
  41. }
  42. func init() {
  43. fakeProvider = fake.New()
  44. v1beta1.ForceRegister(fakeProvider, &v1beta1.SecretStoreProvider{
  45. Fake: &v1beta1.FakeProvider{},
  46. })
  47. }
  48. func checkCondition(status v1alpha1.PushSecretStatus, cond v1alpha1.PushSecretStatusCondition) bool {
  49. for _, condition := range status.Conditions {
  50. if condition.Message == cond.Message &&
  51. condition.Reason == cond.Reason &&
  52. condition.Status == cond.Status &&
  53. condition.Type == cond.Type {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. type testTweaks func(*testCase)
  60. var _ = Describe("ExternalSecret controller", func() {
  61. const (
  62. PushSecretName = "test-es"
  63. PushSecretFQDN = "externalsecrets.external-secrets.io/test-es"
  64. PushSecretStore = "test-store"
  65. SecretName = "test-secret"
  66. PushSecretTargetSecretName = "test-secret"
  67. FakeManager = "fake.manager"
  68. expectedSecretVal = "SOMEVALUE was templated"
  69. targetPropObj = "{{ .targetProperty | toString | upper }} was templated"
  70. FooValue = "map-foo-value"
  71. BarValue = "map-bar-value"
  72. )
  73. var PushSecretNamespace string
  74. // if we are in debug and need to increase the timeout for testing, we can do so by using an env var
  75. if customTimeout := os.Getenv("TEST_CUSTOM_TIMEOUT_SEC"); customTimeout != "" {
  76. if t, err := strconv.Atoi(customTimeout); err == nil {
  77. timeout = time.Second * time.Duration(t)
  78. }
  79. }
  80. BeforeEach(func() {
  81. var err error
  82. PushSecretNamespace, err = ctest.CreateNamespace("test-ns", k8sClient)
  83. Expect(err).ToNot(HaveOccurred())
  84. fakeProvider.Reset()
  85. })
  86. AfterEach(func() {
  87. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  88. ObjectMeta: metav1.ObjectMeta{
  89. Name: PushSecretNamespace,
  90. },
  91. })).To(Succeed())
  92. k8sClient.Delete(context.Background(), &v1beta1.SecretStore{
  93. ObjectMeta: metav1.ObjectMeta{
  94. Name: PushSecretStore,
  95. Namespace: PushSecretNamespace,
  96. },
  97. })
  98. k8sClient.Delete(context.Background(), &v1beta1.ClusterSecretStore{
  99. ObjectMeta: metav1.ObjectMeta{
  100. Name: PushSecretStore,
  101. },
  102. })
  103. k8sClient.Delete(context.Background(), &v1.Secret{
  104. ObjectMeta: metav1.ObjectMeta{
  105. Name: SecretName,
  106. Namespace: PushSecretNamespace,
  107. },
  108. })
  109. })
  110. makeDefaultTestcase := func() *testCase {
  111. return &testCase{
  112. pushsecret: &v1alpha1.PushSecret{
  113. ObjectMeta: metav1.ObjectMeta{
  114. Name: PushSecretName,
  115. Namespace: PushSecretNamespace,
  116. },
  117. Spec: v1alpha1.PushSecretSpec{
  118. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  119. {
  120. Name: PushSecretStore,
  121. Kind: "SecretStore",
  122. },
  123. },
  124. Selector: v1alpha1.PushSecretSelector{
  125. Secret: v1alpha1.PushSecretSecret{
  126. Name: SecretName,
  127. },
  128. },
  129. Data: []v1alpha1.PushSecretData{
  130. {
  131. Match: v1alpha1.PushSecretMatch{
  132. SecretKey: "key",
  133. RemoteRef: v1alpha1.PushSecretRemoteRef{
  134. RemoteKey: "path/to/key",
  135. },
  136. },
  137. },
  138. },
  139. },
  140. },
  141. secret: &v1.Secret{
  142. ObjectMeta: metav1.ObjectMeta{
  143. Name: SecretName,
  144. Namespace: PushSecretNamespace,
  145. },
  146. Data: map[string][]byte{
  147. "key": []byte("value"),
  148. },
  149. },
  150. store: &v1beta1.SecretStore{
  151. ObjectMeta: metav1.ObjectMeta{
  152. Name: PushSecretStore,
  153. Namespace: PushSecretNamespace,
  154. },
  155. Spec: v1beta1.SecretStoreSpec{
  156. Provider: &v1beta1.SecretStoreProvider{
  157. Fake: &v1beta1.FakeProvider{
  158. Data: []v1beta1.FakeProviderData{},
  159. },
  160. },
  161. },
  162. },
  163. }
  164. }
  165. // if target Secret name is not specified it should use the ExternalSecret name.
  166. syncSuccessfully := func(tc *testCase) {
  167. fakeProvider.SetSecretFn = func() error {
  168. return nil
  169. }
  170. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  171. secretValue := secret.Data["key"]
  172. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  173. expected := v1alpha1.PushSecretStatusCondition{
  174. Type: v1alpha1.PushSecretReady,
  175. Status: v1.ConditionTrue,
  176. Reason: v1alpha1.ReasonSynced,
  177. Message: "PushSecret synced successfully",
  178. }
  179. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  180. }
  181. }
  182. // if target Secret name is not specified it should use the ExternalSecret name.
  183. syncMatchingLabels := func(tc *testCase) {
  184. fakeProvider.SetSecretFn = func() error {
  185. return nil
  186. }
  187. tc.pushsecret = &v1alpha1.PushSecret{
  188. ObjectMeta: metav1.ObjectMeta{
  189. Name: PushSecretName,
  190. Namespace: PushSecretNamespace,
  191. },
  192. Spec: v1alpha1.PushSecretSpec{
  193. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  194. {
  195. LabelSelector: &metav1.LabelSelector{
  196. MatchLabels: map[string]string{
  197. "foo": "bar",
  198. },
  199. },
  200. Kind: "SecretStore",
  201. },
  202. },
  203. Selector: v1alpha1.PushSecretSelector{
  204. Secret: v1alpha1.PushSecretSecret{
  205. Name: SecretName,
  206. },
  207. },
  208. Data: []v1alpha1.PushSecretData{
  209. {
  210. Match: v1alpha1.PushSecretMatch{
  211. SecretKey: "key",
  212. RemoteRef: v1alpha1.PushSecretRemoteRef{
  213. RemoteKey: "path/to/key",
  214. },
  215. },
  216. },
  217. },
  218. },
  219. }
  220. tc.store = &v1beta1.SecretStore{
  221. ObjectMeta: metav1.ObjectMeta{
  222. Name: PushSecretStore,
  223. Namespace: PushSecretNamespace,
  224. Labels: map[string]string{
  225. "foo": "bar",
  226. },
  227. },
  228. Spec: v1beta1.SecretStoreSpec{
  229. Provider: &v1beta1.SecretStoreProvider{
  230. Fake: &v1beta1.FakeProvider{
  231. Data: []v1beta1.FakeProviderData{},
  232. },
  233. },
  234. },
  235. }
  236. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  237. secretValue := secret.Data["key"]
  238. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  239. expected := v1alpha1.PushSecretStatusCondition{
  240. Type: v1alpha1.PushSecretReady,
  241. Status: v1.ConditionTrue,
  242. Reason: v1alpha1.ReasonSynced,
  243. Message: "PushSecret synced successfully",
  244. }
  245. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  246. }
  247. }
  248. syncWithClusterStore := func(tc *testCase) {
  249. fakeProvider.SetSecretFn = func() error {
  250. return nil
  251. }
  252. tc.store = &v1beta1.ClusterSecretStore{
  253. ObjectMeta: metav1.ObjectMeta{
  254. Name: PushSecretStore,
  255. },
  256. Spec: v1beta1.SecretStoreSpec{
  257. Provider: &v1beta1.SecretStoreProvider{
  258. Fake: &v1beta1.FakeProvider{
  259. Data: []v1beta1.FakeProviderData{},
  260. },
  261. },
  262. },
  263. }
  264. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  265. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  266. secretValue := secret.Data["key"]
  267. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  268. expected := v1alpha1.PushSecretStatusCondition{
  269. Type: v1alpha1.PushSecretReady,
  270. Status: v1.ConditionTrue,
  271. Reason: v1alpha1.ReasonSynced,
  272. Message: "PushSecret synced successfully",
  273. }
  274. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  275. }
  276. }
  277. // if target Secret name is not specified it should use the ExternalSecret name.
  278. syncWithClusterStoreMatchingLabels := func(tc *testCase) {
  279. fakeProvider.SetSecretFn = func() error {
  280. return nil
  281. }
  282. tc.pushsecret = &v1alpha1.PushSecret{
  283. ObjectMeta: metav1.ObjectMeta{
  284. Name: PushSecretName,
  285. Namespace: PushSecretNamespace,
  286. },
  287. Spec: v1alpha1.PushSecretSpec{
  288. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  289. {
  290. LabelSelector: &metav1.LabelSelector{
  291. MatchLabels: map[string]string{
  292. "foo": "bar",
  293. },
  294. },
  295. Kind: "ClusterSecretStore",
  296. },
  297. },
  298. Selector: v1alpha1.PushSecretSelector{
  299. Secret: v1alpha1.PushSecretSecret{
  300. Name: SecretName,
  301. },
  302. },
  303. Data: []v1alpha1.PushSecretData{
  304. {
  305. Match: v1alpha1.PushSecretMatch{
  306. SecretKey: "key",
  307. RemoteRef: v1alpha1.PushSecretRemoteRef{
  308. RemoteKey: "path/to/key",
  309. },
  310. },
  311. },
  312. },
  313. },
  314. }
  315. tc.store = &v1beta1.ClusterSecretStore{
  316. ObjectMeta: metav1.ObjectMeta{
  317. Name: PushSecretStore,
  318. Labels: map[string]string{
  319. "foo": "bar",
  320. },
  321. },
  322. Spec: v1beta1.SecretStoreSpec{
  323. Provider: &v1beta1.SecretStoreProvider{
  324. Fake: &v1beta1.FakeProvider{
  325. Data: []v1beta1.FakeProviderData{},
  326. },
  327. },
  328. },
  329. }
  330. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  331. secretValue := secret.Data["key"]
  332. providerValue := fakeProvider.SetSecretArgs[ps.Spec.Data[0].Match.RemoteRef.RemoteKey].Value
  333. expected := v1alpha1.PushSecretStatusCondition{
  334. Type: v1alpha1.PushSecretReady,
  335. Status: v1.ConditionTrue,
  336. Reason: v1alpha1.ReasonSynced,
  337. Message: "PushSecret synced successfully",
  338. }
  339. return bytes.Equal(secretValue, providerValue) && checkCondition(ps.Status, expected)
  340. }
  341. }
  342. // if target Secret name is not specified it should use the ExternalSecret name.
  343. failNoSecret := func(tc *testCase) {
  344. fakeProvider.SetSecretFn = func() error {
  345. return nil
  346. }
  347. tc.secret = nil
  348. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  349. expected := v1alpha1.PushSecretStatusCondition{
  350. Type: v1alpha1.PushSecretReady,
  351. Status: v1.ConditionFalse,
  352. Reason: v1alpha1.ReasonErrored,
  353. Message: "could not get source secret",
  354. }
  355. return checkCondition(ps.Status, expected)
  356. }
  357. }
  358. // if target Secret name is not specified it should use the ExternalSecret name.
  359. failNoSecretKey := func(tc *testCase) {
  360. fakeProvider.SetSecretFn = func() error {
  361. return nil
  362. }
  363. tc.pushsecret.Spec.Data[0].Match.SecretKey = "unexisting"
  364. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  365. expected := v1alpha1.PushSecretStatusCondition{
  366. Type: v1alpha1.PushSecretReady,
  367. Status: v1.ConditionFalse,
  368. Reason: v1alpha1.ReasonErrored,
  369. Message: "set secret failed: secret key unexisting does not exist",
  370. }
  371. return checkCondition(ps.Status, expected)
  372. }
  373. }
  374. // if target Secret name is not specified it should use the ExternalSecret name.
  375. failNoSecretStore := func(tc *testCase) {
  376. fakeProvider.SetSecretFn = func() error {
  377. return nil
  378. }
  379. tc.store = nil
  380. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  381. expected := v1alpha1.PushSecretStatusCondition{
  382. Type: v1alpha1.PushSecretReady,
  383. Status: v1.ConditionFalse,
  384. Reason: v1alpha1.ReasonErrored,
  385. Message: "could not get SecretStore \"test-store\", secretstores.external-secrets.io \"test-store\" not found",
  386. }
  387. return checkCondition(ps.Status, expected)
  388. }
  389. }
  390. // if target Secret name is not specified it should use the ExternalSecret name.
  391. failNoClusterStore := func(tc *testCase) {
  392. fakeProvider.SetSecretFn = func() error {
  393. return nil
  394. }
  395. tc.store = nil
  396. tc.pushsecret.Spec.SecretStoreRefs[0].Kind = "ClusterSecretStore"
  397. tc.pushsecret.Spec.SecretStoreRefs[0].Name = "unexisting"
  398. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  399. expected := v1alpha1.PushSecretStatusCondition{
  400. Type: v1alpha1.PushSecretReady,
  401. Status: v1.ConditionFalse,
  402. Reason: v1alpha1.ReasonErrored,
  403. Message: "could not get ClusterSecretStore \"unexisting\", clustersecretstores.external-secrets.io \"unexisting\" not found",
  404. }
  405. return checkCondition(ps.Status, expected)
  406. }
  407. } // if target Secret name is not specified it should use the ExternalSecret name.
  408. setSecretFail := func(tc *testCase) {
  409. fakeProvider.SetSecretFn = func() error {
  410. return fmt.Errorf("boom")
  411. }
  412. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  413. expected := v1alpha1.PushSecretStatusCondition{
  414. Type: v1alpha1.PushSecretReady,
  415. Status: v1.ConditionFalse,
  416. Reason: v1alpha1.ReasonErrored,
  417. Message: "set secret failed: could not write remote ref key to target secretstore test-store: boom",
  418. }
  419. return checkCondition(ps.Status, expected)
  420. }
  421. }
  422. // if target Secret name is not specified it should use the ExternalSecret name.
  423. newClientFail := func(tc *testCase) {
  424. fakeProvider.NewFn = func(context.Context, v1beta1.GenericStore, client.Client, string) (v1beta1.SecretsClient, error) {
  425. return nil, fmt.Errorf("boom")
  426. }
  427. tc.assert = func(ps *v1alpha1.PushSecret, secret *v1.Secret) bool {
  428. expected := v1alpha1.PushSecretStatusCondition{
  429. Type: v1alpha1.PushSecretReady,
  430. Status: v1.ConditionFalse,
  431. Reason: v1alpha1.ReasonErrored,
  432. Message: "set secret failed: could not get secrets client for store test-store: could not start secrets client",
  433. }
  434. return checkCondition(ps.Status, expected)
  435. }
  436. }
  437. DescribeTable("When reconciling a PushSecret",
  438. func(tweaks ...testTweaks) {
  439. tc := makeDefaultTestcase()
  440. for _, tweak := range tweaks {
  441. tweak(tc)
  442. }
  443. ctx := context.Background()
  444. By("creating a secret store, secret and pushsecret")
  445. if tc.store != nil {
  446. Expect(k8sClient.Create(ctx, tc.store)).To(Succeed())
  447. }
  448. if tc.secret != nil {
  449. Expect(k8sClient.Create(ctx, tc.secret)).To(Succeed())
  450. }
  451. if tc.pushsecret != nil {
  452. Expect(k8sClient.Create(ctx, tc.pushsecret)).Should(Succeed())
  453. }
  454. time.Sleep(2 * time.Second)
  455. psKey := types.NamespacedName{Name: PushSecretName, Namespace: PushSecretNamespace}
  456. createdPS := &v1alpha1.PushSecret{}
  457. By("checking the pushSecret condition")
  458. Eventually(func() bool {
  459. err := k8sClient.Get(ctx, psKey, createdPS)
  460. if err != nil {
  461. return false
  462. }
  463. return tc.assert(createdPS, tc.secret)
  464. }, timeout, interval).Should(BeTrue())
  465. // this must be optional so we can test faulty es configuration
  466. },
  467. Entry("should sync", syncSuccessfully),
  468. Entry("should sync to stores matching labels", syncMatchingLabels),
  469. Entry("should sync with ClusterStore", syncWithClusterStore),
  470. Entry("should sync with ClusterStore matching labels", syncWithClusterStoreMatchingLabels),
  471. Entry("should fail if Secret is not created", failNoSecret),
  472. Entry("should fail if Secret Key does not exist", failNoSecretKey),
  473. Entry("should fail if SetSecret fails", setSecretFail),
  474. Entry("should fail if no valid SecretStore", failNoSecretStore),
  475. Entry("should fail if no valid ClusterSecretStore", failNoClusterStore),
  476. Entry("should fail if NewClient fails", newClientFail),
  477. )
  478. })