client_get_test.go 30 KB

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