secretsmanager_test.go 64 KB

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