secretsmanager_test.go 22 KB

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