client_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 kubernetes
  13. import (
  14. "context"
  15. "encoding/base64"
  16. "errors"
  17. "reflect"
  18. "testing"
  19. "github.com/google/go-cmp/cmp"
  20. "github.com/stretchr/testify/assert"
  21. v1 "k8s.io/api/core/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/runtime/schema"
  25. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  26. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. )
  28. const (
  29. errSomethingWentWrong = "Something went wrong"
  30. )
  31. type fakeClient struct {
  32. t *testing.T
  33. secretMap map[string]*v1.Secret
  34. expectedListOptions metav1.ListOptions
  35. err error
  36. }
  37. func (fk *fakeClient) Get(_ context.Context, name string, _ metav1.GetOptions) (*v1.Secret, error) {
  38. if fk.err != nil {
  39. return nil, fk.err
  40. }
  41. secret, ok := fk.secretMap[name]
  42. if !ok {
  43. return nil, apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Secret"}, "secret")
  44. }
  45. // return inmutable to simulate external system and avoid accidental side effects
  46. sCopy := secret.DeepCopy()
  47. // update operation requires to relate names
  48. sCopy.Name = name
  49. return sCopy, nil
  50. }
  51. func (fk *fakeClient) List(_ context.Context, opts metav1.ListOptions) (*v1.SecretList, error) {
  52. assert.Equal(fk.t, fk.expectedListOptions, opts)
  53. list := &v1.SecretList{}
  54. for _, v := range fk.secretMap {
  55. list.Items = append(list.Items, *v)
  56. }
  57. return list, nil
  58. }
  59. func (fk *fakeClient) Delete(_ context.Context, name string, _ metav1.DeleteOptions) error {
  60. if fk.err != nil {
  61. return fk.err
  62. }
  63. _, ok := fk.secretMap[name]
  64. if !ok {
  65. return apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Secret"}, "secret")
  66. }
  67. delete(fk.secretMap, name)
  68. return nil
  69. }
  70. func (fk *fakeClient) Create(_ context.Context, secret *v1.Secret, _ metav1.CreateOptions) (*v1.Secret, error) {
  71. s := &v1.Secret{
  72. Data: secret.Data,
  73. }
  74. fk.secretMap[secret.Name] = s
  75. return s, nil
  76. }
  77. func (fk *fakeClient) Update(_ context.Context, secret *v1.Secret, _ metav1.UpdateOptions) (*v1.Secret, error) {
  78. s, ok := fk.secretMap[secret.Name]
  79. if !ok {
  80. return nil, errors.New("error while updating secret")
  81. }
  82. s.Data = secret.Data
  83. return s, nil
  84. }
  85. var binaryTestData = []byte{0x00, 0xff, 0x00, 0xff, 0xac, 0xab, 0x28, 0x21}
  86. func TestGetSecret(t *testing.T) {
  87. type fields struct {
  88. Client KClient
  89. ReviewClient RClient
  90. Namespace string
  91. }
  92. tests := []struct {
  93. name string
  94. fields fields
  95. ref esv1beta1.ExternalSecretDataRemoteRef
  96. want []byte
  97. wantErr bool
  98. }{
  99. {
  100. name: "secretNotFound",
  101. fields: fields{
  102. Client: &fakeClient{
  103. t: t,
  104. secretMap: map[string]*v1.Secret{
  105. "mysec": {
  106. Data: map[string][]byte{
  107. "token": []byte(`foobar`),
  108. },
  109. },
  110. },
  111. err: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Secret"}, "secret"),
  112. },
  113. Namespace: "default",
  114. },
  115. ref: esv1beta1.ExternalSecretDataRemoteRef{
  116. Key: "mysec",
  117. Property: "token",
  118. },
  119. wantErr: true,
  120. },
  121. {
  122. name: "err GetSecretMap",
  123. fields: fields{
  124. Client: &fakeClient{
  125. t: t,
  126. secretMap: map[string]*v1.Secret{},
  127. },
  128. Namespace: "default",
  129. },
  130. ref: esv1beta1.ExternalSecretDataRemoteRef{
  131. Key: "mysec",
  132. Property: "token",
  133. },
  134. wantErr: true,
  135. },
  136. {
  137. name: "wrong property",
  138. fields: fields{
  139. Client: &fakeClient{
  140. t: t,
  141. secretMap: map[string]*v1.Secret{
  142. "mysec": {
  143. Data: map[string][]byte{
  144. "token": []byte(`foobar`),
  145. },
  146. },
  147. },
  148. },
  149. Namespace: "default",
  150. },
  151. ref: esv1beta1.ExternalSecretDataRemoteRef{
  152. Key: "mysec",
  153. Property: "not-the-token",
  154. },
  155. wantErr: true,
  156. },
  157. {
  158. name: "successful case",
  159. fields: fields{
  160. Client: &fakeClient{
  161. t: t,
  162. secretMap: map[string]*v1.Secret{
  163. "mysec": {
  164. Data: map[string][]byte{
  165. "token": []byte(`foobar`),
  166. },
  167. },
  168. },
  169. },
  170. Namespace: "default",
  171. },
  172. ref: esv1beta1.ExternalSecretDataRemoteRef{
  173. Key: "mysec",
  174. Property: "token",
  175. },
  176. want: []byte(`foobar`),
  177. },
  178. {
  179. name: "successful case with html chars",
  180. fields: fields{
  181. Client: &fakeClient{
  182. t: t,
  183. secretMap: map[string]*v1.Secret{
  184. "mysec": {
  185. Data: map[string][]byte{
  186. "html": []byte(`<foobar>`),
  187. },
  188. },
  189. },
  190. },
  191. Namespace: "default",
  192. },
  193. ref: esv1beta1.ExternalSecretDataRemoteRef{
  194. Key: "mysec",
  195. },
  196. want: []byte(`{"html":"<foobar>"}`),
  197. },
  198. {
  199. name: "successful case metadata with html special chars and without property",
  200. fields: fields{
  201. Client: &fakeClient{
  202. t: t,
  203. secretMap: map[string]*v1.Secret{
  204. "mysec": {
  205. ObjectMeta: metav1.ObjectMeta{
  206. Annotations: map[string]string{"date": "today"},
  207. Labels: map[string]string{"dev": "<seb>"},
  208. },
  209. },
  210. },
  211. },
  212. Namespace: "default",
  213. },
  214. ref: esv1beta1.ExternalSecretDataRemoteRef{
  215. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  216. Key: "mysec",
  217. },
  218. want: []byte(`{"annotations":{"date":"today"},"labels":{"dev":"<seb>"}}`),
  219. },
  220. {
  221. name: "successful case with binary data",
  222. fields: fields{
  223. Client: &fakeClient{
  224. t: t,
  225. secretMap: map[string]*v1.Secret{
  226. "mysec": {
  227. Data: map[string][]byte{
  228. "bindata": binaryTestData,
  229. },
  230. },
  231. },
  232. },
  233. Namespace: "default",
  234. },
  235. ref: esv1beta1.ExternalSecretDataRemoteRef{
  236. Key: "mysec",
  237. Property: "bindata",
  238. },
  239. want: []byte(base64.StdEncoding.EncodeToString(binaryTestData)),
  240. },
  241. {
  242. name: "successful case without property",
  243. fields: fields{
  244. Client: &fakeClient{
  245. t: t,
  246. secretMap: map[string]*v1.Secret{
  247. "mysec": {
  248. Data: map[string][]byte{
  249. "token": []byte(`foobar`),
  250. },
  251. },
  252. },
  253. },
  254. Namespace: "default",
  255. },
  256. ref: esv1beta1.ExternalSecretDataRemoteRef{
  257. Key: "mysec",
  258. },
  259. want: []byte(`{"token":"foobar"}`),
  260. },
  261. {
  262. name: "successful case metadata without property",
  263. fields: fields{
  264. Client: &fakeClient{
  265. t: t,
  266. secretMap: map[string]*v1.Secret{
  267. "mysec": {
  268. ObjectMeta: metav1.ObjectMeta{
  269. Annotations: map[string]string{"date": "today"},
  270. Labels: map[string]string{"dev": "seb"},
  271. },
  272. },
  273. },
  274. },
  275. Namespace: "default",
  276. },
  277. ref: esv1beta1.ExternalSecretDataRemoteRef{
  278. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  279. Key: "mysec",
  280. },
  281. want: []byte(`{"annotations":{"date":"today"},"labels":{"dev":"seb"}}`),
  282. },
  283. {
  284. name: "successful case metadata with single property",
  285. fields: fields{
  286. Client: &fakeClient{
  287. t: t,
  288. secretMap: map[string]*v1.Secret{
  289. "mysec": {
  290. ObjectMeta: metav1.ObjectMeta{
  291. Annotations: map[string]string{"date": "today"},
  292. Labels: map[string]string{"dev": "seb"},
  293. },
  294. },
  295. },
  296. },
  297. Namespace: "default",
  298. },
  299. ref: esv1beta1.ExternalSecretDataRemoteRef{
  300. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  301. Key: "mysec",
  302. Property: "labels",
  303. },
  304. want: []byte(`{"dev":"seb"}`),
  305. },
  306. {
  307. name: "successful case metadata with multiple properties",
  308. fields: fields{
  309. Client: &fakeClient{
  310. t: t,
  311. secretMap: map[string]*v1.Secret{
  312. "mysec": {
  313. ObjectMeta: metav1.ObjectMeta{
  314. Annotations: map[string]string{"date": "today"},
  315. Labels: map[string]string{"dev": "seb"},
  316. },
  317. },
  318. },
  319. },
  320. Namespace: "default",
  321. },
  322. ref: esv1beta1.ExternalSecretDataRemoteRef{
  323. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  324. Key: "mysec",
  325. Property: "labels.dev",
  326. },
  327. want: []byte(`seb`),
  328. },
  329. {
  330. name: "error case metadata with wrong property",
  331. fields: fields{
  332. Client: &fakeClient{
  333. t: t,
  334. secretMap: map[string]*v1.Secret{
  335. "mysec": {
  336. ObjectMeta: metav1.ObjectMeta{
  337. Annotations: map[string]string{"date": "today"},
  338. Labels: map[string]string{"dev": "seb"},
  339. },
  340. },
  341. },
  342. },
  343. Namespace: "default",
  344. },
  345. ref: esv1beta1.ExternalSecretDataRemoteRef{
  346. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  347. Key: "mysec",
  348. Property: "foo",
  349. },
  350. wantErr: true,
  351. },
  352. }
  353. for _, tt := range tests {
  354. t.Run(tt.name, func(t *testing.T) {
  355. p := &Client{
  356. userSecretClient: tt.fields.Client,
  357. userReviewClient: tt.fields.ReviewClient,
  358. namespace: tt.fields.Namespace,
  359. }
  360. got, err := p.GetSecret(context.Background(), tt.ref)
  361. if (err != nil) != tt.wantErr {
  362. t.Errorf("ProviderKubernetes.GetSecret() error = %v, wantErr %v", err, tt.wantErr)
  363. return
  364. }
  365. if !reflect.DeepEqual(got, tt.want) {
  366. t.Errorf("ProviderKubernetes.GetSecret() = %s, want %s", got, tt.want)
  367. }
  368. })
  369. }
  370. }
  371. func TestGetSecretMap(t *testing.T) {
  372. type fields struct {
  373. Client KClient
  374. ReviewClient RClient
  375. Namespace string
  376. }
  377. tests := []struct {
  378. name string
  379. fields fields
  380. ref esv1beta1.ExternalSecretDataRemoteRef
  381. want map[string][]byte
  382. wantErr bool
  383. }{
  384. {
  385. name: "successful case metadata without property",
  386. fields: fields{
  387. Client: &fakeClient{
  388. t: t,
  389. secretMap: map[string]*v1.Secret{
  390. "mysec": {
  391. ObjectMeta: metav1.ObjectMeta{
  392. Annotations: map[string]string{"date": "today"},
  393. Labels: map[string]string{"dev": "seb"},
  394. },
  395. },
  396. },
  397. },
  398. Namespace: "default",
  399. },
  400. ref: esv1beta1.ExternalSecretDataRemoteRef{
  401. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  402. Key: "mysec",
  403. },
  404. want: map[string][]byte{"annotations": []byte("{\"date\":\"today\"}"), "labels": []byte("{\"dev\":\"seb\"}")},
  405. },
  406. {
  407. name: "successful case metadata with single property",
  408. fields: fields{
  409. Client: &fakeClient{
  410. t: t,
  411. secretMap: map[string]*v1.Secret{
  412. "mysec": {
  413. ObjectMeta: metav1.ObjectMeta{
  414. Annotations: map[string]string{"date": "today"},
  415. Labels: map[string]string{"dev": "seb"},
  416. },
  417. },
  418. },
  419. },
  420. Namespace: "default",
  421. },
  422. ref: esv1beta1.ExternalSecretDataRemoteRef{
  423. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  424. Key: "mysec",
  425. Property: "labels",
  426. },
  427. want: map[string][]byte{"dev": []byte("\"seb\"")},
  428. },
  429. {
  430. name: "error case metadata with wrong property",
  431. fields: fields{
  432. Client: &fakeClient{
  433. t: t,
  434. secretMap: map[string]*v1.Secret{
  435. "mysec": {
  436. ObjectMeta: metav1.ObjectMeta{
  437. Annotations: map[string]string{"date": "today"},
  438. Labels: map[string]string{"dev": "seb"},
  439. },
  440. },
  441. },
  442. },
  443. Namespace: "default",
  444. },
  445. ref: esv1beta1.ExternalSecretDataRemoteRef{
  446. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  447. Key: "mysec",
  448. Property: "foo",
  449. },
  450. wantErr: true,
  451. },
  452. }
  453. for _, tt := range tests {
  454. t.Run(tt.name, func(t *testing.T) {
  455. p := &Client{
  456. userSecretClient: tt.fields.Client,
  457. userReviewClient: tt.fields.ReviewClient,
  458. namespace: tt.fields.Namespace,
  459. }
  460. got, err := p.GetSecretMap(context.Background(), tt.ref)
  461. if (err != nil) != tt.wantErr {
  462. t.Errorf("ProviderKubernetes.GetSecretMap() error = %v, wantErr %v", err, tt.wantErr)
  463. return
  464. }
  465. if !reflect.DeepEqual(got, tt.want) {
  466. t.Errorf("ProviderKubernetes.GetSecretMap() = %v, want %v", got, tt.want)
  467. }
  468. })
  469. }
  470. }
  471. func TestGetAllSecrets(t *testing.T) {
  472. type fields struct {
  473. Client KClient
  474. ReviewClient RClient
  475. Namespace string
  476. }
  477. type args struct {
  478. ctx context.Context
  479. ref esv1beta1.ExternalSecretFind
  480. }
  481. tests := []struct {
  482. name string
  483. fields fields
  484. args args
  485. want map[string][]byte
  486. wantErr bool
  487. }{
  488. {
  489. name: "use regex",
  490. fields: fields{
  491. Client: &fakeClient{
  492. t: t,
  493. secretMap: map[string]*v1.Secret{
  494. "mysec": {
  495. ObjectMeta: metav1.ObjectMeta{
  496. Name: "mysec",
  497. },
  498. Data: map[string][]byte{
  499. "token": []byte(`foo`),
  500. },
  501. },
  502. "other": {
  503. ObjectMeta: metav1.ObjectMeta{
  504. Name: "other",
  505. },
  506. Data: map[string][]byte{
  507. "token": []byte(`bar`),
  508. },
  509. },
  510. },
  511. },
  512. },
  513. args: args{
  514. ref: esv1beta1.ExternalSecretFind{
  515. Name: &esv1beta1.FindName{
  516. RegExp: "other",
  517. },
  518. },
  519. },
  520. want: map[string][]byte{
  521. "other": []byte(`{"token":"bar"}`),
  522. },
  523. },
  524. {
  525. name: "use tags/labels",
  526. fields: fields{
  527. Client: &fakeClient{
  528. t: t,
  529. expectedListOptions: metav1.ListOptions{
  530. LabelSelector: "app=foobar",
  531. },
  532. secretMap: map[string]*v1.Secret{
  533. "mysec": {
  534. ObjectMeta: metav1.ObjectMeta{
  535. Name: "mysec",
  536. },
  537. Data: map[string][]byte{
  538. "token": []byte(`foo`),
  539. },
  540. },
  541. "other": {
  542. ObjectMeta: metav1.ObjectMeta{
  543. Name: "other",
  544. },
  545. Data: map[string][]byte{
  546. "token": []byte(`bar`),
  547. },
  548. },
  549. },
  550. },
  551. },
  552. args: args{
  553. ref: esv1beta1.ExternalSecretFind{
  554. Tags: map[string]string{
  555. "app": "foobar",
  556. },
  557. },
  558. },
  559. want: map[string][]byte{
  560. "mysec": []byte(`{"token":"foo"}`),
  561. "other": []byte(`{"token":"bar"}`),
  562. },
  563. },
  564. }
  565. for _, tt := range tests {
  566. t.Run(tt.name, func(t *testing.T) {
  567. p := &Client{
  568. userSecretClient: tt.fields.Client,
  569. userReviewClient: tt.fields.ReviewClient,
  570. namespace: tt.fields.Namespace,
  571. }
  572. got, err := p.GetAllSecrets(tt.args.ctx, tt.args.ref)
  573. if (err != nil) != tt.wantErr {
  574. t.Errorf("ProviderKubernetes.GetAllSecrets() error = %v, wantErr %v", err, tt.wantErr)
  575. return
  576. }
  577. if !reflect.DeepEqual(got, tt.want) {
  578. t.Errorf("ProviderKubernetes.GetAllSecrets() = %v, want %v", got, tt.want)
  579. }
  580. })
  581. }
  582. }
  583. func TestDeleteSecret(t *testing.T) {
  584. type fields struct {
  585. Client KClient
  586. }
  587. tests := []struct {
  588. name string
  589. fields fields
  590. ref esv1beta1.PushRemoteRef
  591. wantSecretMap map[string]*v1.Secret
  592. wantErr bool
  593. }{
  594. {
  595. name: "refuse to delete without property",
  596. fields: fields{
  597. Client: &fakeClient{
  598. t: t,
  599. secretMap: map[string]*v1.Secret{
  600. "mysec": {
  601. Data: map[string][]byte{
  602. "token": []byte(`foobar`),
  603. },
  604. },
  605. },
  606. },
  607. },
  608. ref: v1alpha1.PushSecretRemoteRef{
  609. RemoteKey: "mysec",
  610. },
  611. wantErr: true,
  612. wantSecretMap: map[string]*v1.Secret{
  613. "mysec": {
  614. Data: map[string][]byte{
  615. "token": []byte(`foobar`),
  616. },
  617. },
  618. },
  619. },
  620. {
  621. name: "gracefully ignore not found secret",
  622. fields: fields{
  623. Client: &fakeClient{
  624. t: t,
  625. secretMap: map[string]*v1.Secret{},
  626. },
  627. },
  628. ref: v1alpha1.PushSecretRemoteRef{
  629. RemoteKey: "mysec",
  630. Property: "token",
  631. },
  632. wantErr: false,
  633. wantSecretMap: map[string]*v1.Secret{},
  634. },
  635. {
  636. name: "gracefully ignore not found property",
  637. fields: fields{
  638. Client: &fakeClient{
  639. t: t,
  640. secretMap: map[string]*v1.Secret{
  641. "mysec": {
  642. Data: map[string][]byte{
  643. "token": []byte(`foobar`),
  644. },
  645. },
  646. },
  647. },
  648. },
  649. ref: v1alpha1.PushSecretRemoteRef{
  650. RemoteKey: "mysec",
  651. Property: "secret",
  652. },
  653. wantErr: false,
  654. wantSecretMap: map[string]*v1.Secret{
  655. "mysec": {
  656. Data: map[string][]byte{
  657. "token": []byte(`foobar`),
  658. },
  659. },
  660. },
  661. },
  662. {
  663. name: "unexpected lookup error",
  664. fields: fields{
  665. Client: &fakeClient{
  666. t: t,
  667. secretMap: map[string]*v1.Secret{
  668. "mysec": {
  669. Data: map[string][]byte{
  670. "token": []byte(`foobar`),
  671. },
  672. },
  673. },
  674. err: errors.New(errSomethingWentWrong),
  675. },
  676. },
  677. ref: v1alpha1.PushSecretRemoteRef{
  678. RemoteKey: "mysec",
  679. },
  680. wantErr: true,
  681. wantSecretMap: map[string]*v1.Secret{
  682. "mysec": {
  683. Data: map[string][]byte{
  684. "token": []byte(`foobar`),
  685. },
  686. },
  687. },
  688. },
  689. {
  690. name: "delete whole secret if only property should be removed",
  691. fields: fields{
  692. Client: &fakeClient{
  693. t: t,
  694. secretMap: map[string]*v1.Secret{
  695. "mysec": {
  696. Data: map[string][]byte{
  697. "token": []byte(`foobar`),
  698. },
  699. },
  700. },
  701. },
  702. },
  703. ref: v1alpha1.PushSecretRemoteRef{
  704. RemoteKey: "mysec",
  705. Property: "token",
  706. },
  707. wantErr: false,
  708. wantSecretMap: map[string]*v1.Secret{},
  709. },
  710. {
  711. name: "multiple properties, just remove that one",
  712. fields: fields{
  713. Client: &fakeClient{
  714. t: t,
  715. secretMap: map[string]*v1.Secret{
  716. "mysec": {
  717. Data: map[string][]byte{
  718. "token": []byte(`foo`),
  719. "secret": []byte(`bar`),
  720. },
  721. },
  722. },
  723. },
  724. },
  725. ref: v1alpha1.PushSecretRemoteRef{
  726. RemoteKey: "mysec",
  727. Property: "token",
  728. },
  729. wantErr: false,
  730. wantSecretMap: map[string]*v1.Secret{
  731. "mysec": {
  732. Data: map[string][]byte{
  733. "secret": []byte(`bar`),
  734. },
  735. },
  736. },
  737. },
  738. }
  739. for _, tt := range tests {
  740. t.Run(tt.name, func(t *testing.T) {
  741. p := &Client{
  742. userSecretClient: tt.fields.Client,
  743. }
  744. err := p.DeleteSecret(context.Background(), tt.ref)
  745. if (err != nil) != tt.wantErr {
  746. t.Errorf("ProviderKubernetes.DeleteSecret() error = %v, wantErr %v", err, tt.wantErr)
  747. return
  748. }
  749. fClient := tt.fields.Client.(*fakeClient)
  750. if diff := cmp.Diff(tt.wantSecretMap, fClient.secretMap); diff != "" {
  751. t.Errorf("Unexpected resulting secrets map: -want, +got :\n%s\n", diff)
  752. }
  753. })
  754. }
  755. }
  756. func TestPushSecret(t *testing.T) {
  757. type fields struct {
  758. Client KClient
  759. PushValue string
  760. }
  761. tests := []struct {
  762. name string
  763. fields fields
  764. ref esv1beta1.PushRemoteRef
  765. wantSecretMap map[string]*v1.Secret
  766. wantErr bool
  767. }{
  768. {
  769. name: "refuse to work without property",
  770. fields: fields{
  771. Client: &fakeClient{
  772. t: t,
  773. secretMap: map[string]*v1.Secret{
  774. "mysec": {
  775. Data: map[string][]byte{
  776. "token": []byte(`foo`),
  777. },
  778. },
  779. },
  780. },
  781. PushValue: "bar",
  782. },
  783. ref: v1alpha1.PushSecretRemoteRef{
  784. RemoteKey: "mysec",
  785. },
  786. wantErr: true,
  787. wantSecretMap: map[string]*v1.Secret{
  788. "mysec": {
  789. Data: map[string][]byte{
  790. "token": []byte(`foo`),
  791. },
  792. },
  793. },
  794. },
  795. {
  796. name: "add missing property to existing secret",
  797. fields: fields{
  798. Client: &fakeClient{
  799. t: t,
  800. secretMap: map[string]*v1.Secret{
  801. "mysec": {
  802. Data: map[string][]byte{
  803. "token": []byte(`foo`),
  804. },
  805. },
  806. },
  807. },
  808. PushValue: "bar",
  809. },
  810. ref: v1alpha1.PushSecretRemoteRef{
  811. RemoteKey: "mysec",
  812. Property: "secret",
  813. },
  814. wantErr: false,
  815. wantSecretMap: map[string]*v1.Secret{
  816. "mysec": {
  817. Data: map[string][]byte{
  818. "token": []byte(`foo`),
  819. "secret": []byte(`bar`),
  820. },
  821. },
  822. },
  823. },
  824. {
  825. name: "replace existing property in existing secret",
  826. fields: fields{
  827. Client: &fakeClient{
  828. t: t,
  829. secretMap: map[string]*v1.Secret{
  830. "mysec": {
  831. Data: map[string][]byte{
  832. "token": []byte(`foo`),
  833. },
  834. },
  835. },
  836. },
  837. PushValue: "bar",
  838. },
  839. ref: v1alpha1.PushSecretRemoteRef{
  840. RemoteKey: "mysec",
  841. Property: "token",
  842. },
  843. wantErr: false,
  844. wantSecretMap: map[string]*v1.Secret{
  845. "mysec": {
  846. Data: map[string][]byte{
  847. "token": []byte(`bar`),
  848. },
  849. },
  850. },
  851. },
  852. {
  853. name: "create new secret",
  854. fields: fields{
  855. Client: &fakeClient{
  856. t: t,
  857. secretMap: map[string]*v1.Secret{
  858. "yoursec": {
  859. Data: map[string][]byte{
  860. "token": []byte(`foo`),
  861. },
  862. },
  863. },
  864. },
  865. PushValue: "bar",
  866. },
  867. ref: v1alpha1.PushSecretRemoteRef{
  868. RemoteKey: "mysec",
  869. Property: "secret",
  870. },
  871. wantErr: false,
  872. wantSecretMap: map[string]*v1.Secret{
  873. "yoursec": {
  874. Data: map[string][]byte{
  875. "token": []byte(`foo`),
  876. },
  877. },
  878. "mysec": {
  879. Data: map[string][]byte{
  880. "secret": []byte(`bar`),
  881. },
  882. },
  883. },
  884. },
  885. }
  886. for _, tt := range tests {
  887. t.Run(tt.name, func(t *testing.T) {
  888. p := &Client{
  889. userSecretClient: tt.fields.Client,
  890. store: &esv1beta1.KubernetesProvider{},
  891. }
  892. err := p.PushSecret(context.Background(), []byte(tt.fields.PushValue), nil, tt.ref)
  893. if (err != nil) != tt.wantErr {
  894. t.Errorf("ProviderKubernetes.DeleteSecret() error = %v, wantErr %v", err, tt.wantErr)
  895. return
  896. }
  897. fClient := tt.fields.Client.(*fakeClient)
  898. if diff := cmp.Diff(tt.wantSecretMap, fClient.secretMap); diff != "" {
  899. t.Errorf("Unexpected resulting secrets map: -want, +got :\n%s\n", diff)
  900. }
  901. })
  902. }
  903. }