secretsmanager_test.go 65 KB

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