secretsmanager_test.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. "time"
  20. "github.com/aws/aws-sdk-go/aws"
  21. "github.com/aws/aws-sdk-go/aws/awserr"
  22. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  23. "github.com/google/go-cmp/cmp"
  24. corev1 "k8s.io/api/core/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. fakesm "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager/fake"
  28. "github.com/external-secrets/external-secrets/pkg/provider/aws/util"
  29. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  30. )
  31. type secretsManagerTestCase struct {
  32. fakeClient *fakesm.Client
  33. apiInput *awssm.GetSecretValueInput
  34. apiOutput *awssm.GetSecretValueOutput
  35. remoteRef *esv1beta1.ExternalSecretDataRemoteRef
  36. apiErr error
  37. expectError string
  38. expectedSecret string
  39. // for testing secretmap
  40. expectedData map[string][]byte
  41. // for testing caching
  42. expectedCounter *int
  43. }
  44. const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
  45. const (
  46. tagname1 = "tagname1"
  47. tagvalue1 = "tagvalue1"
  48. tagname2 = "tagname2"
  49. tagvalue2 = "tagvalue2"
  50. )
  51. func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
  52. smtc := secretsManagerTestCase{
  53. fakeClient: fakesm.NewClient(),
  54. apiInput: makeValidAPIInput(),
  55. remoteRef: makeValidRemoteRef(),
  56. apiOutput: makeValidAPIOutput(),
  57. apiErr: nil,
  58. expectError: "",
  59. expectedSecret: "",
  60. expectedData: map[string][]byte{},
  61. }
  62. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  63. return &smtc
  64. }
  65. func makeValidRemoteRef() *esv1beta1.ExternalSecretDataRemoteRef {
  66. return &esv1beta1.ExternalSecretDataRemoteRef{
  67. Key: "/baz",
  68. Version: "AWSCURRENT",
  69. }
  70. }
  71. func makeValidAPIInput() *awssm.GetSecretValueInput {
  72. return &awssm.GetSecretValueInput{
  73. SecretId: aws.String("/baz"),
  74. VersionStage: aws.String("AWSCURRENT"),
  75. }
  76. }
  77. func makeValidAPIOutput() *awssm.GetSecretValueOutput {
  78. return &awssm.GetSecretValueOutput{
  79. SecretString: aws.String(""),
  80. }
  81. }
  82. func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
  83. smtc := makeValidSecretsManagerTestCase()
  84. for _, fn := range tweaks {
  85. fn(smtc)
  86. }
  87. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  88. return smtc
  89. }
  90. // This case can be shared by both GetSecret and GetSecretMap tests.
  91. // bad case: set apiErr.
  92. var setAPIErr = func(smtc *secretsManagerTestCase) {
  93. smtc.apiErr = fmt.Errorf("oh no")
  94. smtc.expectError = "oh no"
  95. }
  96. // test the sm<->aws interface
  97. // make sure correct values are passed and errors are handled accordingly.
  98. func TestSecretsManagerGetSecret(t *testing.T) {
  99. // good case: default version is set
  100. // key is passed in, output is sent back
  101. setSecretString := func(smtc *secretsManagerTestCase) {
  102. smtc.apiOutput.SecretString = aws.String("testtesttest")
  103. smtc.expectedSecret = "testtesttest"
  104. }
  105. // good case: extract property
  106. // Testing that the property exists in the SecretString
  107. setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
  108. smtc.remoteRef.Property = "/shmoo"
  109. smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
  110. smtc.expectedSecret = "bang"
  111. }
  112. // bad case: missing property
  113. setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
  114. smtc.remoteRef.Property = "INVALPROP"
  115. smtc.expectError = "key INVALPROP does not exist in secret"
  116. }
  117. // bad case: extract property failure due to invalid json
  118. setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
  119. smtc.remoteRef.Property = "INVALPROP"
  120. smtc.apiOutput.SecretString = aws.String(`------`)
  121. smtc.expectError = "key INVALPROP does not exist in secret"
  122. }
  123. // good case: set .SecretString to nil but set binary with value
  124. setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
  125. smtc.apiOutput.SecretBinary = []byte("yesplease")
  126. // needs to be set as nil, empty quotes ("") is considered existing
  127. smtc.apiOutput.SecretString = nil
  128. smtc.expectedSecret = "yesplease"
  129. }
  130. // bad case: both .SecretString and .SecretBinary are nil
  131. setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
  132. smtc.apiOutput.SecretBinary = nil
  133. smtc.apiOutput.SecretString = nil
  134. smtc.expectError = "no secret string nor binary for key"
  135. }
  136. // good case: secretOut.SecretBinary JSON parsing
  137. setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
  138. smtc.apiOutput.SecretString = nil
  139. smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
  140. smtc.remoteRef.Property = "foobar.baz"
  141. smtc.expectedSecret = "nestedval"
  142. }
  143. // good case: secretOut.SecretBinary no JSON parsing if name on key
  144. setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
  145. smtc.apiOutput.SecretString = nil
  146. smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
  147. smtc.remoteRef.Property = "foobar.baz"
  148. smtc.expectedSecret = "nestedval"
  149. }
  150. // good case: custom version stage set
  151. setCustomVersionStage := func(smtc *secretsManagerTestCase) {
  152. smtc.apiInput.VersionStage = aws.String("1234")
  153. smtc.remoteRef.Version = "1234"
  154. smtc.apiOutput.SecretString = aws.String("FOOBA!")
  155. smtc.expectedSecret = "FOOBA!"
  156. }
  157. // good case: custom version id set
  158. setCustomVersionID := func(smtc *secretsManagerTestCase) {
  159. smtc.apiInput.VersionStage = nil
  160. smtc.apiInput.VersionId = aws.String("1234-5678")
  161. smtc.remoteRef.Version = "uuid/1234-5678"
  162. smtc.apiOutput.SecretString = aws.String("myvalue")
  163. smtc.expectedSecret = "myvalue"
  164. }
  165. fetchMetadata := func(smtc *secretsManagerTestCase) {
  166. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  167. describeSecretOutput := &awssm.DescribeSecretOutput{
  168. Tags: getTagSlice(),
  169. }
  170. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  171. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  172. smtc.apiOutput.SecretString = &jsonTags
  173. smtc.expectedSecret = jsonTags
  174. }
  175. fetchMetadataProperty := func(smtc *secretsManagerTestCase) {
  176. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  177. describeSecretOutput := &awssm.DescribeSecretOutput{
  178. Tags: getTagSlice(),
  179. }
  180. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  181. smtc.remoteRef.Property = tagname2
  182. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  183. smtc.apiOutput.SecretString = &jsonTags
  184. smtc.expectedSecret = tagvalue2
  185. }
  186. failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
  187. smtc.remoteRef.MetadataPolicy = esv1beta1.ExternalSecretMetadataPolicyFetch
  188. describeSecretOutput := &awssm.DescribeSecretOutput{
  189. Tags: getTagSlice(),
  190. }
  191. smtc.fakeClient.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(describeSecretOutput, nil)
  192. smtc.remoteRef.Property = "fail"
  193. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  194. smtc.apiOutput.SecretString = &jsonTags
  195. smtc.expectError = "key fail does not exist in secret /baz"
  196. }
  197. successCases := []*secretsManagerTestCase{
  198. makeValidSecretsManagerTestCase(),
  199. makeValidSecretsManagerTestCaseCustom(setSecretString),
  200. makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
  201. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
  202. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
  203. makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
  204. makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
  205. makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
  206. makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
  207. makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
  208. makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
  209. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  210. makeValidSecretsManagerTestCaseCustom(fetchMetadata),
  211. makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
  212. makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
  213. }
  214. for k, v := range successCases {
  215. sm := SecretsManager{
  216. cache: make(map[string]*awssm.GetSecretValueOutput),
  217. client: v.fakeClient,
  218. }
  219. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  220. if !ErrorContains(err, v.expectError) {
  221. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  222. }
  223. if err == nil && string(out) != v.expectedSecret {
  224. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  225. }
  226. }
  227. }
  228. func TestCaching(t *testing.T) {
  229. fakeClient := fakesm.NewClient()
  230. // good case: first call, since we are using the same key, results should be cached and the counter should not go
  231. // over 1
  232. firstCall := func(smtc *secretsManagerTestCase) {
  233. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  234. smtc.remoteRef.Property = "foo"
  235. smtc.expectedSecret = "bar"
  236. smtc.expectedCounter = aws.Int(1)
  237. smtc.fakeClient = fakeClient
  238. }
  239. secondCall := func(smtc *secretsManagerTestCase) {
  240. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  241. smtc.remoteRef.Property = "bar"
  242. smtc.expectedSecret = "vodka"
  243. smtc.expectedCounter = aws.Int(1)
  244. smtc.fakeClient = fakeClient
  245. }
  246. notCachedCall := func(smtc *secretsManagerTestCase) {
  247. smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
  248. smtc.remoteRef.Property = "sheldon"
  249. smtc.expectedSecret = "bazinga"
  250. smtc.expectedCounter = aws.Int(2)
  251. smtc.fakeClient = fakeClient
  252. smtc.apiInput.SecretId = aws.String("xyz")
  253. smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
  254. }
  255. cachedCases := []*secretsManagerTestCase{
  256. makeValidSecretsManagerTestCaseCustom(firstCall),
  257. makeValidSecretsManagerTestCaseCustom(firstCall),
  258. makeValidSecretsManagerTestCaseCustom(secondCall),
  259. makeValidSecretsManagerTestCaseCustom(notCachedCall),
  260. }
  261. sm := SecretsManager{
  262. cache: make(map[string]*awssm.GetSecretValueOutput),
  263. }
  264. for k, v := range cachedCases {
  265. sm.client = v.fakeClient
  266. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  267. if !ErrorContains(err, v.expectError) {
  268. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  269. }
  270. if err == nil && string(out) != v.expectedSecret {
  271. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  272. }
  273. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  274. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  275. }
  276. }
  277. }
  278. func TestGetSecretMap(t *testing.T) {
  279. // good case: default version & deserialization
  280. setDeserialization := func(smtc *secretsManagerTestCase) {
  281. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
  282. smtc.expectedData["foo"] = []byte("bar")
  283. }
  284. // good case: nested json
  285. setNestedJSON := func(smtc *secretsManagerTestCase) {
  286. smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
  287. smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
  288. }
  289. // good case: caching
  290. cachedMap := func(smtc *secretsManagerTestCase) {
  291. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
  292. smtc.expectedData["foo"] = []byte("bar")
  293. smtc.expectedData["plus"] = []byte("one")
  294. smtc.expectedCounter = aws.Int(1)
  295. }
  296. // bad case: invalid json
  297. setInvalidJSON := func(smtc *secretsManagerTestCase) {
  298. smtc.apiOutput.SecretString = aws.String(`-----------------`)
  299. smtc.expectError = "unable to unmarshal secret"
  300. }
  301. successCases := []*secretsManagerTestCase{
  302. makeValidSecretsManagerTestCaseCustom(setDeserialization),
  303. makeValidSecretsManagerTestCaseCustom(setNestedJSON),
  304. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  305. makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
  306. makeValidSecretsManagerTestCaseCustom(cachedMap),
  307. }
  308. for k, v := range successCases {
  309. sm := SecretsManager{
  310. cache: make(map[string]*awssm.GetSecretValueOutput),
  311. client: v.fakeClient,
  312. }
  313. out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
  314. if !ErrorContains(err, v.expectError) {
  315. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  316. }
  317. if err == nil && !cmp.Equal(out, v.expectedData) {
  318. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  319. }
  320. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  321. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  322. }
  323. }
  324. }
  325. func ErrorContains(out error, want string) bool {
  326. if out == nil {
  327. return want == ""
  328. }
  329. if want == "" {
  330. return false
  331. }
  332. return strings.Contains(out.Error(), want)
  333. }
  334. func TestSetSecret(t *testing.T) {
  335. managedBy := managedBy
  336. notManagedBy := "not-managed-by"
  337. secretKey := "fake-secret-key"
  338. secretValue := []byte("fake-value")
  339. fakeSecret := &corev1.Secret{
  340. Data: map[string][]byte{
  341. secretKey: secretValue,
  342. },
  343. }
  344. externalSecrets := externalSecrets
  345. noPermission := errors.New("no permission")
  346. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  347. getSecretCorrectErr := awssm.ResourceNotFoundException{}
  348. getSecretWrongErr := awssm.InvalidRequestException{}
  349. secretOutput := &awssm.CreateSecretOutput{
  350. ARN: &arn,
  351. }
  352. externalSecretsTag := []*awssm.Tag{
  353. {
  354. Key: &managedBy,
  355. Value: &externalSecrets,
  356. },
  357. }
  358. externalSecretsTagFaulty := []*awssm.Tag{
  359. {
  360. Key: &notManagedBy,
  361. Value: &externalSecrets,
  362. },
  363. }
  364. tagSecretOutput := &awssm.DescribeSecretOutput{
  365. ARN: &arn,
  366. Tags: externalSecretsTag,
  367. }
  368. tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
  369. ARN: &arn,
  370. Tags: externalSecretsTagFaulty,
  371. }
  372. initialVersion := "00000000-0000-0000-0000-000000000001"
  373. defaultVersion := "00000000-0000-0000-0000-000000000002"
  374. defaultUpdatedVersion := "00000000-0000-0000-0000-000000000003"
  375. randomUUIDVersion := "c2812e8d-84ce-4858-abec-7163d8ab312b"
  376. randomUUIDVersionIncremented := "c2812e8d-84ce-4858-abec-7163d8ab312c"
  377. unparsableVersion := "IAM UNPARSABLE"
  378. secretValueOutput := &awssm.GetSecretValueOutput{
  379. ARN: &arn,
  380. VersionId: &defaultVersion,
  381. }
  382. secretValueOutput2 := &awssm.GetSecretValueOutput{
  383. ARN: &arn,
  384. SecretBinary: secretValue,
  385. VersionId: &defaultVersion,
  386. }
  387. type params struct {
  388. s string
  389. b []byte
  390. version *string
  391. }
  392. secretValueOutputFrom := func(params params) *awssm.GetSecretValueOutput {
  393. var version *string
  394. if params.version == nil {
  395. version = &defaultVersion
  396. } else {
  397. version = params.version
  398. }
  399. return &awssm.GetSecretValueOutput{
  400. ARN: &arn,
  401. SecretString: &params.s,
  402. SecretBinary: params.b,
  403. VersionId: version,
  404. }
  405. }
  406. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  407. putSecretOutput := &awssm.PutSecretValueOutput{
  408. ARN: &arn,
  409. }
  410. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: "fake-key", Property: ""}
  411. pushSecretDataWithProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: "fake-key", Property: "other-fake-property"}
  412. type args struct {
  413. store *esv1beta1.AWSProvider
  414. client fakesm.Client
  415. pushSecretData fake.PushSecretData
  416. }
  417. type want struct {
  418. err error
  419. }
  420. tests := map[string]struct {
  421. reason string
  422. args args
  423. want want
  424. }{
  425. "SetSecretSucceedsWithExistingSecret": {
  426. reason: "a secret can be pushed to aws secrets manager when it already exists",
  427. args: args{
  428. store: makeValidSecretStore().Spec.Provider.AWS,
  429. client: fakesm.Client{
  430. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  431. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  432. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil),
  433. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  434. },
  435. pushSecretData: pushSecretDataWithoutProperty,
  436. },
  437. want: want{
  438. err: nil,
  439. },
  440. },
  441. "SetSecretSucceedsWithNewSecret": {
  442. reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
  443. args: args{
  444. store: makeValidSecretStore().Spec.Provider.AWS,
  445. client: fakesm.Client{
  446. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  447. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil),
  448. },
  449. pushSecretData: pushSecretDataWithoutProperty,
  450. },
  451. want: want{
  452. err: nil,
  453. },
  454. },
  455. "SetSecretWithPropertySucceedsWithNewSecret": {
  456. 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",
  457. args: args{
  458. store: makeValidSecretStore().Spec.Provider.AWS,
  459. client: fakesm.Client{
  460. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  461. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(secretOutput, nil, []byte(`{"other-fake-property":"fake-value"}`)),
  462. },
  463. pushSecretData: pushSecretDataWithProperty,
  464. },
  465. want: want{
  466. err: nil,
  467. },
  468. },
  469. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyBinary": {
  470. 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)",
  471. args: args{
  472. store: makeValidSecretStore().Spec.Provider.AWS,
  473. client: fakesm.Client{
  474. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{b: []byte((`{"fake-property":"fake-value"}`))}), nil),
  475. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  476. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  477. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  478. Version: &defaultUpdatedVersion,
  479. }),
  480. },
  481. pushSecretData: pushSecretDataWithProperty,
  482. },
  483. want: want{
  484. err: nil,
  485. },
  486. },
  487. "SetSecretWithPropertySucceedsWithExistingSecretAndRandomUUIDVersion": {
  488. 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",
  489. args: args{
  490. store: makeValidSecretStore().Spec.Provider.AWS,
  491. client: fakesm.Client{
  492. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{
  493. b: []byte((`{"fake-property":"fake-value"}`)),
  494. version: &randomUUIDVersion,
  495. }), nil),
  496. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  497. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  498. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  499. Version: &randomUUIDVersionIncremented,
  500. }),
  501. },
  502. pushSecretData: pushSecretDataWithProperty,
  503. },
  504. want: want{
  505. err: nil,
  506. },
  507. },
  508. "SetSecretWithPropertySucceedsWithExistingSecretAndVersionThatCantBeParsed": {
  509. reason: "A manually set secret version doesn't have to be a UUID, we only support UUID secret versions though",
  510. args: args{
  511. store: makeValidSecretStore().Spec.Provider.AWS,
  512. client: fakesm.Client{
  513. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{
  514. b: []byte((`{"fake-property":"fake-value"}`)),
  515. version: &unparsableVersion,
  516. }), nil),
  517. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  518. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  519. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  520. Version: &initialVersion,
  521. }),
  522. },
  523. pushSecretData: pushSecretDataWithProperty,
  524. },
  525. want: want{
  526. err: fmt.Errorf("expected secret version in AWS SSM to be a UUID but got '%s'", unparsableVersion),
  527. },
  528. },
  529. "SetSecretWithPropertySucceedsWithExistingSecretAndAbsentVersion": {
  530. reason: "When a secret version is not specified, set it to 1",
  531. args: args{
  532. store: makeValidSecretStore().Spec.Provider.AWS,
  533. client: fakesm.Client{
  534. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(&awssm.GetSecretValueOutput{
  535. ARN: &arn,
  536. SecretBinary: []byte((`{"fake-property":"fake-value"}`)),
  537. }, nil),
  538. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  539. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  540. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  541. Version: &initialVersion,
  542. }),
  543. },
  544. pushSecretData: pushSecretDataWithProperty,
  545. },
  546. want: want{
  547. err: nil,
  548. },
  549. },
  550. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyString": {
  551. 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)",
  552. args: args{
  553. store: makeValidSecretStore().Spec.Provider.AWS,
  554. client: fakesm.Client{
  555. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{s: `{"fake-property":"fake-value"}`}), nil),
  556. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  557. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  558. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  559. Version: &defaultUpdatedVersion,
  560. }),
  561. },
  562. pushSecretData: pushSecretDataWithProperty,
  563. },
  564. want: want{
  565. err: nil,
  566. },
  567. },
  568. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyWithDot": {
  569. 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)",
  570. args: args{
  571. store: makeValidSecretStore().Spec.Provider.AWS,
  572. client: fakesm.Client{
  573. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  574. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  575. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  576. SecretBinary: []byte(`{"fake-property":{"fake-property":"fake-value","other-fake-property":"fake-value"}}`),
  577. Version: &defaultUpdatedVersion,
  578. }),
  579. },
  580. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: "fake-key", Property: "fake-property.other-fake-property"},
  581. },
  582. want: want{
  583. err: nil,
  584. },
  585. },
  586. "SetSecretWithPropertyFailsExistingNonJsonSecret": {
  587. reason: "setting a pushSecretData property is only supported for json secrets",
  588. args: args{
  589. store: makeValidSecretStore().Spec.Provider.AWS,
  590. client: fakesm.Client{
  591. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutputFrom(params{s: `non-json-secret`}), nil),
  592. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  593. },
  594. pushSecretData: pushSecretDataWithProperty,
  595. },
  596. want: want{
  597. err: errors.New("PushSecret for aws secrets manager with a pushSecretData property requires a json secret"),
  598. },
  599. },
  600. "SetSecretCreateSecretFails": {
  601. reason: "CreateSecretWithContext returns an error if it fails",
  602. args: args{
  603. store: makeValidSecretStore().Spec.Provider.AWS,
  604. client: fakesm.Client{
  605. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretCorrectErr),
  606. CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(nil, noPermission),
  607. },
  608. pushSecretData: pushSecretDataWithoutProperty,
  609. },
  610. want: want{
  611. err: noPermission,
  612. },
  613. },
  614. "SetSecretGetSecretFails": {
  615. reason: "GetSecretValueWithContext returns an error if it fails",
  616. args: args{
  617. store: makeValidSecretStore().Spec.Provider.AWS,
  618. client: fakesm.Client{
  619. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, noPermission),
  620. },
  621. pushSecretData: pushSecretDataWithoutProperty,
  622. },
  623. want: want{
  624. err: noPermission,
  625. },
  626. },
  627. "SetSecretWillNotPushSameSecret": {
  628. reason: "secret with the same value will not be pushed",
  629. args: args{
  630. store: makeValidSecretStore().Spec.Provider.AWS,
  631. client: fakesm.Client{
  632. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput2, nil),
  633. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  634. },
  635. pushSecretData: pushSecretDataWithoutProperty,
  636. },
  637. want: want{
  638. err: nil,
  639. },
  640. },
  641. "SetSecretPutSecretValueFails": {
  642. reason: "PutSecretValueWithContext returns an error if it fails",
  643. args: args{
  644. store: makeValidSecretStore().Spec.Provider.AWS,
  645. client: fakesm.Client{
  646. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  647. PutSecretValueWithContextFn: fakesm.NewPutSecretValueWithContextFn(nil, noPermission),
  648. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutput, nil),
  649. },
  650. pushSecretData: pushSecretDataWithoutProperty,
  651. },
  652. want: want{
  653. err: noPermission,
  654. },
  655. },
  656. "SetSecretWrongGetSecretErrFails": {
  657. reason: "GetSecretValueWithContext errors out when anything except awssm.ErrCodeResourceNotFoundException",
  658. args: args{
  659. store: makeValidSecretStore().Spec.Provider.AWS,
  660. client: fakesm.Client{
  661. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(blankSecretValueOutput, &getSecretWrongErr),
  662. },
  663. pushSecretData: pushSecretDataWithoutProperty,
  664. },
  665. want: want{
  666. err: &getSecretWrongErr,
  667. },
  668. },
  669. "SetSecretDescribeSecretFails": {
  670. reason: "secret cannot be described",
  671. args: args{
  672. store: makeValidSecretStore().Spec.Provider.AWS,
  673. client: fakesm.Client{
  674. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  675. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(nil, noPermission),
  676. },
  677. pushSecretData: pushSecretDataWithoutProperty,
  678. },
  679. want: want{
  680. err: noPermission,
  681. },
  682. },
  683. "SetSecretDoesNotOverwriteUntaggedSecret": {
  684. reason: "secret cannot be described",
  685. args: args{
  686. store: makeValidSecretStore().Spec.Provider.AWS,
  687. client: fakesm.Client{
  688. GetSecretValueWithContextFn: fakesm.NewGetSecretValueWithContextFn(secretValueOutput, nil),
  689. DescribeSecretWithContextFn: fakesm.NewDescribeSecretWithContextFn(tagSecretOutputFaulty, nil),
  690. },
  691. pushSecretData: pushSecretDataWithoutProperty,
  692. },
  693. want: want{
  694. err: fmt.Errorf("secret not managed by external-secrets"),
  695. },
  696. },
  697. }
  698. for name, tc := range tests {
  699. t.Run(name, func(t *testing.T) {
  700. sm := SecretsManager{
  701. client: &tc.args.client,
  702. }
  703. err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
  704. // Error nil XOR tc.want.err nil
  705. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  706. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  707. }
  708. // if errors are the same type but their contents do not match
  709. if err != nil && tc.want.err != nil {
  710. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  711. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  712. }
  713. }
  714. })
  715. }
  716. }
  717. func TestDeleteSecret(t *testing.T) {
  718. fakeClient := fakesm.Client{}
  719. managed := managedBy
  720. manager := externalSecrets
  721. secretTag := awssm.Tag{
  722. Key: &managed,
  723. Value: &manager,
  724. }
  725. type args struct {
  726. client fakesm.Client
  727. config esv1beta1.SecretsManager
  728. getSecretOutput *awssm.GetSecretValueOutput
  729. describeSecretOutput *awssm.DescribeSecretOutput
  730. deleteSecretOutput *awssm.DeleteSecretOutput
  731. getSecretErr error
  732. describeSecretErr error
  733. deleteSecretErr error
  734. }
  735. type want struct {
  736. err error
  737. }
  738. type testCase struct {
  739. args args
  740. want want
  741. reason string
  742. }
  743. tests := map[string]testCase{
  744. "Deletes Successfully": {
  745. args: args{
  746. client: fakeClient,
  747. config: esv1beta1.SecretsManager{},
  748. getSecretOutput: &awssm.GetSecretValueOutput{},
  749. describeSecretOutput: &awssm.DescribeSecretOutput{
  750. Tags: []*awssm.Tag{&secretTag},
  751. },
  752. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  753. getSecretErr: nil,
  754. describeSecretErr: nil,
  755. deleteSecretErr: nil,
  756. },
  757. want: want{
  758. err: nil,
  759. },
  760. reason: "",
  761. },
  762. "Deletes Successfully with ForceDeleteWithoutRecovery": {
  763. args: args{
  764. client: fakeClient,
  765. config: esv1beta1.SecretsManager{
  766. ForceDeleteWithoutRecovery: true,
  767. },
  768. getSecretOutput: &awssm.GetSecretValueOutput{},
  769. describeSecretOutput: &awssm.DescribeSecretOutput{
  770. Tags: []*awssm.Tag{&secretTag},
  771. },
  772. deleteSecretOutput: &awssm.DeleteSecretOutput{
  773. DeletionDate: aws.Time(time.Now()),
  774. },
  775. getSecretErr: nil,
  776. describeSecretErr: nil,
  777. deleteSecretErr: nil,
  778. },
  779. want: want{
  780. err: nil,
  781. },
  782. reason: "",
  783. },
  784. "Not Managed by ESO": {
  785. args: args{
  786. client: fakeClient,
  787. config: esv1beta1.SecretsManager{},
  788. getSecretOutput: &awssm.GetSecretValueOutput{},
  789. describeSecretOutput: &awssm.DescribeSecretOutput{
  790. Tags: []*awssm.Tag{},
  791. },
  792. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  793. getSecretErr: nil,
  794. describeSecretErr: nil,
  795. deleteSecretErr: nil,
  796. },
  797. want: want{
  798. err: nil,
  799. },
  800. reason: "",
  801. },
  802. "Invalid Recovery Window": {
  803. args: args{
  804. client: fakesm.Client{},
  805. config: esv1beta1.SecretsManager{
  806. RecoveryWindowInDays: 1,
  807. },
  808. getSecretOutput: &awssm.GetSecretValueOutput{},
  809. describeSecretOutput: &awssm.DescribeSecretOutput{
  810. Tags: []*awssm.Tag{&secretTag},
  811. },
  812. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  813. getSecretErr: nil,
  814. describeSecretErr: nil,
  815. deleteSecretErr: nil,
  816. },
  817. want: want{
  818. err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
  819. },
  820. reason: "",
  821. },
  822. "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
  823. args: args{
  824. client: fakesm.Client{},
  825. config: esv1beta1.SecretsManager{
  826. RecoveryWindowInDays: 7,
  827. ForceDeleteWithoutRecovery: true,
  828. },
  829. getSecretOutput: &awssm.GetSecretValueOutput{},
  830. describeSecretOutput: &awssm.DescribeSecretOutput{
  831. Tags: []*awssm.Tag{&secretTag},
  832. },
  833. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  834. getSecretErr: nil,
  835. describeSecretErr: nil,
  836. deleteSecretErr: nil,
  837. },
  838. want: want{
  839. err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
  840. },
  841. reason: "",
  842. },
  843. "Failed to get Tags": {
  844. args: args{
  845. client: fakeClient,
  846. config: esv1beta1.SecretsManager{},
  847. getSecretOutput: &awssm.GetSecretValueOutput{},
  848. describeSecretOutput: nil,
  849. deleteSecretOutput: nil,
  850. getSecretErr: nil,
  851. describeSecretErr: errors.New("failed to get tags"),
  852. deleteSecretErr: nil,
  853. },
  854. want: want{
  855. err: errors.New("failed to get tags"),
  856. },
  857. reason: "",
  858. },
  859. "Secret Not Found": {
  860. args: args{
  861. client: fakeClient,
  862. config: esv1beta1.SecretsManager{},
  863. getSecretOutput: nil,
  864. describeSecretOutput: nil,
  865. deleteSecretOutput: nil,
  866. getSecretErr: awserr.New(awssm.ErrCodeResourceNotFoundException, "not here, sorry dude", nil),
  867. describeSecretErr: nil,
  868. deleteSecretErr: nil,
  869. },
  870. want: want{
  871. err: nil,
  872. },
  873. },
  874. "Not expected AWS error": {
  875. args: args{
  876. client: fakeClient,
  877. config: esv1beta1.SecretsManager{},
  878. getSecretOutput: nil,
  879. describeSecretOutput: nil,
  880. deleteSecretOutput: nil,
  881. getSecretErr: awserr.New(awssm.ErrCodeEncryptionFailure, "aws unavailable", nil),
  882. describeSecretErr: nil,
  883. deleteSecretErr: nil,
  884. },
  885. want: want{
  886. err: errors.New("aws unavailable"),
  887. },
  888. },
  889. "unexpected error": {
  890. args: args{
  891. client: fakeClient,
  892. config: esv1beta1.SecretsManager{},
  893. getSecretOutput: nil,
  894. describeSecretOutput: nil,
  895. deleteSecretOutput: nil,
  896. getSecretErr: errors.New("timeout"),
  897. describeSecretErr: nil,
  898. deleteSecretErr: nil,
  899. },
  900. want: want{
  901. err: errors.New("timeout"),
  902. },
  903. },
  904. }
  905. for name, tc := range tests {
  906. t.Run(name, func(t *testing.T) {
  907. ref := fake.PushSecretData{RemoteKey: "fake-key"}
  908. sm := SecretsManager{
  909. client: &tc.args.client,
  910. config: &tc.args.config,
  911. }
  912. tc.args.client.GetSecretValueWithContextFn = fakesm.NewGetSecretValueWithContextFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  913. tc.args.client.DescribeSecretWithContextFn = fakesm.NewDescribeSecretWithContextFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  914. tc.args.client.DeleteSecretWithContextFn = fakesm.NewDeleteSecretWithContextFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  915. err := sm.DeleteSecret(context.TODO(), ref)
  916. t.Logf("DeleteSecret error: %v", err)
  917. // Error nil XOR tc.want.err nil
  918. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  919. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  920. }
  921. // if errors are the same type but their contents do not match
  922. if err != nil && tc.want.err != nil {
  923. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  924. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  925. }
  926. }
  927. })
  928. }
  929. }
  930. func makeValidSecretStore() *esv1beta1.SecretStore {
  931. return &esv1beta1.SecretStore{
  932. ObjectMeta: metav1.ObjectMeta{
  933. Name: "aws-secret-store",
  934. Namespace: "default",
  935. },
  936. Spec: esv1beta1.SecretStoreSpec{
  937. Provider: &esv1beta1.SecretStoreProvider{
  938. AWS: &esv1beta1.AWSProvider{
  939. Service: esv1beta1.AWSServiceSecretsManager,
  940. Region: "eu-west-2",
  941. },
  942. },
  943. },
  944. }
  945. }
  946. func getTagSlice() []*awssm.Tag {
  947. tagKey1 := tagname1
  948. tagValue1 := tagvalue1
  949. tagKey2 := tagname2
  950. tagValue2 := tagvalue2
  951. return []*awssm.Tag{
  952. {
  953. Key: &tagKey1,
  954. Value: &tagValue1,
  955. },
  956. {
  957. Key: &tagKey2,
  958. Value: &tagValue2,
  959. },
  960. }
  961. }