secretsmanager_test.go 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package secretsmanager
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "time"
  23. "github.com/aws/aws-sdk-go-v2/aws"
  24. "github.com/aws/aws-sdk-go-v2/credentials"
  25. awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
  26. "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
  27. "github.com/google/go-cmp/cmp"
  28. "github.com/stretchr/testify/assert"
  29. "github.com/stretchr/testify/require"
  30. corev1 "k8s.io/api/core/v1"
  31. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  32. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  33. "k8s.io/utils/ptr"
  34. "sigs.k8s.io/controller-runtime/pkg/client"
  35. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  36. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  37. fakesm "github.com/external-secrets/external-secrets/providers/v1/aws/secretsmanager/fake"
  38. awsutil "github.com/external-secrets/external-secrets/providers/v1/aws/util"
  39. "github.com/external-secrets/external-secrets/runtime/esutils/metadata"
  40. "github.com/external-secrets/external-secrets/runtime/testing/fake"
  41. )
  42. const (
  43. testARN = "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  44. testDefaultVersion = "00000000-0000-0000-0000-000000000002"
  45. )
  46. type secretsManagerTestCase struct {
  47. fakeClient *fakesm.Client
  48. apiInput *awssm.GetSecretValueInput
  49. apiOutput *awssm.GetSecretValueOutput
  50. remoteRef *esv1.ExternalSecretDataRemoteRef
  51. apiErr error
  52. expectError string
  53. expectedSecret string
  54. // for testing secretmap
  55. expectedData map[string][]byte
  56. // for testing caching
  57. expectedCounter *int
  58. prefix string
  59. }
  60. const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
  61. const (
  62. tagname1 = "tagname1"
  63. tagvalue1 = "tagvalue1"
  64. tagname2 = "tagname2"
  65. tagvalue2 = "tagvalue2"
  66. fakeKey = "fake-key"
  67. fakeSecretKey = "fake-secret-key"
  68. )
  69. func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
  70. smtc := secretsManagerTestCase{
  71. fakeClient: fakesm.NewClient(),
  72. apiInput: makeValidAPIInput(),
  73. remoteRef: makeValidRemoteRef(),
  74. apiOutput: makeValidAPIOutput(),
  75. apiErr: nil,
  76. expectError: "",
  77. expectedSecret: "",
  78. expectedData: map[string][]byte{},
  79. }
  80. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  81. return &smtc
  82. }
  83. func makeValidRemoteRef() *esv1.ExternalSecretDataRemoteRef {
  84. return &esv1.ExternalSecretDataRemoteRef{
  85. Key: "/baz",
  86. Version: "AWSCURRENT",
  87. }
  88. }
  89. func makeValidAPIInput() *awssm.GetSecretValueInput {
  90. return &awssm.GetSecretValueInput{
  91. SecretId: aws.String("/baz"),
  92. VersionStage: aws.String("AWSCURRENT"),
  93. }
  94. }
  95. func makeValidAPIOutput() *awssm.GetSecretValueOutput {
  96. return &awssm.GetSecretValueOutput{
  97. SecretString: aws.String(""),
  98. }
  99. }
  100. func makeValidGetResourcePolicyOutput() *awssm.GetResourcePolicyOutput {
  101. return &awssm.GetResourcePolicyOutput{
  102. ResourcePolicy: aws.String(`{
  103. "Version": "2012-10-17",
  104. "Statement": [
  105. {
  106. "Sid": "DenyPolicyChangesExceptAdmins",
  107. "Effect": "Deny",
  108. "Principal": "*",
  109. "Action": [
  110. "secretsmanager:PutResourcePolicy",
  111. "secretsmanager:DeleteResourcePolicy",
  112. "secretsmanager:GetResourcePolicy"
  113. ],
  114. "Resource": "*",
  115. "Condition": {
  116. "ArnNotEquals": {
  117. "aws:PrincipalArn": [
  118. "arn:aws:iam::000000000000:root",
  119. "arn:aws:iam::000000000000:role/admin"
  120. ]
  121. }
  122. }
  123. }
  124. ]
  125. }`),
  126. }
  127. }
  128. func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
  129. smtc := makeValidSecretsManagerTestCase()
  130. for _, fn := range tweaks {
  131. fn(smtc)
  132. }
  133. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  134. return smtc
  135. }
  136. // This case can be shared by both GetSecret and GetSecretMap tests.
  137. // bad case: set apiErr.
  138. var setAPIErr = func(smtc *secretsManagerTestCase) {
  139. smtc.apiErr = errors.New("oh no")
  140. smtc.expectError = "oh no"
  141. }
  142. func TestSecretsManagerResolver(t *testing.T) {
  143. endpointEnvKey := SecretsManagerEndpointEnv
  144. endpointURL := "http://sm.foo"
  145. t.Setenv(endpointEnvKey, endpointURL)
  146. f, err := customEndpointResolver{}.ResolveEndpoint(context.Background(), awssm.EndpointParameters{})
  147. assert.Nil(t, err)
  148. assert.Equal(t, endpointURL, f.URI.String())
  149. }
  150. // test the sm<->aws interface
  151. // make sure correct values are passed and errors are handled accordingly.
  152. func TestSecretsManagerGetSecret(t *testing.T) {
  153. // good case: default version is set
  154. // key is passed in, output is sent back
  155. setSecretString := func(smtc *secretsManagerTestCase) {
  156. smtc.apiOutput.SecretString = aws.String("testtesttest")
  157. smtc.expectedSecret = "testtesttest"
  158. }
  159. // good case: key is passed in with prefix
  160. setSecretStringWithPrefix := func(smtc *secretsManagerTestCase) {
  161. smtc.remoteRef.Key = "secret-key"
  162. smtc.apiInput = &awssm.GetSecretValueInput{
  163. SecretId: aws.String("my-prefix/secret-key"),
  164. VersionStage: aws.String("AWSCURRENT"),
  165. }
  166. smtc.prefix = "my-prefix/"
  167. }
  168. // good case: extract property
  169. // Testing that the property exists in the SecretString
  170. setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
  171. smtc.remoteRef.Property = "/shmoo"
  172. smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
  173. smtc.expectedSecret = "bang"
  174. }
  175. // bad case: missing property
  176. setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
  177. smtc.remoteRef.Property = "INVALPROP"
  178. smtc.expectError = "key INVALPROP does not exist in secret"
  179. }
  180. // bad case: extract property failure due to invalid json
  181. setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
  182. smtc.remoteRef.Property = "INVALPROP"
  183. smtc.apiOutput.SecretString = aws.String(`------`)
  184. smtc.expectError = "key INVALPROP does not exist in secret"
  185. }
  186. // good case: set .SecretString to nil but set binary with value
  187. setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
  188. smtc.apiOutput.SecretBinary = []byte("yesplease")
  189. // needs to be set as nil, empty quotes ("") is considered existing
  190. smtc.apiOutput.SecretString = nil
  191. smtc.expectedSecret = "yesplease"
  192. }
  193. // bad case: both .SecretString and .SecretBinary are nil
  194. setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
  195. smtc.apiOutput.SecretBinary = nil
  196. smtc.apiOutput.SecretString = nil
  197. smtc.expectError = "no secret string nor binary for key"
  198. }
  199. // good case: secretOut.SecretBinary JSON parsing
  200. setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
  201. smtc.apiOutput.SecretString = nil
  202. smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
  203. smtc.remoteRef.Property = "foobar.baz"
  204. smtc.expectedSecret = "nestedval"
  205. }
  206. // good case: secretOut.SecretBinary no JSON parsing if name on key
  207. setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
  208. smtc.apiOutput.SecretString = nil
  209. smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
  210. smtc.remoteRef.Property = "foobar.baz"
  211. smtc.expectedSecret = "nestedval"
  212. }
  213. // good case: custom version stage set
  214. setCustomVersionStage := func(smtc *secretsManagerTestCase) {
  215. smtc.apiInput.VersionStage = aws.String("1234")
  216. smtc.remoteRef.Version = "1234"
  217. smtc.apiOutput.SecretString = aws.String("FOOBA!")
  218. smtc.expectedSecret = "FOOBA!"
  219. }
  220. // good case: custom version id set
  221. setCustomVersionID := func(smtc *secretsManagerTestCase) {
  222. smtc.apiInput.VersionStage = nil
  223. smtc.apiInput.VersionId = aws.String("1234-5678")
  224. smtc.remoteRef.Version = "uuid/1234-5678"
  225. smtc.apiOutput.SecretString = aws.String("myvalue")
  226. smtc.expectedSecret = "myvalue"
  227. }
  228. fetchMetadata := func(smtc *secretsManagerTestCase) {
  229. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  230. describeSecretOutput := &awssm.DescribeSecretOutput{
  231. Tags: getTagSlice(),
  232. }
  233. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  234. jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
  235. smtc.apiOutput.SecretString = &jsonTags
  236. smtc.expectedSecret = jsonTags
  237. }
  238. fetchMetadataProperty := func(smtc *secretsManagerTestCase) {
  239. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  240. describeSecretOutput := &awssm.DescribeSecretOutput{
  241. Tags: getTagSlice(),
  242. }
  243. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  244. smtc.remoteRef.Property = tagname2
  245. jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
  246. smtc.apiOutput.SecretString = &jsonTags
  247. smtc.expectedSecret = tagvalue2
  248. }
  249. failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
  250. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  251. describeSecretOutput := &awssm.DescribeSecretOutput{
  252. Tags: getTagSlice(),
  253. }
  254. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  255. smtc.remoteRef.Property = "fail"
  256. jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
  257. smtc.apiOutput.SecretString = &jsonTags
  258. smtc.expectError = "key fail does not exist in secret /baz"
  259. }
  260. successCases := []*secretsManagerTestCase{
  261. makeValidSecretsManagerTestCase(),
  262. makeValidSecretsManagerTestCaseCustom(setSecretString),
  263. makeValidSecretsManagerTestCaseCustom(setSecretStringWithPrefix),
  264. makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
  265. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
  266. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
  267. makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
  268. makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
  269. makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
  270. makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
  271. makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
  272. makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
  273. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  274. makeValidSecretsManagerTestCaseCustom(fetchMetadata),
  275. makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
  276. makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
  277. }
  278. for k, v := range successCases {
  279. sm := SecretsManager{
  280. cache: make(map[string]*awssm.GetSecretValueOutput),
  281. client: v.fakeClient,
  282. prefix: v.prefix,
  283. }
  284. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  285. if !ErrorContains(err, v.expectError) {
  286. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  287. }
  288. if err == nil && string(out) != v.expectedSecret {
  289. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  290. }
  291. }
  292. }
  293. func TestCaching(t *testing.T) {
  294. fakeClient := fakesm.NewClient()
  295. // good case: first call, since we are using the same key, results should be cached and the counter should not go
  296. // over 1
  297. firstCall := func(smtc *secretsManagerTestCase) {
  298. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  299. smtc.remoteRef.Property = "foo"
  300. smtc.expectedSecret = "bar"
  301. smtc.expectedCounter = aws.Int(1)
  302. smtc.fakeClient = fakeClient
  303. }
  304. secondCall := func(smtc *secretsManagerTestCase) {
  305. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  306. smtc.remoteRef.Property = "bar"
  307. smtc.expectedSecret = "vodka"
  308. smtc.expectedCounter = aws.Int(1)
  309. smtc.fakeClient = fakeClient
  310. }
  311. notCachedCall := func(smtc *secretsManagerTestCase) {
  312. smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
  313. smtc.remoteRef.Property = "sheldon"
  314. smtc.expectedSecret = "bazinga"
  315. smtc.expectedCounter = aws.Int(2)
  316. smtc.fakeClient = fakeClient
  317. smtc.apiInput.SecretId = aws.String("xyz")
  318. smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
  319. }
  320. cachedCases := []*secretsManagerTestCase{
  321. makeValidSecretsManagerTestCaseCustom(firstCall),
  322. makeValidSecretsManagerTestCaseCustom(firstCall),
  323. makeValidSecretsManagerTestCaseCustom(secondCall),
  324. makeValidSecretsManagerTestCaseCustom(notCachedCall),
  325. }
  326. sm := SecretsManager{
  327. cache: make(map[string]*awssm.GetSecretValueOutput),
  328. }
  329. for k, v := range cachedCases {
  330. sm.client = v.fakeClient
  331. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  332. if !ErrorContains(err, v.expectError) {
  333. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  334. }
  335. if err == nil && string(out) != v.expectedSecret {
  336. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  337. }
  338. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  339. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  340. }
  341. }
  342. }
  343. func TestGetSecretMap(t *testing.T) {
  344. // good case: default version & deserialization
  345. setDeserialization := func(smtc *secretsManagerTestCase) {
  346. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
  347. smtc.expectedData["foo"] = []byte("bar")
  348. }
  349. // good case: nested json
  350. setNestedJSON := func(smtc *secretsManagerTestCase) {
  351. smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
  352. smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
  353. }
  354. // good case: caching
  355. cachedMap := func(smtc *secretsManagerTestCase) {
  356. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
  357. smtc.expectedData["foo"] = []byte("bar")
  358. smtc.expectedData["plus"] = []byte("one")
  359. smtc.expectedCounter = aws.Int(1)
  360. }
  361. // bad case: invalid json
  362. setInvalidJSON := func(smtc *secretsManagerTestCase) {
  363. smtc.apiOutput.SecretString = aws.String(`-----------------`)
  364. smtc.expectError = "unable to unmarshal secret"
  365. }
  366. successCases := []*secretsManagerTestCase{
  367. makeValidSecretsManagerTestCaseCustom(setDeserialization),
  368. makeValidSecretsManagerTestCaseCustom(setNestedJSON),
  369. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  370. makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
  371. makeValidSecretsManagerTestCaseCustom(cachedMap),
  372. }
  373. for k, v := range successCases {
  374. sm := SecretsManager{
  375. cache: make(map[string]*awssm.GetSecretValueOutput),
  376. client: v.fakeClient,
  377. }
  378. out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
  379. if !ErrorContains(err, v.expectError) {
  380. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  381. }
  382. if err == nil && !cmp.Equal(out, v.expectedData) {
  383. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  384. }
  385. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  386. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  387. }
  388. }
  389. }
  390. func ErrorContains(out error, want string) bool {
  391. if out == nil {
  392. return want == ""
  393. }
  394. if want == "" {
  395. return false
  396. }
  397. return strings.Contains(out.Error(), want)
  398. }
  399. func TestSetSecret(t *testing.T) {
  400. managedBy := managedBy
  401. notManagedBy := "not-managed-by"
  402. secretKey := fakeSecretKey
  403. secretValue := []byte("fake-value")
  404. fakeSecret := &corev1.Secret{
  405. Data: map[string][]byte{
  406. secretKey: secretValue,
  407. },
  408. }
  409. externalSecrets := externalSecrets
  410. noPermission := errors.New("no permission")
  411. arn := testARN
  412. getSecretCorrectErr := types.ResourceNotFoundException{}
  413. getSecretWrongErr := types.InvalidRequestException{}
  414. secretOutput := &awssm.CreateSecretOutput{
  415. ARN: &arn,
  416. }
  417. externalSecretsTag := []types.Tag{
  418. {
  419. Key: &managedBy,
  420. Value: &externalSecrets,
  421. },
  422. {
  423. Key: ptr.To("taname1"),
  424. Value: ptr.To("tagvalue1"),
  425. },
  426. }
  427. externalSecretsTagFaulty := []types.Tag{
  428. {
  429. Key: &notManagedBy,
  430. Value: &externalSecrets,
  431. },
  432. }
  433. tagSecretOutputNoVersions := &awssm.DescribeSecretOutput{
  434. ARN: &arn,
  435. Tags: externalSecretsTag,
  436. }
  437. defaultVersion := testDefaultVersion
  438. tagSecretOutput := &awssm.DescribeSecretOutput{
  439. ARN: &arn,
  440. Tags: externalSecretsTag,
  441. VersionIdsToStages: map[string][]string{
  442. defaultVersion: {"AWSCURRENT"},
  443. },
  444. }
  445. tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
  446. ARN: &arn,
  447. Tags: externalSecretsTagFaulty,
  448. }
  449. tagSecretOutputFrom := func(versionId string) *awssm.DescribeSecretOutput {
  450. return &awssm.DescribeSecretOutput{
  451. ARN: &arn,
  452. Tags: externalSecretsTag,
  453. VersionIdsToStages: map[string][]string{
  454. versionId: {"AWSCURRENT"},
  455. },
  456. }
  457. }
  458. initialVersion := "00000000-0000-0000-0000-000000000001"
  459. defaultUpdatedVersion := "6c70d57a-f53d-bf4d-9525-3503dd5abe8c"
  460. randomUUIDVersion := "9d6202c2-c216-433e-a2f0-5836c4f025af"
  461. randomUUIDVersionIncremented := "4346824b-7da1-4d82-addf-dee197fd5d71"
  462. unparsableVersion := "IAM UNPARSABLE"
  463. secretValueOutput := &awssm.GetSecretValueOutput{
  464. ARN: &arn,
  465. VersionId: &defaultVersion,
  466. }
  467. secretValueOutput2 := &awssm.GetSecretValueOutput{
  468. ARN: &arn,
  469. SecretBinary: secretValue,
  470. VersionId: &defaultVersion,
  471. }
  472. blankDescribeSecretOutput := &awssm.DescribeSecretOutput{}
  473. type params struct {
  474. s string
  475. b []byte
  476. version *string
  477. }
  478. secretValueOutputFrom := func(params params) *awssm.GetSecretValueOutput {
  479. var version *string
  480. if params.version == nil {
  481. version = &defaultVersion
  482. } else {
  483. version = params.version
  484. }
  485. return &awssm.GetSecretValueOutput{
  486. ARN: &arn,
  487. SecretString: &params.s,
  488. SecretBinary: params.b,
  489. VersionId: version,
  490. }
  491. }
  492. putSecretOutput := &awssm.PutSecretValueOutput{
  493. ARN: &arn,
  494. }
  495. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: ""}
  496. pushSecretDataWithoutSecretKey := fake.PushSecretData{RemoteKey: fakeKey, Property: ""}
  497. pushSecretDataWithMetadata := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  498. Raw: []byte(`{
  499. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  500. "kind": "PushSecretMetadata",
  501. "spec": {
  502. "secretPushFormat": "string"
  503. }
  504. }`)}}
  505. pushSecretDataWithProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "other-fake-property"}
  506. type args struct {
  507. store *esv1.AWSProvider
  508. client fakesm.Client
  509. pushSecretData fake.PushSecretData
  510. newUUID string
  511. kubeclient client.Client
  512. }
  513. type want struct {
  514. err error
  515. }
  516. tests := map[string]struct {
  517. reason string
  518. args args
  519. want want
  520. }{
  521. "SetSecretSucceedsWithExistingSecret": {
  522. reason: "a secret can be pushed to aws secrets manager when it already exists",
  523. args: args{
  524. store: makeValidSecretStore().Spec.Provider.AWS,
  525. client: fakesm.Client{
  526. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  527. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  528. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  529. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  530. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  531. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  532. },
  533. pushSecretData: pushSecretDataWithoutProperty,
  534. },
  535. want: want{
  536. err: nil,
  537. },
  538. },
  539. "SetSecretSucceedsWithExistingSecretButNoSecretVersionsWithoutProperty": {
  540. reason: "a secret can be pushed to aws secrets manager when it already exists but has no secret versions",
  541. args: args{
  542. store: makeValidSecretStore().Spec.Provider.AWS,
  543. client: fakesm.Client{
  544. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputNoVersions, nil),
  545. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  546. SecretBinary: []byte(`fake-value`),
  547. Version: aws.String(initialVersion),
  548. }),
  549. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  550. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  551. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  552. },
  553. pushSecretData: pushSecretDataWithoutProperty,
  554. },
  555. want: want{
  556. err: nil,
  557. },
  558. },
  559. "SetSecretSucceedsWithExistingSecretButNoSecretVersionsWithProperty": {
  560. reason: "a secret can be pushed to aws secrets manager when it already exists but has no secret versions",
  561. args: args{
  562. store: makeValidSecretStore().Spec.Provider.AWS,
  563. client: fakesm.Client{
  564. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputNoVersions, nil),
  565. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  566. SecretBinary: []byte(`{"other-fake-property":"fake-value"}`),
  567. Version: aws.String(initialVersion),
  568. }),
  569. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  570. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  571. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  572. },
  573. pushSecretData: pushSecretDataWithProperty,
  574. },
  575. want: want{
  576. err: nil,
  577. },
  578. },
  579. "SetSecretSucceedsWithoutSecretKey": {
  580. reason: "a secret can be pushed to aws secrets manager without secret key",
  581. args: args{
  582. store: makeValidSecretStore().Spec.Provider.AWS,
  583. client: fakesm.Client{
  584. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  585. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  586. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  587. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  588. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  589. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  590. },
  591. pushSecretData: pushSecretDataWithoutSecretKey,
  592. },
  593. want: want{
  594. err: nil,
  595. },
  596. },
  597. "SetSecretSucceedsWithExistingSecretAndStringFormat": {
  598. reason: "a secret can be pushed to aws secrets manager when it already exists",
  599. args: args{
  600. store: makeValidSecretStore().Spec.Provider.AWS,
  601. client: fakesm.Client{
  602. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  603. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  604. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  605. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  606. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  607. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  608. },
  609. pushSecretData: pushSecretDataWithMetadata,
  610. },
  611. want: want{
  612. err: nil,
  613. },
  614. },
  615. "SetSecretSucceedsWithExistingSecretAndKMSKeyAndDescription": {
  616. reason: "a secret can be pushed to aws secrets manager when it already exists",
  617. args: args{
  618. store: makeValidSecretStore().Spec.Provider.AWS,
  619. client: fakesm.Client{
  620. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, &getSecretCorrectErr),
  621. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  622. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  623. },
  624. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  625. Raw: []byte(`{
  626. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  627. "kind": "PushSecretMetadata",
  628. "spec": {
  629. "kmsKeyID": "bb123123-b2b0-4f60-ac3a-44a13f0e6b6c",
  630. "description": "this is a description"
  631. }
  632. }`)}},
  633. },
  634. want: want{
  635. err: &getSecretCorrectErr,
  636. },
  637. },
  638. "SetSecretSucceedsWithExistingSecretAndAdditionalTags": {
  639. reason: "a secret can be pushed to aws secrets manager when it already exists",
  640. args: args{
  641. store: makeValidSecretStore().Spec.Provider.AWS,
  642. client: fakesm.Client{
  643. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  644. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  645. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  646. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  647. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  648. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  649. },
  650. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  651. Raw: []byte(`{
  652. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  653. "kind": "PushSecretMetadata",
  654. "spec": {
  655. "tags": {"tagname12": "tagvalue1"}
  656. }
  657. }`)}},
  658. },
  659. want: want{
  660. err: nil,
  661. },
  662. },
  663. "SetSecretSucceedsWithNewSecret": {
  664. reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
  665. args: args{
  666. store: makeValidSecretStore().Spec.Provider.AWS,
  667. client: fakesm.Client{
  668. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  669. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  670. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil),
  671. },
  672. pushSecretData: pushSecretDataWithoutProperty,
  673. },
  674. want: want{
  675. err: nil,
  676. },
  677. },
  678. "SetSecretWithPropertySucceedsWithNewSecret": {
  679. 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",
  680. args: args{
  681. store: makeValidSecretStore().Spec.Provider.AWS,
  682. client: fakesm.Client{
  683. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  684. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil, []byte(`{"other-fake-property":"fake-value"}`)),
  685. },
  686. pushSecretData: pushSecretDataWithProperty,
  687. },
  688. want: want{
  689. err: nil,
  690. },
  691. },
  692. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyBinary": {
  693. 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)",
  694. args: args{
  695. newUUID: defaultUpdatedVersion,
  696. store: makeValidSecretStore().Spec.Provider.AWS,
  697. client: fakesm.Client{
  698. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{b: []byte((`{"fake-property":"fake-value"}`))}), nil),
  699. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  700. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  701. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  702. Version: &defaultUpdatedVersion,
  703. }),
  704. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  705. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  706. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  707. },
  708. pushSecretData: pushSecretDataWithProperty,
  709. },
  710. want: want{
  711. err: nil,
  712. },
  713. },
  714. "SetSecretWithPropertySucceedsWithExistingSecretAndRandomUUIDVersion": {
  715. 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",
  716. args: args{
  717. store: makeValidSecretStore().Spec.Provider.AWS,
  718. newUUID: randomUUIDVersionIncremented,
  719. client: fakesm.Client{
  720. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
  721. b: []byte((`{"fake-property":"fake-value"}`)),
  722. version: &randomUUIDVersion,
  723. }), nil),
  724. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFrom(randomUUIDVersion), nil),
  725. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  726. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  727. Version: &randomUUIDVersionIncremented,
  728. }),
  729. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  730. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  731. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  732. },
  733. pushSecretData: pushSecretDataWithProperty,
  734. },
  735. want: want{
  736. err: nil,
  737. },
  738. },
  739. "SetSecretWithPropertySucceedsWithExistingSecretAndVersionThatCantBeParsed": {
  740. reason: "A manually set secret version doesn't have to be a UUID",
  741. args: args{
  742. newUUID: unparsableVersion,
  743. store: makeValidSecretStore().Spec.Provider.AWS,
  744. client: fakesm.Client{
  745. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
  746. b: []byte((`{"fake-property":"fake-value"}`)),
  747. version: &unparsableVersion,
  748. }), nil),
  749. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  750. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  751. SecretBinary: []byte((`fake-value`)),
  752. Version: &unparsableVersion,
  753. }),
  754. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  755. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  756. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  757. },
  758. pushSecretData: pushSecretDataWithoutProperty,
  759. },
  760. want: want{
  761. err: nil,
  762. },
  763. },
  764. "SetSecretWithPropertySucceedsWithExistingSecretAndAbsentVersion": {
  765. reason: "When a secret version is not specified, set it to 1",
  766. args: args{
  767. newUUID: initialVersion,
  768. store: makeValidSecretStore().Spec.Provider.AWS,
  769. client: fakesm.Client{
  770. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  771. ARN: &arn,
  772. SecretBinary: []byte((`{"fake-property":"fake-value"}`)),
  773. }, nil),
  774. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  775. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  776. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  777. Version: &initialVersion,
  778. }),
  779. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  780. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  781. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  782. },
  783. pushSecretData: pushSecretDataWithProperty,
  784. },
  785. want: want{
  786. err: nil,
  787. },
  788. },
  789. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyString": {
  790. 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)",
  791. args: args{
  792. newUUID: defaultUpdatedVersion,
  793. store: makeValidSecretStore().Spec.Provider.AWS,
  794. client: fakesm.Client{
  795. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":"fake-value"}`}), nil),
  796. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  797. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  798. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  799. Version: &defaultUpdatedVersion,
  800. }),
  801. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  802. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  803. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  804. },
  805. pushSecretData: pushSecretDataWithProperty,
  806. },
  807. want: want{
  808. err: nil,
  809. },
  810. },
  811. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyWithDot": {
  812. 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)",
  813. args: args{
  814. newUUID: defaultUpdatedVersion,
  815. store: makeValidSecretStore().Spec.Provider.AWS,
  816. client: fakesm.Client{
  817. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  818. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  819. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  820. SecretBinary: []byte(`{"fake-property":{"fake-property":"fake-value","other-fake-property":"fake-value"}}`),
  821. Version: &defaultUpdatedVersion,
  822. }),
  823. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  824. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  825. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  826. },
  827. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "fake-property.other-fake-property"},
  828. },
  829. want: want{
  830. err: nil,
  831. },
  832. },
  833. "SetSecretWithPropertyFailsExistingNonJsonSecret": {
  834. reason: "setting a pushSecretData property is only supported for json secrets",
  835. args: args{
  836. store: makeValidSecretStore().Spec.Provider.AWS,
  837. client: fakesm.Client{
  838. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `non-json-secret`}), nil),
  839. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  840. },
  841. pushSecretData: pushSecretDataWithProperty,
  842. },
  843. want: want{
  844. err: errors.New("PushSecret for aws secrets manager with a pushSecretData property requires a json secret"),
  845. },
  846. },
  847. "SetSecretCreateSecretFails": {
  848. reason: "CreateSecretWithContext returns an error if it fails",
  849. args: args{
  850. store: makeValidSecretStore().Spec.Provider.AWS,
  851. client: fakesm.Client{
  852. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  853. CreateSecretFn: fakesm.NewCreateSecretFn(nil, noPermission),
  854. },
  855. pushSecretData: pushSecretDataWithoutProperty,
  856. },
  857. want: want{
  858. err: noPermission,
  859. },
  860. },
  861. "SetSecretGetSecretFails": {
  862. reason: "GetSecretValueWithContext returns an error if it fails",
  863. args: args{
  864. store: makeValidSecretStore().Spec.Provider.AWS,
  865. client: fakesm.Client{
  866. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, noPermission),
  867. },
  868. pushSecretData: pushSecretDataWithoutProperty,
  869. },
  870. want: want{
  871. err: noPermission,
  872. },
  873. },
  874. "SetSecretWillNotPushSameSecret": {
  875. reason: "secret with the same value will not be pushed",
  876. args: args{
  877. store: makeValidSecretStore().Spec.Provider.AWS,
  878. client: fakesm.Client{
  879. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput2, nil),
  880. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  881. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  882. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  883. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  884. },
  885. pushSecretData: pushSecretDataWithoutProperty,
  886. },
  887. want: want{
  888. err: nil,
  889. },
  890. },
  891. "SetSecretPutSecretValueFails": {
  892. reason: "PutSecretValueWithContext returns an error if it fails",
  893. args: args{
  894. store: makeValidSecretStore().Spec.Provider.AWS,
  895. client: fakesm.Client{
  896. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  897. PutSecretValueFn: fakesm.NewPutSecretValueFn(nil, noPermission),
  898. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  899. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  900. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  901. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  902. },
  903. pushSecretData: pushSecretDataWithoutProperty,
  904. },
  905. want: want{
  906. err: noPermission,
  907. },
  908. },
  909. "SetSecretWrongGetSecretErrFails": {
  910. reason: "DescribeSecret errors out when anything except awssm.ErrCodeResourceNotFoundException",
  911. args: args{
  912. store: makeValidSecretStore().Spec.Provider.AWS,
  913. client: fakesm.Client{
  914. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretWrongErr),
  915. },
  916. pushSecretData: pushSecretDataWithoutProperty,
  917. },
  918. want: want{
  919. err: &getSecretWrongErr,
  920. },
  921. },
  922. "SetSecretDescribeSecretFails": {
  923. reason: "secret cannot be described",
  924. args: args{
  925. store: makeValidSecretStore().Spec.Provider.AWS,
  926. client: fakesm.Client{
  927. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  928. DescribeSecretFn: fakesm.NewDescribeSecretFn(nil, noPermission),
  929. },
  930. pushSecretData: pushSecretDataWithoutProperty,
  931. },
  932. want: want{
  933. err: noPermission,
  934. },
  935. },
  936. "SetSecretDoesNotOverwriteUntaggedSecret": {
  937. reason: "secret cannot be described",
  938. args: args{
  939. store: makeValidSecretStore().Spec.Provider.AWS,
  940. client: fakesm.Client{
  941. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  942. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFaulty, nil),
  943. },
  944. pushSecretData: pushSecretDataWithoutProperty,
  945. },
  946. want: want{
  947. err: errors.New("secret not managed by external-secrets"),
  948. },
  949. },
  950. "PatchSecretTags": {
  951. reason: "secret key is configured with tags to remove and add",
  952. args: args{
  953. store: &esv1.AWSProvider{
  954. Service: esv1.AWSServiceSecretsManager,
  955. Region: "eu-west-2",
  956. },
  957. client: fakesm.Client{
  958. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  959. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  960. ARN: &arn,
  961. Tags: []types.Tag{
  962. {Key: &managedBy, Value: &externalSecrets},
  963. {Key: ptr.To("team"), Value: ptr.To("paradox")},
  964. },
  965. }, nil),
  966. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  967. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  968. assert.Len(t, input.Tags, 2)
  969. assert.Contains(t, input.Tags, types.Tag{Key: &managedBy, Value: &externalSecrets})
  970. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("env"), Value: ptr.To("sandbox")})
  971. }),
  972. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
  973. assert.Len(t, input.TagKeys, 1)
  974. assert.Equal(t, []string{"team"}, input.TagKeys)
  975. assert.NotContains(t, input.TagKeys, managedBy)
  976. }),
  977. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  978. },
  979. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  980. Raw: []byte(`{
  981. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  982. "kind": "PushSecretMetadata",
  983. "spec": {
  984. "secretPushFormat": "string",
  985. "tags": {
  986. "env": "sandbox"
  987. }
  988. }
  989. }`)}},
  990. },
  991. want: want{
  992. err: nil,
  993. },
  994. },
  995. "SetSecretWithExistingNonChangingResourcePolicy": {
  996. reason: "sync an existing secret without syncing resource policy that has no change",
  997. args: args{
  998. store: makeValidSecretStore().Spec.Provider.AWS,
  999. client: fakesm.Client{
  1000. // NO call to PutResourcePolicy
  1001. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1002. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  1003. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  1004. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  1005. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1006. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(makeValidGetResourcePolicyOutput(), nil),
  1007. },
  1008. pushSecretData: fake.PushSecretData{
  1009. SecretKey: secretKey,
  1010. RemoteKey: fakeKey,
  1011. Property: "",
  1012. Metadata: &apiextensionsv1.JSON{
  1013. Raw: []byte(`{
  1014. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1015. "kind": "PushSecretMetadata",
  1016. "spec": {
  1017. "secretPushFormat": "string",
  1018. "resourcePolicy": {
  1019. "blockPublicPolicy": true,
  1020. "policySourceRef": {
  1021. "kind": "ConfigMap",
  1022. "name": "resource-policy",
  1023. "key": "policy.json"
  1024. }
  1025. }
  1026. }
  1027. }`),
  1028. },
  1029. },
  1030. kubeclient: clientfake.NewFakeClient(&corev1.ConfigMap{
  1031. ObjectMeta: metav1.ObjectMeta{
  1032. Name: "resource-policy",
  1033. },
  1034. // Create a policy that does not match object order of the
  1035. // existing one
  1036. Data: map[string]string{
  1037. "policy.json": `
  1038. {
  1039. "Version": "2012-10-17",
  1040. "Statement": [
  1041. {
  1042. "Resource": "*",
  1043. "Effect": "Deny",
  1044. "Principal": "*",
  1045. "Action": [
  1046. "secretsmanager:PutResourcePolicy",
  1047. "secretsmanager:DeleteResourcePolicy",
  1048. "secretsmanager:GetResourcePolicy"
  1049. ],
  1050. "Condition": {
  1051. "ArnNotEquals": {
  1052. "aws:PrincipalArn": [
  1053. "arn:aws:iam::000000000000:root",
  1054. "arn:aws:iam::000000000000:role/admin"
  1055. ]
  1056. }
  1057. },
  1058. "Sid": "DenyPolicyChangesExceptAdmins"
  1059. }
  1060. ]
  1061. }
  1062. `,
  1063. },
  1064. }),
  1065. },
  1066. want: want{
  1067. err: nil,
  1068. },
  1069. },
  1070. "SetSecretWithExistingChangingResourcePolicy": {
  1071. reason: "sync an existing secret and the resource policy when it has changes",
  1072. args: args{
  1073. store: makeValidSecretStore().Spec.Provider.AWS,
  1074. client: fakesm.Client{
  1075. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1076. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  1077. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  1078. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  1079. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1080. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(makeValidGetResourcePolicyOutput(), nil),
  1081. // Call to PutResourcePolicy since policy does not match
  1082. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil),
  1083. },
  1084. pushSecretData: fake.PushSecretData{
  1085. SecretKey: secretKey,
  1086. RemoteKey: fakeKey,
  1087. Property: "",
  1088. Metadata: &apiextensionsv1.JSON{
  1089. Raw: []byte(`{
  1090. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1091. "kind": "PushSecretMetadata",
  1092. "spec": {
  1093. "secretPushFormat": "string",
  1094. "resourcePolicy": {
  1095. "blockPublicPolicy": true,
  1096. "policySourceRef": {
  1097. "kind": "ConfigMap",
  1098. "name": "resource-policy",
  1099. "key": "policy.json"
  1100. }
  1101. }
  1102. }
  1103. }`),
  1104. },
  1105. },
  1106. kubeclient: clientfake.NewFakeClient(&corev1.ConfigMap{
  1107. ObjectMeta: metav1.ObjectMeta{
  1108. Name: "resource-policy",
  1109. },
  1110. // Create a policy that does not match object order of the
  1111. // existing one
  1112. Data: map[string]string{
  1113. "policy.json": `
  1114. {
  1115. "Version": "2012-10-17",
  1116. "Statement": [
  1117. {
  1118. "Resource": "*",
  1119. "Effect": "Deny",
  1120. "Principal": "*",
  1121. "Action": [
  1122. "secretsmanager:PutResourcePolicy",
  1123. "secretsmanager:DeleteResourcePolicy",
  1124. "secretsmanager:GetResourcePolicy"
  1125. ],
  1126. "Condition": {
  1127. "ArnNotEquals": {
  1128. "aws:PrincipalArn": [
  1129. "arn:aws:iam::000000000000:root",
  1130. "arn:aws:iam::000000000000:role/sudo"
  1131. ]
  1132. }
  1133. },
  1134. "Sid": "DenyPolicyChangesExceptAdmins"
  1135. }
  1136. ]
  1137. }
  1138. `,
  1139. },
  1140. }),
  1141. },
  1142. want: want{
  1143. err: nil,
  1144. },
  1145. },
  1146. }
  1147. for name, tc := range tests {
  1148. t.Run(name, func(t *testing.T) {
  1149. sm := SecretsManager{
  1150. client: &tc.args.client,
  1151. prefix: tc.args.store.Prefix,
  1152. newUUID: func() string { return tc.args.newUUID },
  1153. kube: tc.args.kubeclient,
  1154. }
  1155. err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
  1156. // Error nil XOR tc.want.err nil
  1157. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1158. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1159. }
  1160. // if errors are the same type but their contents do not match
  1161. if err != nil && tc.want.err != nil {
  1162. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1163. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1164. }
  1165. }
  1166. })
  1167. }
  1168. }
  1169. func TestPushSecretTagsUpdatedWhenValueUnchanged(t *testing.T) {
  1170. secretKey := fakeSecretKey
  1171. secretValue := []byte("fake-value")
  1172. fakeSecret := &corev1.Secret{
  1173. Data: map[string][]byte{
  1174. secretKey: secretValue,
  1175. },
  1176. }
  1177. arn := testARN
  1178. defaultVersion := testDefaultVersion
  1179. managedBy := managedBy
  1180. externalSecrets := externalSecrets
  1181. tagResourceCalled := false
  1182. var capturedTagInput *awssm.TagResourceInput
  1183. client := fakesm.Client{
  1184. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  1185. ARN: &arn,
  1186. SecretBinary: secretValue,
  1187. VersionId: &defaultVersion,
  1188. }, nil),
  1189. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  1190. ARN: &arn,
  1191. Tags: []types.Tag{
  1192. {Key: &managedBy, Value: &externalSecrets},
  1193. },
  1194. VersionIdsToStages: map[string][]string{
  1195. defaultVersion: {"AWSCURRENT"},
  1196. },
  1197. }, nil),
  1198. PutSecretValueFn: fakesm.NewPutSecretValueFn(nil, fmt.Errorf("PutSecretValue should not be called when value is unchanged")),
  1199. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  1200. tagResourceCalled = true
  1201. capturedTagInput = input
  1202. }),
  1203. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1204. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  1205. }
  1206. sm := SecretsManager{
  1207. client: &client,
  1208. }
  1209. pushSecretData := fake.PushSecretData{
  1210. SecretKey: secretKey,
  1211. RemoteKey: fakeKey,
  1212. Property: "",
  1213. Metadata: &apiextensionsv1.JSON{
  1214. Raw: []byte(`{
  1215. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1216. "kind": "PushSecretMetadata",
  1217. "spec": {
  1218. "tags": {"newTag": "newValue"}
  1219. }
  1220. }`),
  1221. },
  1222. }
  1223. err := sm.PushSecret(context.Background(), fakeSecret, pushSecretData)
  1224. require.NoError(t, err, "PushSecret should not return error when value is unchanged but tags need updating")
  1225. assert.True(t, tagResourceCalled, "TagResource should be called even when secret value is unchanged")
  1226. require.NotNil(t, capturedTagInput, "TagResourceInput should be captured")
  1227. assert.Len(t, capturedTagInput.Tags, 2)
  1228. assert.Contains(t, capturedTagInput.Tags, types.Tag{Key: &managedBy, Value: &externalSecrets})
  1229. assert.Contains(t, capturedTagInput.Tags, types.Tag{Key: ptr.To("newTag"), Value: ptr.To("newValue")})
  1230. }
  1231. func TestPushSecretResourcePolicyUpdatedWhenValueUnchanged(t *testing.T) {
  1232. secretKey := fakeSecretKey
  1233. secretValue := []byte("fake-value")
  1234. fakeSecret := &corev1.Secret{
  1235. Data: map[string][]byte{
  1236. secretKey: secretValue,
  1237. },
  1238. }
  1239. arn := testARN
  1240. defaultVersion := testDefaultVersion
  1241. managedBy := managedBy
  1242. externalSecrets := externalSecrets
  1243. putResourcePolicyCalled := false
  1244. var capturedPolicyInput *awssm.PutResourcePolicyInput
  1245. putSecretValueCalled := false
  1246. existingPolicy := `{"Version":"2012-10-17","Statement":[{"Sid":"OldPolicy","Effect":"Deny","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`
  1247. newPolicy := `{"Version":"2012-10-17","Statement":[{"Sid":"NewPolicy","Effect":"Allow","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`
  1248. client := fakesm.Client{
  1249. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  1250. ARN: &arn,
  1251. SecretBinary: secretValue,
  1252. VersionId: &defaultVersion,
  1253. }, nil),
  1254. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  1255. ARN: &arn,
  1256. Tags: []types.Tag{
  1257. {Key: &managedBy, Value: &externalSecrets},
  1258. },
  1259. VersionIdsToStages: map[string][]string{
  1260. defaultVersion: {"AWSCURRENT"},
  1261. },
  1262. }, nil),
  1263. PutSecretValueFn: func(_ context.Context, _ *awssm.PutSecretValueInput, _ ...func(*awssm.Options)) (*awssm.PutSecretValueOutput, error) {
  1264. putSecretValueCalled = true
  1265. return nil, fmt.Errorf("PutSecretValue should not be called when value is unchanged")
  1266. },
  1267. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  1268. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1269. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  1270. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(&awssm.GetResourcePolicyOutput{
  1271. ResourcePolicy: &existingPolicy,
  1272. }, nil),
  1273. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil, func(input *awssm.PutResourcePolicyInput) {
  1274. putResourcePolicyCalled = true
  1275. capturedPolicyInput = input
  1276. }),
  1277. }
  1278. kubeclient := clientfake.NewFakeClient(&corev1.ConfigMap{
  1279. ObjectMeta: metav1.ObjectMeta{
  1280. Name: "resource-policy",
  1281. },
  1282. Data: map[string]string{
  1283. "policy.json": newPolicy,
  1284. },
  1285. })
  1286. sm := SecretsManager{
  1287. client: &client,
  1288. kube: kubeclient,
  1289. }
  1290. pushSecretData := fake.PushSecretData{
  1291. SecretKey: secretKey,
  1292. RemoteKey: fakeKey,
  1293. Property: "",
  1294. Metadata: &apiextensionsv1.JSON{
  1295. Raw: []byte(`{
  1296. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1297. "kind": "PushSecretMetadata",
  1298. "spec": {
  1299. "resourcePolicy": {
  1300. "blockPublicPolicy": true,
  1301. "policySourceRef": {
  1302. "kind": "ConfigMap",
  1303. "name": "resource-policy",
  1304. "key": "policy.json"
  1305. }
  1306. }
  1307. }
  1308. }`),
  1309. },
  1310. }
  1311. err := sm.PushSecret(context.Background(), fakeSecret, pushSecretData)
  1312. require.NoError(t, err, "PushSecret should not return error when value is unchanged but resource policy needs updating")
  1313. assert.True(t, putResourcePolicyCalled, "PutResourcePolicy should be called even when secret value is unchanged")
  1314. assert.False(t, putSecretValueCalled, "PutSecretValue should not be called when value is unchanged")
  1315. require.NotNil(t, capturedPolicyInput, "PutResourcePolicyInput should be captured")
  1316. assert.Equal(t, fakeKey, *capturedPolicyInput.SecretId)
  1317. assert.JSONEq(t, newPolicy, *capturedPolicyInput.ResourcePolicy)
  1318. }
  1319. func TestDeleteSecret(t *testing.T) {
  1320. fakeClient := fakesm.Client{}
  1321. managed := managedBy
  1322. manager := externalSecrets
  1323. secretTag := types.Tag{
  1324. Key: &managed,
  1325. Value: &manager,
  1326. }
  1327. type args struct {
  1328. client fakesm.Client
  1329. config esv1.SecretsManager
  1330. prefix string
  1331. getSecretOutput *awssm.GetSecretValueOutput
  1332. describeSecretOutput *awssm.DescribeSecretOutput
  1333. deleteSecretOutput *awssm.DeleteSecretOutput
  1334. getSecretErr error
  1335. describeSecretErr error
  1336. deleteSecretErr error
  1337. }
  1338. type want struct {
  1339. err error
  1340. }
  1341. type testCase struct {
  1342. args args
  1343. want want
  1344. reason string
  1345. }
  1346. tests := map[string]testCase{
  1347. "Deletes Successfully": {
  1348. args: args{
  1349. client: fakeClient,
  1350. config: esv1.SecretsManager{},
  1351. getSecretOutput: &awssm.GetSecretValueOutput{},
  1352. describeSecretOutput: &awssm.DescribeSecretOutput{
  1353. Tags: []types.Tag{secretTag},
  1354. },
  1355. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1356. getSecretErr: nil,
  1357. describeSecretErr: nil,
  1358. deleteSecretErr: nil,
  1359. },
  1360. want: want{
  1361. err: nil,
  1362. },
  1363. reason: "",
  1364. },
  1365. "Deletes Successfully with ForceDeleteWithoutRecovery": {
  1366. args: args{
  1367. client: fakeClient,
  1368. config: esv1.SecretsManager{
  1369. ForceDeleteWithoutRecovery: true,
  1370. },
  1371. getSecretOutput: &awssm.GetSecretValueOutput{},
  1372. describeSecretOutput: &awssm.DescribeSecretOutput{
  1373. Tags: []types.Tag{secretTag},
  1374. },
  1375. deleteSecretOutput: &awssm.DeleteSecretOutput{
  1376. DeletionDate: aws.Time(time.Now()),
  1377. },
  1378. getSecretErr: nil,
  1379. describeSecretErr: nil,
  1380. deleteSecretErr: nil,
  1381. },
  1382. want: want{
  1383. err: nil,
  1384. },
  1385. reason: "",
  1386. },
  1387. "Not Managed by ESO": {
  1388. args: args{
  1389. client: fakeClient,
  1390. config: esv1.SecretsManager{},
  1391. getSecretOutput: &awssm.GetSecretValueOutput{},
  1392. describeSecretOutput: &awssm.DescribeSecretOutput{
  1393. Tags: []types.Tag{},
  1394. },
  1395. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1396. getSecretErr: nil,
  1397. describeSecretErr: nil,
  1398. deleteSecretErr: nil,
  1399. },
  1400. want: want{
  1401. err: nil,
  1402. },
  1403. reason: "",
  1404. },
  1405. "Invalid Recovery Window": {
  1406. args: args{
  1407. client: fakesm.Client{},
  1408. config: esv1.SecretsManager{
  1409. RecoveryWindowInDays: 1,
  1410. },
  1411. getSecretOutput: &awssm.GetSecretValueOutput{},
  1412. describeSecretOutput: &awssm.DescribeSecretOutput{
  1413. Tags: []types.Tag{secretTag},
  1414. },
  1415. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1416. getSecretErr: nil,
  1417. describeSecretErr: nil,
  1418. deleteSecretErr: nil,
  1419. },
  1420. want: want{
  1421. err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
  1422. },
  1423. reason: "",
  1424. },
  1425. "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
  1426. args: args{
  1427. client: fakesm.Client{},
  1428. config: esv1.SecretsManager{
  1429. RecoveryWindowInDays: 7,
  1430. ForceDeleteWithoutRecovery: true,
  1431. },
  1432. getSecretOutput: &awssm.GetSecretValueOutput{},
  1433. describeSecretOutput: &awssm.DescribeSecretOutput{
  1434. Tags: []types.Tag{secretTag},
  1435. },
  1436. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1437. getSecretErr: nil,
  1438. describeSecretErr: nil,
  1439. deleteSecretErr: nil,
  1440. },
  1441. want: want{
  1442. err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
  1443. },
  1444. reason: "",
  1445. },
  1446. "Failed to get Tags": {
  1447. args: args{
  1448. client: fakeClient,
  1449. config: esv1.SecretsManager{},
  1450. getSecretOutput: &awssm.GetSecretValueOutput{},
  1451. describeSecretOutput: nil,
  1452. deleteSecretOutput: nil,
  1453. getSecretErr: nil,
  1454. describeSecretErr: errors.New("failed to get tags"),
  1455. deleteSecretErr: nil,
  1456. },
  1457. want: want{
  1458. err: errors.New("failed to get tags"),
  1459. },
  1460. reason: "",
  1461. },
  1462. "Secret Not Found": {
  1463. args: args{
  1464. client: fakeClient,
  1465. config: esv1.SecretsManager{},
  1466. getSecretOutput: nil,
  1467. describeSecretOutput: nil,
  1468. deleteSecretOutput: nil,
  1469. getSecretErr: errors.New("not here, sorry dude"),
  1470. describeSecretErr: nil,
  1471. deleteSecretErr: nil,
  1472. },
  1473. want: want{
  1474. err: errors.New("not here, sorry dude"),
  1475. },
  1476. },
  1477. "Not expected AWS error": {
  1478. args: args{
  1479. client: fakeClient,
  1480. config: esv1.SecretsManager{},
  1481. getSecretOutput: nil,
  1482. describeSecretOutput: nil,
  1483. deleteSecretOutput: nil,
  1484. getSecretErr: errors.New("aws unavailable"),
  1485. describeSecretErr: nil,
  1486. deleteSecretErr: nil,
  1487. },
  1488. want: want{
  1489. err: errors.New("aws unavailable"),
  1490. },
  1491. },
  1492. "unexpected error": {
  1493. args: args{
  1494. client: fakeClient,
  1495. config: esv1.SecretsManager{},
  1496. getSecretOutput: nil,
  1497. describeSecretOutput: nil,
  1498. deleteSecretOutput: nil,
  1499. getSecretErr: errors.New("timeout"),
  1500. describeSecretErr: nil,
  1501. deleteSecretErr: nil,
  1502. },
  1503. want: want{
  1504. err: errors.New("timeout"),
  1505. },
  1506. },
  1507. "DeleteWithPrefix": {
  1508. args: args{
  1509. client: fakesm.Client{
  1510. GetSecretValueFn: func(ctx context.Context, input *awssm.GetSecretValueInput, opts ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  1511. // Verify that the input secret ID has the prefix applied
  1512. if *input.SecretId != "my-prefix-"+fakeKey {
  1513. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1514. }
  1515. return &awssm.GetSecretValueOutput{}, nil
  1516. },
  1517. DescribeSecretFn: func(ctx context.Context, input *awssm.DescribeSecretInput, opts ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error) {
  1518. // Verify that the input secret ID has the prefix applied
  1519. if *input.SecretId != "my-prefix-"+fakeKey {
  1520. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1521. }
  1522. return &awssm.DescribeSecretOutput{
  1523. Tags: []types.Tag{secretTag},
  1524. }, nil
  1525. },
  1526. DeleteSecretFn: func(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
  1527. return &awssm.DeleteSecretOutput{}, nil
  1528. },
  1529. },
  1530. config: esv1.SecretsManager{},
  1531. prefix: "my-prefix-",
  1532. getSecretOutput: nil,
  1533. describeSecretOutput: nil,
  1534. deleteSecretOutput: nil,
  1535. getSecretErr: nil,
  1536. describeSecretErr: nil,
  1537. deleteSecretErr: nil,
  1538. },
  1539. want: want{
  1540. err: nil,
  1541. },
  1542. reason: "Verifies that the prefix is correctly applied when deleting a secret",
  1543. },
  1544. }
  1545. for name, tc := range tests {
  1546. t.Run(name, func(t *testing.T) {
  1547. ref := fake.PushSecretData{RemoteKey: fakeKey}
  1548. sm := SecretsManager{
  1549. client: &tc.args.client,
  1550. config: &tc.args.config,
  1551. prefix: tc.args.prefix,
  1552. }
  1553. if tc.args.client.GetSecretValueFn == nil {
  1554. tc.args.client.GetSecretValueFn = fakesm.NewGetSecretValueFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  1555. }
  1556. if tc.args.client.DescribeSecretFn == nil {
  1557. tc.args.client.DescribeSecretFn = fakesm.NewDescribeSecretFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  1558. }
  1559. if tc.args.client.DeleteSecretFn == nil {
  1560. tc.args.client.DeleteSecretFn = fakesm.NewDeleteSecretFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  1561. }
  1562. err := sm.DeleteSecret(context.TODO(), ref)
  1563. t.Logf("DeleteSecret error: %v", err)
  1564. // Error nil XOR tc.want.err nil
  1565. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1566. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1567. }
  1568. // if errors are the same type but their contents do not match
  1569. if err != nil && tc.want.err != nil {
  1570. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1571. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1572. }
  1573. }
  1574. })
  1575. }
  1576. }
  1577. func makeValidSecretStore() *esv1.SecretStore {
  1578. return &esv1.SecretStore{
  1579. ObjectMeta: metav1.ObjectMeta{
  1580. Name: "aws-secret-store",
  1581. Namespace: "default",
  1582. },
  1583. Spec: esv1.SecretStoreSpec{
  1584. Provider: &esv1.SecretStoreProvider{
  1585. AWS: &esv1.AWSProvider{
  1586. Service: esv1.AWSServiceSecretsManager,
  1587. Region: "eu-west-2",
  1588. },
  1589. },
  1590. },
  1591. }
  1592. }
  1593. func getTagSlice() []types.Tag {
  1594. tagKey1 := tagname1
  1595. tagValue1 := tagvalue1
  1596. tagKey2 := tagname2
  1597. tagValue2 := tagvalue2
  1598. return []types.Tag{
  1599. {
  1600. Key: &tagKey1,
  1601. Value: &tagValue1,
  1602. },
  1603. {
  1604. Key: &tagKey2,
  1605. Value: &tagValue2,
  1606. },
  1607. }
  1608. }
  1609. func TestSecretsManagerGetAllSecrets(t *testing.T) {
  1610. ctx := context.Background()
  1611. errBoom := errors.New("boom")
  1612. secretName := "my-secret"
  1613. secretVersion := "AWSCURRENT"
  1614. secretPath := "/path/to/secret"
  1615. secretValue := "secret value"
  1616. secretTags := map[string]string{
  1617. "foo": "bar",
  1618. }
  1619. // Test cases
  1620. testCases := []struct {
  1621. name string
  1622. ref esv1.ExternalSecretFind
  1623. secretName string
  1624. secretVersion string
  1625. secretValue string
  1626. batchGetSecretValueFn func(context.Context, *awssm.BatchGetSecretValueInput, ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error)
  1627. listSecretsFn func(context.Context, *awssm.ListSecretsInput, ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error)
  1628. getSecretValueFn func(context.Context, *awssm.GetSecretValueInput, ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error)
  1629. expectedData map[string][]byte
  1630. expectedError string
  1631. }{
  1632. {
  1633. name: "Matching secrets found",
  1634. ref: esv1.ExternalSecretFind{
  1635. Name: &esv1.FindName{
  1636. RegExp: secretName,
  1637. },
  1638. Path: ptr.To(secretPath),
  1639. },
  1640. secretName: secretName,
  1641. secretVersion: secretVersion,
  1642. secretValue: secretValue,
  1643. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1644. assert.Len(t, input.Filters, 1)
  1645. assert.Equal(t, "name", string(input.Filters[0].Key))
  1646. assert.Equal(t, secretPath, input.Filters[0].Values[0])
  1647. return &awssm.BatchGetSecretValueOutput{
  1648. SecretValues: []types.SecretValueEntry{
  1649. {
  1650. Name: ptr.To(secretName),
  1651. VersionStages: []string{secretVersion},
  1652. SecretBinary: []byte(secretValue),
  1653. },
  1654. },
  1655. }, nil
  1656. },
  1657. expectedData: map[string][]byte{
  1658. secretName: []byte(secretValue),
  1659. },
  1660. expectedError: "",
  1661. },
  1662. {
  1663. name: "Error occurred while fetching secret value",
  1664. ref: esv1.ExternalSecretFind{
  1665. Name: &esv1.FindName{
  1666. RegExp: secretName,
  1667. },
  1668. Path: ptr.To(secretPath),
  1669. },
  1670. secretName: secretName,
  1671. secretVersion: secretVersion,
  1672. secretValue: secretValue,
  1673. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1674. return &awssm.BatchGetSecretValueOutput{
  1675. SecretValues: []types.SecretValueEntry{
  1676. {
  1677. Name: ptr.To(secretName),
  1678. },
  1679. },
  1680. }, errBoom
  1681. },
  1682. expectedData: nil,
  1683. expectedError: errBoom.Error(),
  1684. },
  1685. {
  1686. name: "regexp: error occurred while listing secrets",
  1687. ref: esv1.ExternalSecretFind{
  1688. Name: &esv1.FindName{
  1689. RegExp: secretName,
  1690. },
  1691. },
  1692. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1693. return nil, errBoom
  1694. },
  1695. expectedData: nil,
  1696. expectedError: errBoom.Error(),
  1697. },
  1698. {
  1699. name: "regep: no matching secrets found",
  1700. ref: esv1.ExternalSecretFind{
  1701. Name: &esv1.FindName{
  1702. RegExp: secretName,
  1703. },
  1704. },
  1705. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1706. return &awssm.ListSecretsOutput{
  1707. SecretList: []types.SecretListEntry{
  1708. {
  1709. Name: ptr.To("other-secret"),
  1710. },
  1711. },
  1712. }, nil
  1713. },
  1714. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1715. return &awssm.BatchGetSecretValueOutput{
  1716. SecretValues: []types.SecretValueEntry{
  1717. {
  1718. Name: ptr.To("other-secret"),
  1719. },
  1720. },
  1721. }, nil
  1722. },
  1723. expectedData: make(map[string][]byte),
  1724. expectedError: "",
  1725. },
  1726. {
  1727. name: "invalid regexp",
  1728. ref: esv1.ExternalSecretFind{
  1729. Name: &esv1.FindName{
  1730. RegExp: "[",
  1731. },
  1732. },
  1733. expectedData: nil,
  1734. expectedError: "could not compile find.name.regexp [[]: error parsing regexp: missing closing ]: `[`",
  1735. },
  1736. {
  1737. name: "tags: Matching secrets found",
  1738. ref: esv1.ExternalSecretFind{
  1739. Tags: secretTags,
  1740. },
  1741. secretName: secretName,
  1742. secretVersion: secretVersion,
  1743. secretValue: secretValue,
  1744. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1745. assert.Len(t, input.Filters, 2)
  1746. assert.Equal(t, "tag-key", string(input.Filters[0].Key))
  1747. assert.Equal(t, "foo", input.Filters[0].Values[0])
  1748. assert.Equal(t, "tag-value", string(input.Filters[1].Key))
  1749. assert.Equal(t, "bar", input.Filters[1].Values[0])
  1750. return &awssm.BatchGetSecretValueOutput{
  1751. SecretValues: []types.SecretValueEntry{
  1752. {
  1753. Name: ptr.To(secretName),
  1754. VersionStages: []string{secretVersion},
  1755. SecretBinary: []byte(secretValue),
  1756. },
  1757. },
  1758. }, nil
  1759. },
  1760. expectedData: map[string][]byte{
  1761. secretName: []byte(secretValue),
  1762. },
  1763. expectedError: "",
  1764. },
  1765. {
  1766. name: "name and tags: matching secrets found",
  1767. ref: esv1.ExternalSecretFind{
  1768. Name: &esv1.FindName{
  1769. RegExp: secretName,
  1770. },
  1771. Tags: secretTags,
  1772. },
  1773. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1774. allSecrets := []types.SecretListEntry{
  1775. {
  1776. Name: ptr.To(secretName),
  1777. Tags: []types.Tag{
  1778. {Key: ptr.To("foo"), Value: ptr.To("bar")},
  1779. },
  1780. },
  1781. {
  1782. Name: ptr.To(fmt.Sprintf("%ssomeothertext", secretName)),
  1783. },
  1784. {
  1785. Name: ptr.To("unmatched-secret"),
  1786. Tags: []types.Tag{
  1787. {Key: ptr.To("foo"), Value: ptr.To("bar")},
  1788. },
  1789. },
  1790. }
  1791. filtered := make([]types.SecretListEntry, 0, len(allSecrets))
  1792. for _, secret := range allSecrets {
  1793. exclude := false
  1794. tagMap := map[string]string{}
  1795. for _, t := range secret.Tags {
  1796. if t.Key != nil && t.Value != nil {
  1797. tagMap[*t.Key] = *t.Value
  1798. }
  1799. }
  1800. for _, f := range input.Filters {
  1801. switch f.Key {
  1802. case types.FilterNameStringTypeName:
  1803. if secret.Name != nil {
  1804. for _, v := range f.Values {
  1805. if strings.Contains(*secret.Name, v) {
  1806. exclude = true
  1807. break
  1808. }
  1809. }
  1810. }
  1811. case types.FilterNameStringTypeTagKey:
  1812. for _, v := range f.Values {
  1813. if tagMap[v] == "" {
  1814. exclude = true
  1815. break
  1816. }
  1817. }
  1818. case types.FilterNameStringTypeDescription,
  1819. types.FilterNameStringTypeTagValue,
  1820. types.FilterNameStringTypePrimaryRegion,
  1821. types.FilterNameStringTypeOwningService,
  1822. types.FilterNameStringTypeAll:
  1823. continue
  1824. }
  1825. }
  1826. if !exclude {
  1827. filtered = append(filtered, secret)
  1828. }
  1829. }
  1830. return &awssm.ListSecretsOutput{SecretList: filtered}, nil
  1831. },
  1832. getSecretValueFn: func(_ context.Context, input *awssm.GetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  1833. if *input.SecretId == secretName {
  1834. return &awssm.GetSecretValueOutput{
  1835. Name: ptr.To(secretName),
  1836. VersionStages: []string{secretVersion},
  1837. SecretBinary: []byte(secretValue),
  1838. }, nil
  1839. }
  1840. if *input.SecretId == "unmatched-secret" {
  1841. return &awssm.GetSecretValueOutput{
  1842. Name: ptr.To("unmatched-secret"),
  1843. VersionStages: []string{secretVersion},
  1844. SecretBinary: []byte("othervalue"),
  1845. }, nil
  1846. }
  1847. return &awssm.GetSecretValueOutput{
  1848. Name: ptr.To(fmt.Sprintf("%ssomeothertext", secretName)),
  1849. VersionStages: []string{secretVersion},
  1850. SecretBinary: []byte("someothervalue"),
  1851. }, nil
  1852. },
  1853. expectedData: map[string][]byte{
  1854. secretName: []byte(secretValue),
  1855. },
  1856. expectedError: "",
  1857. },
  1858. {
  1859. name: "tags: error occurred while fetching secret value",
  1860. ref: esv1.ExternalSecretFind{
  1861. Tags: secretTags,
  1862. },
  1863. secretName: secretName,
  1864. secretVersion: secretVersion,
  1865. secretValue: secretValue,
  1866. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1867. return &awssm.BatchGetSecretValueOutput{
  1868. SecretValues: []types.SecretValueEntry{
  1869. {
  1870. Name: ptr.To(secretName),
  1871. VersionStages: []string{secretVersion},
  1872. SecretBinary: []byte(secretValue),
  1873. },
  1874. },
  1875. }, errBoom
  1876. },
  1877. expectedData: nil,
  1878. expectedError: errBoom.Error(),
  1879. },
  1880. {
  1881. name: "tags: error occurred while listing secrets",
  1882. ref: esv1.ExternalSecretFind{
  1883. Tags: secretTags,
  1884. },
  1885. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1886. return nil, errBoom
  1887. },
  1888. expectedData: nil,
  1889. expectedError: errBoom.Error(),
  1890. },
  1891. }
  1892. for _, tc := range testCases {
  1893. t.Run(tc.name, func(t *testing.T) {
  1894. fc := fakesm.NewClient()
  1895. fc.BatchGetSecretValueFn = tc.batchGetSecretValueFn
  1896. fc.ListSecretsFn = tc.listSecretsFn
  1897. fc.GetSecretValueFn = tc.getSecretValueFn
  1898. sm := SecretsManager{
  1899. client: fc,
  1900. cache: make(map[string]*awssm.GetSecretValueOutput),
  1901. }
  1902. data, err := sm.GetAllSecrets(ctx, tc.ref)
  1903. if err != nil && err.Error() != tc.expectedError {
  1904. t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
  1905. }
  1906. if !reflect.DeepEqual(data, tc.expectedData) {
  1907. t.Errorf("unexpected data: got %v, want %v", data, tc.expectedData)
  1908. }
  1909. })
  1910. }
  1911. }
  1912. func TestSecretsManagerValidate(t *testing.T) {
  1913. type fields struct {
  1914. cfg *aws.Config
  1915. referentAuth bool
  1916. }
  1917. validConfig := &aws.Config{
  1918. Credentials: credentials.NewStaticCredentialsProvider(
  1919. "fake",
  1920. "fake",
  1921. "fake",
  1922. ),
  1923. }
  1924. invalidConfig := &aws.Config{
  1925. Credentials: &FakeCredProvider{
  1926. retrieveFunc: func() (aws.Credentials, error) {
  1927. return aws.Credentials{}, errors.New("invalid credentials")
  1928. },
  1929. },
  1930. }
  1931. tests := []struct {
  1932. name string
  1933. fields fields
  1934. want esv1.ValidationResult
  1935. wantErr bool
  1936. }{
  1937. {
  1938. name: "ReferentAuth should always return unknown",
  1939. fields: fields{
  1940. referentAuth: true,
  1941. },
  1942. want: esv1.ValidationResultUnknown,
  1943. },
  1944. {
  1945. name: "Valid credentials should return ready",
  1946. fields: fields{
  1947. cfg: validConfig,
  1948. },
  1949. want: esv1.ValidationResultReady,
  1950. },
  1951. {
  1952. name: "Invalid credentials should return error",
  1953. fields: fields{
  1954. cfg: invalidConfig,
  1955. },
  1956. want: esv1.ValidationResultError,
  1957. wantErr: true,
  1958. },
  1959. }
  1960. for _, tt := range tests {
  1961. t.Run(tt.name, func(t *testing.T) {
  1962. sm := &SecretsManager{
  1963. cfg: tt.fields.cfg,
  1964. referentAuth: tt.fields.referentAuth,
  1965. }
  1966. got, err := sm.Validate()
  1967. if (err != nil) != tt.wantErr {
  1968. t.Errorf("SecretsManager.Validate() error = %v, wantErr %v", err, tt.wantErr)
  1969. return
  1970. }
  1971. if !reflect.DeepEqual(got, tt.want) {
  1972. t.Errorf("SecretsManager.Validate() = %v, want %v", got, tt.want)
  1973. }
  1974. })
  1975. }
  1976. }
  1977. func TestSecretExists(t *testing.T) {
  1978. arn := testARN
  1979. defaultVersion := testDefaultVersion
  1980. secretValueOutput := &awssm.GetSecretValueOutput{
  1981. ARN: &arn,
  1982. VersionId: &defaultVersion,
  1983. }
  1984. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  1985. getSecretCorrectErr := types.ResourceNotFoundException{}
  1986. getSecretWrongErr := types.InvalidRequestException{}
  1987. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: fakeSecretKey, RemoteKey: fakeKey, Property: ""}
  1988. type args struct {
  1989. store *esv1.AWSProvider
  1990. client fakesm.Client
  1991. pushSecretData fake.PushSecretData
  1992. }
  1993. type want struct {
  1994. err error
  1995. wantError bool
  1996. }
  1997. tests := map[string]struct {
  1998. args args
  1999. want want
  2000. }{
  2001. "SecretExistsReturnsTrueForExistingSecret": {
  2002. args: args{
  2003. store: makeValidSecretStore().Spec.Provider.AWS,
  2004. client: fakesm.Client{
  2005. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  2006. },
  2007. pushSecretData: pushSecretDataWithoutProperty,
  2008. },
  2009. want: want{
  2010. err: nil,
  2011. wantError: true,
  2012. },
  2013. },
  2014. "SecretExistsReturnsFalseForNonExistingSecret": {
  2015. args: args{
  2016. store: makeValidSecretStore().Spec.Provider.AWS,
  2017. client: fakesm.Client{
  2018. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  2019. },
  2020. pushSecretData: pushSecretDataWithoutProperty,
  2021. },
  2022. want: want{
  2023. err: nil,
  2024. wantError: false,
  2025. },
  2026. },
  2027. "SecretExistsReturnsFalseForErroredSecret": {
  2028. args: args{
  2029. store: makeValidSecretStore().Spec.Provider.AWS,
  2030. client: fakesm.Client{
  2031. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretWrongErr),
  2032. },
  2033. pushSecretData: pushSecretDataWithoutProperty,
  2034. },
  2035. want: want{
  2036. err: &getSecretWrongErr,
  2037. wantError: false,
  2038. },
  2039. },
  2040. }
  2041. for name, tc := range tests {
  2042. t.Run(name, func(t *testing.T) {
  2043. sm := &SecretsManager{
  2044. client: &tc.args.client,
  2045. }
  2046. got, err := sm.SecretExists(context.Background(), tc.args.pushSecretData)
  2047. assert.Equal(
  2048. t,
  2049. tc.want,
  2050. want{
  2051. err: err,
  2052. wantError: got,
  2053. })
  2054. })
  2055. }
  2056. }
  2057. func TestConstructMetadataWithDefaults(t *testing.T) {
  2058. tests := []struct {
  2059. name string
  2060. input *apiextensionsv1.JSON
  2061. expected *metadata.PushSecretMetadata[PushSecretMetadataSpec]
  2062. expectError bool
  2063. }{
  2064. {
  2065. name: "Valid metadata with multiple fields",
  2066. input: &apiextensionsv1.JSON{Raw: []byte(`{
  2067. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2068. "kind": "PushSecretMetadata",
  2069. "spec": {
  2070. "description": "test description",
  2071. "secretPushFormat":"string",
  2072. "kmsKeyID": "custom-kms-key",
  2073. "tags": {
  2074. "customKey": "customValue"
  2075. },
  2076. }
  2077. }`)},
  2078. expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
  2079. APIVersion: "kubernetes.external-secrets.io/v1alpha1",
  2080. Kind: "PushSecretMetadata",
  2081. Spec: PushSecretMetadataSpec{
  2082. Description: "test description",
  2083. SecretPushFormat: "string",
  2084. KMSKeyID: "custom-kms-key",
  2085. Tags: map[string]string{
  2086. "customKey": "customValue",
  2087. managedBy: externalSecrets,
  2088. },
  2089. },
  2090. },
  2091. },
  2092. {
  2093. name: "Empty metadata, defaults applied",
  2094. input: nil,
  2095. expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
  2096. Spec: PushSecretMetadataSpec{
  2097. Description: fmt.Sprintf("secret '%s:%s'", managedBy, externalSecrets),
  2098. SecretPushFormat: "binary",
  2099. KMSKeyID: "alias/aws/secretsmanager",
  2100. Tags: map[string]string{
  2101. managedBy: externalSecrets,
  2102. },
  2103. },
  2104. },
  2105. },
  2106. {
  2107. name: "Added default metadata with 'managed-by' tag",
  2108. input: &apiextensionsv1.JSON{Raw: []byte(`{
  2109. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2110. "kind": "PushSecretMetadata",
  2111. "spec": {
  2112. "tags": {
  2113. "managed-by": "external-secrets",
  2114. "customKey": "customValue"
  2115. },
  2116. }
  2117. }`)},
  2118. expected: nil,
  2119. expectError: true,
  2120. },
  2121. {
  2122. name: "Invalid metadata format",
  2123. input: &apiextensionsv1.JSON{Raw: []byte(`invalid-json`)},
  2124. expected: nil,
  2125. expectError: true,
  2126. },
  2127. {
  2128. name: "Metadata with 'managed-by' tag specified",
  2129. input: &apiextensionsv1.JSON{Raw: []byte(`{"tags":{"managed-by":"invalid"}}`)},
  2130. expected: nil,
  2131. expectError: true,
  2132. },
  2133. }
  2134. for _, tt := range tests {
  2135. t.Run(tt.name, func(t *testing.T) {
  2136. result, err := (&SecretsManager{}).constructMetadataWithDefaults(tt.input)
  2137. if tt.expectError {
  2138. assert.Error(t, err)
  2139. } else {
  2140. assert.NoError(t, err)
  2141. assert.Equal(t, tt.expected, result)
  2142. }
  2143. })
  2144. }
  2145. }
  2146. func TestComputeTagsToUpdate(t *testing.T) {
  2147. tests := []struct {
  2148. name string
  2149. tags map[string]string
  2150. metaTags map[string]string
  2151. expected []types.Tag
  2152. modified bool
  2153. }{
  2154. {
  2155. name: "No tags to update",
  2156. tags: map[string]string{
  2157. "key1": "value1",
  2158. "key2": "value2",
  2159. },
  2160. metaTags: map[string]string{
  2161. "key1": "value1",
  2162. "key2": "value2",
  2163. },
  2164. expected: []types.Tag{
  2165. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2166. {Key: ptr.To("key2"), Value: ptr.To("value2")},
  2167. },
  2168. modified: false,
  2169. },
  2170. {
  2171. name: "No tags to update as managed-by tag is ignored",
  2172. tags: map[string]string{
  2173. "key1": "value1",
  2174. "key2": "value2",
  2175. },
  2176. metaTags: map[string]string{
  2177. "key1": "value1",
  2178. "key2": "value2",
  2179. managedBy: externalSecrets,
  2180. },
  2181. expected: []types.Tag{
  2182. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2183. {Key: ptr.To("key2"), Value: ptr.To("value2")},
  2184. {Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)},
  2185. },
  2186. modified: false,
  2187. },
  2188. {
  2189. name: "Add new tag",
  2190. tags: map[string]string{
  2191. "key1": "value1",
  2192. },
  2193. metaTags: map[string]string{
  2194. "key1": "value1",
  2195. "key2": "value2",
  2196. },
  2197. expected: []types.Tag{
  2198. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2199. {Key: ptr.To("key2"), Value: ptr.To("value2")},
  2200. },
  2201. modified: true,
  2202. },
  2203. {
  2204. name: "Update existing tag value",
  2205. tags: map[string]string{
  2206. "key1": "value1",
  2207. },
  2208. metaTags: map[string]string{
  2209. "key1": "newValue",
  2210. },
  2211. expected: []types.Tag{
  2212. {Key: ptr.To("key1"), Value: ptr.To("newValue")},
  2213. },
  2214. modified: true,
  2215. },
  2216. {
  2217. name: "Empty tags and metaTags",
  2218. tags: map[string]string{},
  2219. metaTags: map[string]string{},
  2220. expected: []types.Tag{},
  2221. modified: false,
  2222. },
  2223. {
  2224. name: "Empty tags with non-empty metaTags",
  2225. tags: map[string]string{},
  2226. metaTags: map[string]string{
  2227. "key1": "value1",
  2228. },
  2229. expected: []types.Tag{
  2230. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2231. },
  2232. modified: true,
  2233. },
  2234. }
  2235. for _, tt := range tests {
  2236. t.Run(tt.name, func(t *testing.T) {
  2237. result, modified := computeTagsToUpdate(tt.tags, tt.metaTags)
  2238. assert.ElementsMatch(t, tt.expected, result)
  2239. assert.Equal(t, tt.modified, modified)
  2240. })
  2241. }
  2242. }
  2243. func TestPatchTags(t *testing.T) {
  2244. type call struct {
  2245. untagCalled bool
  2246. tagCalled bool
  2247. }
  2248. tests := []struct {
  2249. name string
  2250. existingTags map[string]string
  2251. metaTags map[string]string
  2252. expectUntag bool
  2253. expectTag bool
  2254. assertsTag func(input *awssm.TagResourceInput)
  2255. assertsUntag func(input *awssm.UntagResourceInput)
  2256. }{
  2257. {
  2258. name: "no changes",
  2259. existingTags: map[string]string{"a": "1"},
  2260. metaTags: map[string]string{"a": "1"},
  2261. expectUntag: false,
  2262. expectTag: false,
  2263. assertsTag: func(input *awssm.TagResourceInput) {
  2264. assert.Fail(t, "Expected TagResource to not be called")
  2265. },
  2266. assertsUntag: func(input *awssm.UntagResourceInput) {
  2267. assert.Fail(t, "Expected UntagResource to not be called")
  2268. },
  2269. },
  2270. {
  2271. name: "update tag value",
  2272. existingTags: map[string]string{"a": "1"},
  2273. metaTags: map[string]string{"a": "2"},
  2274. expectUntag: false,
  2275. expectTag: true,
  2276. assertsTag: func(input *awssm.TagResourceInput) {
  2277. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)})
  2278. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("a"), Value: ptr.To("2")})
  2279. },
  2280. assertsUntag: func(input *awssm.UntagResourceInput) {
  2281. assert.Fail(t, "Expected UntagResource to not be called")
  2282. },
  2283. },
  2284. {
  2285. name: "remove tag",
  2286. existingTags: map[string]string{"a": "1", "b": "2"},
  2287. metaTags: map[string]string{"a": "1"},
  2288. expectUntag: true,
  2289. expectTag: false,
  2290. assertsTag: func(input *awssm.TagResourceInput) {
  2291. assert.Fail(t, "Expected TagResource to not be called")
  2292. },
  2293. assertsUntag: func(input *awssm.UntagResourceInput) {
  2294. assert.Equal(t, []string{"b"}, input.TagKeys)
  2295. },
  2296. },
  2297. {
  2298. name: "add tags",
  2299. existingTags: map[string]string{"a": "1"},
  2300. metaTags: map[string]string{"a": "1", "b": "2"},
  2301. expectUntag: false,
  2302. expectTag: true,
  2303. assertsTag: func(input *awssm.TagResourceInput) {
  2304. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)})
  2305. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("a"), Value: ptr.To("1")})
  2306. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("b"), Value: ptr.To("2")})
  2307. },
  2308. assertsUntag: func(input *awssm.UntagResourceInput) {
  2309. assert.Fail(t, "Expected UntagResource to not be called")
  2310. },
  2311. },
  2312. }
  2313. for _, tt := range tests {
  2314. t.Run(tt.name, func(t *testing.T) {
  2315. calls := call{}
  2316. fakeClient := &fakesm.Client{
  2317. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  2318. tt.assertsTag(input)
  2319. calls.tagCalled = true
  2320. }),
  2321. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
  2322. tt.assertsUntag(input)
  2323. calls.untagCalled = true
  2324. }),
  2325. }
  2326. sm := &SecretsManager{client: fakeClient}
  2327. metaMap := map[string]interface{}{
  2328. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2329. "kind": "PushSecretMetadata",
  2330. "spec": map[string]interface{}{
  2331. "description": "adding managed-by tag explicitly",
  2332. "tags": tt.metaTags,
  2333. },
  2334. }
  2335. raw, err := json.Marshal(metaMap)
  2336. require.NoError(t, err)
  2337. meta := &apiextensionsv1.JSON{Raw: raw}
  2338. secretId := "secret"
  2339. err = sm.patchTags(context.Background(), meta, &secretId, tt.existingTags)
  2340. require.NoError(t, err)
  2341. assert.Equal(t, tt.expectUntag, calls.untagCalled)
  2342. assert.Equal(t, tt.expectTag, calls.tagCalled)
  2343. })
  2344. }
  2345. }
  2346. // FakeCredProvider implements the AWS credentials.Provider interface
  2347. // It is used to inject an error into the AWS config to cause a
  2348. // validation error.
  2349. type FakeCredProvider struct {
  2350. retrieveFunc func() (aws.Credentials, error)
  2351. }
  2352. func (f *FakeCredProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
  2353. return f.retrieveFunc()
  2354. }
  2355. func (f *FakeCredProvider) IsExpired() bool {
  2356. return true
  2357. }