| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011 |
- /*
- Copyright © 2025 ESO Maintainer Team
- 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 secretsmanager
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "reflect"
- "strings"
- "testing"
- "time"
- "github.com/aws/aws-sdk-go-v2/aws"
- "github.com/aws/aws-sdk-go-v2/credentials"
- awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
- "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
- "github.com/external-secrets/external-secrets/pkg/esutils/metadata"
- "github.com/google/go-cmp/cmp"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- corev1 "k8s.io/api/core/v1"
- apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/utils/ptr"
- esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
- fakesm "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager/fake"
- "github.com/external-secrets/external-secrets/pkg/provider/aws/util"
- "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
- )
- type secretsManagerTestCase struct {
- fakeClient *fakesm.Client
- apiInput *awssm.GetSecretValueInput
- apiOutput *awssm.GetSecretValueOutput
- remoteRef *esv1.ExternalSecretDataRemoteRef
- apiErr error
- expectError string
- expectedSecret string
- // for testing secretmap
- expectedData map[string][]byte
- // for testing caching
- expectedCounter *int
- prefix string
- }
- const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
- const (
- tagname1 = "tagname1"
- tagvalue1 = "tagvalue1"
- tagname2 = "tagname2"
- tagvalue2 = "tagvalue2"
- fakeKey = "fake-key"
- )
- func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
- smtc := secretsManagerTestCase{
- fakeClient: fakesm.NewClient(),
- apiInput: makeValidAPIInput(),
- remoteRef: makeValidRemoteRef(),
- apiOutput: makeValidAPIOutput(),
- apiErr: nil,
- expectError: "",
- expectedSecret: "",
- expectedData: map[string][]byte{},
- }
- smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
- return &smtc
- }
- func makeValidRemoteRef() *esv1.ExternalSecretDataRemoteRef {
- return &esv1.ExternalSecretDataRemoteRef{
- Key: "/baz",
- Version: "AWSCURRENT",
- }
- }
- func makeValidAPIInput() *awssm.GetSecretValueInput {
- return &awssm.GetSecretValueInput{
- SecretId: aws.String("/baz"),
- VersionStage: aws.String("AWSCURRENT"),
- }
- }
- func makeValidAPIOutput() *awssm.GetSecretValueOutput {
- return &awssm.GetSecretValueOutput{
- SecretString: aws.String(""),
- }
- }
- func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
- smtc := makeValidSecretsManagerTestCase()
- for _, fn := range tweaks {
- fn(smtc)
- }
- smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
- return smtc
- }
- // This case can be shared by both GetSecret and GetSecretMap tests.
- // bad case: set apiErr.
- var setAPIErr = func(smtc *secretsManagerTestCase) {
- smtc.apiErr = errors.New("oh no")
- smtc.expectError = "oh no"
- }
- func TestSecretsManagerResolver(t *testing.T) {
- endpointEnvKey := SecretsManagerEndpointEnv
- endpointURL := "http://sm.foo"
- t.Setenv(endpointEnvKey, endpointURL)
- f, err := customEndpointResolver{}.ResolveEndpoint(context.Background(), awssm.EndpointParameters{})
- assert.Nil(t, err)
- assert.Equal(t, endpointURL, f.URI.String())
- }
- // test the sm<->aws interface
- // make sure correct values are passed and errors are handled accordingly.
- func TestSecretsManagerGetSecret(t *testing.T) {
- // good case: default version is set
- // key is passed in, output is sent back
- setSecretString := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String("testtesttest")
- smtc.expectedSecret = "testtesttest"
- }
- // good case: key is passed in with prefix
- setSecretStringWithPrefix := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.Key = "secret-key"
- smtc.apiInput = &awssm.GetSecretValueInput{
- SecretId: aws.String("my-prefix/secret-key"),
- VersionStage: aws.String("AWSCURRENT"),
- }
- smtc.prefix = "my-prefix/"
- }
- // good case: extract property
- // Testing that the property exists in the SecretString
- setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.Property = "/shmoo"
- smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
- smtc.expectedSecret = "bang"
- }
- // bad case: missing property
- setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.Property = "INVALPROP"
- smtc.expectError = "key INVALPROP does not exist in secret"
- }
- // bad case: extract property failure due to invalid json
- setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.Property = "INVALPROP"
- smtc.apiOutput.SecretString = aws.String(`------`)
- smtc.expectError = "key INVALPROP does not exist in secret"
- }
- // good case: set .SecretString to nil but set binary with value
- setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretBinary = []byte("yesplease")
- // needs to be set as nil, empty quotes ("") is considered existing
- smtc.apiOutput.SecretString = nil
- smtc.expectedSecret = "yesplease"
- }
- // bad case: both .SecretString and .SecretBinary are nil
- setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretBinary = nil
- smtc.apiOutput.SecretString = nil
- smtc.expectError = "no secret string nor binary for key"
- }
- // good case: secretOut.SecretBinary JSON parsing
- setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = nil
- smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
- smtc.remoteRef.Property = "foobar.baz"
- smtc.expectedSecret = "nestedval"
- }
- // good case: secretOut.SecretBinary no JSON parsing if name on key
- setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = nil
- smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
- smtc.remoteRef.Property = "foobar.baz"
- smtc.expectedSecret = "nestedval"
- }
- // good case: custom version stage set
- setCustomVersionStage := func(smtc *secretsManagerTestCase) {
- smtc.apiInput.VersionStage = aws.String("1234")
- smtc.remoteRef.Version = "1234"
- smtc.apiOutput.SecretString = aws.String("FOOBA!")
- smtc.expectedSecret = "FOOBA!"
- }
- // good case: custom version id set
- setCustomVersionID := func(smtc *secretsManagerTestCase) {
- smtc.apiInput.VersionStage = nil
- smtc.apiInput.VersionId = aws.String("1234-5678")
- smtc.remoteRef.Version = "uuid/1234-5678"
- smtc.apiOutput.SecretString = aws.String("myvalue")
- smtc.expectedSecret = "myvalue"
- }
- fetchMetadata := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
- describeSecretOutput := &awssm.DescribeSecretOutput{
- Tags: getTagSlice(),
- }
- smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
- jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
- smtc.apiOutput.SecretString = &jsonTags
- smtc.expectedSecret = jsonTags
- }
- fetchMetadataProperty := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
- describeSecretOutput := &awssm.DescribeSecretOutput{
- Tags: getTagSlice(),
- }
- smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
- smtc.remoteRef.Property = tagname2
- jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
- smtc.apiOutput.SecretString = &jsonTags
- smtc.expectedSecret = tagvalue2
- }
- failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
- smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
- describeSecretOutput := &awssm.DescribeSecretOutput{
- Tags: getTagSlice(),
- }
- smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
- smtc.remoteRef.Property = "fail"
- jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
- smtc.apiOutput.SecretString = &jsonTags
- smtc.expectError = "key fail does not exist in secret /baz"
- }
- successCases := []*secretsManagerTestCase{
- makeValidSecretsManagerTestCase(),
- makeValidSecretsManagerTestCaseCustom(setSecretString),
- makeValidSecretsManagerTestCaseCustom(setSecretStringWithPrefix),
- makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
- makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
- makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
- makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
- makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
- makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
- makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
- makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
- makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
- makeValidSecretsManagerTestCaseCustom(setAPIErr),
- makeValidSecretsManagerTestCaseCustom(fetchMetadata),
- makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
- makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
- }
- for k, v := range successCases {
- sm := SecretsManager{
- cache: make(map[string]*awssm.GetSecretValueOutput),
- client: v.fakeClient,
- prefix: v.prefix,
- }
- out, err := sm.GetSecret(context.Background(), *v.remoteRef)
- if !ErrorContains(err, v.expectError) {
- t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
- }
- if err == nil && string(out) != v.expectedSecret {
- t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
- }
- }
- }
- func TestCaching(t *testing.T) {
- fakeClient := fakesm.NewClient()
- // good case: first call, since we are using the same key, results should be cached and the counter should not go
- // over 1
- firstCall := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
- smtc.remoteRef.Property = "foo"
- smtc.expectedSecret = "bar"
- smtc.expectedCounter = aws.Int(1)
- smtc.fakeClient = fakeClient
- }
- secondCall := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
- smtc.remoteRef.Property = "bar"
- smtc.expectedSecret = "vodka"
- smtc.expectedCounter = aws.Int(1)
- smtc.fakeClient = fakeClient
- }
- notCachedCall := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
- smtc.remoteRef.Property = "sheldon"
- smtc.expectedSecret = "bazinga"
- smtc.expectedCounter = aws.Int(2)
- smtc.fakeClient = fakeClient
- smtc.apiInput.SecretId = aws.String("xyz")
- smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
- }
- cachedCases := []*secretsManagerTestCase{
- makeValidSecretsManagerTestCaseCustom(firstCall),
- makeValidSecretsManagerTestCaseCustom(firstCall),
- makeValidSecretsManagerTestCaseCustom(secondCall),
- makeValidSecretsManagerTestCaseCustom(notCachedCall),
- }
- sm := SecretsManager{
- cache: make(map[string]*awssm.GetSecretValueOutput),
- }
- for k, v := range cachedCases {
- sm.client = v.fakeClient
- out, err := sm.GetSecret(context.Background(), *v.remoteRef)
- if !ErrorContains(err, v.expectError) {
- t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
- }
- if err == nil && string(out) != v.expectedSecret {
- t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
- }
- if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
- t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
- }
- }
- }
- func TestGetSecretMap(t *testing.T) {
- // good case: default version & deserialization
- setDeserialization := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
- smtc.expectedData["foo"] = []byte("bar")
- }
- // good case: nested json
- setNestedJSON := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
- smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
- }
- // good case: caching
- cachedMap := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
- smtc.expectedData["foo"] = []byte("bar")
- smtc.expectedData["plus"] = []byte("one")
- smtc.expectedCounter = aws.Int(1)
- }
- // bad case: invalid json
- setInvalidJSON := func(smtc *secretsManagerTestCase) {
- smtc.apiOutput.SecretString = aws.String(`-----------------`)
- smtc.expectError = "unable to unmarshal secret"
- }
- successCases := []*secretsManagerTestCase{
- makeValidSecretsManagerTestCaseCustom(setDeserialization),
- makeValidSecretsManagerTestCaseCustom(setNestedJSON),
- makeValidSecretsManagerTestCaseCustom(setAPIErr),
- makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
- makeValidSecretsManagerTestCaseCustom(cachedMap),
- }
- for k, v := range successCases {
- sm := SecretsManager{
- cache: make(map[string]*awssm.GetSecretValueOutput),
- client: v.fakeClient,
- }
- out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
- if !ErrorContains(err, v.expectError) {
- t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
- }
- if err == nil && !cmp.Equal(out, v.expectedData) {
- t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
- }
- if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
- t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
- }
- }
- }
- func ErrorContains(out error, want string) bool {
- if out == nil {
- return want == ""
- }
- if want == "" {
- return false
- }
- return strings.Contains(out.Error(), want)
- }
- func TestSetSecret(t *testing.T) {
- managedBy := managedBy
- notManagedBy := "not-managed-by"
- secretKey := "fake-secret-key"
- secretValue := []byte("fake-value")
- fakeSecret := &corev1.Secret{
- Data: map[string][]byte{
- secretKey: secretValue,
- },
- }
- externalSecrets := externalSecrets
- noPermission := errors.New("no permission")
- arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
- getSecretCorrectErr := types.ResourceNotFoundException{}
- getSecretWrongErr := types.InvalidRequestException{}
- secretOutput := &awssm.CreateSecretOutput{
- ARN: &arn,
- }
- externalSecretsTag := []types.Tag{
- {
- Key: &managedBy,
- Value: &externalSecrets,
- },
- {
- Key: ptr.To("taname1"),
- Value: ptr.To("tagvalue1"),
- },
- }
- externalSecretsTagFaulty := []types.Tag{
- {
- Key: ¬ManagedBy,
- Value: &externalSecrets,
- },
- }
- tagSecretOutputNoVersions := &awssm.DescribeSecretOutput{
- ARN: &arn,
- Tags: externalSecretsTag,
- }
- defaultVersion := "00000000-0000-0000-0000-000000000002"
- tagSecretOutput := &awssm.DescribeSecretOutput{
- ARN: &arn,
- Tags: externalSecretsTag,
- VersionIdsToStages: map[string][]string{
- defaultVersion: {"AWSCURRENT"},
- },
- }
- tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
- ARN: &arn,
- Tags: externalSecretsTagFaulty,
- }
- tagSecretOutputFrom := func(versionId string) *awssm.DescribeSecretOutput {
- return &awssm.DescribeSecretOutput{
- ARN: &arn,
- Tags: externalSecretsTag,
- VersionIdsToStages: map[string][]string{
- versionId: {"AWSCURRENT"},
- },
- }
- }
- initialVersion := "00000000-0000-0000-0000-000000000001"
- defaultUpdatedVersion := "6c70d57a-f53d-bf4d-9525-3503dd5abe8c"
- randomUUIDVersion := "9d6202c2-c216-433e-a2f0-5836c4f025af"
- randomUUIDVersionIncremented := "4346824b-7da1-4d82-addf-dee197fd5d71"
- unparsableVersion := "IAM UNPARSABLE"
- secretValueOutput := &awssm.GetSecretValueOutput{
- ARN: &arn,
- VersionId: &defaultVersion,
- }
- secretValueOutput2 := &awssm.GetSecretValueOutput{
- ARN: &arn,
- SecretBinary: secretValue,
- VersionId: &defaultVersion,
- }
- blankDescribeSecretOutput := &awssm.DescribeSecretOutput{}
- type params struct {
- s string
- b []byte
- version *string
- }
- secretValueOutputFrom := func(params params) *awssm.GetSecretValueOutput {
- var version *string
- if params.version == nil {
- version = &defaultVersion
- } else {
- version = params.version
- }
- return &awssm.GetSecretValueOutput{
- ARN: &arn,
- SecretString: ¶ms.s,
- SecretBinary: params.b,
- VersionId: version,
- }
- }
- putSecretOutput := &awssm.PutSecretValueOutput{
- ARN: &arn,
- }
- pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: ""}
- pushSecretDataWithoutSecretKey := fake.PushSecretData{RemoteKey: fakeKey, Property: ""}
- pushSecretDataWithMetadata := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
- Raw: []byte(`{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": {
- "secretPushFormat": "string"
- }
- }`)}}
- pushSecretDataWithProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "other-fake-property"}
- type args struct {
- store *esv1.AWSProvider
- client fakesm.Client
- pushSecretData fake.PushSecretData
- newUUID string
- }
- type want struct {
- err error
- }
- tests := map[string]struct {
- reason string
- args args
- want want
- }{
- "SetSecretSucceedsWithExistingSecret": {
- reason: "a secret can be pushed to aws secrets manager when it already exists",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretSucceedsWithExistingSecretButNoSecretVersionsWithoutProperty": {
- reason: "a secret can be pushed to aws secrets manager when it already exists but has no secret versions",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputNoVersions, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`fake-value`),
- Version: aws.String(initialVersion),
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretSucceedsWithExistingSecretButNoSecretVersionsWithProperty": {
- reason: "a secret can be pushed to aws secrets manager when it already exists but has no secret versions",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputNoVersions, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`{"other-fake-property":"fake-value"}`),
- Version: aws.String(initialVersion),
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretSucceedsWithoutSecretKey": {
- reason: "a secret can be pushed to aws secrets manager without secret key",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithoutSecretKey,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretSucceedsWithExistingSecretAndStringFormat": {
- reason: "a secret can be pushed to aws secrets manager when it already exists",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithMetadata,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretSucceedsWithExistingSecretAndKMSKeyAndDescription": {
- reason: "a secret can be pushed to aws secrets manager when it already exists",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, &getSecretCorrectErr),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- },
- pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
- Raw: []byte(`{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": {
- "kmsKeyID": "bb123123-b2b0-4f60-ac3a-44a13f0e6b6c",
- "description": "this is a description"
- }
- }`)}},
- },
- want: want{
- err: &getSecretCorrectErr,
- },
- },
- "SetSecretSucceedsWithExistingSecretAndAdditionalTags": {
- reason: "a secret can be pushed to aws secrets manager when it already exists",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
- Raw: []byte(`{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": {
- "tags": {"tagname12": "tagvalue1"}
- }
- }`)}},
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretSucceedsWithNewSecret": {
- reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
- CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithNewSecret": {
- reason: "if a new secret is pushed to aws sm and a pushSecretData property is specified, create a json secret with the pushSecretData property as a key",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
- CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil, []byte(`{"other-fake-property":"fake-value"}`)),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyBinary": {
- reason: "when a pushSecretData property is specified, this property will be added to the sm secret if it is currently absent (sm secret is binary)",
- args: args{
- newUUID: defaultUpdatedVersion,
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{b: []byte((`{"fake-property":"fake-value"}`))}), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
- Version: &defaultUpdatedVersion,
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithExistingSecretAndRandomUUIDVersion": {
- reason: "When a secret version is not specified, the client sets a random uuid by default. We should treat a version that can't be parsed to an int as not having a version",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- newUUID: randomUUIDVersionIncremented,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
- b: []byte((`{"fake-property":"fake-value"}`)),
- version: &randomUUIDVersion,
- }), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFrom(randomUUIDVersion), nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
- Version: &randomUUIDVersionIncremented,
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithExistingSecretAndVersionThatCantBeParsed": {
- reason: "A manually set secret version doesn't have to be a UUID",
- args: args{
- newUUID: unparsableVersion,
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
- b: []byte((`{"fake-property":"fake-value"}`)),
- version: &unparsableVersion,
- }), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte((`fake-value`)),
- Version: &unparsableVersion,
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithExistingSecretAndAbsentVersion": {
- reason: "When a secret version is not specified, set it to 1",
- args: args{
- newUUID: initialVersion,
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
- ARN: &arn,
- SecretBinary: []byte((`{"fake-property":"fake-value"}`)),
- }, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
- Version: &initialVersion,
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyString": {
- reason: "when a pushSecretData property is specified, this property will be added to the sm secret if it is currently absent (sm secret is a string)",
- args: args{
- newUUID: defaultUpdatedVersion,
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":"fake-value"}`}), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
- Version: &defaultUpdatedVersion,
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyWithDot": {
- reason: "when a pushSecretData property is specified, this property will be added to the sm secret if it is currently absent (pushSecretData property is a sub-object)",
- args: args{
- newUUID: defaultUpdatedVersion,
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
- SecretBinary: []byte(`{"fake-property":{"fake-property":"fake-value","other-fake-property":"fake-value"}}`),
- Version: &defaultUpdatedVersion,
- }),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "fake-property.other-fake-property"},
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretWithPropertyFailsExistingNonJsonSecret": {
- reason: "setting a pushSecretData property is only supported for json secrets",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `non-json-secret`}), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- },
- pushSecretData: pushSecretDataWithProperty,
- },
- want: want{
- err: errors.New("PushSecret for aws secrets manager with a pushSecretData property requires a json secret"),
- },
- },
- "SetSecretCreateSecretFails": {
- reason: "CreateSecretWithContext returns an error if it fails",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
- CreateSecretFn: fakesm.NewCreateSecretFn(nil, noPermission),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: noPermission,
- },
- },
- "SetSecretGetSecretFails": {
- reason: "GetSecretValueWithContext returns an error if it fails",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, noPermission),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: noPermission,
- },
- },
- "SetSecretWillNotPushSameSecret": {
- reason: "secret with the same value will not be pushed",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput2, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- },
- },
- "SetSecretPutSecretValueFails": {
- reason: "PutSecretValueWithContext returns an error if it fails",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(nil, noPermission),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: noPermission,
- },
- },
- "SetSecretWrongGetSecretErrFails": {
- reason: "DescribeSecret errors out when anything except awssm.ErrCodeResourceNotFoundException",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretWrongErr),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: &getSecretWrongErr,
- },
- },
- "SetSecretDescribeSecretFails": {
- reason: "secret cannot be described",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(nil, noPermission),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: noPermission,
- },
- },
- "SetSecretDoesNotOverwriteUntaggedSecret": {
- reason: "secret cannot be described",
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFaulty, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: errors.New("secret not managed by external-secrets"),
- },
- },
- "PatchSecretTags": {
- reason: "secret key is configured with tags to remove and add",
- args: args{
- store: &esv1.AWSProvider{
- Service: esv1.AWSServiceSecretsManager,
- Region: "eu-west-2",
- },
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
- DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
- ARN: &arn,
- Tags: []types.Tag{
- {Key: &managedBy, Value: &externalSecrets},
- {Key: ptr.To("team"), Value: ptr.To("paradox")},
- },
- }, nil),
- PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
- assert.Len(t, input.Tags, 2)
- assert.Contains(t, input.Tags, types.Tag{Key: &managedBy, Value: &externalSecrets})
- assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("env"), Value: ptr.To("sandbox")})
- }),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
- assert.Len(t, input.TagKeys, 1)
- assert.Equal(t, []string{"team"}, input.TagKeys)
- assert.NotContains(t, input.TagKeys, managedBy)
- }),
- },
- pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
- Raw: []byte(`{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": {
- "secretPushFormat": "string",
- "tags": {
- "env": "sandbox"
- }
- }
- }`)}},
- },
- want: want{
- err: nil,
- },
- },
- }
- for name, tc := range tests {
- t.Run(name, func(t *testing.T) {
- sm := SecretsManager{
- client: &tc.args.client,
- prefix: tc.args.store.Prefix,
- newUUID: func() string { return tc.args.newUUID },
- }
- err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
- // Error nil XOR tc.want.err nil
- if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
- t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
- }
- // if errors are the same type but their contents do not match
- if err != nil && tc.want.err != nil {
- if !strings.Contains(err.Error(), tc.want.err.Error()) {
- t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
- }
- }
- })
- }
- }
- func TestDeleteSecret(t *testing.T) {
- fakeClient := fakesm.Client{}
- managed := managedBy
- manager := externalSecrets
- secretTag := types.Tag{
- Key: &managed,
- Value: &manager,
- }
- type args struct {
- client fakesm.Client
- config esv1.SecretsManager
- prefix string
- getSecretOutput *awssm.GetSecretValueOutput
- describeSecretOutput *awssm.DescribeSecretOutput
- deleteSecretOutput *awssm.DeleteSecretOutput
- getSecretErr error
- describeSecretErr error
- deleteSecretErr error
- }
- type want struct {
- err error
- }
- type testCase struct {
- args args
- want want
- reason string
- }
- tests := map[string]testCase{
- "Deletes Successfully": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{},
- getSecretOutput: &awssm.GetSecretValueOutput{},
- describeSecretOutput: &awssm.DescribeSecretOutput{
- Tags: []types.Tag{secretTag},
- },
- deleteSecretOutput: &awssm.DeleteSecretOutput{},
- getSecretErr: nil,
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: nil,
- },
- reason: "",
- },
- "Deletes Successfully with ForceDeleteWithoutRecovery": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{
- ForceDeleteWithoutRecovery: true,
- },
- getSecretOutput: &awssm.GetSecretValueOutput{},
- describeSecretOutput: &awssm.DescribeSecretOutput{
- Tags: []types.Tag{secretTag},
- },
- deleteSecretOutput: &awssm.DeleteSecretOutput{
- DeletionDate: aws.Time(time.Now()),
- },
- getSecretErr: nil,
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: nil,
- },
- reason: "",
- },
- "Not Managed by ESO": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{},
- getSecretOutput: &awssm.GetSecretValueOutput{},
- describeSecretOutput: &awssm.DescribeSecretOutput{
- Tags: []types.Tag{},
- },
- deleteSecretOutput: &awssm.DeleteSecretOutput{},
- getSecretErr: nil,
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: nil,
- },
- reason: "",
- },
- "Invalid Recovery Window": {
- args: args{
- client: fakesm.Client{},
- config: esv1.SecretsManager{
- RecoveryWindowInDays: 1,
- },
- getSecretOutput: &awssm.GetSecretValueOutput{},
- describeSecretOutput: &awssm.DescribeSecretOutput{
- Tags: []types.Tag{secretTag},
- },
- deleteSecretOutput: &awssm.DeleteSecretOutput{},
- getSecretErr: nil,
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
- },
- reason: "",
- },
- "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
- args: args{
- client: fakesm.Client{},
- config: esv1.SecretsManager{
- RecoveryWindowInDays: 7,
- ForceDeleteWithoutRecovery: true,
- },
- getSecretOutput: &awssm.GetSecretValueOutput{},
- describeSecretOutput: &awssm.DescribeSecretOutput{
- Tags: []types.Tag{secretTag},
- },
- deleteSecretOutput: &awssm.DeleteSecretOutput{},
- getSecretErr: nil,
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
- },
- reason: "",
- },
- "Failed to get Tags": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{},
- getSecretOutput: &awssm.GetSecretValueOutput{},
- describeSecretOutput: nil,
- deleteSecretOutput: nil,
- getSecretErr: nil,
- describeSecretErr: errors.New("failed to get tags"),
- deleteSecretErr: nil,
- },
- want: want{
- err: errors.New("failed to get tags"),
- },
- reason: "",
- },
- "Secret Not Found": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{},
- getSecretOutput: nil,
- describeSecretOutput: nil,
- deleteSecretOutput: nil,
- getSecretErr: errors.New("not here, sorry dude"),
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: errors.New("not here, sorry dude"),
- },
- },
- "Not expected AWS error": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{},
- getSecretOutput: nil,
- describeSecretOutput: nil,
- deleteSecretOutput: nil,
- getSecretErr: errors.New("aws unavailable"),
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: errors.New("aws unavailable"),
- },
- },
- "unexpected error": {
- args: args{
- client: fakeClient,
- config: esv1.SecretsManager{},
- getSecretOutput: nil,
- describeSecretOutput: nil,
- deleteSecretOutput: nil,
- getSecretErr: errors.New("timeout"),
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: errors.New("timeout"),
- },
- },
- "DeleteWithPrefix": {
- args: args{
- client: fakesm.Client{
- GetSecretValueFn: func(ctx context.Context, input *awssm.GetSecretValueInput, opts ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
- // Verify that the input secret ID has the prefix applied
- if *input.SecretId != "my-prefix-"+fakeKey {
- return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
- }
- return &awssm.GetSecretValueOutput{}, nil
- },
- DescribeSecretFn: func(ctx context.Context, input *awssm.DescribeSecretInput, opts ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error) {
- // Verify that the input secret ID has the prefix applied
- if *input.SecretId != "my-prefix-"+fakeKey {
- return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
- }
- return &awssm.DescribeSecretOutput{
- Tags: []types.Tag{secretTag},
- }, nil
- },
- DeleteSecretFn: func(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
- return &awssm.DeleteSecretOutput{}, nil
- },
- },
- config: esv1.SecretsManager{},
- prefix: "my-prefix-",
- getSecretOutput: nil,
- describeSecretOutput: nil,
- deleteSecretOutput: nil,
- getSecretErr: nil,
- describeSecretErr: nil,
- deleteSecretErr: nil,
- },
- want: want{
- err: nil,
- },
- reason: "Verifies that the prefix is correctly applied when deleting a secret",
- },
- }
- for name, tc := range tests {
- t.Run(name, func(t *testing.T) {
- ref := fake.PushSecretData{RemoteKey: fakeKey}
- sm := SecretsManager{
- client: &tc.args.client,
- config: &tc.args.config,
- prefix: tc.args.prefix,
- }
- if tc.args.client.GetSecretValueFn == nil {
- tc.args.client.GetSecretValueFn = fakesm.NewGetSecretValueFn(tc.args.getSecretOutput, tc.args.getSecretErr)
- }
- if tc.args.client.DescribeSecretFn == nil {
- tc.args.client.DescribeSecretFn = fakesm.NewDescribeSecretFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
- }
- if tc.args.client.DeleteSecretFn == nil {
- tc.args.client.DeleteSecretFn = fakesm.NewDeleteSecretFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
- }
- err := sm.DeleteSecret(context.TODO(), ref)
- t.Logf("DeleteSecret error: %v", err)
- // Error nil XOR tc.want.err nil
- if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
- t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
- }
- // if errors are the same type but their contents do not match
- if err != nil && tc.want.err != nil {
- if !strings.Contains(err.Error(), tc.want.err.Error()) {
- t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
- }
- }
- })
- }
- }
- func makeValidSecretStore() *esv1.SecretStore {
- return &esv1.SecretStore{
- ObjectMeta: metav1.ObjectMeta{
- Name: "aws-secret-store",
- Namespace: "default",
- },
- Spec: esv1.SecretStoreSpec{
- Provider: &esv1.SecretStoreProvider{
- AWS: &esv1.AWSProvider{
- Service: esv1.AWSServiceSecretsManager,
- Region: "eu-west-2",
- },
- },
- },
- }
- }
- func getTagSlice() []types.Tag {
- tagKey1 := tagname1
- tagValue1 := tagvalue1
- tagKey2 := tagname2
- tagValue2 := tagvalue2
- return []types.Tag{
- {
- Key: &tagKey1,
- Value: &tagValue1,
- },
- {
- Key: &tagKey2,
- Value: &tagValue2,
- },
- }
- }
- func TestSecretsManagerGetAllSecrets(t *testing.T) {
- ctx := context.Background()
- errBoom := errors.New("boom")
- secretName := "my-secret"
- secretVersion := "AWSCURRENT"
- secretPath := "/path/to/secret"
- secretValue := "secret value"
- secretTags := map[string]string{
- "foo": "bar",
- }
- // Test cases
- testCases := []struct {
- name string
- ref esv1.ExternalSecretFind
- secretName string
- secretVersion string
- secretValue string
- batchGetSecretValueFn func(context.Context, *awssm.BatchGetSecretValueInput, ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error)
- listSecretsFn func(context.Context, *awssm.ListSecretsInput, ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error)
- expectedData map[string][]byte
- expectedError string
- }{
- {
- name: "Matching secrets found",
- ref: esv1.ExternalSecretFind{
- Name: &esv1.FindName{
- RegExp: secretName,
- },
- Path: ptr.To(secretPath),
- },
- secretName: secretName,
- secretVersion: secretVersion,
- secretValue: secretValue,
- batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
- assert.Len(t, input.Filters, 1)
- assert.Equal(t, "name", string(input.Filters[0].Key))
- assert.Equal(t, secretPath, input.Filters[0].Values[0])
- return &awssm.BatchGetSecretValueOutput{
- SecretValues: []types.SecretValueEntry{
- {
- Name: ptr.To(secretName),
- VersionStages: []string{secretVersion},
- SecretBinary: []byte(secretValue),
- },
- },
- }, nil
- },
- expectedData: map[string][]byte{
- secretName: []byte(secretValue),
- },
- expectedError: "",
- },
- {
- name: "Error occurred while fetching secret value",
- ref: esv1.ExternalSecretFind{
- Name: &esv1.FindName{
- RegExp: secretName,
- },
- Path: ptr.To(secretPath),
- },
- secretName: secretName,
- secretVersion: secretVersion,
- secretValue: secretValue,
- batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
- return &awssm.BatchGetSecretValueOutput{
- SecretValues: []types.SecretValueEntry{
- {
- Name: ptr.To(secretName),
- },
- },
- }, errBoom
- },
- expectedData: nil,
- expectedError: errBoom.Error(),
- },
- {
- name: "regexp: error occurred while listing secrets",
- ref: esv1.ExternalSecretFind{
- Name: &esv1.FindName{
- RegExp: secretName,
- },
- },
- listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
- return nil, errBoom
- },
- expectedData: nil,
- expectedError: errBoom.Error(),
- },
- {
- name: "regep: no matching secrets found",
- ref: esv1.ExternalSecretFind{
- Name: &esv1.FindName{
- RegExp: secretName,
- },
- },
- listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
- return &awssm.ListSecretsOutput{
- SecretList: []types.SecretListEntry{
- {
- Name: ptr.To("other-secret"),
- },
- },
- }, nil
- },
- batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
- return &awssm.BatchGetSecretValueOutput{
- SecretValues: []types.SecretValueEntry{
- {
- Name: ptr.To("other-secret"),
- },
- },
- }, nil
- },
- expectedData: make(map[string][]byte),
- expectedError: "",
- },
- {
- name: "invalid regexp",
- ref: esv1.ExternalSecretFind{
- Name: &esv1.FindName{
- RegExp: "[",
- },
- },
- expectedData: nil,
- expectedError: "could not compile find.name.regexp [[]: error parsing regexp: missing closing ]: `[`",
- },
- {
- name: "tags: Matching secrets found",
- ref: esv1.ExternalSecretFind{
- Tags: secretTags,
- },
- secretName: secretName,
- secretVersion: secretVersion,
- secretValue: secretValue,
- batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
- assert.Len(t, input.Filters, 2)
- assert.Equal(t, "tag-key", string(input.Filters[0].Key))
- assert.Equal(t, "foo", input.Filters[0].Values[0])
- assert.Equal(t, "tag-value", string(input.Filters[1].Key))
- assert.Equal(t, "bar", input.Filters[1].Values[0])
- return &awssm.BatchGetSecretValueOutput{
- SecretValues: []types.SecretValueEntry{
- {
- Name: ptr.To(secretName),
- VersionStages: []string{secretVersion},
- SecretBinary: []byte(secretValue),
- },
- },
- }, nil
- },
- expectedData: map[string][]byte{
- secretName: []byte(secretValue),
- },
- expectedError: "",
- },
- {
- name: "tags: error occurred while fetching secret value",
- ref: esv1.ExternalSecretFind{
- Tags: secretTags,
- },
- secretName: secretName,
- secretVersion: secretVersion,
- secretValue: secretValue,
- batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
- return &awssm.BatchGetSecretValueOutput{
- SecretValues: []types.SecretValueEntry{
- {
- Name: ptr.To(secretName),
- VersionStages: []string{secretVersion},
- SecretBinary: []byte(secretValue),
- },
- },
- }, errBoom
- },
- expectedData: nil,
- expectedError: errBoom.Error(),
- },
- {
- name: "tags: error occurred while listing secrets",
- ref: esv1.ExternalSecretFind{
- Tags: secretTags,
- },
- batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
- return nil, errBoom
- },
- expectedData: nil,
- expectedError: errBoom.Error(),
- },
- }
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- fc := fakesm.NewClient()
- fc.BatchGetSecretValueFn = tc.batchGetSecretValueFn
- fc.ListSecretsFn = tc.listSecretsFn
- sm := SecretsManager{
- client: fc,
- cache: make(map[string]*awssm.GetSecretValueOutput),
- }
- data, err := sm.GetAllSecrets(ctx, tc.ref)
- if err != nil && err.Error() != tc.expectedError {
- t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
- }
- if !reflect.DeepEqual(data, tc.expectedData) {
- t.Errorf("unexpected data: got %v, want %v", data, tc.expectedData)
- }
- })
- }
- }
- func TestSecretsManagerValidate(t *testing.T) {
- type fields struct {
- cfg *aws.Config
- referentAuth bool
- }
- validConfig := &aws.Config{
- Credentials: credentials.NewStaticCredentialsProvider(
- "fake",
- "fake",
- "fake",
- ),
- }
- invalidConfig := &aws.Config{
- Credentials: &FakeCredProvider{
- retrieveFunc: func() (aws.Credentials, error) {
- return aws.Credentials{}, errors.New("invalid credentials")
- },
- },
- }
- tests := []struct {
- name string
- fields fields
- want esv1.ValidationResult
- wantErr bool
- }{
- {
- name: "ReferentAuth should always return unknown",
- fields: fields{
- referentAuth: true,
- },
- want: esv1.ValidationResultUnknown,
- },
- {
- name: "Valid credentials should return ready",
- fields: fields{
- cfg: validConfig,
- },
- want: esv1.ValidationResultReady,
- },
- {
- name: "Invalid credentials should return error",
- fields: fields{
- cfg: invalidConfig,
- },
- want: esv1.ValidationResultError,
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- sm := &SecretsManager{
- cfg: tt.fields.cfg,
- referentAuth: tt.fields.referentAuth,
- }
- got, err := sm.Validate()
- if (err != nil) != tt.wantErr {
- t.Errorf("SecretsManager.Validate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("SecretsManager.Validate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestSecretExists(t *testing.T) {
- arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
- defaultVersion := "00000000-0000-0000-0000-000000000002"
- secretValueOutput := &awssm.GetSecretValueOutput{
- ARN: &arn,
- VersionId: &defaultVersion,
- }
- blankSecretValueOutput := &awssm.GetSecretValueOutput{}
- getSecretCorrectErr := types.ResourceNotFoundException{}
- getSecretWrongErr := types.InvalidRequestException{}
- pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: "fake-secret-key", RemoteKey: fakeKey, Property: ""}
- type args struct {
- store *esv1.AWSProvider
- client fakesm.Client
- pushSecretData fake.PushSecretData
- }
- type want struct {
- err error
- wantError bool
- }
- tests := map[string]struct {
- args args
- want want
- }{
- "SecretExistsReturnsTrueForExistingSecret": {
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- wantError: true,
- },
- },
- "SecretExistsReturnsFalseForNonExistingSecret": {
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: nil,
- wantError: false,
- },
- },
- "SecretExistsReturnsFalseForErroredSecret": {
- args: args{
- store: makeValidSecretStore().Spec.Provider.AWS,
- client: fakesm.Client{
- GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretWrongErr),
- },
- pushSecretData: pushSecretDataWithoutProperty,
- },
- want: want{
- err: &getSecretWrongErr,
- wantError: false,
- },
- },
- }
- for name, tc := range tests {
- t.Run(name, func(t *testing.T) {
- sm := &SecretsManager{
- client: &tc.args.client,
- }
- got, err := sm.SecretExists(context.Background(), tc.args.pushSecretData)
- assert.Equal(
- t,
- tc.want,
- want{
- err: err,
- wantError: got,
- })
- })
- }
- }
- func TestConstructMetadataWithDefaults(t *testing.T) {
- tests := []struct {
- name string
- input *apiextensionsv1.JSON
- expected *metadata.PushSecretMetadata[PushSecretMetadataSpec]
- expectError bool
- }{
- {
- name: "Valid metadata with multiple fields",
- input: &apiextensionsv1.JSON{Raw: []byte(`{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": {
- "description": "test description",
- "secretPushFormat":"string",
- "kmsKeyID": "custom-kms-key",
- "tags": {
- "customKey": "customValue"
- },
- }
- }`)},
- expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
- APIVersion: "kubernetes.external-secrets.io/v1alpha1",
- Kind: "PushSecretMetadata",
- Spec: PushSecretMetadataSpec{
- Description: "test description",
- SecretPushFormat: "string",
- KMSKeyID: "custom-kms-key",
- Tags: map[string]string{
- "customKey": "customValue",
- managedBy: externalSecrets,
- },
- },
- },
- },
- {
- name: "Empty metadata, defaults applied",
- input: nil,
- expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
- Spec: PushSecretMetadataSpec{
- Description: fmt.Sprintf("secret '%s:%s'", managedBy, externalSecrets),
- SecretPushFormat: "binary",
- KMSKeyID: "alias/aws/secretsmanager",
- Tags: map[string]string{
- managedBy: externalSecrets,
- },
- },
- },
- },
- {
- name: "Added default metadata with 'managed-by' tag",
- input: &apiextensionsv1.JSON{Raw: []byte(`{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": {
- "tags": {
- "managed-by": "external-secrets",
- "customKey": "customValue"
- },
- }
- }`)},
- expected: nil,
- expectError: true,
- },
- {
- name: "Invalid metadata format",
- input: &apiextensionsv1.JSON{Raw: []byte(`invalid-json`)},
- expected: nil,
- expectError: true,
- },
- {
- name: "Metadata with 'managed-by' tag specified",
- input: &apiextensionsv1.JSON{Raw: []byte(`{"tags":{"managed-by":"invalid"}}`)},
- expected: nil,
- expectError: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, err := (&SecretsManager{}).constructMetadataWithDefaults(tt.input)
- if tt.expectError {
- assert.Error(t, err)
- } else {
- assert.NoError(t, err)
- assert.Equal(t, tt.expected, result)
- }
- })
- }
- }
- func TestComputeTagsToUpdate(t *testing.T) {
- tests := []struct {
- name string
- tags map[string]string
- metaTags map[string]string
- expected []types.Tag
- modified bool
- }{
- {
- name: "No tags to update",
- tags: map[string]string{
- "key1": "value1",
- "key2": "value2",
- },
- metaTags: map[string]string{
- "key1": "value1",
- "key2": "value2",
- },
- expected: []types.Tag{
- {Key: ptr.To("key1"), Value: ptr.To("value1")},
- {Key: ptr.To("key2"), Value: ptr.To("value2")},
- },
- modified: false,
- },
- {
- name: "No tags to update as managed-by tag is ignored",
- tags: map[string]string{
- "key1": "value1",
- "key2": "value2",
- },
- metaTags: map[string]string{
- "key1": "value1",
- "key2": "value2",
- managedBy: externalSecrets,
- },
- expected: []types.Tag{
- {Key: ptr.To("key1"), Value: ptr.To("value1")},
- {Key: ptr.To("key2"), Value: ptr.To("value2")},
- {Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)},
- },
- modified: false,
- },
- {
- name: "Add new tag",
- tags: map[string]string{
- "key1": "value1",
- },
- metaTags: map[string]string{
- "key1": "value1",
- "key2": "value2",
- },
- expected: []types.Tag{
- {Key: ptr.To("key1"), Value: ptr.To("value1")},
- {Key: ptr.To("key2"), Value: ptr.To("value2")},
- },
- modified: true,
- },
- {
- name: "Update existing tag value",
- tags: map[string]string{
- "key1": "value1",
- },
- metaTags: map[string]string{
- "key1": "newValue",
- },
- expected: []types.Tag{
- {Key: ptr.To("key1"), Value: ptr.To("newValue")},
- },
- modified: true,
- },
- {
- name: "Empty tags and metaTags",
- tags: map[string]string{},
- metaTags: map[string]string{},
- expected: []types.Tag{},
- modified: false,
- },
- {
- name: "Empty tags with non-empty metaTags",
- tags: map[string]string{},
- metaTags: map[string]string{
- "key1": "value1",
- },
- expected: []types.Tag{
- {Key: ptr.To("key1"), Value: ptr.To("value1")},
- },
- modified: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, modified := computeTagsToUpdate(tt.tags, tt.metaTags)
- assert.ElementsMatch(t, tt.expected, result)
- assert.Equal(t, tt.modified, modified)
- })
- }
- }
- func TestPatchTags(t *testing.T) {
- type call struct {
- untagCalled bool
- tagCalled bool
- }
- tests := []struct {
- name string
- existingTags map[string]string
- metaTags map[string]string
- expectUntag bool
- expectTag bool
- assertsTag func(input *awssm.TagResourceInput)
- assertsUntag func(input *awssm.UntagResourceInput)
- }{
- {
- name: "no changes",
- existingTags: map[string]string{"a": "1"},
- metaTags: map[string]string{"a": "1"},
- expectUntag: false,
- expectTag: false,
- assertsTag: func(input *awssm.TagResourceInput) {
- assert.Fail(t, "Expected TagResource to not be called")
- },
- assertsUntag: func(input *awssm.UntagResourceInput) {
- assert.Fail(t, "Expected UntagResource to not be called")
- },
- },
- {
- name: "update tag value",
- existingTags: map[string]string{"a": "1"},
- metaTags: map[string]string{"a": "2"},
- expectUntag: false,
- expectTag: true,
- assertsTag: func(input *awssm.TagResourceInput) {
- assert.Contains(t, input.Tags, types.Tag{Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)})
- assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("a"), Value: ptr.To("2")})
- },
- assertsUntag: func(input *awssm.UntagResourceInput) {
- assert.Fail(t, "Expected UntagResource to not be called")
- },
- },
- {
- name: "remove tag",
- existingTags: map[string]string{"a": "1", "b": "2"},
- metaTags: map[string]string{"a": "1"},
- expectUntag: true,
- expectTag: false,
- assertsTag: func(input *awssm.TagResourceInput) {
- assert.Fail(t, "Expected TagResource to not be called")
- },
- assertsUntag: func(input *awssm.UntagResourceInput) {
- assert.Equal(t, []string{"b"}, input.TagKeys)
- },
- },
- {
- name: "add tags",
- existingTags: map[string]string{"a": "1"},
- metaTags: map[string]string{"a": "1", "b": "2"},
- expectUntag: false,
- expectTag: true,
- assertsTag: func(input *awssm.TagResourceInput) {
- assert.Contains(t, input.Tags, types.Tag{Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)})
- assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("a"), Value: ptr.To("1")})
- assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("b"), Value: ptr.To("2")})
- },
- assertsUntag: func(input *awssm.UntagResourceInput) {
- assert.Fail(t, "Expected UntagResource to not be called")
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- calls := call{}
- fakeClient := &fakesm.Client{
- TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
- tt.assertsTag(input)
- calls.tagCalled = true
- }),
- UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
- tt.assertsUntag(input)
- calls.untagCalled = true
- }),
- }
- sm := &SecretsManager{client: fakeClient}
- metaMap := map[string]interface{}{
- "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
- "kind": "PushSecretMetadata",
- "spec": map[string]interface{}{
- "description": "adding managed-by tag explicitly",
- "tags": tt.metaTags,
- },
- }
- raw, err := json.Marshal(metaMap)
- require.NoError(t, err)
- meta := &apiextensionsv1.JSON{Raw: raw}
- secretId := "secret"
- err = sm.patchTags(context.Background(), meta, &secretId, tt.existingTags)
- require.NoError(t, err)
- assert.Equal(t, tt.expectUntag, calls.untagCalled)
- assert.Equal(t, tt.expectTag, calls.tagCalled)
- })
- }
- }
- // FakeCredProvider implements the AWS credentials.Provider interface
- // It is used to inject an error into the AWS config to cause a
- // validation error.
- type FakeCredProvider struct {
- retrieveFunc func() (aws.Credentials, error)
- }
- func (f *FakeCredProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
- return f.retrieveFunc()
- }
- func (f *FakeCredProvider) IsExpired() bool {
- return true
- }
|