secretsmanager_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. "strings"
  18. "testing"
  19. "github.com/aws/aws-sdk-go/aws"
  20. "github.com/aws/aws-sdk-go/aws/awserr"
  21. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  22. "github.com/google/go-cmp/cmp"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. fakesm "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager/fake"
  26. )
  27. type secretsManagerTestCase struct {
  28. fakeClient *fakesm.Client
  29. apiInput *awssm.GetSecretValueInput
  30. apiOutput *awssm.GetSecretValueOutput
  31. remoteRef *esv1beta1.ExternalSecretDataRemoteRef
  32. apiErr error
  33. expectError string
  34. expectedSecret string
  35. // for testing secretmap
  36. expectedData map[string][]byte
  37. // for testing caching
  38. expectedCounter *int
  39. }
  40. const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
  41. const (
  42. tagname1 = "tagname1"
  43. tagvalue1 = "tagvalue1"
  44. tagname2 = "tagname2"
  45. tagvalue2 = "tagvalue2"
  46. )
  47. func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
  48. smtc := secretsManagerTestCase{
  49. fakeClient: fakesm.NewClient(),
  50. apiInput: makeValidAPIInput(),
  51. remoteRef: makeValidRemoteRef(),
  52. apiOutput: makeValidAPIOutput(),
  53. apiErr: nil,
  54. expectError: "",
  55. expectedSecret: "",
  56. expectedData: map[string][]byte{},
  57. }
  58. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  59. return &smtc
  60. }
  61. func makeValidRemoteRef() *esv1beta1.ExternalSecretDataRemoteRef {
  62. return &esv1beta1.ExternalSecretDataRemoteRef{
  63. Key: "/baz",
  64. Version: "AWSCURRENT",
  65. }
  66. }
  67. func makeValidAPIInput() *awssm.GetSecretValueInput {
  68. return &awssm.GetSecretValueInput{
  69. SecretId: aws.String("/baz"),
  70. VersionStage: aws.String("AWSCURRENT"),
  71. }
  72. }
  73. func makeValidAPIOutput() *awssm.GetSecretValueOutput {
  74. return &awssm.GetSecretValueOutput{
  75. SecretString: aws.String(""),
  76. }
  77. }
  78. func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
  79. smtc := makeValidSecretsManagerTestCase()
  80. for _, fn := range tweaks {
  81. fn(smtc)
  82. }
  83. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  84. return smtc
  85. }
  86. // This case can be shared by both GetSecret and GetSecretMap tests.
  87. // bad case: set apiErr.
  88. var setAPIErr = func(smtc *secretsManagerTestCase) {
  89. smtc.apiErr = fmt.Errorf("oh no")
  90. smtc.expectError = "oh no"
  91. }
  92. // test the sm<->aws interface
  93. // make sure correct values are passed and errors are handled accordingly.
  94. func TestSecretsManagerGetSecret(t *testing.T) {
  95. // good case: default version is set
  96. // key is passed in, output is sent back
  97. setSecretString := func(smtc *secretsManagerTestCase) {
  98. smtc.apiOutput.SecretString = aws.String("testtesttest")
  99. smtc.expectedSecret = "testtesttest"
  100. }
  101. // good case: extract property
  102. // Testing that the property exists in the SecretString
  103. setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
  104. smtc.remoteRef.Property = "/shmoo"
  105. smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
  106. smtc.expectedSecret = "bang"
  107. }
  108. // bad case: missing property
  109. setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
  110. smtc.remoteRef.Property = "INVALPROP"
  111. smtc.expectError = "key INVALPROP does not exist in secret"
  112. }
  113. // bad case: extract property failure due to invalid json
  114. setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
  115. smtc.remoteRef.Property = "INVALPROP"
  116. smtc.apiOutput.SecretString = aws.String(`------`)
  117. smtc.expectError = "key INVALPROP does not exist in secret"
  118. }
  119. // good case: set .SecretString to nil but set binary with value
  120. setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
  121. smtc.apiOutput.SecretBinary = []byte("yesplease")
  122. // needs to be set as nil, empty quotes ("") is considered existing
  123. smtc.apiOutput.SecretString = nil
  124. smtc.expectedSecret = "yesplease"
  125. }
  126. // bad case: both .SecretString and .SecretBinary are nil
  127. setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
  128. smtc.apiOutput.SecretBinary = nil
  129. smtc.apiOutput.SecretString = nil
  130. smtc.expectError = "no secret string nor binary for key"
  131. }
  132. // good case: secretOut.SecretBinary JSON parsing
  133. setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
  134. smtc.apiOutput.SecretString = nil
  135. smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
  136. smtc.remoteRef.Property = "foobar.baz"
  137. smtc.expectedSecret = "nestedval"
  138. }
  139. // good case: secretOut.SecretBinary no JSON parsing if name on key
  140. setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
  141. smtc.apiOutput.SecretString = nil
  142. smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
  143. smtc.remoteRef.Property = "foobar.baz"
  144. smtc.expectedSecret = "nestedval"
  145. }
  146. // good case: custom version stage set
  147. setCustomVersionStage := func(smtc *secretsManagerTestCase) {
  148. smtc.apiInput.VersionStage = aws.String("1234")
  149. smtc.remoteRef.Version = "1234"
  150. smtc.apiOutput.SecretString = aws.String("FOOBA!")
  151. smtc.expectedSecret = "FOOBA!"
  152. }
  153. // good case: custom version id set
  154. setCustomVersionID := func(smtc *secretsManagerTestCase) {
  155. smtc.apiInput.VersionStage = nil
  156. smtc.apiInput.VersionId = aws.String("1234-5678")
  157. smtc.remoteRef.Version = "uuid/1234-5678"
  158. smtc.apiOutput.SecretString = aws.String("myvalue")
  159. smtc.expectedSecret = "myvalue"
  160. }
  161. fetchMetadata := func(smtc *secretsManagerTestCase) {
  162. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  163. describeSecretOutput := &awssm.DescribeSecretOutput{
  164. Tags: getTagSlice(),
  165. }
  166. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  167. jsonTags, _ := TagsToJSONString(getTagSlice())
  168. smtc.apiOutput.SecretString = &jsonTags
  169. smtc.expectedSecret = jsonTags
  170. }
  171. fetchMetadataProperty := 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. smtc.remoteRef.Property = tagname2
  178. jsonTags, _ := TagsToJSONString(getTagSlice())
  179. smtc.apiOutput.SecretString = &jsonTags
  180. smtc.expectedSecret = tagvalue2
  181. }
  182. failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
  183. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  184. describeSecretOutput := &awssm.DescribeSecretOutput{
  185. Tags: getTagSlice(),
  186. }
  187. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  188. smtc.remoteRef.Property = "fail"
  189. jsonTags, _ := TagsToJSONString(getTagSlice())
  190. smtc.apiOutput.SecretString = &jsonTags
  191. smtc.expectError = "key fail does not exist in secret /baz"
  192. }
  193. successCases := []*secretsManagerTestCase{
  194. makeValidSecretsManagerTestCase(),
  195. makeValidSecretsManagerTestCaseCustom(setSecretString),
  196. makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
  197. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
  198. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
  199. makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
  200. makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
  201. makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
  202. makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
  203. makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
  204. makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
  205. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  206. makeValidSecretsManagerTestCaseCustom(fetchMetadata),
  207. makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
  208. makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
  209. }
  210. for k, v := range successCases {
  211. sm := SecretsManager{
  212. cache: make(map[string]*awssm.GetSecretValueOutput),
  213. client: v.fakeClient,
  214. }
  215. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  216. if !ErrorContains(err, v.expectError) {
  217. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  218. }
  219. if err == nil && string(out) != v.expectedSecret {
  220. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  221. }
  222. }
  223. }
  224. func TestCaching(t *testing.T) {
  225. fakeClient := fakesm.NewClient()
  226. // good case: first call, since we are using the same key, results should be cached and the counter should not go
  227. // over 1
  228. firstCall := func(smtc *secretsManagerTestCase) {
  229. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  230. smtc.remoteRef.Property = "foo"
  231. smtc.expectedSecret = "bar"
  232. smtc.expectedCounter = aws.Int(1)
  233. smtc.fakeClient = fakeClient
  234. }
  235. secondCall := func(smtc *secretsManagerTestCase) {
  236. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  237. smtc.remoteRef.Property = "bar"
  238. smtc.expectedSecret = "vodka"
  239. smtc.expectedCounter = aws.Int(1)
  240. smtc.fakeClient = fakeClient
  241. }
  242. notCachedCall := func(smtc *secretsManagerTestCase) {
  243. smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
  244. smtc.remoteRef.Property = "sheldon"
  245. smtc.expectedSecret = "bazinga"
  246. smtc.expectedCounter = aws.Int(2)
  247. smtc.fakeClient = fakeClient
  248. smtc.apiInput.SecretId = aws.String("xyz")
  249. smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
  250. }
  251. cachedCases := []*secretsManagerTestCase{
  252. makeValidSecretsManagerTestCaseCustom(firstCall),
  253. makeValidSecretsManagerTestCaseCustom(firstCall),
  254. makeValidSecretsManagerTestCaseCustom(secondCall),
  255. makeValidSecretsManagerTestCaseCustom(notCachedCall),
  256. }
  257. sm := SecretsManager{
  258. cache: make(map[string]*awssm.GetSecretValueOutput),
  259. }
  260. for k, v := range cachedCases {
  261. sm.client = v.fakeClient
  262. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  263. if !ErrorContains(err, v.expectError) {
  264. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  265. }
  266. if err == nil && string(out) != v.expectedSecret {
  267. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  268. }
  269. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  270. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  271. }
  272. }
  273. }
  274. func TestGetSecretMap(t *testing.T) {
  275. // good case: default version & deserialization
  276. setDeserialization := func(smtc *secretsManagerTestCase) {
  277. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
  278. smtc.expectedData["foo"] = []byte("bar")
  279. }
  280. // good case: nested json
  281. setNestedJSON := func(smtc *secretsManagerTestCase) {
  282. smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
  283. smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
  284. }
  285. // good case: caching
  286. cachedMap := func(smtc *secretsManagerTestCase) {
  287. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
  288. smtc.expectedData["foo"] = []byte("bar")
  289. smtc.expectedData["plus"] = []byte("one")
  290. smtc.expectedCounter = aws.Int(1)
  291. }
  292. // bad case: invalid json
  293. setInvalidJSON := func(smtc *secretsManagerTestCase) {
  294. smtc.apiOutput.SecretString = aws.String(`-----------------`)
  295. smtc.expectError = "unable to unmarshal secret"
  296. }
  297. successCases := []*secretsManagerTestCase{
  298. makeValidSecretsManagerTestCaseCustom(setDeserialization),
  299. makeValidSecretsManagerTestCaseCustom(setNestedJSON),
  300. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  301. makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
  302. makeValidSecretsManagerTestCaseCustom(cachedMap),
  303. }
  304. for k, v := range successCases {
  305. sm := SecretsManager{
  306. cache: make(map[string]*awssm.GetSecretValueOutput),
  307. client: v.fakeClient,
  308. }
  309. out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
  310. if !ErrorContains(err, v.expectError) {
  311. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  312. }
  313. if err == nil && !cmp.Equal(out, v.expectedData) {
  314. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  315. }
  316. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  317. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  318. }
  319. }
  320. }
  321. func ErrorContains(out error, want string) bool {
  322. if out == nil {
  323. return want == ""
  324. }
  325. if want == "" {
  326. return false
  327. }
  328. return strings.Contains(out.Error(), want)
  329. }
  330. type fakeRef struct {
  331. key string
  332. }
  333. func (f fakeRef) GetRemoteKey() string {
  334. return f.key
  335. }
  336. func TestSetSecret(t *testing.T) {
  337. managedBy := managedBy
  338. notManagedBy := "not-managed-by"
  339. secretValue := []byte("fake-value")
  340. externalSecrets := externalSecrets
  341. noPermission := errors.New("no permission")
  342. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  343. getSecretCorrectErr := awssm.ResourceNotFoundException{}
  344. getSecretWrongErr := awssm.InvalidRequestException{}
  345. secretOutput := &awssm.CreateSecretOutput{
  346. ARN: &arn,
  347. }
  348. externalSecretsTag := []*awssm.Tag{
  349. {
  350. Key: &managedBy,
  351. Value: &externalSecrets,
  352. },
  353. }
  354. externalSecretsTagFaulty := []*awssm.Tag{
  355. {
  356. Key: &notManagedBy,
  357. Value: &externalSecrets,
  358. },
  359. }
  360. tagSecretOutput := &awssm.DescribeSecretOutput{
  361. ARN: &arn,
  362. Tags: externalSecretsTag,
  363. }
  364. tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
  365. ARN: &arn,
  366. Tags: externalSecretsTagFaulty,
  367. }
  368. secretValueOutput := &awssm.GetSecretValueOutput{
  369. ARN: &arn,
  370. }
  371. secretValueOutput2 := &awssm.GetSecretValueOutput{
  372. ARN: &arn,
  373. SecretBinary: secretValue,
  374. }
  375. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  376. putSecretOutput := &awssm.PutSecretValueOutput{
  377. ARN: &arn,
  378. }
  379. type args struct {
  380. store *esv1beta1.AWSProvider
  381. client fakesm.Client
  382. }
  383. type want struct {
  384. err error
  385. }
  386. tests := map[string]struct {
  387. reason string
  388. args args
  389. want want
  390. }{
  391. "SetSecretSucceedsWithExistingSecret": {
  392. reason: "a secret can be pushed to aws secrets manager when it already exists",
  393. args: args{
  394. store: makeValidSecretStore().Spec.Provider.AWS,
  395. client: fakesm.Client{
  396. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  397. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  398. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  399. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  400. },
  401. },
  402. want: want{
  403. err: nil,
  404. },
  405. },
  406. "SetSecretSucceedsWithNewSecret": {
  407. reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
  408. args: args{
  409. store: makeValidSecretStore().Spec.Provider.AWS,
  410. client: fakesm.Client{
  411. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  412. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  413. },
  414. },
  415. want: want{
  416. err: nil,
  417. },
  418. },
  419. "SetSecretCreateSecretFails": {
  420. reason: "CreateSecretWithContext returns an error if it fails",
  421. args: args{
  422. store: makeValidSecretStore().Spec.Provider.AWS,
  423. client: fakesm.Client{
  424. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  425. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(nil, noPermission),
  426. },
  427. },
  428. want: want{
  429. err: noPermission,
  430. },
  431. },
  432. "SetSecretGetSecretFails": {
  433. reason: "GetSecretValueWithContext returns an error if it fails",
  434. args: args{
  435. store: makeValidSecretStore().Spec.Provider.AWS,
  436. client: fakesm.Client{
  437. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, noPermission),
  438. },
  439. },
  440. want: want{
  441. err: noPermission,
  442. },
  443. },
  444. "SetSecretWillNotPushSameSecret": {
  445. reason: "secret with the same value will not be pushed",
  446. args: args{
  447. store: makeValidSecretStore().Spec.Provider.AWS,
  448. client: fakesm.Client{
  449. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput2, nil),
  450. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  451. },
  452. },
  453. want: want{
  454. err: nil,
  455. },
  456. },
  457. "SetSecretPutSecretValueFails": {
  458. reason: "PutSecretValueWithContext returns an error if it fails",
  459. args: args{
  460. store: makeValidSecretStore().Spec.Provider.AWS,
  461. client: fakesm.Client{
  462. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  463. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(nil, noPermission),
  464. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  465. },
  466. },
  467. want: want{
  468. err: noPermission,
  469. },
  470. },
  471. "SetSecretWrongGetSecretErrFails": {
  472. reason: "GetSecretValueWithContext errors out when anything except awssm.ErrCodeResourceNotFoundException",
  473. args: args{
  474. store: makeValidSecretStore().Spec.Provider.AWS,
  475. client: fakesm.Client{
  476. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretWrongErr),
  477. },
  478. },
  479. want: want{
  480. err: &getSecretWrongErr,
  481. },
  482. },
  483. "SetSecretDescribeSecretFails": {
  484. reason: "secret cannot be described",
  485. args: args{
  486. store: makeValidSecretStore().Spec.Provider.AWS,
  487. client: fakesm.Client{
  488. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  489. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(nil, noPermission),
  490. },
  491. },
  492. want: want{
  493. err: noPermission,
  494. },
  495. },
  496. "SetSecretDoesNotOverwriteUntaggedSecret": {
  497. reason: "secret cannot be described",
  498. args: args{
  499. store: makeValidSecretStore().Spec.Provider.AWS,
  500. client: fakesm.Client{
  501. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  502. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutputFaulty, nil),
  503. },
  504. },
  505. want: want{
  506. err: fmt.Errorf("secret not managed by external-secrets"),
  507. },
  508. },
  509. }
  510. for name, tc := range tests {
  511. t.Run(name, func(t *testing.T) {
  512. ref := fakeRef{key: "fake-key"}
  513. sm := SecretsManager{
  514. client: &tc.args.client,
  515. }
  516. err := sm.PushSecret(context.Background(), []byte("fake-value"), ref)
  517. // Error nil XOR tc.want.err nil
  518. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  519. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  520. }
  521. // if errors are the same type but their contents do not match
  522. if err != nil && tc.want.err != nil {
  523. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  524. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  525. }
  526. }
  527. })
  528. }
  529. }
  530. func TestDeleteSecret(t *testing.T) {
  531. fakeClient := fakesm.Client{}
  532. managed := managedBy
  533. manager := externalSecrets
  534. secretTag := awssm.Tag{
  535. Key: &managed,
  536. Value: &manager,
  537. }
  538. type args struct {
  539. client fakesm.Client
  540. getSecretOutput *awssm.GetSecretValueOutput
  541. describeSecretOutput *awssm.DescribeSecretOutput
  542. deleteSecretOutput *awssm.DeleteSecretOutput
  543. getSecretErr error
  544. describeSecretErr error
  545. deleteSecretErr error
  546. }
  547. type want struct {
  548. err error
  549. }
  550. type testCase struct {
  551. args args
  552. want want
  553. reason string
  554. }
  555. tests := map[string]testCase{
  556. "Deletes Successfully": {
  557. args: args{
  558. client: fakeClient,
  559. getSecretOutput: &awssm.GetSecretValueOutput{},
  560. describeSecretOutput: &awssm.DescribeSecretOutput{
  561. Tags: []*awssm.Tag{&secretTag},
  562. },
  563. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  564. getSecretErr: nil,
  565. describeSecretErr: nil,
  566. deleteSecretErr: nil,
  567. },
  568. want: want{
  569. err: nil,
  570. },
  571. reason: "",
  572. },
  573. "Not Managed by ESO": {
  574. args: args{
  575. client: fakeClient,
  576. getSecretOutput: &awssm.GetSecretValueOutput{},
  577. describeSecretOutput: &awssm.DescribeSecretOutput{
  578. Tags: []*awssm.Tag{},
  579. },
  580. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  581. getSecretErr: nil,
  582. describeSecretErr: nil,
  583. deleteSecretErr: nil,
  584. },
  585. want: want{
  586. err: nil,
  587. },
  588. reason: "",
  589. },
  590. "Failed to get Tags": {
  591. args: args{
  592. client: fakeClient,
  593. getSecretOutput: &awssm.GetSecretValueOutput{},
  594. describeSecretOutput: nil,
  595. deleteSecretOutput: nil,
  596. getSecretErr: nil,
  597. describeSecretErr: errors.New("failed to get tags"),
  598. deleteSecretErr: nil,
  599. },
  600. want: want{
  601. err: errors.New("failed to get tags"),
  602. },
  603. reason: "",
  604. },
  605. "Secret Not Found": {
  606. args: args{
  607. client: fakeClient,
  608. getSecretOutput: nil,
  609. describeSecretOutput: nil,
  610. deleteSecretOutput: nil,
  611. getSecretErr: awserr.New(awssm.ErrCodeResourceNotFoundException, "not here, sorry dude", nil),
  612. describeSecretErr: nil,
  613. deleteSecretErr: nil,
  614. },
  615. want: want{
  616. err: nil,
  617. },
  618. },
  619. "Not expected AWS error": {
  620. args: args{
  621. client: fakeClient,
  622. getSecretOutput: nil,
  623. describeSecretOutput: nil,
  624. deleteSecretOutput: nil,
  625. getSecretErr: awserr.New(awssm.ErrCodeEncryptionFailure, "aws unavailable", nil),
  626. describeSecretErr: nil,
  627. deleteSecretErr: nil,
  628. },
  629. want: want{
  630. err: errors.New("aws unavailable"),
  631. },
  632. },
  633. "unexpected error": {
  634. args: args{
  635. client: fakeClient,
  636. getSecretOutput: nil,
  637. describeSecretOutput: nil,
  638. deleteSecretOutput: nil,
  639. getSecretErr: errors.New("timeout"),
  640. describeSecretErr: nil,
  641. deleteSecretErr: nil,
  642. },
  643. want: want{
  644. err: errors.New("timeout"),
  645. },
  646. },
  647. }
  648. for name, tc := range tests {
  649. t.Run(name, func(t *testing.T) {
  650. ref := fakeRef{key: "fake-key"}
  651. sm := SecretsManager{
  652. client: &tc.args.client,
  653. }
  654. tc.args.client.GetSecretValueWithContextFn = fakesm.NewGetSecretValueWithContextFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  655. tc.args.client.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  656. tc.args.client.DeleteSecretWithContextFn = fakesm.NewDeleteSecretWithContextFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  657. err := sm.DeleteSecret(context.TODO(), ref)
  658. // Error nil XOR tc.want.err nil
  659. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  660. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  661. }
  662. // if errors are the same type but their contents do not match
  663. if err != nil && tc.want.err != nil {
  664. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  665. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  666. }
  667. }
  668. })
  669. }
  670. }
  671. func makeValidSecretStore() *esv1beta1.SecretStore {
  672. return &esv1beta1.SecretStore{
  673. ObjectMeta: metav1.ObjectMeta{
  674. Name: "aws-secret-store",
  675. Namespace: "default",
  676. },
  677. Spec: esv1beta1.SecretStoreSpec{
  678. Provider: &esv1beta1.SecretStoreProvider{
  679. AWS: &esv1beta1.AWSProvider{
  680. Service: esv1beta1.AWSServiceSecretsManager,
  681. Region: "eu-west-2",
  682. },
  683. },
  684. },
  685. }
  686. }
  687. func getTagSlice() []*awssm.Tag {
  688. tagKey1 := tagname1
  689. tagValue1 := tagvalue1
  690. tagKey2 := tagname2
  691. tagValue2 := tagvalue2
  692. return []*awssm.Tag{
  693. {
  694. Key: &tagKey1,
  695. Value: &tagValue1,
  696. },
  697. {
  698. Key: &tagKey2,
  699. Value: &tagValue2,
  700. },
  701. }
  702. }