| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037 |
- /*
- Copyright © The ESO Authors
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- https://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package vault
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "reflect"
- "testing"
- "github.com/google/go-cmp/cmp"
- vault "github.com/hashicorp/vault/api"
- kclient "sigs.k8s.io/controller-runtime/pkg/client"
- esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
- "github.com/external-secrets/external-secrets/providers/v1/vault/fake"
- vaultutil "github.com/external-secrets/external-secrets/providers/v1/vault/util"
- testingfake "github.com/external-secrets/external-secrets/runtime/testing/fake"
- )
- func TestGetSecret(t *testing.T) {
- errBoom := errors.New("boom")
- secret := map[string]any{
- "access_key": "access_key",
- "access_secret": "access_secret",
- }
- secretWithNilVal := map[string]any{
- "access_key": "access_key",
- "access_secret": "access_secret",
- "token": nil,
- }
- secretWithNestedVal := map[string]any{
- "access_key": "access_key",
- "access_secret": "access_secret",
- "nested.bar": "something different",
- "nested": map[string]string{
- "foo": "oke",
- "bar": "also ok?",
- },
- "list_of_values": []string{
- "first_value",
- "second_value",
- "third_value",
- },
- "json_number": json.Number("42"),
- }
- metadataWithSystemVal := map[string]any{
- "custom_metadata": map[string]any{
- "access_key": "access_key",
- "created_time": "custom-time",
- },
- "created_time": "2026-04-27T12:32:50.049520998Z",
- "current_version": json.Number("2"),
- "delete_version_after": "0s",
- "max_versions": json.Number("0"),
- "cas_required": false,
- }
- metadataNilCustomVal := map[string]any{
- "custom_metadata": nil,
- "created_time": "2026-04-27T12:32:50.049520998Z",
- "current_version": json.Number("2"),
- "delete_version_after": "0s",
- }
- type args struct {
- store *esv1.VaultProvider
- kube kclient.Client
- vLogical vaultutil.Logical
- ns string
- data esv1.ExternalSecretDataRemoteRef
- }
- type want struct {
- err error
- val []byte
- }
- cases := map[string]struct {
- reason string
- args args
- want want
- }{
- "ReadSecret": {
- reason: "Should return the secret with property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "access_key",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("access_key"),
- },
- },
- "ReadSecretWithNil": {
- reason: "Should return the secret with property if it has a nil val",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "access_key",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNilVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("access_key"),
- },
- },
- "ReadSecretWithoutProperty": {
- reason: "Should return the json encoded secret without property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{},
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte(`{"access_key":"access_key","access_secret":"access_secret"}`),
- },
- },
- "ReadSecretWithNestedValue": {
- reason: "Should return a nested property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "nested.foo",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("oke"),
- },
- },
- "ReadSecretWithNestedValueFromData": {
- reason: "Should return a nested property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- //
- Property: "nested.bar",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("something different"),
- },
- },
- "ReadSecretWithMissingValueFromData": {
- reason: "Should return a NoSecretErr",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "not-relevant",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, nil),
- },
- },
- want: want{
- err: esv1.NoSecretErr,
- val: nil,
- },
- },
- "ReadSecretWithSliceValue": {
- reason: "Should return property as a joined slice",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "list_of_values",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("first_value\nsecond_value\nthird_value"),
- },
- },
- "ReadSecretWithJsonNumber": {
- reason: "Should return parsed json.Number property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "json_number",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("42"),
- },
- },
- "NonexistentProperty": {
- reason: "Should return error property does not exist.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "nop.doesnt.exist",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNestedVal, nil),
- },
- },
- want: want{
- err: fmt.Errorf(errSecretKeyFmt, "nop.doesnt.exist"),
- },
- },
- "ReadSecretError": {
- reason: "Should return error if vault client fails to read secret.",
- args: args{
- store: makeSecretStore().Spec.Provider.Vault,
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errBoom),
- },
- },
- want: want{
- err: fmt.Errorf(errReadSecret, errBoom),
- },
- },
- "ReadSecretNotFound": {
- reason: "Secret doesn't exist",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "access_key",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: func(_ context.Context, _ string, _ map[string][]string) (*vault.Secret, error) {
- return nil, nil
- },
- },
- },
- want: want{
- err: esv1.NoSecretError{},
- },
- },
- "ReadSecretMetadataWithoutProperty": {
- reason: "Should return the json encoded metadata",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(secret, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte(`{"access_key":"access_key","access_secret":"access_secret"}`),
- },
- },
- "ReadSecretMetadataWithProperty": {
- reason: "Should return the access_key value from the metadata",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- Property: "access_key",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(secret, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("access_key"),
- },
- },
- "FailReadSecretMetadataInvalidProperty": {
- reason: "Should return error of non existent key inmetadata",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- Property: "does_not_exist",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(secret, nil),
- },
- },
- want: want{
- err: fmt.Errorf(errSecretKeyFmt, "does_not_exist"),
- },
- },
- "FailReadSecretMetadataNoMetadata": {
- reason: "Should return the access_key value from the metadata",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(nil, nil),
- },
- },
- want: want{
- err: errors.New(errNotFound),
- },
- },
- "FailReadSecretMetadataWrongVersion": {
- reason: "Should return the access_key value from the metadata",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(nil, nil),
- },
- },
- want: want{
- err: errors.New(errUnsupportedMetadataKvVersion),
- },
- },
- "ReadSecretMetadataWithSystemMetadata": {
- reason: "Should return system metadata alongside custom metadata, custom keys taking precedence",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(metadataWithSystemVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte(`{"access_key":"access_key","created_time":"custom-time","current_version":"2","delete_version_after":"0s"}`),
- },
- },
- "ReadSecretMetadataSystemProperty": {
- reason: "Should return a single system metadata value via property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- Property: "current_version",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(metadataWithSystemVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte("2"),
- },
- },
- "ReadSecretMetadataNilCustomMetadata": {
- reason: "Should return system metadata when custom_metadata is null",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- MetadataPolicy: "Fetch",
- },
- vLogical: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadMetadataWithContextFn(metadataNilCustomVal, nil),
- },
- },
- want: want{
- err: nil,
- val: []byte(`{"created_time":"2026-04-27T12:32:50.049520998Z","current_version":"2","delete_version_after":"0s"}`),
- },
- },
- }
- for name, tc := range cases {
- t.Run(name, func(t *testing.T) {
- vStore := &client{
- kube: tc.args.kube,
- logical: tc.args.vLogical,
- store: tc.args.store,
- namespace: tc.args.ns,
- }
- val, err := vStore.GetSecret(context.Background(), tc.args.data)
- if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
- t.Errorf("\n%s\nvault.GetSecret(...): -want error, +got error:\n%s", tc.reason, diff)
- }
- if diff := cmp.Diff(string(tc.want.val), string(val)); diff != "" {
- t.Errorf("\n%s\nvault.GetSecret(...): -want val, +got val:\n%s", tc.reason, diff)
- }
- })
- }
- }
- func TestGetSecretMap(t *testing.T) {
- errBoom := errors.New("boom")
- secret := map[string]any{
- "access_key": "access_key",
- "access_secret": "access_secret",
- }
- secretWithSpecialCharacter := map[string]any{
- "access_key": "acc<ess_&ke.,y",
- "access_secret": "acce&?ss_s>ecret",
- }
- secretWithNilVal := map[string]any{
- "access_key": "access_key",
- "access_secret": "access_secret",
- "token": nil,
- }
- secretWithNestedVal := map[string]any{
- "access_key": "access_key",
- "access_secret": "access_secret",
- "nested": map[string]any{
- "foo": map[string]string{
- "oke": "yup",
- "mhkeih": "yada yada",
- },
- },
- }
- secretWithTypes := map[string]any{
- "access_secret": "access_secret",
- "f32": float32(2.12),
- "f64": float64(2.1234534153423423),
- "int": 42,
- "bool": true,
- "bt": []byte("foobar"),
- }
- type args struct {
- store *esv1.VaultProvider
- kube kclient.Client
- vClient vaultutil.Logical
- ns string
- data esv1.ExternalSecretDataRemoteRef
- }
- type want struct {
- err error
- val map[string][]byte
- }
- cases := map[string]struct {
- reason string
- args args
- want want
- }{
- "ReadSecretKV1": {
- reason: "Should read a v1 secret",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_key": []byte("access_key"),
- "access_secret": []byte("access_secret"),
- },
- },
- },
- "ReadSecretKV2": {
- reason: "Should read a v2 secret",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secret,
- }, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_key": []byte("access_key"),
- "access_secret": []byte("access_secret"),
- },
- },
- },
- "ReadSecretWithSpecialCharactersKV1": {
- reason: "Should read a v1 secret with special characters",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithSpecialCharacter, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_key": []byte("acc<ess_&ke.,y"),
- "access_secret": []byte("acce&?ss_s>ecret"),
- },
- },
- },
- "ReadSecretWithSpecialCharactersKV2": {
- reason: "Should read a v2 secret with special characters",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secretWithSpecialCharacter,
- }, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_key": []byte("acc<ess_&ke.,y"),
- "access_secret": []byte("acce&?ss_s>ecret"),
- },
- },
- },
- "ReadSecretWithNilValueKV1": {
- reason: "Should read v1 secret with a nil value",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNilVal, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_key": []byte("access_key"),
- "access_secret": []byte("access_secret"),
- "token": []byte(nil),
- },
- },
- },
- "ReadSecretWithNilValueKV2": {
- reason: "Should read v2 secret with a nil value",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secretWithNilVal}, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_key": []byte("access_key"),
- "access_secret": []byte("access_secret"),
- "token": []byte(nil),
- },
- },
- },
- "ReadSecretWithTypesKV2": {
- reason: "Should read v2 secret with different types",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secretWithTypes}, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "access_secret": []byte("access_secret"),
- "f32": []byte("2.12"),
- "f64": []byte("2.1234534153423423"),
- "int": []byte("42"),
- "bool": []byte("true"),
- "bt": []byte("Zm9vYmFy"), // base64
- },
- },
- },
- "ReadNestedSecret": {
- reason: "Should read the secret with nested property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "nested",
- },
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secretWithNestedVal}, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "foo": []byte(`{"mhkeih":"yada yada","oke":"yup"}`),
- },
- },
- },
- "ReadDeeplyNestedSecret": {
- reason: "Should read the secret for deeply nested property",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- data: esv1.ExternalSecretDataRemoteRef{
- Property: "nested.foo",
- },
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secretWithNestedVal}, nil),
- },
- },
- want: want{
- err: nil,
- val: map[string][]byte{
- "oke": []byte("yup"),
- "mhkeih": []byte("yada yada"),
- },
- },
- },
- "ReadSecretError": {
- reason: "Should return error if vault client fails to read secret.",
- args: args{
- store: makeSecretStore().Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errBoom),
- },
- },
- want: want{
- err: fmt.Errorf(errReadSecret, errBoom),
- },
- },
- }
- for name, tc := range cases {
- t.Run(name, func(t *testing.T) {
- vStore := &client{
- kube: tc.args.kube,
- logical: tc.args.vClient,
- store: tc.args.store,
- namespace: tc.args.ns,
- }
- val, err := vStore.GetSecretMap(context.Background(), tc.args.data)
- if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
- t.Errorf("\n%s\nvault.GetSecretMap(...): -want error, +got error:\n%s", tc.reason, diff)
- }
- if diff := cmp.Diff(tc.want.val, val); diff != "" {
- t.Errorf("\n%s\nvault.GetSecretMap(...): -want val, +got val:\n%s", tc.reason, diff)
- }
- })
- }
- }
- func TestGetSecretPath(t *testing.T) {
- storeV2 := makeValidSecretStore()
- storeV2NoPath := storeV2.DeepCopy()
- multiPath := "secret/path"
- storeV2.Spec.Provider.Vault.Path = &multiPath
- storeV2NoPath.Spec.Provider.Vault.Path = nil
- storeV1 := makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1)
- storeV1NoPath := storeV1.DeepCopy()
- storeV1.Spec.Provider.Vault.Path = &multiPath
- storeV1NoPath.Spec.Provider.Vault.Path = nil
- type args struct {
- store *esv1.VaultProvider
- path string
- expected string
- }
- cases := map[string]struct {
- reason string
- args args
- }{
- "PathWithoutFormatV2": {
- reason: "path should compose with mount point if set",
- args: args{
- store: storeV2.Spec.Provider.Vault,
- path: "secret/path/data/test",
- expected: "secret/path/data/test",
- },
- },
- "PathWithoutFormatV2_NoData": {
- reason: "path should compose with mount point if set without data",
- args: args{
- store: storeV2.Spec.Provider.Vault,
- path: "secret/path/test",
- expected: "secret/path/data/test",
- },
- },
- "PathWithoutFormatV2_NoPath": {
- reason: "if no mountpoint and no data available, needs to be set in second element",
- args: args{
- store: storeV2NoPath.Spec.Provider.Vault,
- path: "secret/test/big/path",
- expected: "secret/data/test/big/path",
- },
- },
- "PathWithoutFormatV2_NoPathWithData": {
- reason: "if data is available, should respect order",
- args: args{
- store: storeV2NoPath.Spec.Provider.Vault,
- path: "secret/test/data/not/the/first/and/data/twice",
- expected: "secret/test/data/not/the/first/and/data/twice",
- },
- },
- "PathWithoutFormatV1": {
- reason: "v1 mountpoint should be added but not enforce 'data'",
- args: args{
- store: storeV1.Spec.Provider.Vault,
- path: "secret/path/test",
- expected: "secret/path/test",
- },
- },
- "PathWithoutFormatV1_NoPath": {
- reason: "Should not append any path information if v1 with no mountpoint",
- args: args{
- store: storeV1NoPath.Spec.Provider.Vault,
- path: "secret/test",
- expected: "secret/test",
- },
- },
- "WithoutPathButMountpointV2": {
- reason: "Mountpoint needs to be set in addition to data",
- args: args{
- store: storeV2.Spec.Provider.Vault,
- path: "test",
- expected: "secret/path/data/test",
- },
- },
- "WithoutPathButMountpointV1": {
- reason: "Mountpoint needs to be set in addition to data",
- args: args{
- store: storeV1.Spec.Provider.Vault,
- path: "test",
- expected: "secret/path/test",
- },
- },
- }
- for name, tc := range cases {
- t.Run(name, func(t *testing.T) {
- vStore := &client{
- store: tc.args.store,
- }
- want := vStore.buildPath(tc.args.path)
- if diff := cmp.Diff(want, tc.args.expected); diff != "" {
- t.Errorf("\n%s\nvault.buildPath(...): -want expected, +got error:\n%s", tc.reason, diff)
- }
- })
- }
- }
- func TestGetSecretMetadataPath(t *testing.T) {
- storeV2 := makeValidSecretStore()
- storeV2NoPath := storeV2.DeepCopy()
- multiPath := "secret/path"
- storeV2.Spec.Provider.Vault.Path = &multiPath
- storeV2NoPath.Spec.Provider.Vault.Path = nil
- storeV1 := makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1)
- storeV1NoPath := storeV1.DeepCopy()
- storeV1.Spec.Provider.Vault.Path = &multiPath
- storeV1NoPath.Spec.Provider.Vault.Path = nil
- type args struct {
- store *esv1.VaultProvider
- path string
- expected string
- }
- cases := map[string]struct {
- reason string
- args args
- }{
- "PathForV1": {
- reason: "path should compose with mount point if set",
- args: args{
- store: storeV1.Spec.Provider.Vault,
- path: "data/test",
- expected: "secret/path/data/test",
- },
- },
- "PathForV2": {
- reason: "path should compose with mount point if set without data",
- args: args{
- store: storeV2.Spec.Provider.Vault,
- path: "secret/path/data/test",
- expected: "secret/path/metadata/secret/path/data/test",
- },
- },
- "PathForV2WithData": {
- reason: "if data is in the path it shouldn't be changed",
- args: args{
- store: storeV2NoPath.Spec.Provider.Vault,
- path: "my_data/data/path",
- expected: "my_data/metadata/path",
- },
- },
- }
- for name, tc := range cases {
- t.Run(name, func(t *testing.T) {
- vStore := &client{
- store: tc.args.store,
- }
- want, _ := vStore.buildMetadataPath(tc.args.path)
- if diff := cmp.Diff(want, tc.args.expected); diff != "" {
- t.Errorf("\n%s\nvault.buildPath(...): -want expected, +got error:\n%s", tc.reason, diff)
- }
- })
- }
- }
- func TestSecretExists(t *testing.T) {
- secret := map[string]any{
- "foo": "bar",
- }
- secretWithNil := map[string]any{
- "hi": nil,
- }
- errNope := errors.New("nope")
- type args struct {
- store *esv1.VaultProvider
- vClient vaultutil.Logical
- }
- type want struct {
- exists bool
- err error
- }
- tests := map[string]struct {
- reason string
- args args
- ref *testingfake.PushSecretData
- want want
- }{
- "NoExistingSecretV1": {
- reason: "Should return false, nil if secret does not exist in provider.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, esv1.NoSecretError{}),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret"},
- want: want{
- exists: false,
- err: nil,
- },
- },
- "NoExistingSecretV2": {
- reason: "Should return false, nil if secret does not exist in provider.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, esv1.NoSecretError{}),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret"},
- want: want{
- exists: false,
- err: nil,
- },
- },
- "NoExistingSecretWithPropertyV2": {
- reason: "Should return false, nil if secret with property does not exist in provider.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secret,
- }, nil),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "different"},
- want: want{
- exists: false,
- err: nil,
- },
- },
- "NoExistingSecretWithPropertyV1": {
- reason: "Should return false, nil if secret with property does not exist in provider.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "different"},
- want: want{
- exists: false,
- err: nil,
- },
- },
- "ExistingSecretV1": {
- reason: "Should return true, nil if secret exists in provider.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secret, nil),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret"},
- want: want{
- exists: true,
- err: nil,
- },
- },
- "ExistingSecretV2": {
- reason: "Should return true, nil if secret exists in provider.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secret,
- }, nil),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret"},
- want: want{
- exists: true,
- err: nil,
- },
- },
- "ExistingSecretWithNilV1": {
- reason: "Should return false, nil if secret in provider has nil value.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(secretWithNil, nil),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "hi"},
- want: want{
- exists: false,
- err: nil,
- },
- },
- "ExistingSecretWithNilV2": {
- reason: "Should return false, nil if secret in provider has nil value.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(map[string]any{
- "data": secretWithNil,
- }, nil),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret", Property: "hi"},
- want: want{
- exists: false,
- err: nil,
- },
- },
- "ErrorReadingSecretV1": {
- reason: "Should return error if secret existence cannot be verified.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV1).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errNope),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret"},
- want: want{
- exists: false,
- err: fmt.Errorf(errReadSecret, errNope),
- },
- },
- "ErrorReadingSecretV2": {
- reason: "Should return error if secret existence cannot be verified.",
- args: args{
- store: makeValidSecretStoreWithVersion(esv1.VaultKVStoreV2).Spec.Provider.Vault,
- vClient: &fake.Logical{
- ReadWithDataWithContextFn: fake.NewReadWithContextFn(nil, errNope),
- },
- },
- ref: &testingfake.PushSecretData{RemoteKey: "secret"},
- want: want{
- exists: false,
- err: fmt.Errorf(errReadSecret, errNope),
- },
- },
- }
- for name, tc := range tests {
- t.Run(name, func(t *testing.T) {
- client := &client{
- logical: tc.args.vClient,
- store: tc.args.store,
- }
- exists, err := client.SecretExists(context.Background(), tc.ref)
- if diff := cmp.Diff(exists, tc.want.exists); diff != "" {
- t.Errorf("\n%s\nvault.SecretExists(...): -want exists, +got exists:\n%s", tc.reason, diff)
- }
- if diff := cmp.Diff(tc.want.err, err, EquateErrors()); diff != "" {
- t.Errorf("\n%s\nvault.GetSecret(...): -want error, +got error:\n%s", tc.reason, diff)
- }
- })
- }
- }
- // EquateErrors returns true if the supplied errors are of the same type and
- // produce identical strings. This mirrors the error comparison behavior of
- // https://github.com/go-test/deep, which most Crossplane tests targeted before
- // we switched to go-cmp.
- //
- // This differs from cmpopts.EquateErrors, which does not test for error strings
- // and instead returns whether one error 'is' (in the errors.Is sense) the
- // other.
- func EquateErrors() cmp.Option {
- return cmp.Comparer(func(a, b error) bool {
- if a == nil || b == nil {
- return a == nil && b == nil
- }
- av := reflect.ValueOf(a)
- bv := reflect.ValueOf(b)
- if av.Type() != bv.Type() {
- return false
- }
- return a.Error() == b.Error()
- })
- }
|