secretsmanager_test.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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. "errors"
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/aws/aws-sdk-go/aws"
  22. "github.com/aws/aws-sdk-go/aws/awserr"
  23. "github.com/aws/aws-sdk-go/aws/credentials"
  24. "github.com/aws/aws-sdk-go/aws/request"
  25. "github.com/aws/aws-sdk-go/aws/session"
  26. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  27. "github.com/google/go-cmp/cmp"
  28. "github.com/stretchr/testify/assert"
  29. corev1 "k8s.io/api/core/v1"
  30. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  31. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  32. "k8s.io/utils/ptr"
  33. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  34. fakesm "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager/fake"
  35. "github.com/external-secrets/external-secrets/pkg/provider/aws/util"
  36. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  37. )
  38. type secretsManagerTestCase struct {
  39. fakeClient *fakesm.Client
  40. apiInput *awssm.GetSecretValueInput
  41. apiOutput *awssm.GetSecretValueOutput
  42. remoteRef *esv1beta1.ExternalSecretDataRemoteRef
  43. apiErr error
  44. expectError string
  45. expectedSecret string
  46. // for testing secretmap
  47. expectedData map[string][]byte
  48. // for testing caching
  49. expectedCounter *int
  50. prefix string
  51. }
  52. const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
  53. const (
  54. tagname1 = "tagname1"
  55. tagvalue1 = "tagvalue1"
  56. tagname2 = "tagname2"
  57. tagvalue2 = "tagvalue2"
  58. fakeKey = "fake-key"
  59. )
  60. func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
  61. smtc := secretsManagerTestCase{
  62. fakeClient: fakesm.NewClient(),
  63. apiInput: makeValidAPIInput(),
  64. remoteRef: makeValidRemoteRef(),
  65. apiOutput: makeValidAPIOutput(),
  66. apiErr: nil,
  67. expectError: "",
  68. expectedSecret: "",
  69. expectedData: map[string][]byte{},
  70. }
  71. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  72. return &smtc
  73. }
  74. func makeValidRemoteRef() *esv1beta1.ExternalSecretDataRemoteRef {
  75. return &esv1beta1.ExternalSecretDataRemoteRef{
  76. Key: "/baz",
  77. Version: "AWSCURRENT",
  78. }
  79. }
  80. func makeValidAPIInput() *awssm.GetSecretValueInput {
  81. return &awssm.GetSecretValueInput{
  82. SecretId: aws.String("/baz"),
  83. VersionStage: aws.String("AWSCURRENT"),
  84. }
  85. }
  86. func makeValidAPIOutput() *awssm.GetSecretValueOutput {
  87. return &awssm.GetSecretValueOutput{
  88. SecretString: aws.String(""),
  89. }
  90. }
  91. func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
  92. smtc := makeValidSecretsManagerTestCase()
  93. for _, fn := range tweaks {
  94. fn(smtc)
  95. }
  96. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  97. return smtc
  98. }
  99. // This case can be shared by both GetSecret and GetSecretMap tests.
  100. // bad case: set apiErr.
  101. var setAPIErr = func(smtc *secretsManagerTestCase) {
  102. smtc.apiErr = errors.New("oh no")
  103. smtc.expectError = "oh no"
  104. }
  105. // test the sm<->aws interface
  106. // make sure correct values are passed and errors are handled accordingly.
  107. func TestSecretsManagerGetSecret(t *testing.T) {
  108. // good case: default version is set
  109. // key is passed in, output is sent back
  110. setSecretString := func(smtc *secretsManagerTestCase) {
  111. smtc.apiOutput.SecretString = aws.String("testtesttest")
  112. smtc.expectedSecret = "testtesttest"
  113. }
  114. // good case: key is passed in with prefix
  115. setSecretStringWithPrefix := func(smtc *secretsManagerTestCase) {
  116. smtc.remoteRef.Key = "secret-key"
  117. smtc.apiInput = &awssm.GetSecretValueInput{
  118. SecretId: aws.String("my-prefix/secret-key"),
  119. VersionStage: aws.String("AWSCURRENT"),
  120. }
  121. smtc.prefix = "my-prefix/"
  122. }
  123. // good case: extract property
  124. // Testing that the property exists in the SecretString
  125. setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
  126. smtc.remoteRef.Property = "/shmoo"
  127. smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
  128. smtc.expectedSecret = "bang"
  129. }
  130. // bad case: missing property
  131. setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
  132. smtc.remoteRef.Property = "INVALPROP"
  133. smtc.expectError = "key INVALPROP does not exist in secret"
  134. }
  135. // bad case: extract property failure due to invalid json
  136. setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
  137. smtc.remoteRef.Property = "INVALPROP"
  138. smtc.apiOutput.SecretString = aws.String(`------`)
  139. smtc.expectError = "key INVALPROP does not exist in secret"
  140. }
  141. // good case: set .SecretString to nil but set binary with value
  142. setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
  143. smtc.apiOutput.SecretBinary = []byte("yesplease")
  144. // needs to be set as nil, empty quotes ("") is considered existing
  145. smtc.apiOutput.SecretString = nil
  146. smtc.expectedSecret = "yesplease"
  147. }
  148. // bad case: both .SecretString and .SecretBinary are nil
  149. setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
  150. smtc.apiOutput.SecretBinary = nil
  151. smtc.apiOutput.SecretString = nil
  152. smtc.expectError = "no secret string nor binary for key"
  153. }
  154. // good case: secretOut.SecretBinary JSON parsing
  155. setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
  156. smtc.apiOutput.SecretString = nil
  157. smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
  158. smtc.remoteRef.Property = "foobar.baz"
  159. smtc.expectedSecret = "nestedval"
  160. }
  161. // good case: secretOut.SecretBinary no JSON parsing if name on key
  162. setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
  163. smtc.apiOutput.SecretString = nil
  164. smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
  165. smtc.remoteRef.Property = "foobar.baz"
  166. smtc.expectedSecret = "nestedval"
  167. }
  168. // good case: custom version stage set
  169. setCustomVersionStage := func(smtc *secretsManagerTestCase) {
  170. smtc.apiInput.VersionStage = aws.String("1234")
  171. smtc.remoteRef.Version = "1234"
  172. smtc.apiOutput.SecretString = aws.String("FOOBA!")
  173. smtc.expectedSecret = "FOOBA!"
  174. }
  175. // good case: custom version id set
  176. setCustomVersionID := func(smtc *secretsManagerTestCase) {
  177. smtc.apiInput.VersionStage = nil
  178. smtc.apiInput.VersionId = aws.String("1234-5678")
  179. smtc.remoteRef.Version = "uuid/1234-5678"
  180. smtc.apiOutput.SecretString = aws.String("myvalue")
  181. smtc.expectedSecret = "myvalue"
  182. }
  183. fetchMetadata := func(smtc *secretsManagerTestCase) {
  184. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  185. describeSecretOutput := &awssm.DescribeSecretOutput{
  186. Tags: getTagSlice(),
  187. }
  188. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  189. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  190. smtc.apiOutput.SecretString = &jsonTags
  191. smtc.expectedSecret = jsonTags
  192. }
  193. fetchMetadataProperty := func(smtc *secretsManagerTestCase) {
  194. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  195. describeSecretOutput := &awssm.DescribeSecretOutput{
  196. Tags: getTagSlice(),
  197. }
  198. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  199. smtc.remoteRef.Property = tagname2
  200. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  201. smtc.apiOutput.SecretString = &jsonTags
  202. smtc.expectedSecret = tagvalue2
  203. }
  204. failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
  205. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  206. describeSecretOutput := &awssm.DescribeSecretOutput{
  207. Tags: getTagSlice(),
  208. }
  209. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  210. smtc.remoteRef.Property = "fail"
  211. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  212. smtc.apiOutput.SecretString = &jsonTags
  213. smtc.expectError = "key fail does not exist in secret /baz"
  214. }
  215. successCases := []*secretsManagerTestCase{
  216. makeValidSecretsManagerTestCase(),
  217. makeValidSecretsManagerTestCaseCustom(setSecretString),
  218. makeValidSecretsManagerTestCaseCustom(setSecretStringWithPrefix),
  219. makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
  220. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
  221. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
  222. makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
  223. makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
  224. makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
  225. makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
  226. makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
  227. makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
  228. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  229. makeValidSecretsManagerTestCaseCustom(fetchMetadata),
  230. makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
  231. makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
  232. }
  233. for k, v := range successCases {
  234. sm := SecretsManager{
  235. cache: make(map[string]*awssm.GetSecretValueOutput),
  236. client: v.fakeClient,
  237. prefix: v.prefix,
  238. }
  239. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  240. if !ErrorContains(err, v.expectError) {
  241. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  242. }
  243. if err == nil && string(out) != v.expectedSecret {
  244. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  245. }
  246. }
  247. }
  248. func TestCaching(t *testing.T) {
  249. fakeClient := fakesm.NewClient()
  250. // good case: first call, since we are using the same key, results should be cached and the counter should not go
  251. // over 1
  252. firstCall := func(smtc *secretsManagerTestCase) {
  253. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  254. smtc.remoteRef.Property = "foo"
  255. smtc.expectedSecret = "bar"
  256. smtc.expectedCounter = aws.Int(1)
  257. smtc.fakeClient = fakeClient
  258. }
  259. secondCall := func(smtc *secretsManagerTestCase) {
  260. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  261. smtc.remoteRef.Property = "bar"
  262. smtc.expectedSecret = "vodka"
  263. smtc.expectedCounter = aws.Int(1)
  264. smtc.fakeClient = fakeClient
  265. }
  266. notCachedCall := func(smtc *secretsManagerTestCase) {
  267. smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
  268. smtc.remoteRef.Property = "sheldon"
  269. smtc.expectedSecret = "bazinga"
  270. smtc.expectedCounter = aws.Int(2)
  271. smtc.fakeClient = fakeClient
  272. smtc.apiInput.SecretId = aws.String("xyz")
  273. smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
  274. }
  275. cachedCases := []*secretsManagerTestCase{
  276. makeValidSecretsManagerTestCaseCustom(firstCall),
  277. makeValidSecretsManagerTestCaseCustom(firstCall),
  278. makeValidSecretsManagerTestCaseCustom(secondCall),
  279. makeValidSecretsManagerTestCaseCustom(notCachedCall),
  280. }
  281. sm := SecretsManager{
  282. cache: make(map[string]*awssm.GetSecretValueOutput),
  283. }
  284. for k, v := range cachedCases {
  285. sm.client = v.fakeClient
  286. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  287. if !ErrorContains(err, v.expectError) {
  288. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  289. }
  290. if err == nil && string(out) != v.expectedSecret {
  291. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  292. }
  293. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  294. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  295. }
  296. }
  297. }
  298. func TestGetSecretMap(t *testing.T) {
  299. // good case: default version & deserialization
  300. setDeserialization := func(smtc *secretsManagerTestCase) {
  301. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
  302. smtc.expectedData["foo"] = []byte("bar")
  303. }
  304. // good case: nested json
  305. setNestedJSON := func(smtc *secretsManagerTestCase) {
  306. smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
  307. smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
  308. }
  309. // good case: caching
  310. cachedMap := func(smtc *secretsManagerTestCase) {
  311. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
  312. smtc.expectedData["foo"] = []byte("bar")
  313. smtc.expectedData["plus"] = []byte("one")
  314. smtc.expectedCounter = aws.Int(1)
  315. }
  316. // bad case: invalid json
  317. setInvalidJSON := func(smtc *secretsManagerTestCase) {
  318. smtc.apiOutput.SecretString = aws.String(`-----------------`)
  319. smtc.expectError = "unable to unmarshal secret"
  320. }
  321. successCases := []*secretsManagerTestCase{
  322. makeValidSecretsManagerTestCaseCustom(setDeserialization),
  323. makeValidSecretsManagerTestCaseCustom(setNestedJSON),
  324. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  325. makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
  326. makeValidSecretsManagerTestCaseCustom(cachedMap),
  327. }
  328. for k, v := range successCases {
  329. sm := SecretsManager{
  330. cache: make(map[string]*awssm.GetSecretValueOutput),
  331. client: v.fakeClient,
  332. }
  333. out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
  334. if !ErrorContains(err, v.expectError) {
  335. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  336. }
  337. if err == nil && !cmp.Equal(out, v.expectedData) {
  338. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  339. }
  340. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  341. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  342. }
  343. }
  344. }
  345. func ErrorContains(out error, want string) bool {
  346. if out == nil {
  347. return want == ""
  348. }
  349. if want == "" {
  350. return false
  351. }
  352. return strings.Contains(out.Error(), want)
  353. }
  354. func TestSetSecret(t *testing.T) {
  355. managedBy := managedBy
  356. notManagedBy := "not-managed-by"
  357. secretKey := "fake-secret-key"
  358. secretValue := []byte("fake-value")
  359. fakeSecret := &corev1.Secret{
  360. Data: map[string][]byte{
  361. secretKey: secretValue,
  362. },
  363. }
  364. externalSecrets := externalSecrets
  365. noPermission := errors.New("no permission")
  366. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  367. getSecretCorrectErr := awssm.ResourceNotFoundException{}
  368. getSecretWrongErr := awssm.InvalidRequestException{}
  369. secretOutput := &awssm.CreateSecretOutput{
  370. ARN: &arn,
  371. }
  372. externalSecretsTag := []*awssm.Tag{
  373. {
  374. Key: &managedBy,
  375. Value: &externalSecrets,
  376. },
  377. {
  378. Key: ptr.To("taname1"),
  379. Value: ptr.To("tagvalue1"),
  380. },
  381. }
  382. externalSecretsTagFaulty := []*awssm.Tag{
  383. {
  384. Key: &notManagedBy,
  385. Value: &externalSecrets,
  386. },
  387. }
  388. tagSecretOutput := &awssm.DescribeSecretOutput{
  389. ARN: &arn,
  390. Tags: externalSecretsTag,
  391. }
  392. tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
  393. ARN: &arn,
  394. Tags: externalSecretsTagFaulty,
  395. }
  396. initialVersion := "00000000-0000-0000-0000-000000000001"
  397. defaultVersion := "00000000-0000-0000-0000-000000000002"
  398. defaultUpdatedVersion := "00000000-0000-0000-0000-000000000003"
  399. randomUUIDVersion := "c2812e8d-84ce-4858-abec-7163d8ab312b"
  400. randomUUIDVersionIncremented := "c2812e8d-84ce-4858-abec-7163d8ab312c"
  401. unparsableVersion := "IAM UNPARSABLE"
  402. secretValueOutput := &awssm.GetSecretValueOutput{
  403. ARN: &arn,
  404. VersionId: &defaultVersion,
  405. }
  406. secretValueOutput2 := &awssm.GetSecretValueOutput{
  407. ARN: &arn,
  408. SecretBinary: secretValue,
  409. VersionId: &defaultVersion,
  410. }
  411. type params struct {
  412. s string
  413. b []byte
  414. version *string
  415. }
  416. secretValueOutputFrom := func(params params) *awssm.GetSecretValueOutput {
  417. var version *string
  418. if params.version == nil {
  419. version = &defaultVersion
  420. } else {
  421. version = params.version
  422. }
  423. return &awssm.GetSecretValueOutput{
  424. ARN: &arn,
  425. SecretString: &params.s,
  426. SecretBinary: params.b,
  427. VersionId: version,
  428. }
  429. }
  430. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  431. putSecretOutput := &awssm.PutSecretValueOutput{
  432. ARN: &arn,
  433. }
  434. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: ""}
  435. pushSecretDataWithoutSecretKey := fake.PushSecretData{RemoteKey: fakeKey, Property: ""}
  436. pushSecretDataWithMetadata := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  437. Raw: []byte(`{
  438. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  439. "kind": "PushSecretMetadata",
  440. "spec": {
  441. "secretPushFormat": "string"
  442. }
  443. }`)}}
  444. pushSecretDataWithProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "other-fake-property"}
  445. type args struct {
  446. store *esv1beta1.AWSProvider
  447. client fakesm.Client
  448. pushSecretData fake.PushSecretData
  449. }
  450. type want struct {
  451. err error
  452. }
  453. tests := map[string]struct {
  454. reason string
  455. args args
  456. want want
  457. }{
  458. "SetSecretSucceedsWithExistingSecret": {
  459. reason: "a secret can be pushed to aws secrets manager when it already exists",
  460. args: args{
  461. store: makeValidSecretStore().Spec.Provider.AWS,
  462. client: fakesm.Client{
  463. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  464. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  465. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  466. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  467. },
  468. pushSecretData: pushSecretDataWithoutProperty,
  469. },
  470. want: want{
  471. err: nil,
  472. },
  473. },
  474. "SetSecretSucceedsWithoutSecretKey": {
  475. reason: "a secret can be pushed to aws secrets manager without secret key",
  476. args: args{
  477. store: makeValidSecretStore().Spec.Provider.AWS,
  478. client: fakesm.Client{
  479. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  480. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  481. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  482. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  483. },
  484. pushSecretData: pushSecretDataWithoutSecretKey,
  485. },
  486. want: want{
  487. err: nil,
  488. },
  489. },
  490. "SetSecretSucceedsWithExistingSecretAndStringFormat": {
  491. reason: "a secret can be pushed to aws secrets manager when it already exists",
  492. args: args{
  493. store: makeValidSecretStore().Spec.Provider.AWS,
  494. client: fakesm.Client{
  495. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  496. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  497. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  498. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  499. },
  500. pushSecretData: pushSecretDataWithMetadata,
  501. },
  502. want: want{
  503. err: nil,
  504. },
  505. },
  506. "SetSecretSucceedsWithExistingSecretAndKMSKeyAndDescription": {
  507. reason: "a secret can be pushed to aws secrets manager when it already exists",
  508. args: args{
  509. store: makeValidSecretStore().Spec.Provider.AWS,
  510. client: fakesm.Client{
  511. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, &getSecretCorrectErr),
  512. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  513. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  514. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  515. },
  516. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  517. Raw: []byte(`{
  518. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  519. "kind": "PushSecretMetadata",
  520. "spec": {
  521. "kmsKeyID": "bb123123-b2b0-4f60-ac3a-44a13f0e6b6c",
  522. "description": "this is a description"
  523. }
  524. }`)}},
  525. },
  526. want: want{
  527. err: nil,
  528. },
  529. },
  530. "SetSecretSucceedsWithExistingSecretAndAdditionalTags": {
  531. reason: "a secret can be pushed to aws secrets manager when it already exists",
  532. args: args{
  533. store: makeValidSecretStore().Spec.Provider.AWS,
  534. client: fakesm.Client{
  535. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  536. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  537. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  538. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  539. },
  540. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  541. Raw: []byte(`{
  542. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  543. "kind": "PushSecretMetadata",
  544. "spec": {
  545. "tags": {"tagname12": "tagvalue1"}
  546. }
  547. }`)}},
  548. },
  549. want: want{
  550. err: nil,
  551. },
  552. },
  553. "SetSecretSucceedsWithNewSecret": {
  554. reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
  555. args: args{
  556. store: makeValidSecretStore().Spec.Provider.AWS,
  557. client: fakesm.Client{
  558. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  559. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  560. },
  561. pushSecretData: pushSecretDataWithoutProperty,
  562. },
  563. want: want{
  564. err: nil,
  565. },
  566. },
  567. "SetSecretWithPropertySucceedsWithNewSecret": {
  568. 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",
  569. args: args{
  570. store: makeValidSecretStore().Spec.Provider.AWS,
  571. client: fakesm.Client{
  572. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  573. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil, []byte(`{"other-fake-property":"fake-value"}`)),
  574. },
  575. pushSecretData: pushSecretDataWithProperty,
  576. },
  577. want: want{
  578. err: nil,
  579. },
  580. },
  581. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyBinary": {
  582. 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)",
  583. args: args{
  584. store: makeValidSecretStore().Spec.Provider.AWS,
  585. client: fakesm.Client{
  586. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{b: []byte((`{"fake-property":"fake-value"}`))}), nil),
  587. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  588. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  589. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  590. Version: &defaultUpdatedVersion,
  591. }),
  592. },
  593. pushSecretData: pushSecretDataWithProperty,
  594. },
  595. want: want{
  596. err: nil,
  597. },
  598. },
  599. "SetSecretWithPropertySucceedsWithExistingSecretAndRandomUUIDVersion": {
  600. 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",
  601. args: args{
  602. store: makeValidSecretStore().Spec.Provider.AWS,
  603. client: fakesm.Client{
  604. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{
  605. b: []byte((`{"fake-property":"fake-value"}`)),
  606. version: &randomUUIDVersion,
  607. }), nil),
  608. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  609. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  610. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  611. Version: &randomUUIDVersionIncremented,
  612. }),
  613. },
  614. pushSecretData: pushSecretDataWithProperty,
  615. },
  616. want: want{
  617. err: nil,
  618. },
  619. },
  620. "SetSecretWithPropertySucceedsWithExistingSecretAndVersionThatCantBeParsed": {
  621. reason: "A manually set secret version doesn't have to be a UUID, we only support UUID secret versions though",
  622. args: args{
  623. store: makeValidSecretStore().Spec.Provider.AWS,
  624. client: fakesm.Client{
  625. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{
  626. b: []byte((`{"fake-property":"fake-value"}`)),
  627. version: &unparsableVersion,
  628. }), nil),
  629. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  630. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  631. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  632. Version: &initialVersion,
  633. }),
  634. },
  635. pushSecretData: pushSecretDataWithProperty,
  636. },
  637. want: want{
  638. err: fmt.Errorf("expected secret version in AWS SSM to be a UUID but got '%s'", unparsableVersion),
  639. },
  640. },
  641. "SetSecretWithPropertySucceedsWithExistingSecretAndAbsentVersion": {
  642. reason: "When a secret version is not specified, set it to 1",
  643. args: args{
  644. store: makeValidSecretStore().Spec.Provider.AWS,
  645. client: fakesm.Client{
  646. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(&awssm.GetSecretValueOutput{
  647. ARN: &arn,
  648. SecretBinary: []byte((`{"fake-property":"fake-value"}`)),
  649. }, nil),
  650. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  651. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  652. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  653. Version: &initialVersion,
  654. }),
  655. },
  656. pushSecretData: pushSecretDataWithProperty,
  657. },
  658. want: want{
  659. err: nil,
  660. },
  661. },
  662. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyString": {
  663. 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)",
  664. args: args{
  665. store: makeValidSecretStore().Spec.Provider.AWS,
  666. client: fakesm.Client{
  667. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{s: `{"fake-property":"fake-value"}`}), nil),
  668. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  669. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  670. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  671. Version: &defaultUpdatedVersion,
  672. }),
  673. },
  674. pushSecretData: pushSecretDataWithProperty,
  675. },
  676. want: want{
  677. err: nil,
  678. },
  679. },
  680. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyWithDot": {
  681. 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)",
  682. args: args{
  683. store: makeValidSecretStore().Spec.Provider.AWS,
  684. client: fakesm.Client{
  685. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  686. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  687. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  688. SecretBinary: []byte(`{"fake-property":{"fake-property":"fake-value","other-fake-property":"fake-value"}}`),
  689. Version: &defaultUpdatedVersion,
  690. }),
  691. },
  692. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "fake-property.other-fake-property"},
  693. },
  694. want: want{
  695. err: nil,
  696. },
  697. },
  698. "SetSecretWithPropertyFailsExistingNonJsonSecret": {
  699. reason: "setting a pushSecretData property is only supported for json secrets",
  700. args: args{
  701. store: makeValidSecretStore().Spec.Provider.AWS,
  702. client: fakesm.Client{
  703. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{s: `non-json-secret`}), nil),
  704. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  705. },
  706. pushSecretData: pushSecretDataWithProperty,
  707. },
  708. want: want{
  709. err: errors.New("PushSecret for aws secrets manager with a pushSecretData property requires a json secret"),
  710. },
  711. },
  712. "SetSecretCreateSecretFails": {
  713. reason: "CreateSecretWithContext returns an error if it fails",
  714. args: args{
  715. store: makeValidSecretStore().Spec.Provider.AWS,
  716. client: fakesm.Client{
  717. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  718. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(nil, noPermission),
  719. },
  720. pushSecretData: pushSecretDataWithoutProperty,
  721. },
  722. want: want{
  723. err: noPermission,
  724. },
  725. },
  726. "SetSecretGetSecretFails": {
  727. reason: "GetSecretValueWithContext returns an error if it fails",
  728. args: args{
  729. store: makeValidSecretStore().Spec.Provider.AWS,
  730. client: fakesm.Client{
  731. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, noPermission),
  732. },
  733. pushSecretData: pushSecretDataWithoutProperty,
  734. },
  735. want: want{
  736. err: noPermission,
  737. },
  738. },
  739. "SetSecretWillNotPushSameSecret": {
  740. reason: "secret with the same value will not be pushed",
  741. args: args{
  742. store: makeValidSecretStore().Spec.Provider.AWS,
  743. client: fakesm.Client{
  744. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput2, nil),
  745. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  746. },
  747. pushSecretData: pushSecretDataWithoutProperty,
  748. },
  749. want: want{
  750. err: nil,
  751. },
  752. },
  753. "SetSecretPutSecretValueFails": {
  754. reason: "PutSecretValueWithContext returns an error if it fails",
  755. args: args{
  756. store: makeValidSecretStore().Spec.Provider.AWS,
  757. client: fakesm.Client{
  758. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  759. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(nil, noPermission),
  760. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  761. },
  762. pushSecretData: pushSecretDataWithoutProperty,
  763. },
  764. want: want{
  765. err: noPermission,
  766. },
  767. },
  768. "SetSecretWrongGetSecretErrFails": {
  769. reason: "GetSecretValueWithContext errors out when anything except awssm.ErrCodeResourceNotFoundException",
  770. args: args{
  771. store: makeValidSecretStore().Spec.Provider.AWS,
  772. client: fakesm.Client{
  773. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretWrongErr),
  774. },
  775. pushSecretData: pushSecretDataWithoutProperty,
  776. },
  777. want: want{
  778. err: &getSecretWrongErr,
  779. },
  780. },
  781. "SetSecretDescribeSecretFails": {
  782. reason: "secret cannot be described",
  783. args: args{
  784. store: makeValidSecretStore().Spec.Provider.AWS,
  785. client: fakesm.Client{
  786. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  787. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(nil, noPermission),
  788. },
  789. pushSecretData: pushSecretDataWithoutProperty,
  790. },
  791. want: want{
  792. err: noPermission,
  793. },
  794. },
  795. "SetSecretDoesNotOverwriteUntaggedSecret": {
  796. reason: "secret cannot be described",
  797. args: args{
  798. store: makeValidSecretStore().Spec.Provider.AWS,
  799. client: fakesm.Client{
  800. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  801. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutputFaulty, nil),
  802. },
  803. pushSecretData: pushSecretDataWithoutProperty,
  804. },
  805. want: want{
  806. err: errors.New("secret not managed by external-secrets"),
  807. },
  808. },
  809. "SetSecretWithPrefix": {
  810. reason: "secret key is properly prefixed when creating a new secret",
  811. args: args{
  812. store: &esv1beta1.AWSProvider{
  813. Service: esv1beta1.AWSServiceSecretsManager,
  814. Region: "eu-west-2",
  815. Prefix: "prefix-",
  816. },
  817. client: fakesm.Client{
  818. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  819. CreateSecretWithContextFn: func(ctx aws.Context, input *awssm.CreateSecretInput, opts ...request.Option) (*awssm.CreateSecretOutput, error) {
  820. // Verify that the input name has the prefix applied
  821. if *input.Name != "prefix-"+fakeKey {
  822. return nil, fmt.Errorf("expected secret name to be prefixed with 'prefix-', got %s", *input.Name)
  823. }
  824. return secretOutput, nil
  825. },
  826. },
  827. pushSecretData: pushSecretDataWithoutProperty,
  828. },
  829. want: want{
  830. err: nil,
  831. },
  832. },
  833. }
  834. for name, tc := range tests {
  835. t.Run(name, func(t *testing.T) {
  836. sm := SecretsManager{
  837. client: &tc.args.client,
  838. prefix: tc.args.store.Prefix,
  839. }
  840. err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
  841. // Error nil XOR tc.want.err nil
  842. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  843. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  844. }
  845. // if errors are the same type but their contents do not match
  846. if err != nil && tc.want.err != nil {
  847. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  848. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  849. }
  850. }
  851. })
  852. }
  853. }
  854. func TestDeleteSecret(t *testing.T) {
  855. fakeClient := fakesm.Client{}
  856. managed := managedBy
  857. manager := externalSecrets
  858. secretTag := awssm.Tag{
  859. Key: &managed,
  860. Value: &manager,
  861. }
  862. type args struct {
  863. client fakesm.Client
  864. config esv1beta1.SecretsManager
  865. prefix string
  866. getSecretOutput *awssm.GetSecretValueOutput
  867. describeSecretOutput *awssm.DescribeSecretOutput
  868. deleteSecretOutput *awssm.DeleteSecretOutput
  869. getSecretErr error
  870. describeSecretErr error
  871. deleteSecretErr error
  872. }
  873. type want struct {
  874. err error
  875. }
  876. type testCase struct {
  877. args args
  878. want want
  879. reason string
  880. }
  881. tests := map[string]testCase{
  882. "Deletes Successfully": {
  883. args: args{
  884. client: fakeClient,
  885. config: esv1beta1.SecretsManager{},
  886. getSecretOutput: &awssm.GetSecretValueOutput{},
  887. describeSecretOutput: &awssm.DescribeSecretOutput{
  888. Tags: []*awssm.Tag{&secretTag},
  889. },
  890. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  891. getSecretErr: nil,
  892. describeSecretErr: nil,
  893. deleteSecretErr: nil,
  894. },
  895. want: want{
  896. err: nil,
  897. },
  898. reason: "",
  899. },
  900. "Deletes Successfully with ForceDeleteWithoutRecovery": {
  901. args: args{
  902. client: fakeClient,
  903. config: esv1beta1.SecretsManager{
  904. ForceDeleteWithoutRecovery: true,
  905. },
  906. getSecretOutput: &awssm.GetSecretValueOutput{},
  907. describeSecretOutput: &awssm.DescribeSecretOutput{
  908. Tags: []*awssm.Tag{&secretTag},
  909. },
  910. deleteSecretOutput: &awssm.DeleteSecretOutput{
  911. DeletionDate: aws.Time(time.Now()),
  912. },
  913. getSecretErr: nil,
  914. describeSecretErr: nil,
  915. deleteSecretErr: nil,
  916. },
  917. want: want{
  918. err: nil,
  919. },
  920. reason: "",
  921. },
  922. "Not Managed by ESO": {
  923. args: args{
  924. client: fakeClient,
  925. config: esv1beta1.SecretsManager{},
  926. getSecretOutput: &awssm.GetSecretValueOutput{},
  927. describeSecretOutput: &awssm.DescribeSecretOutput{
  928. Tags: []*awssm.Tag{},
  929. },
  930. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  931. getSecretErr: nil,
  932. describeSecretErr: nil,
  933. deleteSecretErr: nil,
  934. },
  935. want: want{
  936. err: nil,
  937. },
  938. reason: "",
  939. },
  940. "Invalid Recovery Window": {
  941. args: args{
  942. client: fakesm.Client{},
  943. config: esv1beta1.SecretsManager{
  944. RecoveryWindowInDays: 1,
  945. },
  946. getSecretOutput: &awssm.GetSecretValueOutput{},
  947. describeSecretOutput: &awssm.DescribeSecretOutput{
  948. Tags: []*awssm.Tag{&secretTag},
  949. },
  950. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  951. getSecretErr: nil,
  952. describeSecretErr: nil,
  953. deleteSecretErr: nil,
  954. },
  955. want: want{
  956. err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
  957. },
  958. reason: "",
  959. },
  960. "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
  961. args: args{
  962. client: fakesm.Client{},
  963. config: esv1beta1.SecretsManager{
  964. RecoveryWindowInDays: 7,
  965. ForceDeleteWithoutRecovery: true,
  966. },
  967. getSecretOutput: &awssm.GetSecretValueOutput{},
  968. describeSecretOutput: &awssm.DescribeSecretOutput{
  969. Tags: []*awssm.Tag{&secretTag},
  970. },
  971. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  972. getSecretErr: nil,
  973. describeSecretErr: nil,
  974. deleteSecretErr: nil,
  975. },
  976. want: want{
  977. err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
  978. },
  979. reason: "",
  980. },
  981. "Failed to get Tags": {
  982. args: args{
  983. client: fakeClient,
  984. config: esv1beta1.SecretsManager{},
  985. getSecretOutput: &awssm.GetSecretValueOutput{},
  986. describeSecretOutput: nil,
  987. deleteSecretOutput: nil,
  988. getSecretErr: nil,
  989. describeSecretErr: errors.New("failed to get tags"),
  990. deleteSecretErr: nil,
  991. },
  992. want: want{
  993. err: errors.New("failed to get tags"),
  994. },
  995. reason: "",
  996. },
  997. "Secret Not Found": {
  998. args: args{
  999. client: fakeClient,
  1000. config: esv1beta1.SecretsManager{},
  1001. getSecretOutput: nil,
  1002. describeSecretOutput: nil,
  1003. deleteSecretOutput: nil,
  1004. getSecretErr: awserr.New(awssm.ErrCodeResourceNotFoundException, "not here, sorry dude", nil),
  1005. describeSecretErr: nil,
  1006. deleteSecretErr: nil,
  1007. },
  1008. want: want{
  1009. err: nil,
  1010. },
  1011. },
  1012. "Not expected AWS error": {
  1013. args: args{
  1014. client: fakeClient,
  1015. config: esv1beta1.SecretsManager{},
  1016. getSecretOutput: nil,
  1017. describeSecretOutput: nil,
  1018. deleteSecretOutput: nil,
  1019. getSecretErr: awserr.New(awssm.ErrCodeEncryptionFailure, "aws unavailable", nil),
  1020. describeSecretErr: nil,
  1021. deleteSecretErr: nil,
  1022. },
  1023. want: want{
  1024. err: errors.New("aws unavailable"),
  1025. },
  1026. },
  1027. "unexpected error": {
  1028. args: args{
  1029. client: fakeClient,
  1030. config: esv1beta1.SecretsManager{},
  1031. getSecretOutput: nil,
  1032. describeSecretOutput: nil,
  1033. deleteSecretOutput: nil,
  1034. getSecretErr: errors.New("timeout"),
  1035. describeSecretErr: nil,
  1036. deleteSecretErr: nil,
  1037. },
  1038. want: want{
  1039. err: errors.New("timeout"),
  1040. },
  1041. },
  1042. "DeleteWithPrefix": {
  1043. args: args{
  1044. client: fakesm.Client{
  1045. GetSecretValueWithContextFn: func(ctx aws.Context, input *awssm.GetSecretValueInput, opts ...request.Option) (*awssm.GetSecretValueOutput, error) {
  1046. // Verify that the input secret ID has the prefix applied
  1047. if *input.SecretId != "my-prefix-"+fakeKey {
  1048. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1049. }
  1050. return &awssm.GetSecretValueOutput{}, nil
  1051. },
  1052. DescribeSecretWithContextFn: func(ctx aws.Context, input *awssm.DescribeSecretInput, opts ...request.Option) (*awssm.DescribeSecretOutput, error) {
  1053. // Verify that the input secret ID has the prefix applied
  1054. if *input.SecretId != "my-prefix-"+fakeKey {
  1055. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1056. }
  1057. return &awssm.DescribeSecretOutput{
  1058. Tags: []*awssm.Tag{&secretTag},
  1059. }, nil
  1060. },
  1061. DeleteSecretWithContextFn: func(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (*awssm.DeleteSecretOutput, error) {
  1062. return &awssm.DeleteSecretOutput{}, nil
  1063. },
  1064. },
  1065. config: esv1beta1.SecretsManager{},
  1066. prefix: "my-prefix-",
  1067. getSecretOutput: nil,
  1068. describeSecretOutput: nil,
  1069. deleteSecretOutput: nil,
  1070. getSecretErr: nil,
  1071. describeSecretErr: nil,
  1072. deleteSecretErr: nil,
  1073. },
  1074. want: want{
  1075. err: nil,
  1076. },
  1077. reason: "Verifies that the prefix is correctly applied when deleting a secret",
  1078. },
  1079. }
  1080. for name, tc := range tests {
  1081. t.Run(name, func(t *testing.T) {
  1082. ref := fake.PushSecretData{RemoteKey: fakeKey}
  1083. sm := SecretsManager{
  1084. client: &tc.args.client,
  1085. config: &tc.args.config,
  1086. prefix: tc.args.prefix,
  1087. }
  1088. if tc.args.client.GetSecretValueWithContextFn == nil {
  1089. tc.args.client.GetSecretValueWithContextFn = fakesm.NewGetSecretValueWithContextFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  1090. }
  1091. if tc.args.client.DescribeSecretWithContextFn == nil {
  1092. tc.args.client.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  1093. }
  1094. if tc.args.client.DeleteSecretWithContextFn == nil {
  1095. tc.args.client.DeleteSecretWithContextFn = fakesm.NewDeleteSecretWithContextFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  1096. }
  1097. err := sm.DeleteSecret(context.TODO(), ref)
  1098. t.Logf("DeleteSecret error: %v", err)
  1099. // Error nil XOR tc.want.err nil
  1100. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1101. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1102. }
  1103. // if errors are the same type but their contents do not match
  1104. if err != nil && tc.want.err != nil {
  1105. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1106. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1107. }
  1108. }
  1109. })
  1110. }
  1111. }
  1112. func makeValidSecretStore() *esv1beta1.SecretStore {
  1113. return &esv1beta1.SecretStore{
  1114. ObjectMeta: metav1.ObjectMeta{
  1115. Name: "aws-secret-store",
  1116. Namespace: "default",
  1117. },
  1118. Spec: esv1beta1.SecretStoreSpec{
  1119. Provider: &esv1beta1.SecretStoreProvider{
  1120. AWS: &esv1beta1.AWSProvider{
  1121. Service: esv1beta1.AWSServiceSecretsManager,
  1122. Region: "eu-west-2",
  1123. },
  1124. },
  1125. },
  1126. }
  1127. }
  1128. func getTagSlice() []*awssm.Tag {
  1129. tagKey1 := tagname1
  1130. tagValue1 := tagvalue1
  1131. tagKey2 := tagname2
  1132. tagValue2 := tagvalue2
  1133. return []*awssm.Tag{
  1134. {
  1135. Key: &tagKey1,
  1136. Value: &tagValue1,
  1137. },
  1138. {
  1139. Key: &tagKey2,
  1140. Value: &tagValue2,
  1141. },
  1142. }
  1143. }
  1144. func TestSecretsManagerGetAllSecrets(t *testing.T) {
  1145. ctx := context.Background()
  1146. errBoom := errors.New("boom")
  1147. secretName := "my-secret"
  1148. secretVersion := "AWSCURRENT"
  1149. secretPath := "/path/to/secret"
  1150. secretValue := "secret value"
  1151. secretTags := map[string]string{
  1152. "foo": "bar",
  1153. }
  1154. // Test cases
  1155. testCases := []struct {
  1156. name string
  1157. ref esv1beta1.ExternalSecretFind
  1158. secretName string
  1159. secretVersion string
  1160. secretValue string
  1161. batchGetSecretValueWithContextFn func(aws.Context, *awssm.BatchGetSecretValueInput, ...request.Option) (*awssm.BatchGetSecretValueOutput, error)
  1162. listSecretsFn func(ctx context.Context, input *awssm.ListSecretsInput, opts ...request.Option) (*awssm.ListSecretsOutput, error)
  1163. expectedData map[string][]byte
  1164. expectedError string
  1165. }{
  1166. {
  1167. name: "Matching secrets found",
  1168. ref: esv1beta1.ExternalSecretFind{
  1169. Name: &esv1beta1.FindName{
  1170. RegExp: secretName,
  1171. },
  1172. Path: ptr.To(secretPath),
  1173. },
  1174. secretName: secretName,
  1175. secretVersion: secretVersion,
  1176. secretValue: secretValue,
  1177. batchGetSecretValueWithContextFn: func(_ aws.Context, input *awssm.BatchGetSecretValueInput, _ ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  1178. assert.Len(t, input.Filters, 1)
  1179. assert.Equal(t, "name", *input.Filters[0].Key)
  1180. assert.Equal(t, secretPath, *input.Filters[0].Values[0])
  1181. return &awssm.BatchGetSecretValueOutput{
  1182. SecretValues: []*awssm.SecretValueEntry{
  1183. {
  1184. Name: ptr.To(secretName),
  1185. VersionStages: []*string{ptr.To(secretVersion)},
  1186. SecretBinary: []byte(secretValue),
  1187. },
  1188. },
  1189. }, nil
  1190. },
  1191. expectedData: map[string][]byte{
  1192. secretName: []byte(secretValue),
  1193. },
  1194. expectedError: "",
  1195. },
  1196. {
  1197. name: "Error occurred while fetching secret value",
  1198. ref: esv1beta1.ExternalSecretFind{
  1199. Name: &esv1beta1.FindName{
  1200. RegExp: secretName,
  1201. },
  1202. Path: ptr.To(secretPath),
  1203. },
  1204. secretName: secretName,
  1205. secretVersion: secretVersion,
  1206. secretValue: secretValue,
  1207. batchGetSecretValueWithContextFn: func(aws.Context, *awssm.BatchGetSecretValueInput, ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  1208. return &awssm.BatchGetSecretValueOutput{
  1209. SecretValues: []*awssm.SecretValueEntry{
  1210. {
  1211. Name: ptr.To(secretName),
  1212. },
  1213. },
  1214. }, errBoom
  1215. },
  1216. expectedData: nil,
  1217. expectedError: errBoom.Error(),
  1218. },
  1219. {
  1220. name: "regexp: error occurred while listing secrets",
  1221. ref: esv1beta1.ExternalSecretFind{
  1222. Name: &esv1beta1.FindName{
  1223. RegExp: secretName,
  1224. },
  1225. },
  1226. listSecretsFn: func(ctx context.Context, input *awssm.ListSecretsInput, opts ...request.Option) (*awssm.ListSecretsOutput, error) {
  1227. return nil, errBoom
  1228. },
  1229. expectedData: nil,
  1230. expectedError: errBoom.Error(),
  1231. },
  1232. {
  1233. name: "regep: no matching secrets found",
  1234. ref: esv1beta1.ExternalSecretFind{
  1235. Name: &esv1beta1.FindName{
  1236. RegExp: secretName,
  1237. },
  1238. },
  1239. listSecretsFn: func(ctx context.Context, input *awssm.ListSecretsInput, opts ...request.Option) (*awssm.ListSecretsOutput, error) {
  1240. return &awssm.ListSecretsOutput{
  1241. SecretList: []*awssm.SecretListEntry{
  1242. {
  1243. Name: ptr.To("other-secret"),
  1244. },
  1245. },
  1246. }, nil
  1247. },
  1248. batchGetSecretValueWithContextFn: func(aws.Context, *awssm.BatchGetSecretValueInput, ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  1249. return &awssm.BatchGetSecretValueOutput{
  1250. SecretValues: []*awssm.SecretValueEntry{
  1251. {
  1252. Name: ptr.To("other-secret"),
  1253. },
  1254. },
  1255. }, nil
  1256. },
  1257. expectedData: make(map[string][]byte),
  1258. expectedError: "",
  1259. },
  1260. {
  1261. name: "invalid regexp",
  1262. ref: esv1beta1.ExternalSecretFind{
  1263. Name: &esv1beta1.FindName{
  1264. RegExp: "[",
  1265. },
  1266. },
  1267. expectedData: nil,
  1268. expectedError: "could not compile find.name.regexp [[]: error parsing regexp: missing closing ]: `[`",
  1269. },
  1270. {
  1271. name: "tags: Matching secrets found",
  1272. ref: esv1beta1.ExternalSecretFind{
  1273. Tags: secretTags,
  1274. },
  1275. secretName: secretName,
  1276. secretVersion: secretVersion,
  1277. secretValue: secretValue,
  1278. batchGetSecretValueWithContextFn: func(_ aws.Context, input *awssm.BatchGetSecretValueInput, _ ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  1279. assert.Len(t, input.Filters, 2)
  1280. assert.Equal(t, "tag-key", *input.Filters[0].Key)
  1281. assert.Equal(t, "foo", *input.Filters[0].Values[0])
  1282. assert.Equal(t, "tag-value", *input.Filters[1].Key)
  1283. assert.Equal(t, "bar", *input.Filters[1].Values[0])
  1284. return &awssm.BatchGetSecretValueOutput{
  1285. SecretValues: []*awssm.SecretValueEntry{
  1286. {
  1287. Name: ptr.To(secretName),
  1288. VersionStages: []*string{ptr.To(secretVersion)},
  1289. SecretBinary: []byte(secretValue),
  1290. },
  1291. },
  1292. }, nil
  1293. },
  1294. expectedData: map[string][]byte{
  1295. secretName: []byte(secretValue),
  1296. },
  1297. expectedError: "",
  1298. },
  1299. {
  1300. name: "tags: error occurred while fetching secret value",
  1301. ref: esv1beta1.ExternalSecretFind{
  1302. Tags: secretTags,
  1303. },
  1304. secretName: secretName,
  1305. secretVersion: secretVersion,
  1306. secretValue: secretValue,
  1307. batchGetSecretValueWithContextFn: func(aws.Context, *awssm.BatchGetSecretValueInput, ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  1308. return &awssm.BatchGetSecretValueOutput{
  1309. SecretValues: []*awssm.SecretValueEntry{
  1310. {
  1311. Name: ptr.To(secretName),
  1312. VersionStages: []*string{ptr.To(secretVersion)},
  1313. SecretBinary: []byte(secretValue),
  1314. },
  1315. },
  1316. }, errBoom
  1317. },
  1318. expectedData: nil,
  1319. expectedError: errBoom.Error(),
  1320. },
  1321. {
  1322. name: "tags: error occurred while listing secrets",
  1323. ref: esv1beta1.ExternalSecretFind{
  1324. Tags: secretTags,
  1325. },
  1326. batchGetSecretValueWithContextFn: func(aws.Context, *awssm.BatchGetSecretValueInput, ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  1327. return nil, errBoom
  1328. },
  1329. expectedData: nil,
  1330. expectedError: errBoom.Error(),
  1331. },
  1332. }
  1333. for _, tc := range testCases {
  1334. t.Run(tc.name, func(t *testing.T) {
  1335. fc := fakesm.NewClient()
  1336. fc.BatchGetSecretValueWithContextFn = tc.batchGetSecretValueWithContextFn
  1337. fc.ListSecretsFn = tc.listSecretsFn
  1338. sm := SecretsManager{
  1339. client: fc,
  1340. cache: make(map[string]*awssm.GetSecretValueOutput),
  1341. }
  1342. data, err := sm.GetAllSecrets(ctx, tc.ref)
  1343. if err != nil && err.Error() != tc.expectedError {
  1344. t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
  1345. }
  1346. if !reflect.DeepEqual(data, tc.expectedData) {
  1347. t.Errorf("unexpected data: got %v, want %v", data, tc.expectedData)
  1348. }
  1349. })
  1350. }
  1351. }
  1352. func TestSecretsManagerValidate(t *testing.T) {
  1353. type fields struct {
  1354. sess *session.Session
  1355. referentAuth bool
  1356. }
  1357. validSession, _ := session.NewSession(aws.NewConfig().WithCredentials(credentials.NewStaticCredentials("fake", "fake", "fake")))
  1358. invalidSession, _ := session.NewSession(aws.NewConfig().WithCredentials(credentials.NewCredentials(&FakeCredProvider{
  1359. retrieveFunc: func() (credentials.Value, error) {
  1360. return credentials.Value{}, errors.New("invalid credentials")
  1361. },
  1362. })))
  1363. tests := []struct {
  1364. name string
  1365. fields fields
  1366. want esv1beta1.ValidationResult
  1367. wantErr bool
  1368. }{
  1369. {
  1370. name: "ReferentAuth should always return unknown",
  1371. fields: fields{
  1372. referentAuth: true,
  1373. },
  1374. want: esv1beta1.ValidationResultUnknown,
  1375. },
  1376. {
  1377. name: "Valid credentials should return ready",
  1378. fields: fields{
  1379. sess: validSession,
  1380. },
  1381. want: esv1beta1.ValidationResultReady,
  1382. },
  1383. {
  1384. name: "Invalid credentials should return error",
  1385. fields: fields{
  1386. sess: invalidSession,
  1387. },
  1388. want: esv1beta1.ValidationResultError,
  1389. wantErr: true,
  1390. },
  1391. }
  1392. for _, tt := range tests {
  1393. t.Run(tt.name, func(t *testing.T) {
  1394. sm := &SecretsManager{
  1395. sess: tt.fields.sess,
  1396. referentAuth: tt.fields.referentAuth,
  1397. }
  1398. got, err := sm.Validate()
  1399. if (err != nil) != tt.wantErr {
  1400. t.Errorf("SecretsManager.Validate() error = %v, wantErr %v", err, tt.wantErr)
  1401. return
  1402. }
  1403. if !reflect.DeepEqual(got, tt.want) {
  1404. t.Errorf("SecretsManager.Validate() = %v, want %v", got, tt.want)
  1405. }
  1406. })
  1407. }
  1408. }
  1409. func TestSecretExists(t *testing.T) {
  1410. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  1411. defaultVersion := "00000000-0000-0000-0000-000000000002"
  1412. secretValueOutput := &awssm.GetSecretValueOutput{
  1413. ARN: &arn,
  1414. VersionId: &defaultVersion,
  1415. }
  1416. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  1417. getSecretCorrectErr := awssm.ResourceNotFoundException{}
  1418. getSecretWrongErr := awssm.InvalidRequestException{}
  1419. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: "fake-secret-key", RemoteKey: fakeKey, Property: ""}
  1420. type args struct {
  1421. store *esv1beta1.AWSProvider
  1422. client fakesm.Client
  1423. pushSecretData fake.PushSecretData
  1424. }
  1425. type want struct {
  1426. err error
  1427. wantError bool
  1428. }
  1429. tests := map[string]struct {
  1430. args args
  1431. want want
  1432. }{
  1433. "SecretExistsReturnsTrueForExistingSecret": {
  1434. args: args{
  1435. store: makeValidSecretStore().Spec.Provider.AWS,
  1436. client: fakesm.Client{
  1437. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  1438. },
  1439. pushSecretData: pushSecretDataWithoutProperty,
  1440. },
  1441. want: want{
  1442. err: nil,
  1443. wantError: true,
  1444. },
  1445. },
  1446. "SecretExistsReturnsFalseForNonExistingSecret": {
  1447. args: args{
  1448. store: makeValidSecretStore().Spec.Provider.AWS,
  1449. client: fakesm.Client{
  1450. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  1451. },
  1452. pushSecretData: pushSecretDataWithoutProperty,
  1453. },
  1454. want: want{
  1455. err: nil,
  1456. wantError: false,
  1457. },
  1458. },
  1459. "SecretExistsReturnsFalseForErroredSecret": {
  1460. args: args{
  1461. store: makeValidSecretStore().Spec.Provider.AWS,
  1462. client: fakesm.Client{
  1463. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretWrongErr),
  1464. },
  1465. pushSecretData: pushSecretDataWithoutProperty,
  1466. },
  1467. want: want{
  1468. err: &getSecretWrongErr,
  1469. wantError: false,
  1470. },
  1471. },
  1472. }
  1473. for name, tc := range tests {
  1474. t.Run(name, func(t *testing.T) {
  1475. sm := &SecretsManager{
  1476. client: &tc.args.client,
  1477. }
  1478. got, err := sm.SecretExists(context.Background(), tc.args.pushSecretData)
  1479. assert.Equal(
  1480. t,
  1481. tc.want,
  1482. want{
  1483. err: err,
  1484. wantError: got,
  1485. })
  1486. })
  1487. }
  1488. }
  1489. // FakeCredProvider implements the AWS credentials.Provider interface
  1490. // It is used to inject an error into the AWS session to cause a
  1491. // validation error.
  1492. type FakeCredProvider struct {
  1493. retrieveFunc func() (credentials.Value, error)
  1494. }
  1495. func (f *FakeCredProvider) Retrieve() (credentials.Value, error) {
  1496. return f.retrieveFunc()
  1497. }
  1498. func (f *FakeCredProvider) IsExpired() bool {
  1499. return true
  1500. }