secretsmanager_test.go 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784
  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. "SetSecretWithRegionReplication": {
  1194. reason: "create a new secret with replication to extra regions",
  1195. args: args{
  1196. store: makeValidSecretStore().Spec.Provider.AWS,
  1197. client: fakesm.Client{
  1198. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  1199. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  1200. },
  1201. pushSecretData: fake.PushSecretData{
  1202. SecretKey: secretKey,
  1203. RemoteKey: fakeKey,
  1204. Property: "",
  1205. Metadata: &apiextensionsv1.JSON{
  1206. Raw: []byte(`{
  1207. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1208. "kind": "PushSecretMetadata",
  1209. "spec": {
  1210. "secretPushFormat": "string",
  1211. "replicationLocations": [
  1212. "eu-north-1",
  1213. "eu-central-1"
  1214. ]
  1215. }
  1216. }`),
  1217. },
  1218. },
  1219. },
  1220. want: want{
  1221. err: nil,
  1222. },
  1223. },
  1224. "SetReplicationOnSecretWhileKeepingExistingReplication": {
  1225. reason: "sync an existing secret with existing replication region previously setup",
  1226. args: args{
  1227. store: makeValidSecretStore().Spec.Provider.AWS,
  1228. client: fakesm.Client{
  1229. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1230. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  1231. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  1232. ARN: &arn,
  1233. Tags: externalSecretsTag,
  1234. VersionIdsToStages: map[string][]string{
  1235. defaultVersion: {"AWSCURRENT"},
  1236. },
  1237. KmsKeyId: aws.String("bb123123-b2b0-4f60-ac3a-44a13f0e6b6c"),
  1238. ReplicationStatus: []types.ReplicationStatusType{
  1239. // Existing replication region not part of the desired state (to be removed)
  1240. {KmsKeyId: aws.String("bb123123-b2b0-4f60-ac3a-44a13f0e6b6c"), Region: aws.String("eu-west-3"), Status: types.StatusTypeInSync},
  1241. // Existing replication region part of the desired state (kept).
  1242. {KmsKeyId: aws.String("bb123123-b2b0-4f60-ac3a-44a13f0e6b6c"), Region: aws.String("eu-north-1"), Status: types.StatusTypeInSync},
  1243. // Existing replication region not part of the desired state with failed status
  1244. {KmsKeyId: aws.String("bb123123-b2b0-4f60-ac3a-44a13f0e6b6c"), Region: aws.String("sa-east-1"), Status: types.StatusTypeFailed},
  1245. // Existing replication region not part of the desired state with in-progress status
  1246. {KmsKeyId: aws.String("bb123123-b2b0-4f60-ac3a-44a13f0e6b6c"), Region: aws.String("ap-southeast-2"), Status: types.StatusTypeInProgress},
  1247. },
  1248. }, nil),
  1249. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(nil, &types.ResourceNotFoundException{}),
  1250. ReplicateSecretToRegionsFn: fakesm.NewReplicateSecretToRegionsFn(
  1251. &awssm.ReplicateSecretToRegionsOutput{},
  1252. nil,
  1253. func(got *awssm.ReplicateSecretToRegionsInput) {
  1254. assert.Len(t, got.AddReplicaRegions, 1)
  1255. assert.EqualValues(t, got.AddReplicaRegions[0].Region, aws.String("eu-central-1"))
  1256. },
  1257. ),
  1258. RemoveRegionsFromReplicationFn: fakesm.NewRemoveRegionsFromReplicationFn(
  1259. &awssm.RemoveRegionsFromReplicationOutput{},
  1260. nil,
  1261. func(got *awssm.RemoveRegionsFromReplicationInput) {
  1262. assert.ElementsMatch(t, []string{"eu-west-3", "sa-east-1", "ap-southeast-2"}, got.RemoveReplicaRegions)
  1263. },
  1264. ),
  1265. },
  1266. pushSecretData: fake.PushSecretData{
  1267. SecretKey: secretKey,
  1268. RemoteKey: fakeKey,
  1269. Property: "",
  1270. Metadata: &apiextensionsv1.JSON{
  1271. Raw: []byte(`{
  1272. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1273. "kind": "PushSecretMetadata",
  1274. "spec": {
  1275. "secretPushFormat": "string",
  1276. "replicationLocations": [
  1277. "eu-north-1",
  1278. "eu-central-1"
  1279. ]
  1280. }
  1281. }`),
  1282. },
  1283. },
  1284. },
  1285. want: want{
  1286. err: nil,
  1287. },
  1288. },
  1289. "SetReplicationOnSecretWithoutPreviousExistingReplications": {
  1290. reason: "sync an existing secret with no previous replication region previously setup",
  1291. args: args{
  1292. store: makeValidSecretStore().Spec.Provider.AWS,
  1293. client: fakesm.Client{
  1294. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1295. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  1296. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  1297. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(nil, &types.ResourceNotFoundException{}),
  1298. ReplicateSecretToRegionsFn: fakesm.NewReplicateSecretToRegionsFn(
  1299. &awssm.ReplicateSecretToRegionsOutput{},
  1300. nil,
  1301. func(got *awssm.ReplicateSecretToRegionsInput) {
  1302. assert.EqualValues(t, got.AddReplicaRegions, []types.ReplicaRegionType{{Region: aws.String("eu-north-1")}, {Region: aws.String("eu-central-1")}})
  1303. assert.Len(t, got.AddReplicaRegions, 2)
  1304. assert.EqualValues(t, got.AddReplicaRegions[0].Region, aws.String("eu-north-1"))
  1305. assert.EqualValues(t, got.AddReplicaRegions[1].Region, aws.String("eu-central-1"))
  1306. },
  1307. ),
  1308. },
  1309. pushSecretData: fake.PushSecretData{
  1310. SecretKey: secretKey,
  1311. RemoteKey: fakeKey,
  1312. Property: "",
  1313. Metadata: &apiextensionsv1.JSON{
  1314. Raw: []byte(`{
  1315. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1316. "kind": "PushSecretMetadata",
  1317. "spec": {
  1318. "secretPushFormat": "string",
  1319. "replicationLocations": [
  1320. "eu-north-1",
  1321. "eu-central-1"
  1322. ]
  1323. }
  1324. }`),
  1325. },
  1326. },
  1327. },
  1328. want: want{
  1329. err: nil,
  1330. },
  1331. },
  1332. "SetReplicationForInvalidRegionFails": {
  1333. reason: "sync an existing secret with existing replication region previously setup",
  1334. args: args{
  1335. store: makeValidSecretStore().Spec.Provider.AWS,
  1336. client: fakesm.Client{
  1337. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1338. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  1339. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  1340. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(nil, &types.ResourceNotFoundException{}),
  1341. ReplicateSecretToRegionsFn: fakesm.NewReplicateSecretToRegionsFn(nil, &types.InvalidRequestException{}),
  1342. },
  1343. pushSecretData: fake.PushSecretData{
  1344. SecretKey: secretKey,
  1345. RemoteKey: fakeKey,
  1346. Property: "",
  1347. Metadata: &apiextensionsv1.JSON{
  1348. Raw: []byte(`{
  1349. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1350. "kind": "PushSecretMetadata",
  1351. "spec": {
  1352. "secretPushFormat": "string",
  1353. "replicationLocations": [
  1354. "xx-invalid-1"
  1355. ]
  1356. }
  1357. }`),
  1358. },
  1359. },
  1360. },
  1361. want: want{
  1362. err: errors.New("failed to replicate existing secret to regions"),
  1363. },
  1364. },
  1365. }
  1366. for name, tc := range tests {
  1367. t.Run(name, func(t *testing.T) {
  1368. sm := SecretsManager{
  1369. client: &tc.args.client,
  1370. prefix: tc.args.store.Prefix,
  1371. newUUID: func() string { return tc.args.newUUID },
  1372. kube: tc.args.kubeclient,
  1373. }
  1374. err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
  1375. // Error nil XOR tc.want.err nil
  1376. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1377. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1378. }
  1379. // if errors are the same type but their contents do not match
  1380. if err != nil && tc.want.err != nil {
  1381. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1382. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1383. }
  1384. }
  1385. })
  1386. }
  1387. }
  1388. func TestPushSecretTagsUpdatedWhenValueUnchanged(t *testing.T) {
  1389. secretKey := fakeSecretKey
  1390. secretValue := []byte("fake-value")
  1391. fakeSecret := &corev1.Secret{
  1392. Data: map[string][]byte{
  1393. secretKey: secretValue,
  1394. },
  1395. }
  1396. arn := testARN
  1397. defaultVersion := testDefaultVersion
  1398. managedBy := managedBy
  1399. externalSecrets := externalSecrets
  1400. tagResourceCalled := false
  1401. var capturedTagInput *awssm.TagResourceInput
  1402. client := fakesm.Client{
  1403. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  1404. ARN: &arn,
  1405. SecretBinary: secretValue,
  1406. VersionId: &defaultVersion,
  1407. }, nil),
  1408. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  1409. ARN: &arn,
  1410. Tags: []types.Tag{
  1411. {Key: &managedBy, Value: &externalSecrets},
  1412. },
  1413. VersionIdsToStages: map[string][]string{
  1414. defaultVersion: {"AWSCURRENT"},
  1415. },
  1416. }, nil),
  1417. PutSecretValueFn: fakesm.NewPutSecretValueFn(nil, fmt.Errorf("PutSecretValue should not be called when value is unchanged")),
  1418. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  1419. tagResourceCalled = true
  1420. capturedTagInput = input
  1421. }),
  1422. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1423. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  1424. }
  1425. sm := SecretsManager{
  1426. client: &client,
  1427. }
  1428. pushSecretData := fake.PushSecretData{
  1429. SecretKey: secretKey,
  1430. RemoteKey: fakeKey,
  1431. Property: "",
  1432. Metadata: &apiextensionsv1.JSON{
  1433. Raw: []byte(`{
  1434. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1435. "kind": "PushSecretMetadata",
  1436. "spec": {
  1437. "tags": {"newTag": "newValue"}
  1438. }
  1439. }`),
  1440. },
  1441. }
  1442. err := sm.PushSecret(context.Background(), fakeSecret, pushSecretData)
  1443. require.NoError(t, err, "PushSecret should not return error when value is unchanged but tags need updating")
  1444. assert.True(t, tagResourceCalled, "TagResource should be called even when secret value is unchanged")
  1445. require.NotNil(t, capturedTagInput, "TagResourceInput should be captured")
  1446. assert.Len(t, capturedTagInput.Tags, 2)
  1447. assert.Contains(t, capturedTagInput.Tags, types.Tag{Key: &managedBy, Value: &externalSecrets})
  1448. assert.Contains(t, capturedTagInput.Tags, types.Tag{Key: new("newTag"), Value: new("newValue")})
  1449. }
  1450. func TestPushSecretResourcePolicyUpdatedWhenValueUnchanged(t *testing.T) {
  1451. secretKey := fakeSecretKey
  1452. secretValue := []byte("fake-value")
  1453. fakeSecret := &corev1.Secret{
  1454. Data: map[string][]byte{
  1455. secretKey: secretValue,
  1456. },
  1457. }
  1458. arn := testARN
  1459. defaultVersion := testDefaultVersion
  1460. managedBy := managedBy
  1461. externalSecrets := externalSecrets
  1462. putResourcePolicyCalled := false
  1463. var capturedPolicyInput *awssm.PutResourcePolicyInput
  1464. putSecretValueCalled := false
  1465. existingPolicy := `{"Version":"2012-10-17","Statement":[{"Sid":"OldPolicy","Effect":"Deny","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`
  1466. newPolicy := `{"Version":"2012-10-17","Statement":[{"Sid":"NewPolicy","Effect":"Allow","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`
  1467. client := fakesm.Client{
  1468. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  1469. ARN: &arn,
  1470. SecretBinary: secretValue,
  1471. VersionId: &defaultVersion,
  1472. }, nil),
  1473. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  1474. ARN: &arn,
  1475. Tags: []types.Tag{
  1476. {Key: &managedBy, Value: &externalSecrets},
  1477. },
  1478. VersionIdsToStages: map[string][]string{
  1479. defaultVersion: {"AWSCURRENT"},
  1480. },
  1481. }, nil),
  1482. PutSecretValueFn: func(_ context.Context, _ *awssm.PutSecretValueInput, _ ...func(*awssm.Options)) (*awssm.PutSecretValueOutput, error) {
  1483. putSecretValueCalled = true
  1484. return nil, fmt.Errorf("PutSecretValue should not be called when value is unchanged")
  1485. },
  1486. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  1487. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1488. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  1489. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(&awssm.GetResourcePolicyOutput{
  1490. ResourcePolicy: &existingPolicy,
  1491. }, nil),
  1492. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil, func(input *awssm.PutResourcePolicyInput) {
  1493. putResourcePolicyCalled = true
  1494. capturedPolicyInput = input
  1495. }),
  1496. }
  1497. kubeclient := clientfake.NewFakeClient(&corev1.ConfigMap{
  1498. ObjectMeta: metav1.ObjectMeta{
  1499. Name: "resource-policy",
  1500. },
  1501. Data: map[string]string{
  1502. "policy.json": newPolicy,
  1503. },
  1504. })
  1505. sm := SecretsManager{
  1506. client: &client,
  1507. kube: kubeclient,
  1508. }
  1509. pushSecretData := fake.PushSecretData{
  1510. SecretKey: secretKey,
  1511. RemoteKey: fakeKey,
  1512. Property: "",
  1513. Metadata: &apiextensionsv1.JSON{
  1514. Raw: []byte(`{
  1515. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1516. "kind": "PushSecretMetadata",
  1517. "spec": {
  1518. "resourcePolicy": {
  1519. "blockPublicPolicy": true,
  1520. "policySourceRef": {
  1521. "kind": "ConfigMap",
  1522. "name": "resource-policy",
  1523. "key": "policy.json"
  1524. }
  1525. }
  1526. }
  1527. }`),
  1528. },
  1529. }
  1530. err := sm.PushSecret(context.Background(), fakeSecret, pushSecretData)
  1531. require.NoError(t, err, "PushSecret should not return error when value is unchanged but resource policy needs updating")
  1532. assert.True(t, putResourcePolicyCalled, "PutResourcePolicy should be called even when secret value is unchanged")
  1533. assert.False(t, putSecretValueCalled, "PutSecretValue should not be called when value is unchanged")
  1534. require.NotNil(t, capturedPolicyInput, "PutResourcePolicyInput should be captured")
  1535. assert.Equal(t, fakeKey, *capturedPolicyInput.SecretId)
  1536. assert.JSONEq(t, newPolicy, *capturedPolicyInput.ResourcePolicy)
  1537. }
  1538. func TestPushSecretEmptyExistingResourcePolicy(t *testing.T) {
  1539. secretKey := fakeSecretKey
  1540. secretValue := []byte("fake-value")
  1541. fakeSecret := &corev1.Secret{
  1542. Data: map[string][]byte{
  1543. secretKey: secretValue,
  1544. },
  1545. }
  1546. arn := testARN
  1547. defaultVersion := testDefaultVersion
  1548. managed := managedBy
  1549. manager := externalSecrets
  1550. putResourcePolicyCalled := false
  1551. newPolicy := `{"Version":"2012-10-17","Statement":[{"Sid":"DenyAll","Effect":"Deny","Principal":"*","Action":"secretsmanager:GetSecretValue","Resource":"*"}]}`
  1552. client := fakesm.Client{
  1553. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  1554. ARN: &arn,
  1555. SecretBinary: secretValue,
  1556. VersionId: &defaultVersion,
  1557. }, nil),
  1558. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  1559. ARN: &arn,
  1560. Tags: []types.Tag{
  1561. {Key: &managed, Value: &manager},
  1562. },
  1563. VersionIdsToStages: map[string][]string{
  1564. defaultVersion: {"AWSCURRENT"},
  1565. },
  1566. }, nil),
  1567. PutSecretValueFn: fakesm.NewPutSecretValueFn(&awssm.PutSecretValueOutput{}, nil),
  1568. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  1569. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1570. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(&awssm.GetResourcePolicyOutput{}, nil),
  1571. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil, func(input *awssm.PutResourcePolicyInput) {
  1572. putResourcePolicyCalled = true
  1573. }),
  1574. }
  1575. kubeclient := clientfake.NewFakeClient(&corev1.ConfigMap{
  1576. ObjectMeta: metav1.ObjectMeta{
  1577. Name: "resource-policy",
  1578. },
  1579. Data: map[string]string{
  1580. "policy.json": newPolicy,
  1581. },
  1582. })
  1583. sm := SecretsManager{
  1584. client: &client,
  1585. kube: kubeclient,
  1586. }
  1587. pushSecretData := fake.PushSecretData{
  1588. SecretKey: secretKey,
  1589. RemoteKey: fakeKey,
  1590. Property: "",
  1591. Metadata: &apiextensionsv1.JSON{
  1592. Raw: []byte(`{
  1593. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1594. "kind": "PushSecretMetadata",
  1595. "spec": {
  1596. "secretPushFormat": "string",
  1597. "resourcePolicy": {
  1598. "blockPublicPolicy": true,
  1599. "policySourceRef": {
  1600. "kind": "ConfigMap",
  1601. "name": "resource-policy",
  1602. "key": "policy.json"
  1603. }
  1604. }
  1605. }
  1606. }`),
  1607. },
  1608. }
  1609. err := sm.PushSecret(context.Background(), fakeSecret, pushSecretData)
  1610. require.NoError(t, err)
  1611. assert.True(t, putResourcePolicyCalled, "PutResourcePolicy should be called when existing policy is empty")
  1612. }
  1613. func TestDeleteSecret(t *testing.T) {
  1614. fakeClient := fakesm.Client{}
  1615. managed := managedBy
  1616. manager := externalSecrets
  1617. secretTag := types.Tag{
  1618. Key: &managed,
  1619. Value: &manager,
  1620. }
  1621. type args struct {
  1622. client fakesm.Client
  1623. config esv1.SecretsManager
  1624. prefix string
  1625. getSecretOutput *awssm.GetSecretValueOutput
  1626. describeSecretOutput *awssm.DescribeSecretOutput
  1627. deleteSecretOutput *awssm.DeleteSecretOutput
  1628. getSecretErr error
  1629. describeSecretErr error
  1630. deleteSecretErr error
  1631. }
  1632. type want struct {
  1633. err error
  1634. }
  1635. type testCase struct {
  1636. args args
  1637. want want
  1638. reason string
  1639. }
  1640. tests := map[string]testCase{
  1641. "Deletes Successfully": {
  1642. args: args{
  1643. client: fakeClient,
  1644. config: esv1.SecretsManager{},
  1645. getSecretOutput: &awssm.GetSecretValueOutput{},
  1646. describeSecretOutput: &awssm.DescribeSecretOutput{
  1647. Tags: []types.Tag{secretTag},
  1648. },
  1649. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1650. getSecretErr: nil,
  1651. describeSecretErr: nil,
  1652. deleteSecretErr: nil,
  1653. },
  1654. want: want{
  1655. err: nil,
  1656. },
  1657. reason: "",
  1658. },
  1659. "Deletes Successfully with ForceDeleteWithoutRecovery": {
  1660. args: args{
  1661. client: fakeClient,
  1662. config: esv1.SecretsManager{
  1663. ForceDeleteWithoutRecovery: true,
  1664. },
  1665. getSecretOutput: &awssm.GetSecretValueOutput{},
  1666. describeSecretOutput: &awssm.DescribeSecretOutput{
  1667. Tags: []types.Tag{secretTag},
  1668. },
  1669. deleteSecretOutput: &awssm.DeleteSecretOutput{
  1670. DeletionDate: aws.Time(time.Now()),
  1671. },
  1672. getSecretErr: nil,
  1673. describeSecretErr: nil,
  1674. deleteSecretErr: nil,
  1675. },
  1676. want: want{
  1677. err: nil,
  1678. },
  1679. reason: "",
  1680. },
  1681. "Not Managed by ESO": {
  1682. args: args{
  1683. client: fakeClient,
  1684. config: esv1.SecretsManager{},
  1685. getSecretOutput: &awssm.GetSecretValueOutput{},
  1686. describeSecretOutput: &awssm.DescribeSecretOutput{
  1687. Tags: []types.Tag{},
  1688. },
  1689. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1690. getSecretErr: nil,
  1691. describeSecretErr: nil,
  1692. deleteSecretErr: nil,
  1693. },
  1694. want: want{
  1695. err: nil,
  1696. },
  1697. reason: "",
  1698. },
  1699. "Invalid Recovery Window": {
  1700. args: args{
  1701. client: fakesm.Client{},
  1702. config: esv1.SecretsManager{
  1703. RecoveryWindowInDays: 1,
  1704. },
  1705. getSecretOutput: &awssm.GetSecretValueOutput{},
  1706. describeSecretOutput: &awssm.DescribeSecretOutput{
  1707. Tags: []types.Tag{secretTag},
  1708. },
  1709. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1710. getSecretErr: nil,
  1711. describeSecretErr: nil,
  1712. deleteSecretErr: nil,
  1713. },
  1714. want: want{
  1715. err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
  1716. },
  1717. reason: "",
  1718. },
  1719. "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
  1720. args: args{
  1721. client: fakesm.Client{},
  1722. config: esv1.SecretsManager{
  1723. RecoveryWindowInDays: 7,
  1724. ForceDeleteWithoutRecovery: true,
  1725. },
  1726. getSecretOutput: &awssm.GetSecretValueOutput{},
  1727. describeSecretOutput: &awssm.DescribeSecretOutput{
  1728. Tags: []types.Tag{secretTag},
  1729. },
  1730. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1731. getSecretErr: nil,
  1732. describeSecretErr: nil,
  1733. deleteSecretErr: nil,
  1734. },
  1735. want: want{
  1736. err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
  1737. },
  1738. reason: "",
  1739. },
  1740. "Failed to get Tags": {
  1741. args: args{
  1742. client: fakeClient,
  1743. config: esv1.SecretsManager{},
  1744. getSecretOutput: &awssm.GetSecretValueOutput{},
  1745. describeSecretOutput: nil,
  1746. deleteSecretOutput: nil,
  1747. getSecretErr: nil,
  1748. describeSecretErr: errors.New("failed to get tags"),
  1749. deleteSecretErr: nil,
  1750. },
  1751. want: want{
  1752. err: errors.New("failed to get tags"),
  1753. },
  1754. reason: "",
  1755. },
  1756. "Secret Not Found": {
  1757. args: args{
  1758. client: fakeClient,
  1759. config: esv1.SecretsManager{},
  1760. getSecretOutput: nil,
  1761. describeSecretOutput: nil,
  1762. deleteSecretOutput: nil,
  1763. getSecretErr: errors.New("not here, sorry dude"),
  1764. describeSecretErr: nil,
  1765. deleteSecretErr: nil,
  1766. },
  1767. want: want{
  1768. err: errors.New("not here, sorry dude"),
  1769. },
  1770. },
  1771. "Not expected AWS error": {
  1772. args: args{
  1773. client: fakeClient,
  1774. config: esv1.SecretsManager{},
  1775. getSecretOutput: nil,
  1776. describeSecretOutput: nil,
  1777. deleteSecretOutput: nil,
  1778. getSecretErr: errors.New("aws unavailable"),
  1779. describeSecretErr: nil,
  1780. deleteSecretErr: nil,
  1781. },
  1782. want: want{
  1783. err: errors.New("aws unavailable"),
  1784. },
  1785. },
  1786. "unexpected error": {
  1787. args: args{
  1788. client: fakeClient,
  1789. config: esv1.SecretsManager{},
  1790. getSecretOutput: nil,
  1791. describeSecretOutput: nil,
  1792. deleteSecretOutput: nil,
  1793. getSecretErr: errors.New("timeout"),
  1794. describeSecretErr: nil,
  1795. deleteSecretErr: nil,
  1796. },
  1797. want: want{
  1798. err: errors.New("timeout"),
  1799. },
  1800. },
  1801. "DeleteWithPrefix": {
  1802. args: args{
  1803. client: fakesm.Client{
  1804. GetSecretValueFn: func(ctx context.Context, input *awssm.GetSecretValueInput, opts ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  1805. // Verify that the input secret ID has the prefix applied
  1806. if *input.SecretId != "my-prefix-"+fakeKey {
  1807. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1808. }
  1809. return &awssm.GetSecretValueOutput{}, nil
  1810. },
  1811. DescribeSecretFn: func(ctx context.Context, input *awssm.DescribeSecretInput, opts ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error) {
  1812. // Verify that the input secret ID has the prefix applied
  1813. if *input.SecretId != "my-prefix-"+fakeKey {
  1814. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1815. }
  1816. return &awssm.DescribeSecretOutput{
  1817. Tags: []types.Tag{secretTag},
  1818. }, nil
  1819. },
  1820. DeleteSecretFn: func(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
  1821. return &awssm.DeleteSecretOutput{}, nil
  1822. },
  1823. },
  1824. config: esv1.SecretsManager{},
  1825. prefix: "my-prefix-",
  1826. getSecretOutput: nil,
  1827. describeSecretOutput: nil,
  1828. deleteSecretOutput: nil,
  1829. getSecretErr: nil,
  1830. describeSecretErr: nil,
  1831. deleteSecretErr: nil,
  1832. },
  1833. want: want{
  1834. err: nil,
  1835. },
  1836. reason: "Verifies that the prefix is correctly applied when deleting a secret",
  1837. },
  1838. }
  1839. for name, tc := range tests {
  1840. t.Run(name, func(t *testing.T) {
  1841. ref := fake.PushSecretData{RemoteKey: fakeKey}
  1842. sm := SecretsManager{
  1843. client: &tc.args.client,
  1844. config: &tc.args.config,
  1845. prefix: tc.args.prefix,
  1846. }
  1847. if tc.args.client.GetSecretValueFn == nil {
  1848. tc.args.client.GetSecretValueFn = fakesm.NewGetSecretValueFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  1849. }
  1850. if tc.args.client.DescribeSecretFn == nil {
  1851. tc.args.client.DescribeSecretFn = fakesm.NewDescribeSecretFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  1852. }
  1853. if tc.args.client.DeleteSecretFn == nil {
  1854. tc.args.client.DeleteSecretFn = fakesm.NewDeleteSecretFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  1855. }
  1856. err := sm.DeleteSecret(context.TODO(), ref)
  1857. t.Logf("DeleteSecret error: %v", err)
  1858. // Error nil XOR tc.want.err nil
  1859. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1860. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1861. }
  1862. // if errors are the same type but their contents do not match
  1863. if err != nil && tc.want.err != nil {
  1864. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1865. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1866. }
  1867. }
  1868. })
  1869. }
  1870. }
  1871. func makeValidSecretStore() *esv1.SecretStore {
  1872. return &esv1.SecretStore{
  1873. ObjectMeta: metav1.ObjectMeta{
  1874. Name: "aws-secret-store",
  1875. Namespace: "default",
  1876. },
  1877. Spec: esv1.SecretStoreSpec{
  1878. Provider: &esv1.SecretStoreProvider{
  1879. AWS: &esv1.AWSProvider{
  1880. Service: esv1.AWSServiceSecretsManager,
  1881. Region: "eu-west-2",
  1882. },
  1883. },
  1884. },
  1885. }
  1886. }
  1887. func getTagSlice() []types.Tag {
  1888. tagKey1 := tagname1
  1889. tagValue1 := tagvalue1
  1890. tagKey2 := tagname2
  1891. tagValue2 := tagvalue2
  1892. return []types.Tag{
  1893. {
  1894. Key: &tagKey1,
  1895. Value: &tagValue1,
  1896. },
  1897. {
  1898. Key: &tagKey2,
  1899. Value: &tagValue2,
  1900. },
  1901. }
  1902. }
  1903. func TestSecretsManagerGetAllSecrets(t *testing.T) {
  1904. ctx := context.Background()
  1905. errBoom := errors.New("boom")
  1906. secretName := "my-secret"
  1907. secretVersion := "AWSCURRENT"
  1908. secretPath := "/path/to/secret"
  1909. secretValue := "secret value"
  1910. secretTags := map[string]string{
  1911. "foo": "bar",
  1912. }
  1913. // Test cases
  1914. testCases := []struct {
  1915. name string
  1916. ref esv1.ExternalSecretFind
  1917. secretName string
  1918. secretVersion string
  1919. secretValue string
  1920. batchGetSecretValueFn func(context.Context, *awssm.BatchGetSecretValueInput, ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error)
  1921. listSecretsFn func(context.Context, *awssm.ListSecretsInput, ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error)
  1922. getSecretValueFn func(context.Context, *awssm.GetSecretValueInput, ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error)
  1923. expectedData map[string][]byte
  1924. expectedError string
  1925. }{
  1926. {
  1927. name: "Matching secrets found",
  1928. ref: esv1.ExternalSecretFind{
  1929. Name: &esv1.FindName{
  1930. RegExp: secretName,
  1931. },
  1932. Path: new(secretPath),
  1933. },
  1934. secretName: secretName,
  1935. secretVersion: secretVersion,
  1936. secretValue: secretValue,
  1937. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1938. assert.Len(t, input.Filters, 1)
  1939. assert.Equal(t, "name", string(input.Filters[0].Key))
  1940. assert.Equal(t, secretPath, input.Filters[0].Values[0])
  1941. return &awssm.BatchGetSecretValueOutput{
  1942. SecretValues: []types.SecretValueEntry{
  1943. {
  1944. Name: new(secretName),
  1945. VersionStages: []string{secretVersion},
  1946. SecretBinary: []byte(secretValue),
  1947. },
  1948. },
  1949. }, nil
  1950. },
  1951. expectedData: map[string][]byte{
  1952. secretName: []byte(secretValue),
  1953. },
  1954. expectedError: "",
  1955. },
  1956. {
  1957. name: "Error occurred while fetching secret value",
  1958. ref: esv1.ExternalSecretFind{
  1959. Name: &esv1.FindName{
  1960. RegExp: secretName,
  1961. },
  1962. Path: new(secretPath),
  1963. },
  1964. secretName: secretName,
  1965. secretVersion: secretVersion,
  1966. secretValue: secretValue,
  1967. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1968. return &awssm.BatchGetSecretValueOutput{
  1969. SecretValues: []types.SecretValueEntry{
  1970. {
  1971. Name: new(secretName),
  1972. },
  1973. },
  1974. }, errBoom
  1975. },
  1976. expectedData: nil,
  1977. expectedError: errBoom.Error(),
  1978. },
  1979. {
  1980. name: "regexp: error occurred while listing secrets",
  1981. ref: esv1.ExternalSecretFind{
  1982. Name: &esv1.FindName{
  1983. RegExp: secretName,
  1984. },
  1985. },
  1986. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1987. return nil, errBoom
  1988. },
  1989. expectedData: nil,
  1990. expectedError: errBoom.Error(),
  1991. },
  1992. {
  1993. name: "regep: no matching secrets found",
  1994. ref: esv1.ExternalSecretFind{
  1995. Name: &esv1.FindName{
  1996. RegExp: secretName,
  1997. },
  1998. },
  1999. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  2000. return &awssm.ListSecretsOutput{
  2001. SecretList: []types.SecretListEntry{
  2002. {
  2003. Name: new("other-secret"),
  2004. },
  2005. },
  2006. }, nil
  2007. },
  2008. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  2009. return &awssm.BatchGetSecretValueOutput{
  2010. SecretValues: []types.SecretValueEntry{
  2011. {
  2012. Name: new("other-secret"),
  2013. },
  2014. },
  2015. }, nil
  2016. },
  2017. expectedData: make(map[string][]byte),
  2018. expectedError: "",
  2019. },
  2020. {
  2021. name: "invalid regexp",
  2022. ref: esv1.ExternalSecretFind{
  2023. Name: &esv1.FindName{
  2024. RegExp: "[",
  2025. },
  2026. },
  2027. expectedData: nil,
  2028. expectedError: "could not compile find.name.regexp [[]: error parsing regexp: missing closing ]: `[`",
  2029. },
  2030. {
  2031. name: "tags: Matching secrets found",
  2032. ref: esv1.ExternalSecretFind{
  2033. Tags: secretTags,
  2034. },
  2035. secretName: secretName,
  2036. secretVersion: secretVersion,
  2037. secretValue: secretValue,
  2038. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  2039. assert.Len(t, input.Filters, 2)
  2040. assert.Equal(t, "tag-key", string(input.Filters[0].Key))
  2041. assert.Equal(t, "foo", input.Filters[0].Values[0])
  2042. assert.Equal(t, "tag-value", string(input.Filters[1].Key))
  2043. assert.Equal(t, "bar", input.Filters[1].Values[0])
  2044. return &awssm.BatchGetSecretValueOutput{
  2045. SecretValues: []types.SecretValueEntry{
  2046. {
  2047. Name: new(secretName),
  2048. VersionStages: []string{secretVersion},
  2049. SecretBinary: []byte(secretValue),
  2050. },
  2051. },
  2052. }, nil
  2053. },
  2054. expectedData: map[string][]byte{
  2055. secretName: []byte(secretValue),
  2056. },
  2057. expectedError: "",
  2058. },
  2059. {
  2060. name: "name and tags: matching secrets found",
  2061. ref: esv1.ExternalSecretFind{
  2062. Name: &esv1.FindName{
  2063. RegExp: secretName,
  2064. },
  2065. Tags: secretTags,
  2066. },
  2067. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  2068. allSecrets := []types.SecretListEntry{
  2069. {
  2070. Name: new(secretName),
  2071. Tags: []types.Tag{
  2072. {Key: new("foo"), Value: new("bar")},
  2073. },
  2074. },
  2075. {
  2076. Name: new(fmt.Sprintf("%ssomeothertext", secretName)),
  2077. },
  2078. {
  2079. Name: new("unmatched-secret"),
  2080. Tags: []types.Tag{
  2081. {Key: new("foo"), Value: new("bar")},
  2082. },
  2083. },
  2084. }
  2085. filtered := make([]types.SecretListEntry, 0, len(allSecrets))
  2086. for _, secret := range allSecrets {
  2087. exclude := false
  2088. tagMap := map[string]string{}
  2089. for _, t := range secret.Tags {
  2090. if t.Key != nil && t.Value != nil {
  2091. tagMap[*t.Key] = *t.Value
  2092. }
  2093. }
  2094. for _, f := range input.Filters {
  2095. switch f.Key {
  2096. case types.FilterNameStringTypeName:
  2097. if secret.Name != nil {
  2098. for _, v := range f.Values {
  2099. if strings.Contains(*secret.Name, v) {
  2100. exclude = true
  2101. break
  2102. }
  2103. }
  2104. }
  2105. case types.FilterNameStringTypeTagKey:
  2106. for _, v := range f.Values {
  2107. if tagMap[v] == "" {
  2108. exclude = true
  2109. break
  2110. }
  2111. }
  2112. case types.FilterNameStringTypeDescription,
  2113. types.FilterNameStringTypeTagValue,
  2114. types.FilterNameStringTypePrimaryRegion,
  2115. types.FilterNameStringTypeOwningService,
  2116. types.FilterNameStringTypeAll:
  2117. continue
  2118. }
  2119. }
  2120. if !exclude {
  2121. filtered = append(filtered, secret)
  2122. }
  2123. }
  2124. return &awssm.ListSecretsOutput{SecretList: filtered}, nil
  2125. },
  2126. getSecretValueFn: func(_ context.Context, input *awssm.GetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  2127. if *input.SecretId == secretName {
  2128. return &awssm.GetSecretValueOutput{
  2129. Name: new(secretName),
  2130. VersionStages: []string{secretVersion},
  2131. SecretBinary: []byte(secretValue),
  2132. }, nil
  2133. }
  2134. if *input.SecretId == "unmatched-secret" {
  2135. return &awssm.GetSecretValueOutput{
  2136. Name: new("unmatched-secret"),
  2137. VersionStages: []string{secretVersion},
  2138. SecretBinary: []byte("othervalue"),
  2139. }, nil
  2140. }
  2141. return &awssm.GetSecretValueOutput{
  2142. Name: new(fmt.Sprintf("%ssomeothertext", secretName)),
  2143. VersionStages: []string{secretVersion},
  2144. SecretBinary: []byte("someothervalue"),
  2145. }, nil
  2146. },
  2147. expectedData: map[string][]byte{
  2148. secretName: []byte(secretValue),
  2149. },
  2150. expectedError: "",
  2151. },
  2152. {
  2153. name: "tags: error occurred while fetching secret value",
  2154. ref: esv1.ExternalSecretFind{
  2155. Tags: secretTags,
  2156. },
  2157. secretName: secretName,
  2158. secretVersion: secretVersion,
  2159. secretValue: secretValue,
  2160. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  2161. return &awssm.BatchGetSecretValueOutput{
  2162. SecretValues: []types.SecretValueEntry{
  2163. {
  2164. Name: new(secretName),
  2165. VersionStages: []string{secretVersion},
  2166. SecretBinary: []byte(secretValue),
  2167. },
  2168. },
  2169. }, errBoom
  2170. },
  2171. expectedData: nil,
  2172. expectedError: errBoom.Error(),
  2173. },
  2174. {
  2175. name: "tags: error occurred while listing secrets",
  2176. ref: esv1.ExternalSecretFind{
  2177. Tags: secretTags,
  2178. },
  2179. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  2180. return nil, errBoom
  2181. },
  2182. expectedData: nil,
  2183. expectedError: errBoom.Error(),
  2184. },
  2185. }
  2186. for _, tc := range testCases {
  2187. t.Run(tc.name, func(t *testing.T) {
  2188. fc := fakesm.NewClient()
  2189. fc.BatchGetSecretValueFn = tc.batchGetSecretValueFn
  2190. fc.ListSecretsFn = tc.listSecretsFn
  2191. fc.GetSecretValueFn = tc.getSecretValueFn
  2192. sm := SecretsManager{
  2193. client: fc,
  2194. cache: make(map[string]*awssm.GetSecretValueOutput),
  2195. }
  2196. data, err := sm.GetAllSecrets(ctx, tc.ref)
  2197. if err != nil && err.Error() != tc.expectedError {
  2198. t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
  2199. }
  2200. if !reflect.DeepEqual(data, tc.expectedData) {
  2201. t.Errorf("unexpected data: got %v, want %v", data, tc.expectedData)
  2202. }
  2203. })
  2204. }
  2205. }
  2206. func TestSecretsManagerValidate(t *testing.T) {
  2207. type fields struct {
  2208. cfg *aws.Config
  2209. referentAuth bool
  2210. }
  2211. validConfig := &aws.Config{
  2212. Credentials: credentials.NewStaticCredentialsProvider(
  2213. "fake",
  2214. "fake",
  2215. "fake",
  2216. ),
  2217. }
  2218. invalidConfig := &aws.Config{
  2219. Credentials: &FakeCredProvider{
  2220. retrieveFunc: func() (aws.Credentials, error) {
  2221. return aws.Credentials{}, errors.New("invalid credentials")
  2222. },
  2223. },
  2224. }
  2225. tests := []struct {
  2226. name string
  2227. fields fields
  2228. want esv1.ValidationResult
  2229. wantErr bool
  2230. }{
  2231. {
  2232. name: "ReferentAuth should always return unknown",
  2233. fields: fields{
  2234. referentAuth: true,
  2235. },
  2236. want: esv1.ValidationResultUnknown,
  2237. },
  2238. {
  2239. name: "Valid credentials should return ready",
  2240. fields: fields{
  2241. cfg: validConfig,
  2242. },
  2243. want: esv1.ValidationResultReady,
  2244. },
  2245. {
  2246. name: "Invalid credentials should return error",
  2247. fields: fields{
  2248. cfg: invalidConfig,
  2249. },
  2250. want: esv1.ValidationResultError,
  2251. wantErr: true,
  2252. },
  2253. }
  2254. for _, tt := range tests {
  2255. t.Run(tt.name, func(t *testing.T) {
  2256. sm := &SecretsManager{
  2257. cfg: tt.fields.cfg,
  2258. referentAuth: tt.fields.referentAuth,
  2259. }
  2260. got, err := sm.Validate()
  2261. if (err != nil) != tt.wantErr {
  2262. t.Errorf("SecretsManager.Validate() error = %v, wantErr %v", err, tt.wantErr)
  2263. return
  2264. }
  2265. if !reflect.DeepEqual(got, tt.want) {
  2266. t.Errorf("SecretsManager.Validate() = %v, want %v", got, tt.want)
  2267. }
  2268. })
  2269. }
  2270. }
  2271. func TestSecretExists(t *testing.T) {
  2272. arn := testARN
  2273. defaultVersion := testDefaultVersion
  2274. secretValueOutput := &awssm.GetSecretValueOutput{
  2275. ARN: &arn,
  2276. VersionId: &defaultVersion,
  2277. }
  2278. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  2279. getSecretCorrectErr := types.ResourceNotFoundException{}
  2280. getSecretWrongErr := types.InvalidRequestException{}
  2281. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: fakeSecretKey, RemoteKey: fakeKey, Property: ""}
  2282. type args struct {
  2283. store *esv1.AWSProvider
  2284. client fakesm.Client
  2285. pushSecretData fake.PushSecretData
  2286. }
  2287. type want struct {
  2288. err error
  2289. wantError bool
  2290. }
  2291. tests := map[string]struct {
  2292. args args
  2293. want want
  2294. }{
  2295. "SecretExistsReturnsTrueForExistingSecret": {
  2296. args: args{
  2297. store: makeValidSecretStore().Spec.Provider.AWS,
  2298. client: fakesm.Client{
  2299. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  2300. },
  2301. pushSecretData: pushSecretDataWithoutProperty,
  2302. },
  2303. want: want{
  2304. err: nil,
  2305. wantError: true,
  2306. },
  2307. },
  2308. "SecretExistsReturnsFalseForNonExistingSecret": {
  2309. args: args{
  2310. store: makeValidSecretStore().Spec.Provider.AWS,
  2311. client: fakesm.Client{
  2312. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  2313. },
  2314. pushSecretData: pushSecretDataWithoutProperty,
  2315. },
  2316. want: want{
  2317. err: nil,
  2318. wantError: false,
  2319. },
  2320. },
  2321. "SecretExistsReturnsFalseForErroredSecret": {
  2322. args: args{
  2323. store: makeValidSecretStore().Spec.Provider.AWS,
  2324. client: fakesm.Client{
  2325. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretWrongErr),
  2326. },
  2327. pushSecretData: pushSecretDataWithoutProperty,
  2328. },
  2329. want: want{
  2330. err: &getSecretWrongErr,
  2331. wantError: false,
  2332. },
  2333. },
  2334. }
  2335. for name, tc := range tests {
  2336. t.Run(name, func(t *testing.T) {
  2337. sm := &SecretsManager{
  2338. client: &tc.args.client,
  2339. }
  2340. got, err := sm.SecretExists(context.Background(), tc.args.pushSecretData)
  2341. assert.Equal(
  2342. t,
  2343. tc.want,
  2344. want{
  2345. err: err,
  2346. wantError: got,
  2347. })
  2348. })
  2349. }
  2350. }
  2351. func TestConstructMetadataWithDefaults(t *testing.T) {
  2352. tests := []struct {
  2353. name string
  2354. input *apiextensionsv1.JSON
  2355. expected *metadata.PushSecretMetadata[PushSecretMetadataSpec]
  2356. expectError bool
  2357. }{
  2358. {
  2359. name: "Valid metadata with multiple fields",
  2360. input: &apiextensionsv1.JSON{Raw: []byte(`{
  2361. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2362. "kind": "PushSecretMetadata",
  2363. "spec": {
  2364. "description": "test description",
  2365. "secretPushFormat":"string",
  2366. "kmsKeyID": "custom-kms-key",
  2367. "tags": {
  2368. "customKey": "customValue"
  2369. },
  2370. }
  2371. }`)},
  2372. expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
  2373. APIVersion: "kubernetes.external-secrets.io/v1alpha1",
  2374. Kind: "PushSecretMetadata",
  2375. Spec: PushSecretMetadataSpec{
  2376. Description: "test description",
  2377. SecretPushFormat: "string",
  2378. KMSKeyID: "custom-kms-key",
  2379. Tags: map[string]string{
  2380. "customKey": "customValue",
  2381. managedBy: externalSecrets,
  2382. },
  2383. },
  2384. },
  2385. },
  2386. {
  2387. name: "Empty metadata, defaults applied",
  2388. input: nil,
  2389. expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
  2390. Spec: PushSecretMetadataSpec{
  2391. Description: fmt.Sprintf("secret '%s:%s'", managedBy, externalSecrets),
  2392. SecretPushFormat: "binary",
  2393. KMSKeyID: "alias/aws/secretsmanager",
  2394. Tags: map[string]string{
  2395. managedBy: externalSecrets,
  2396. },
  2397. },
  2398. },
  2399. },
  2400. {
  2401. name: "Added default metadata with 'managed-by' tag",
  2402. input: &apiextensionsv1.JSON{Raw: []byte(`{
  2403. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2404. "kind": "PushSecretMetadata",
  2405. "spec": {
  2406. "tags": {
  2407. "managed-by": "external-secrets",
  2408. "customKey": "customValue"
  2409. },
  2410. }
  2411. }`)},
  2412. expected: nil,
  2413. expectError: true,
  2414. },
  2415. {
  2416. name: "Invalid metadata format",
  2417. input: &apiextensionsv1.JSON{Raw: []byte(`invalid-json`)},
  2418. expected: nil,
  2419. expectError: true,
  2420. },
  2421. {
  2422. name: "Metadata with 'managed-by' tag specified",
  2423. input: &apiextensionsv1.JSON{Raw: []byte(`{"tags":{"managed-by":"invalid"}}`)},
  2424. expected: nil,
  2425. expectError: true,
  2426. },
  2427. }
  2428. for _, tt := range tests {
  2429. t.Run(tt.name, func(t *testing.T) {
  2430. result, err := (&SecretsManager{}).constructMetadataWithDefaults(tt.input)
  2431. if tt.expectError {
  2432. assert.Error(t, err)
  2433. } else {
  2434. assert.NoError(t, err)
  2435. assert.Equal(t, tt.expected, result)
  2436. }
  2437. })
  2438. }
  2439. }
  2440. func TestComputeTagsToUpdate(t *testing.T) {
  2441. tests := []struct {
  2442. name string
  2443. tags map[string]string
  2444. metaTags map[string]string
  2445. expected []types.Tag
  2446. modified bool
  2447. }{
  2448. {
  2449. name: "No tags to update",
  2450. tags: map[string]string{
  2451. "key1": "value1",
  2452. "key2": "value2",
  2453. },
  2454. metaTags: map[string]string{
  2455. "key1": "value1",
  2456. "key2": "value2",
  2457. },
  2458. expected: []types.Tag{
  2459. {Key: new("key1"), Value: new("value1")},
  2460. {Key: new("key2"), Value: new("value2")},
  2461. },
  2462. modified: false,
  2463. },
  2464. {
  2465. name: "No tags to update as managed-by tag is ignored",
  2466. tags: map[string]string{
  2467. "key1": "value1",
  2468. "key2": "value2",
  2469. },
  2470. metaTags: map[string]string{
  2471. "key1": "value1",
  2472. "key2": "value2",
  2473. managedBy: externalSecrets,
  2474. },
  2475. expected: []types.Tag{
  2476. {Key: new("key1"), Value: new("value1")},
  2477. {Key: new("key2"), Value: new("value2")},
  2478. {Key: new(managedBy), Value: new(externalSecrets)},
  2479. },
  2480. modified: false,
  2481. },
  2482. {
  2483. name: "Add new tag",
  2484. tags: map[string]string{
  2485. "key1": "value1",
  2486. },
  2487. metaTags: map[string]string{
  2488. "key1": "value1",
  2489. "key2": "value2",
  2490. },
  2491. expected: []types.Tag{
  2492. {Key: new("key1"), Value: new("value1")},
  2493. {Key: new("key2"), Value: new("value2")},
  2494. },
  2495. modified: true,
  2496. },
  2497. {
  2498. name: "Update existing tag value",
  2499. tags: map[string]string{
  2500. "key1": "value1",
  2501. },
  2502. metaTags: map[string]string{
  2503. "key1": "newValue",
  2504. },
  2505. expected: []types.Tag{
  2506. {Key: new("key1"), Value: new("newValue")},
  2507. },
  2508. modified: true,
  2509. },
  2510. {
  2511. name: "Empty tags and metaTags",
  2512. tags: map[string]string{},
  2513. metaTags: map[string]string{},
  2514. expected: []types.Tag{},
  2515. modified: false,
  2516. },
  2517. {
  2518. name: "Empty tags with non-empty metaTags",
  2519. tags: map[string]string{},
  2520. metaTags: map[string]string{
  2521. "key1": "value1",
  2522. },
  2523. expected: []types.Tag{
  2524. {Key: new("key1"), Value: new("value1")},
  2525. },
  2526. modified: true,
  2527. },
  2528. }
  2529. for _, tt := range tests {
  2530. t.Run(tt.name, func(t *testing.T) {
  2531. result, modified := computeTagsToUpdate(tt.tags, tt.metaTags)
  2532. assert.ElementsMatch(t, tt.expected, result)
  2533. assert.Equal(t, tt.modified, modified)
  2534. })
  2535. }
  2536. }
  2537. func TestPatchTags(t *testing.T) {
  2538. type call struct {
  2539. untagCalled bool
  2540. tagCalled bool
  2541. }
  2542. tests := []struct {
  2543. name string
  2544. existingTags map[string]string
  2545. metaTags map[string]string
  2546. expectUntag bool
  2547. expectTag bool
  2548. assertsTag func(input *awssm.TagResourceInput)
  2549. assertsUntag func(input *awssm.UntagResourceInput)
  2550. }{
  2551. {
  2552. name: "no changes",
  2553. existingTags: map[string]string{"a": "1"},
  2554. metaTags: map[string]string{"a": "1"},
  2555. expectUntag: false,
  2556. expectTag: false,
  2557. assertsTag: func(input *awssm.TagResourceInput) {
  2558. assert.Fail(t, "Expected TagResource to not be called")
  2559. },
  2560. assertsUntag: func(input *awssm.UntagResourceInput) {
  2561. assert.Fail(t, "Expected UntagResource to not be called")
  2562. },
  2563. },
  2564. {
  2565. name: "update tag value",
  2566. existingTags: map[string]string{"a": "1"},
  2567. metaTags: map[string]string{"a": "2"},
  2568. expectUntag: false,
  2569. expectTag: true,
  2570. assertsTag: func(input *awssm.TagResourceInput) {
  2571. assert.Contains(t, input.Tags, types.Tag{Key: new(managedBy), Value: new(externalSecrets)})
  2572. assert.Contains(t, input.Tags, types.Tag{Key: new("a"), Value: new("2")})
  2573. },
  2574. assertsUntag: func(input *awssm.UntagResourceInput) {
  2575. assert.Fail(t, "Expected UntagResource to not be called")
  2576. },
  2577. },
  2578. {
  2579. name: "remove tag",
  2580. existingTags: map[string]string{"a": "1", "b": "2"},
  2581. metaTags: map[string]string{"a": "1"},
  2582. expectUntag: true,
  2583. expectTag: false,
  2584. assertsTag: func(input *awssm.TagResourceInput) {
  2585. assert.Fail(t, "Expected TagResource to not be called")
  2586. },
  2587. assertsUntag: func(input *awssm.UntagResourceInput) {
  2588. assert.Equal(t, []string{"b"}, input.TagKeys)
  2589. },
  2590. },
  2591. {
  2592. name: "add tags",
  2593. existingTags: map[string]string{"a": "1"},
  2594. metaTags: map[string]string{"a": "1", "b": "2"},
  2595. expectUntag: false,
  2596. expectTag: true,
  2597. assertsTag: func(input *awssm.TagResourceInput) {
  2598. assert.Contains(t, input.Tags, types.Tag{Key: new(managedBy), Value: new(externalSecrets)})
  2599. assert.Contains(t, input.Tags, types.Tag{Key: new("a"), Value: new("1")})
  2600. assert.Contains(t, input.Tags, types.Tag{Key: new("b"), Value: new("2")})
  2601. },
  2602. assertsUntag: func(input *awssm.UntagResourceInput) {
  2603. assert.Fail(t, "Expected UntagResource to not be called")
  2604. },
  2605. },
  2606. }
  2607. for _, tt := range tests {
  2608. t.Run(tt.name, func(t *testing.T) {
  2609. calls := call{}
  2610. fakeClient := &fakesm.Client{
  2611. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  2612. tt.assertsTag(input)
  2613. calls.tagCalled = true
  2614. }),
  2615. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
  2616. tt.assertsUntag(input)
  2617. calls.untagCalled = true
  2618. }),
  2619. }
  2620. sm := &SecretsManager{client: fakeClient}
  2621. metaMap := map[string]any{
  2622. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2623. "kind": "PushSecretMetadata",
  2624. "spec": map[string]any{
  2625. "description": "adding managed-by tag explicitly",
  2626. "tags": tt.metaTags,
  2627. },
  2628. }
  2629. raw, err := json.Marshal(metaMap)
  2630. require.NoError(t, err)
  2631. meta := &apiextensionsv1.JSON{Raw: raw}
  2632. secretId := "secret"
  2633. err = sm.patchTags(context.Background(), meta, &secretId, tt.existingTags)
  2634. require.NoError(t, err)
  2635. assert.Equal(t, tt.expectUntag, calls.untagCalled)
  2636. assert.Equal(t, tt.expectTag, calls.tagCalled)
  2637. })
  2638. }
  2639. }
  2640. // FakeCredProvider implements the AWS credentials.Provider interface
  2641. // It is used to inject an error into the AWS config to cause a
  2642. // validation error.
  2643. type FakeCredProvider struct {
  2644. retrieveFunc func() (aws.Credentials, error)
  2645. }
  2646. func (f *FakeCredProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
  2647. return f.retrieveFunc()
  2648. }
  2649. func (f *FakeCredProvider) IsExpired() bool {
  2650. return true
  2651. }