secretsmanager_test.go 43 KB

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