secretsmanager_test.go 33 KB

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