secretsmanager_test.go 85 KB

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