client_get_test.go 21 KB

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