client_get_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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: errors.New(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: errors.New(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 TestGetSecretMetadataPath(t *testing.T) {
  679. storeV2 := makeValidSecretStore()
  680. storeV2NoPath := storeV2.DeepCopy()
  681. multiPath := "secret/path"
  682. storeV2.Spec.Provider.Vault.Path = &multiPath
  683. storeV2NoPath.Spec.Provider.Vault.Path = nil
  684. storeV1 := makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1)
  685. storeV1NoPath := storeV1.DeepCopy()
  686. storeV1.Spec.Provider.Vault.Path = &multiPath
  687. storeV1NoPath.Spec.Provider.Vault.Path = nil
  688. type args struct {
  689. store *esv1beta1.VaultProvider
  690. path string
  691. expected string
  692. }
  693. cases := map[string]struct {
  694. reason string
  695. args args
  696. }{
  697. "PathForV1": {
  698. reason: "path should compose with mount point if set",
  699. args: args{
  700. store: storeV1.Spec.Provider.Vault,
  701. path: "data/test",
  702. expected: "secret/path/data/test",
  703. },
  704. },
  705. "PathForV2": {
  706. reason: "path should compose with mount point if set without data",
  707. args: args{
  708. store: storeV2.Spec.Provider.Vault,
  709. path: "secret/path/data/test",
  710. expected: "secret/path/metadata/secret/path/data/test",
  711. },
  712. },
  713. "PathForV2WithData": {
  714. reason: "if data is in the path it shouldn't be changed",
  715. args: args{
  716. store: storeV2NoPath.Spec.Provider.Vault,
  717. path: "my_data/data/path",
  718. expected: "my_data/metadata/path",
  719. },
  720. },
  721. }
  722. for name, tc := range cases {
  723. t.Run(name, func(t *testing.T) {
  724. vStore := &client{
  725. store: tc.args.store,
  726. }
  727. want, _ := vStore.buildMetadataPath(tc.args.path)
  728. if diff := cmp.Diff(want, tc.args.expected); diff != "" {
  729. t.Errorf("\n%s\nvault.buildPath(...): -want expected, +got error:\n%s", tc.reason, diff)
  730. }
  731. })
  732. }
  733. }
  734. func TestSecretExists(t *testing.T) {
  735. secret := map[string]any{
  736. "foo": "bar",
  737. }
  738. secretWithNil := map[string]any{
  739. "hi": nil,
  740. }
  741. errNope := errors.New("nope")
  742. type args struct {
  743. store *esv1beta1.VaultProvider
  744. vClient util.Logical
  745. }
  746. type want struct {
  747. exists bool
  748. err error
  749. }
  750. tests := map[string]struct {
  751. reason string
  752. args args
  753. ref *testingfake.PushSecretData
  754. want want
  755. }{
  756. "NoExistingSecretV1": {
  757. reason: "Should return false, nil if secret does not exist in provider.",
  758. args: args{
  759. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  760. vClient: &fake.Logical{
  761. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, esv1beta1.NoSecretError{}),
  762. },
  763. },
  764. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  765. want: want{
  766. exists: false,
  767. err: nil,
  768. },
  769. },
  770. "NoExistingSecretV2": {
  771. reason: "Should return false, nil if secret does not exist in provider.",
  772. args: args{
  773. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  774. vClient: &fake.Logical{
  775. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, esv1beta1.NoSecretError{}),
  776. },
  777. },
  778. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  779. want: want{
  780. exists: false,
  781. err: nil,
  782. },
  783. },
  784. "NoExistingSecretWithPropertyV2": {
  785. reason: "Should return false, nil if secret with property does not exist in provider.",
  786. args: args{
  787. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  788. vClient: &fake.Logical{
  789. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  790. "data": secret,
  791. }, nil),
  792. },
  793. },
  794. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "different"},
  795. want: want{
  796. exists: false,
  797. err: nil,
  798. },
  799. },
  800. "NoExistingSecretWithPropertyV1": {
  801. reason: "Should return false, nil if secret with property does not exist in provider.",
  802. args: args{
  803. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  804. vClient: &fake.Logical{
  805. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  806. },
  807. },
  808. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "different"},
  809. want: want{
  810. exists: false,
  811. err: nil,
  812. },
  813. },
  814. "ExistingSecretV1": {
  815. reason: "Should return true, nil if secret exists in provider.",
  816. args: args{
  817. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  818. vClient: &fake.Logical{
  819. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
  820. },
  821. },
  822. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  823. want: want{
  824. exists: true,
  825. err: nil,
  826. },
  827. },
  828. "ExistingSecretV2": {
  829. reason: "Should return true, nil if secret exists in provider.",
  830. args: args{
  831. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  832. vClient: &fake.Logical{
  833. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  834. "data": secret,
  835. }, nil),
  836. },
  837. },
  838. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  839. want: want{
  840. exists: true,
  841. err: nil,
  842. },
  843. },
  844. "ExistingSecretWithNilV1": {
  845. reason: "Should return false, nil if secret in provider has nil value.",
  846. args: args{
  847. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  848. vClient: &fake.Logical{
  849. ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNil, nil),
  850. },
  851. },
  852. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "hi"},
  853. want: want{
  854. exists: false,
  855. err: nil,
  856. },
  857. },
  858. "ExistingSecretWithNilV2": {
  859. reason: "Should return false, nil if secret in provider has nil value.",
  860. args: args{
  861. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  862. vClient: &fake.Logical{
  863. ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
  864. "data": secretWithNil,
  865. }, nil),
  866. },
  867. },
  868. ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "hi"},
  869. want: want{
  870. exists: false,
  871. err: nil,
  872. },
  873. },
  874. "ErrorReadingSecretV1": {
  875. reason: "Should return error if secret existence cannot be verified.",
  876. args: args{
  877. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV1).Spec.Provider.Vault,
  878. vClient: &fake.Logical{
  879. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errNope),
  880. },
  881. },
  882. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  883. want: want{
  884. exists: false,
  885. err: fmt.Errorf(errReadSecret, errNope),
  886. },
  887. },
  888. "ErrorReadingSecretV2": {
  889. reason: "Should return error if secret existence cannot be verified.",
  890. args: args{
  891. store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
  892. vClient: &fake.Logical{
  893. ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errNope),
  894. },
  895. },
  896. ref: &testingfake.PushSecretData{RemoteKey: "secret"},
  897. want: want{
  898. exists: false,
  899. err: fmt.Errorf(errReadSecret, errNope),
  900. },
  901. },
  902. }
  903. for name, tc := range tests {
  904. t.Run(name, func(t *testing.T) {
  905. client := &client{
  906. logical: tc.args.vClient,
  907. store: tc.args.store,
  908. }
  909. exists, err := client.SecretExists(context.Background(), tc.ref)
  910. if diff := cmp.Diff(exists, tc.want.exists); diff != "" {
  911. t.Errorf("\n%s\nvault.SecretExists(...): -want exists, +got exists:\n%s", tc.reason, diff)
  912. }
  913. if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
  914. t.Errorf("\n%s\nvault.GetSecret(...): -want error, +got error:\n%s", tc.reason, diff)
  915. }
  916. })
  917. }
  918. }
  919. // EquateErrors returns true if the supplied errors are of the same type and
  920. // produce identical strings. This mirrors the error comparison behavior of
  921. // https://github.com/go-test/deep, which most Crossplane tests targeted before
  922. // we switched to go-cmp.
  923. //
  924. // This differs from cmpopts.EquateErrors, which does not test for error strings
  925. // and instead returns whether one error 'is' (in the errors.Is sense) the
  926. // other.
  927. func EquateErrors() cmp.Option {
  928. return cmp.Comparer(func(a, b error) bool {
  929. if a == nil || b == nil {
  930. return a == nil && b == nil
  931. }
  932. av := reflect.ValueOf(a)
  933. bv := reflect.ValueOf(b)
  934. if av.Type() != bv.Type() {
  935. return false
  936. }
  937. return a.Error() == b.Error()
  938. })
  939. }