client_get_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 vault
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. "github.com/google/go-cmp/cmp"
  21. vault "github.com/hashicorp/vault/api"
  22. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  23. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  24. testingfake "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  25. "github.com/external-secrets/external-secrets/pkg/provider/vault/fake"
  26. "github.com/external-secrets/external-secrets/pkg/provider/vault/util"
  27. )
  28. func TestGetSecret(t *testing.T) {
  29. errBoom := errors.New("boom")
  30. secret := map[string]any{
  31. "access_key": "access_key",
  32. "access_secret": "access_secret",
  33. }
  34. secretWithNilVal := map[string]any{
  35. "access_key": "access_key",
  36. "access_secret": "access_secret",
  37. "token": nil,
  38. }
  39. secretWithNestedVal := map[string]any{
  40. "access_key": "access_key",
  41. "access_secret": "access_secret",
  42. "nested.bar": "something different",
  43. "nested": map[string]string{
  44. "foo": "oke",
  45. "bar": "also ok?",
  46. },
  47. "list_of_values": []string{
  48. "first_value",
  49. "second_value",
  50. "third_value",
  51. },
  52. "json_number": json.Number("42"),
  53. }
  54. type args struct {
  55. store *esv1beta1.VaultProvider
  56. kube kclient.Client
  57. vLogical util.Logical
  58. ns string
  59. data esv1beta1.ExternalSecretDataRemoteRef
  60. }
  61. type want struct {
  62. err error
  63. val []byte
  64. }
  65. cases := map[string]struct {
  66. reason string
  67. args args
  68. want want
  69. }{
  70. "ReadSecret": {
  71. reason: "Should return the secret with property",
  72. args: args{
  73. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  74. data: esv1beta1.ExternalSecretDataRemoteRef{
  75. Property: "access_key",
  76. },
  77. vLogical: &fake.Logical{
  78. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  79. },
  80. },
  81. want: want{
  82. err: nil,
  83. val: []byte("access_key"),
  84. },
  85. },
  86. "ReadSecretWithNil": {
  87. reason: "Should return the secret with property if it has a nil val",
  88. args: args{
  89. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  90. data: esv1beta1.ExternalSecretDataRemoteRef{
  91. Property: "access_key",
  92. },
  93. vLogical: &fake.Logical{
  94. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNilVal, nil),
  95. },
  96. },
  97. want: want{
  98. err: nil,
  99. val: []byte("access_key"),
  100. },
  101. },
  102. "ReadSecretWithoutProperty": {
  103. reason: "Should return the json encoded secret without property",
  104. args: args{
  105. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  106. data: esv1beta1.ExternalSecretDataRemoteRef{},
  107. vLogical: &fake.Logical{
  108. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  109. },
  110. },
  111. want: want{
  112. err: nil,
  113. val: []byte(`{"access_key":"access_key","access_secret":"access_secret"}`),
  114. },
  115. },
  116. "ReadSecretWithNestedValue": {
  117. reason: "Should return a nested property",
  118. args: args{
  119. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  120. data: esv1beta1.ExternalSecretDataRemoteRef{
  121. Property: "nested.foo",
  122. },
  123. vLogical: &fake.Logical{
  124. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
  125. },
  126. },
  127. want: want{
  128. err: nil,
  129. val: []byte("oke"),
  130. },
  131. },
  132. "ReadSecretWithNestedValueFromData": {
  133. reason: "Should return a nested property",
  134. args: args{
  135. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  136. data: esv1beta1.ExternalSecretDataRemoteRef{
  137. //
  138. Property: "nested.bar",
  139. },
  140. vLogical: &fake.Logical{
  141. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
  142. },
  143. },
  144. want: want{
  145. err: nil,
  146. val: []byte("something different"),
  147. },
  148. },
  149. "ReadSecretWithMissingValueFromData": {
  150. reason: "Should return a NoSecretErr",
  151. args: args{
  152. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  153. data: esv1beta1.ExternalSecretDataRemoteRef{
  154. Property: "not-relevant",
  155. },
  156. vLogical: &fake.Logical{
  157. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, nil),
  158. },
  159. },
  160. want: want{
  161. err: esv1beta1.NoSecretErr,
  162. val: nil,
  163. },
  164. },
  165. "ReadSecretWithSliceValue": {
  166. reason: "Should return property as a joined slice",
  167. args: args{
  168. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  169. data: esv1beta1.ExternalSecretDataRemoteRef{
  170. Property: "list_of_values",
  171. },
  172. vLogical: &fake.Logical{
  173. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
  174. },
  175. },
  176. want: want{
  177. err: nil,
  178. val: []byte("first_value\nsecond_value\nthird_value"),
  179. },
  180. },
  181. "ReadSecretWithJsonNumber": {
  182. reason: "Should return parsed json.Number property",
  183. args: args{
  184. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  185. data: esv1beta1.ExternalSecretDataRemoteRef{
  186. Property: "json_number",
  187. },
  188. vLogical: &fake.Logical{
  189. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
  190. },
  191. },
  192. want: want{
  193. err: nil,
  194. val: []byte("42"),
  195. },
  196. },
  197. "NonexistentProperty": {
  198. reason: "Should return error property does not exist.",
  199. args: args{
  200. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  201. data: esv1beta1.ExternalSecretDataRemoteRef{
  202. Property: "nop.doesnt.exist",
  203. },
  204. vLogical: &fake.Logical{
  205. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
  206. },
  207. },
  208. want: want{
  209. err: fmt.Errorf(errSecretKeyFmt, "nop.doesnt.exist"),
  210. },
  211. },
  212. "ReadSecretError": {
  213. reason: "Should return error if vault client fails to read secret.",
  214. args: args{
  215. store: makeSecretStore().Spec.Provider.Vault,
  216. vLogical: &fake.Logical{
  217. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errBoom),
  218. },
  219. },
  220. want: want{
  221. err: fmt.Errorf(errReadSecret, errBoom),
  222. },
  223. },
  224. "ReadSecretNotFound": {
  225. reason: "Secret doesn't exist",
  226. args: args{
  227. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  228. data: esv1beta1.ExternalSecretDataRemoteRef{
  229. Property: "access_key",
  230. },
  231. vLogical: &fake.Logical{
  232. ReadWithDataWithContextFn: func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  233. return nil, nil
  234. },
  235. },
  236. },
  237. want: want{
  238. err: esv1beta1.NoSecretError{},
  239. },
  240. },
  241. "ReadSecretMetadataWithoutProperty": {
  242. reason: "Should return the json encoded metadata",
  243. args: args{
  244. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  245. data: esv1beta1.ExternalSecretDataRemoteRef{
  246. MetadataPolicy: "Fetch",
  247. },
  248. vLogical: &fake.Logical{
  249. ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(secret, nil),
  250. },
  251. },
  252. want: want{
  253. err: nil,
  254. val: []byte(`{"access_key":"access_key","access_secret":"access_secret"}`),
  255. },
  256. },
  257. "ReadSecretMetadataWithProperty": {
  258. reason: "Should return the access_key value from the metadata",
  259. args: args{
  260. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  261. data: esv1beta1.ExternalSecretDataRemoteRef{
  262. MetadataPolicy: "Fetch",
  263. Property: "access_key",
  264. },
  265. vLogical: &fake.Logical{
  266. ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(secret, nil),
  267. },
  268. },
  269. want: want{
  270. err: nil,
  271. val: []byte("access_key"),
  272. },
  273. },
  274. "FailReadSecretMetadataInvalidProperty": {
  275. reason: "Should return error of non existent key inmetadata",
  276. args: args{
  277. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  278. data: esv1beta1.ExternalSecretDataRemoteRef{
  279. MetadataPolicy: "Fetch",
  280. Property: "does_not_exist",
  281. },
  282. vLogical: &fake.Logical{
  283. ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(secret, nil),
  284. },
  285. },
  286. want: want{
  287. err: fmt.Errorf(errSecretKeyFmt, "does_not_exist"),
  288. },
  289. },
  290. "FailReadSecretMetadataNoMetadata": {
  291. reason: "Should return the access_key value from the metadata",
  292. args: args{
  293. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  294. data: esv1beta1.ExternalSecretDataRemoteRef{
  295. MetadataPolicy: "Fetch",
  296. },
  297. vLogical: &fake.Logical{
  298. ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(nil, nil),
  299. },
  300. },
  301. want: want{
  302. err: fmt.Errorf(errNotFound),
  303. },
  304. },
  305. "FailReadSecretMetadataWrongVersion": {
  306. reason: "Should return the access_key value from the metadata",
  307. args: args{
  308. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  309. data: esv1beta1.ExternalSecretDataRemoteRef{
  310. MetadataPolicy: "Fetch",
  311. },
  312. vLogical: &fake.Logical{
  313. ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(nil, nil),
  314. },
  315. },
  316. want: want{
  317. err: fmt.Errorf(errUnsupportedMetadataKvVersion),
  318. },
  319. },
  320. }
  321. for name, tc := range cases {
  322. t.Run(name, func(t *testing.T) {
  323. vStore := &client{
  324. kube: tc.args.kube,
  325. logical: tc.args.vLogical,
  326. store: tc.args.store,
  327. namespace: tc.args.ns,
  328. }
  329. val, err := vStore.GetSecret(context.Background(), tc.args.data)
  330. if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
  331. t.Errorf("\n%s\nvault.GetSecret(...): -want error, +got error:\n%s", tc.reason, diff)
  332. }
  333. if diff := cmp.Diff(string(tc.want.val), string(val)); diff != "" {
  334. t.Errorf("\n%s\nvault.GetSecret(...): -want val, +got val:\n%s", tc.reason, diff)
  335. }
  336. })
  337. }
  338. }
  339. func TestGetSecretMap(t *testing.T) {
  340. errBoom := errors.New("boom")
  341. secret := map[string]any{
  342. "access_key": "access_key",
  343. "access_secret": "access_secret",
  344. }
  345. secretWithSpecialCharacter := map[string]any{
  346. "access_key": "acc<ess_&ke.,y",
  347. "access_secret": "acce&?ss_s>ecret",
  348. }
  349. secretWithNilVal := map[string]any{
  350. "access_key": "access_key",
  351. "access_secret": "access_secret",
  352. "token": nil,
  353. }
  354. secretWithNestedVal := map[string]any{
  355. "access_key": "access_key",
  356. "access_secret": "access_secret",
  357. "nested": map[string]any{
  358. "foo": map[string]string{
  359. "oke": "yup",
  360. "mhkeih": "yada yada",
  361. },
  362. },
  363. }
  364. secretWithTypes := map[string]any{
  365. "access_secret": "access_secret",
  366. "f32": float32(2.12),
  367. "f64": float64(2.1234534153423423),
  368. "int": 42,
  369. "bool": true,
  370. "bt": []byte("foobar"),
  371. }
  372. type args struct {
  373. store *esv1beta1.VaultProvider
  374. kube kclient.Client
  375. vClient util.Logical
  376. ns string
  377. data esv1beta1.ExternalSecretDataRemoteRef
  378. }
  379. type want struct {
  380. err error
  381. val map[string][]byte
  382. }
  383. cases := map[string]struct {
  384. reason string
  385. args args
  386. want want
  387. }{
  388. "ReadSecretKV1": {
  389. reason: "Should read a v1 secret",
  390. args: args{
  391. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  392. vClient: &fake.Logical{
  393. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  394. },
  395. },
  396. want: want{
  397. err: nil,
  398. val: map[string][]byte{
  399. "access_key": []byte("access_key"),
  400. "access_secret": []byte("access_secret"),
  401. },
  402. },
  403. },
  404. "ReadSecretKV2": {
  405. reason: "Should read a v2 secret",
  406. args: args{
  407. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  408. vClient: &fake.Logical{
  409. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  410. "data": secret,
  411. }, nil),
  412. },
  413. },
  414. want: want{
  415. err: nil,
  416. val: map[string][]byte{
  417. "access_key": []byte("access_key"),
  418. "access_secret": []byte("access_secret"),
  419. },
  420. },
  421. },
  422. "ReadSecretWithSpecialCharactersKV1": {
  423. reason: "Should read a v1 secret with special characters",
  424. args: args{
  425. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  426. vClient: &fake.Logical{
  427. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithSpecialCharacter, nil),
  428. },
  429. },
  430. want: want{
  431. err: nil,
  432. val: map[string][]byte{
  433. "access_key": []byte("acc<ess_&ke.,y"),
  434. "access_secret": []byte("acce&?ss_s>ecret"),
  435. },
  436. },
  437. },
  438. "ReadSecretWithSpecialCharactersKV2": {
  439. reason: "Should read a v2 secret with special characters",
  440. args: args{
  441. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  442. vClient: &fake.Logical{
  443. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  444. "data": secretWithSpecialCharacter,
  445. }, nil),
  446. },
  447. },
  448. want: want{
  449. err: nil,
  450. val: map[string][]byte{
  451. "access_key": []byte("acc<ess_&ke.,y"),
  452. "access_secret": []byte("acce&?ss_s>ecret"),
  453. },
  454. },
  455. },
  456. "ReadSecretWithNilValueKV1": {
  457. reason: "Should read v1 secret with a nil value",
  458. args: args{
  459. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  460. vClient: &fake.Logical{
  461. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNilVal, nil),
  462. },
  463. },
  464. want: want{
  465. err: nil,
  466. val: map[string][]byte{
  467. "access_key": []byte("access_key"),
  468. "access_secret": []byte("access_secret"),
  469. "token": []byte(nil),
  470. },
  471. },
  472. },
  473. "ReadSecretWithNilValueKV2": {
  474. reason: "Should read v2 secret with a nil value",
  475. args: args{
  476. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  477. vClient: &fake.Logical{
  478. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  479. "data": secretWithNilVal}, nil),
  480. },
  481. },
  482. want: want{
  483. err: nil,
  484. val: map[string][]byte{
  485. "access_key": []byte("access_key"),
  486. "access_secret": []byte("access_secret"),
  487. "token": []byte(nil),
  488. },
  489. },
  490. },
  491. "ReadSecretWithTypesKV2": {
  492. reason: "Should read v2 secret with different types",
  493. args: args{
  494. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  495. vClient: &fake.Logical{
  496. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  497. "data": secretWithTypes}, nil),
  498. },
  499. },
  500. want: want{
  501. err: nil,
  502. val: map[string][]byte{
  503. "access_secret": []byte("access_secret"),
  504. "f32": []byte("2.12"),
  505. "f64": []byte("2.1234534153423423"),
  506. "int": []byte("42"),
  507. "bool": []byte("true"),
  508. "bt": []byte("Zm9vYmFy"), // base64
  509. },
  510. },
  511. },
  512. "ReadNestedSecret": {
  513. reason: "Should read the secret with nested property",
  514. args: args{
  515. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  516. data: esv1beta1.ExternalSecretDataRemoteRef{
  517. Property: "nested",
  518. },
  519. vClient: &fake.Logical{
  520. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  521. "data": secretWithNestedVal}, nil),
  522. },
  523. },
  524. want: want{
  525. err: nil,
  526. val: map[string][]byte{
  527. "foo": []byte(`{"mhkeih":"yada yada","oke":"yup"}`),
  528. },
  529. },
  530. },
  531. "ReadDeeplyNestedSecret": {
  532. reason: "Should read the secret for deeply nested property",
  533. args: args{
  534. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  535. data: esv1beta1.ExternalSecretDataRemoteRef{
  536. Property: "nested.foo",
  537. },
  538. vClient: &fake.Logical{
  539. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  540. "data": secretWithNestedVal}, nil),
  541. },
  542. },
  543. want: want{
  544. err: nil,
  545. val: map[string][]byte{
  546. "oke": []byte("yup"),
  547. "mhkeih": []byte("yada yada"),
  548. },
  549. },
  550. },
  551. "ReadSecretError": {
  552. reason: "Should return error if vault client fails to read secret.",
  553. args: args{
  554. store: makeSecretStore().Spec.Provider.Vault,
  555. vClient: &fake.Logical{
  556. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errBoom),
  557. },
  558. },
  559. want: want{
  560. err: fmt.Errorf(errReadSecret, errBoom),
  561. },
  562. },
  563. }
  564. for name, tc := range cases {
  565. t.Run(name, func(t *testing.T) {
  566. vStore := &client{
  567. kube: tc.args.kube,
  568. logical: tc.args.vClient,
  569. store: tc.args.store,
  570. namespace: tc.args.ns,
  571. }
  572. val, err := vStore.GetSecretMap(context.Background(), tc.args.data)
  573. if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
  574. t.Errorf("\n%s\nvault.GetSecretMap(...): -want error, +got error:\n%s", tc.reason, diff)
  575. }
  576. if diff := cmp.Diff(tc.want.val, val); diff != "" {
  577. t.Errorf("\n%s\nvault.GetSecretMap(...): -want val, +got val:\n%s", tc.reason, diff)
  578. }
  579. })
  580. }
  581. }
  582. func TestGetSecretPath(t *testing.T) {
  583. storeV2 := makeValidSecretStore()
  584. storeV2NoPath := storeV2.DeepCopy()
  585. multiPath := "secret/path"
  586. storeV2.Spec.Provider.Vault.Path = &multiPath
  587. storeV2NoPath.Spec.Provider.Vault.Path = nil
  588. storeV1 := makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1)
  589. storeV1NoPath := storeV1.DeepCopy()
  590. storeV1.Spec.Provider.Vault.Path = &multiPath
  591. storeV1NoPath.Spec.Provider.Vault.Path = nil
  592. type args struct {
  593. store *esv1beta1.VaultProvider
  594. path string
  595. expected string
  596. }
  597. cases := map[string]struct {
  598. reason string
  599. args args
  600. }{
  601. "PathWithoutFormatV2": {
  602. reason: "path should compose with mount point if set",
  603. args: args{
  604. store: storeV2.Spec.Provider.Vault,
  605. path: "secret/path/data/test",
  606. expected: "secret/path/data/test",
  607. },
  608. },
  609. "PathWithoutFormatV2_NoData": {
  610. reason: "path should compose with mount point if set without data",
  611. args: args{
  612. store: storeV2.Spec.Provider.Vault,
  613. path: "secret/path/test",
  614. expected: "secret/path/data/test",
  615. },
  616. },
  617. "PathWithoutFormatV2_NoPath": {
  618. reason: "if no mountpoint and no data available, needs to be set in second element",
  619. args: args{
  620. store: storeV2NoPath.Spec.Provider.Vault,
  621. path: "secret/test/big/path",
  622. expected: "secret/data/test/big/path",
  623. },
  624. },
  625. "PathWithoutFormatV2_NoPathWithData": {
  626. reason: "if data is available, should respect order",
  627. args: args{
  628. store: storeV2NoPath.Spec.Provider.Vault,
  629. path: "secret/test/data/not/the/first/and/data/twice",
  630. expected: "secret/test/data/not/the/first/and/data/twice",
  631. },
  632. },
  633. "PathWithoutFormatV1": {
  634. reason: "v1 mountpoint should be added but not enforce 'data'",
  635. args: args{
  636. store: storeV1.Spec.Provider.Vault,
  637. path: "secret/path/test",
  638. expected: "secret/path/test",
  639. },
  640. },
  641. "PathWithoutFormatV1_NoPath": {
  642. reason: "Should not append any path information if v1 with no mountpoint",
  643. args: args{
  644. store: storeV1NoPath.Spec.Provider.Vault,
  645. path: "secret/test",
  646. expected: "secret/test",
  647. },
  648. },
  649. "WithoutPathButMountpointV2": {
  650. reason: "Mountpoint needs to be set in addition to data",
  651. args: args{
  652. store: storeV2.Spec.Provider.Vault,
  653. path: "test",
  654. expected: "secret/path/data/test",
  655. },
  656. },
  657. "WithoutPathButMountpointV1": {
  658. reason: "Mountpoint needs to be set in addition to data",
  659. args: args{
  660. store: storeV1.Spec.Provider.Vault,
  661. path: "test",
  662. expected: "secret/path/test",
  663. },
  664. },
  665. }
  666. for name, tc := range cases {
  667. t.Run(name, func(t *testing.T) {
  668. vStore := &client{
  669. store: tc.args.store,
  670. }
  671. want := vStore.buildPath(tc.args.path)
  672. if diff := cmp.Diff(want, tc.args.expected); diff != "" {
  673. t.Errorf("\n%s\nvault.buildPath(...): -want expected, +got error:\n%s", tc.reason, diff)
  674. }
  675. })
  676. }
  677. }
  678. func TestSecretExists(t *testing.T) {
  679. secret := map[string]any{
  680. "foo": "bar",
  681. }
  682. secretWithNil := map[string]any{
  683. "hi": nil,
  684. }
  685. errNope := errors.New("nope")
  686. type args struct {
  687. store *esv1beta1.VaultProvider
  688. vClient util.Logical
  689. }
  690. type want struct {
  691. exists bool
  692. err error
  693. }
  694. tests := map[string]struct {
  695. reason string
  696. args args
  697. ref *testingfake.PushSecretData
  698. want want
  699. }{
  700. "NoExistingSecretV1": {
  701. reason: "Should return false, nil if secret does not exist in provider.",
  702. args: args{
  703. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  704. vClient: &fake.Logical{
  705. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, esv1beta1.NoSecretError{}),
  706. },
  707. },
  708. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  709. want: want{
  710. exists: false,
  711. err: nil,
  712. },
  713. },
  714. "NoExistingSecretV2": {
  715. reason: "Should return false, nil if secret does not exist in provider.",
  716. args: args{
  717. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  718. vClient: &fake.Logical{
  719. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, esv1beta1.NoSecretError{}),
  720. },
  721. },
  722. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  723. want: want{
  724. exists: false,
  725. err: nil,
  726. },
  727. },
  728. "NoExistingSecretWithPropertyV2": {
  729. reason: "Should return false, nil if secret with property does not exist in provider.",
  730. args: args{
  731. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  732. vClient: &fake.Logical{
  733. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  734. "data": secret,
  735. }, nil),
  736. },
  737. },
  738. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "different"},
  739. want: want{
  740. exists: false,
  741. err: nil,
  742. },
  743. },
  744. "NoExistingSecretWithPropertyV1": {
  745. reason: "Should return false, nil if secret with property does not exist in provider.",
  746. args: args{
  747. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  748. vClient: &fake.Logical{
  749. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  750. },
  751. },
  752. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "different"},
  753. want: want{
  754. exists: false,
  755. err: nil,
  756. },
  757. },
  758. "ExistingSecretV1": {
  759. reason: "Should return true, nil if secret exists in provider.",
  760. args: args{
  761. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  762. vClient: &fake.Logical{
  763. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  764. },
  765. },
  766. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  767. want: want{
  768. exists: true,
  769. err: nil,
  770. },
  771. },
  772. "ExistingSecretV2": {
  773. reason: "Should return true, nil if secret exists in provider.",
  774. args: args{
  775. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  776. vClient: &fake.Logical{
  777. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  778. "data": secret,
  779. }, nil),
  780. },
  781. },
  782. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  783. want: want{
  784. exists: true,
  785. err: nil,
  786. },
  787. },
  788. "ExistingSecretWithNilV1": {
  789. reason: "Should return false, nil if secret in provider has nil value.",
  790. args: args{
  791. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  792. vClient: &fake.Logical{
  793. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNil, nil),
  794. },
  795. },
  796. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "hi"},
  797. want: want{
  798. exists: false,
  799. err: nil,
  800. },
  801. },
  802. "ExistingSecretWithNilV2": {
  803. reason: "Should return false, nil if secret in provider has nil value.",
  804. args: args{
  805. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  806. vClient: &fake.Logical{
  807. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  808. "data": secretWithNil,
  809. }, nil),
  810. },
  811. },
  812. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "hi"},
  813. want: want{
  814. exists: false,
  815. err: nil,
  816. },
  817. },
  818. "ErrorReadingSecretV1": {
  819. reason: "Should return error if secret existence cannot be verified.",
  820. args: args{
  821. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  822. vClient: &fake.Logical{
  823. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errNope),
  824. },
  825. },
  826. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  827. want: want{
  828. exists: false,
  829. err: fmt.Errorf(errReadSecret, errNope),
  830. },
  831. },
  832. "ErrorReadingSecretV2": {
  833. reason: "Should return error if secret existence cannot be verified.",
  834. args: args{
  835. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  836. vClient: &fake.Logical{
  837. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errNope),
  838. },
  839. },
  840. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  841. want: want{
  842. exists: false,
  843. err: fmt.Errorf(errReadSecret, errNope),
  844. },
  845. },
  846. }
  847. for name, tc := range tests {
  848. t.Run(name, func(t *testing.T) {
  849. client := &client{
  850. logical: tc.args.vClient,
  851. store: tc.args.store,
  852. }
  853. exists, err := client.SecretExists(context.Background(), tc.ref)
  854. if diff := cmp.Diff(exists, tc.want.exists); diff != "" {
  855. t.Errorf("\n%s\nvault.SecretExists(...): -want exists, +got exists:\n%s", tc.reason, diff)
  856. }
  857. if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
  858. t.Errorf("\n%s\nvault.GetSecret(...): -want error, +got error:\n%s", tc.reason, diff)
  859. }
  860. })
  861. }
  862. }
  863. // EquateErrors returns true if the supplied errors are of the same type and
  864. // produce identical strings. This mirrors the error comparison behavior of
  865. // https://github.com/go-test/deep, which most Crossplane tests targeted before
  866. // we switched to go-cmp.
  867. //
  868. // This differs from cmpopts.EquateErrors, which does not test for error strings
  869. // and instead returns whether one error 'is' (in the errors.Is sense) the
  870. // other.
  871. func EquateErrors() cmp.Option {
  872. return cmp.Comparer(func(a, b error) bool {
  873. if a == nil || b == nil {
  874. return a == nil && b == nil
  875. }
  876. av := reflect.ValueOf(a)
  877. bv := reflect.ValueOf(b)
  878. if av.Type() != bv.Type() {
  879. return false
  880. }
  881. return a.Error() == b.Error()
  882. })
  883. }