client_get_test.go 27 KB

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