secretsmanager_test.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package secretsmanager
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/aws/aws-sdk-go-v2/aws"
  22. "github.com/aws/aws-sdk-go-v2/credentials"
  23. awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
  24. "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
  25. "github.com/google/go-cmp/cmp"
  26. "github.com/stretchr/testify/assert"
  27. corev1 "k8s.io/api/core/v1"
  28. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  29. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  30. "k8s.io/utils/ptr"
  31. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  32. fakesm "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager/fake"
  33. "github.com/external-secrets/external-secrets/pkg/provider/aws/util"
  34. "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  35. )
  36. type secretsManagerTestCase struct {
  37. fakeClient *fakesm.Client
  38. apiInput *awssm.GetSecretValueInput
  39. apiOutput *awssm.GetSecretValueOutput
  40. remoteRef *esv1.ExternalSecretDataRemoteRef
  41. apiErr error
  42. expectError string
  43. expectedSecret string
  44. // for testing secretmap
  45. expectedData map[string][]byte
  46. // for testing caching
  47. expectedCounter *int
  48. prefix string
  49. }
  50. const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
  51. const (
  52. tagname1 = "tagname1"
  53. tagvalue1 = "tagvalue1"
  54. tagname2 = "tagname2"
  55. tagvalue2 = "tagvalue2"
  56. fakeKey = "fake-key"
  57. )
  58. func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
  59. smtc := secretsManagerTestCase{
  60. fakeClient: fakesm.NewClient(),
  61. apiInput: makeValidAPIInput(),
  62. remoteRef: makeValidRemoteRef(),
  63. apiOutput: makeValidAPIOutput(),
  64. apiErr: nil,
  65. expectError: "",
  66. expectedSecret: "",
  67. expectedData: map[string][]byte{},
  68. }
  69. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  70. return &smtc
  71. }
  72. func makeValidRemoteRef() *esv1.ExternalSecretDataRemoteRef {
  73. return &esv1.ExternalSecretDataRemoteRef{
  74. Key: "/baz",
  75. Version: "AWSCURRENT",
  76. }
  77. }
  78. func makeValidAPIInput() *awssm.GetSecretValueInput {
  79. return &awssm.GetSecretValueInput{
  80. SecretId: aws.String("/baz"),
  81. VersionStage: aws.String("AWSCURRENT"),
  82. }
  83. }
  84. func makeValidAPIOutput() *awssm.GetSecretValueOutput {
  85. return &awssm.GetSecretValueOutput{
  86. SecretString: aws.String(""),
  87. }
  88. }
  89. func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
  90. smtc := makeValidSecretsManagerTestCase()
  91. for _, fn := range tweaks {
  92. fn(smtc)
  93. }
  94. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  95. return smtc
  96. }
  97. // This case can be shared by both GetSecret and GetSecretMap tests.
  98. // bad case: set apiErr.
  99. var setAPIErr = func(smtc *secretsManagerTestCase) {
  100. smtc.apiErr = errors.New("oh no")
  101. smtc.expectError = "oh no"
  102. }
  103. func TestSecretsManagerResolver(t *testing.T) {
  104. endpointEnvKey := SecretsManagerEndpointEnv
  105. endpointURL := "http://sm.foo"
  106. t.Setenv(endpointEnvKey, endpointURL)
  107. f, err := customEndpointResolver{}.ResolveEndpoint(context.Background(), awssm.EndpointParameters{})
  108. assert.Nil(t, err)
  109. assert.Equal(t, endpointURL, f.URI.String())
  110. }
  111. // test the sm<->aws interface
  112. // make sure correct values are passed and errors are handled accordingly.
  113. func TestSecretsManagerGetSecret(t *testing.T) {
  114. // good case: default version is set
  115. // key is passed in, output is sent back
  116. setSecretString := func(smtc *secretsManagerTestCase) {
  117. smtc.apiOutput.SecretString = aws.String("testtesttest")
  118. smtc.expectedSecret = "testtesttest"
  119. }
  120. // good case: key is passed in with prefix
  121. setSecretStringWithPrefix := func(smtc *secretsManagerTestCase) {
  122. smtc.remoteRef.Key = "secret-key"
  123. smtc.apiInput = &awssm.GetSecretValueInput{
  124. SecretId: aws.String("my-prefix/secret-key"),
  125. VersionStage: aws.String("AWSCURRENT"),
  126. }
  127. smtc.prefix = "my-prefix/"
  128. }
  129. // good case: extract property
  130. // Testing that the property exists in the SecretString
  131. setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
  132. smtc.remoteRef.Property = "/shmoo"
  133. smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
  134. smtc.expectedSecret = "bang"
  135. }
  136. // bad case: missing property
  137. setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
  138. smtc.remoteRef.Property = "INVALPROP"
  139. smtc.expectError = "key INVALPROP does not exist in secret"
  140. }
  141. // bad case: extract property failure due to invalid json
  142. setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
  143. smtc.remoteRef.Property = "INVALPROP"
  144. smtc.apiOutput.SecretString = aws.String(`------`)
  145. smtc.expectError = "key INVALPROP does not exist in secret"
  146. }
  147. // good case: set .SecretString to nil but set binary with value
  148. setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
  149. smtc.apiOutput.SecretBinary = []byte("yesplease")
  150. // needs to be set as nil, empty quotes ("") is considered existing
  151. smtc.apiOutput.SecretString = nil
  152. smtc.expectedSecret = "yesplease"
  153. }
  154. // bad case: both .SecretString and .SecretBinary are nil
  155. setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
  156. smtc.apiOutput.SecretBinary = nil
  157. smtc.apiOutput.SecretString = nil
  158. smtc.expectError = "no secret string nor binary for key"
  159. }
  160. // good case: secretOut.SecretBinary JSON parsing
  161. setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
  162. smtc.apiOutput.SecretString = nil
  163. smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
  164. smtc.remoteRef.Property = "foobar.baz"
  165. smtc.expectedSecret = "nestedval"
  166. }
  167. // good case: secretOut.SecretBinary no JSON parsing if name on key
  168. setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
  169. smtc.apiOutput.SecretString = nil
  170. smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
  171. smtc.remoteRef.Property = "foobar.baz"
  172. smtc.expectedSecret = "nestedval"
  173. }
  174. // good case: custom version stage set
  175. setCustomVersionStage := func(smtc *secretsManagerTestCase) {
  176. smtc.apiInput.VersionStage = aws.String("1234")
  177. smtc.remoteRef.Version = "1234"
  178. smtc.apiOutput.SecretString = aws.String("FOOBA!")
  179. smtc.expectedSecret = "FOOBA!"
  180. }
  181. // good case: custom version id set
  182. setCustomVersionID := func(smtc *secretsManagerTestCase) {
  183. smtc.apiInput.VersionStage = nil
  184. smtc.apiInput.VersionId = aws.String("1234-5678")
  185. smtc.remoteRef.Version = "uuid/1234-5678"
  186. smtc.apiOutput.SecretString = aws.String("myvalue")
  187. smtc.expectedSecret = "myvalue"
  188. }
  189. fetchMetadata := func(smtc *secretsManagerTestCase) {
  190. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  191. describeSecretOutput := &awssm.DescribeSecretOutput{
  192. Tags: getTagSlice(),
  193. }
  194. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  195. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  196. smtc.apiOutput.SecretString = &jsonTags
  197. smtc.expectedSecret = jsonTags
  198. }
  199. fetchMetadataProperty := func(smtc *secretsManagerTestCase) {
  200. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  201. describeSecretOutput := &awssm.DescribeSecretOutput{
  202. Tags: getTagSlice(),
  203. }
  204. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  205. smtc.remoteRef.Property = tagname2
  206. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  207. smtc.apiOutput.SecretString = &jsonTags
  208. smtc.expectedSecret = tagvalue2
  209. }
  210. failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
  211. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  212. describeSecretOutput := &awssm.DescribeSecretOutput{
  213. Tags: getTagSlice(),
  214. }
  215. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  216. smtc.remoteRef.Property = "fail"
  217. jsonTags, _ := util.SecretTagsToJSONString(getTagSlice())
  218. smtc.apiOutput.SecretString = &jsonTags
  219. smtc.expectError = "key fail does not exist in secret /baz"
  220. }
  221. successCases := []*secretsManagerTestCase{
  222. makeValidSecretsManagerTestCase(),
  223. makeValidSecretsManagerTestCaseCustom(setSecretString),
  224. makeValidSecretsManagerTestCaseCustom(setSecretStringWithPrefix),
  225. makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
  226. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
  227. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
  228. makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
  229. makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
  230. makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
  231. makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
  232. makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
  233. makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
  234. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  235. makeValidSecretsManagerTestCaseCustom(fetchMetadata),
  236. makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
  237. makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
  238. }
  239. for k, v := range successCases {
  240. sm := SecretsManager{
  241. cache: make(map[string]*awssm.GetSecretValueOutput),
  242. client: v.fakeClient,
  243. prefix: v.prefix,
  244. }
  245. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  246. if !ErrorContains(err, v.expectError) {
  247. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  248. }
  249. if err == nil && string(out) != v.expectedSecret {
  250. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  251. }
  252. }
  253. }
  254. func TestCaching(t *testing.T) {
  255. fakeClient := fakesm.NewClient()
  256. // good case: first call, since we are using the same key, results should be cached and the counter should not go
  257. // over 1
  258. firstCall := func(smtc *secretsManagerTestCase) {
  259. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  260. smtc.remoteRef.Property = "foo"
  261. smtc.expectedSecret = "bar"
  262. smtc.expectedCounter = aws.Int(1)
  263. smtc.fakeClient = fakeClient
  264. }
  265. secondCall := func(smtc *secretsManagerTestCase) {
  266. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  267. smtc.remoteRef.Property = "bar"
  268. smtc.expectedSecret = "vodka"
  269. smtc.expectedCounter = aws.Int(1)
  270. smtc.fakeClient = fakeClient
  271. }
  272. notCachedCall := func(smtc *secretsManagerTestCase) {
  273. smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
  274. smtc.remoteRef.Property = "sheldon"
  275. smtc.expectedSecret = "bazinga"
  276. smtc.expectedCounter = aws.Int(2)
  277. smtc.fakeClient = fakeClient
  278. smtc.apiInput.SecretId = aws.String("xyz")
  279. smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
  280. }
  281. cachedCases := []*secretsManagerTestCase{
  282. makeValidSecretsManagerTestCaseCustom(firstCall),
  283. makeValidSecretsManagerTestCaseCustom(firstCall),
  284. makeValidSecretsManagerTestCaseCustom(secondCall),
  285. makeValidSecretsManagerTestCaseCustom(notCachedCall),
  286. }
  287. sm := SecretsManager{
  288. cache: make(map[string]*awssm.GetSecretValueOutput),
  289. }
  290. for k, v := range cachedCases {
  291. sm.client = v.fakeClient
  292. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  293. if !ErrorContains(err, v.expectError) {
  294. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  295. }
  296. if err == nil && string(out) != v.expectedSecret {
  297. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  298. }
  299. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  300. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  301. }
  302. }
  303. }
  304. func TestGetSecretMap(t *testing.T) {
  305. // good case: default version & deserialization
  306. setDeserialization := func(smtc *secretsManagerTestCase) {
  307. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
  308. smtc.expectedData["foo"] = []byte("bar")
  309. }
  310. // good case: nested json
  311. setNestedJSON := func(smtc *secretsManagerTestCase) {
  312. smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
  313. smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
  314. }
  315. // good case: caching
  316. cachedMap := func(smtc *secretsManagerTestCase) {
  317. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
  318. smtc.expectedData["foo"] = []byte("bar")
  319. smtc.expectedData["plus"] = []byte("one")
  320. smtc.expectedCounter = aws.Int(1)
  321. }
  322. // bad case: invalid json
  323. setInvalidJSON := func(smtc *secretsManagerTestCase) {
  324. smtc.apiOutput.SecretString = aws.String(`-----------------`)
  325. smtc.expectError = "unable to unmarshal secret"
  326. }
  327. successCases := []*secretsManagerTestCase{
  328. makeValidSecretsManagerTestCaseCustom(setDeserialization),
  329. makeValidSecretsManagerTestCaseCustom(setNestedJSON),
  330. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  331. makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
  332. makeValidSecretsManagerTestCaseCustom(cachedMap),
  333. }
  334. for k, v := range successCases {
  335. sm := SecretsManager{
  336. cache: make(map[string]*awssm.GetSecretValueOutput),
  337. client: v.fakeClient,
  338. }
  339. out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
  340. if !ErrorContains(err, v.expectError) {
  341. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  342. }
  343. if err == nil && !cmp.Equal(out, v.expectedData) {
  344. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  345. }
  346. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  347. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  348. }
  349. }
  350. }
  351. func ErrorContains(out error, want string) bool {
  352. if out == nil {
  353. return want == ""
  354. }
  355. if want == "" {
  356. return false
  357. }
  358. return strings.Contains(out.Error(), want)
  359. }
  360. func TestSetSecret(t *testing.T) {
  361. managedBy := managedBy
  362. notManagedBy := "not-managed-by"
  363. secretKey := "fake-secret-key"
  364. secretValue := []byte("fake-value")
  365. fakeSecret := &corev1.Secret{
  366. Data: map[string][]byte{
  367. secretKey: secretValue,
  368. },
  369. }
  370. externalSecrets := externalSecrets
  371. noPermission := errors.New("no permission")
  372. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  373. getSecretCorrectErr := types.ResourceNotFoundException{}
  374. getSecretWrongErr := types.InvalidRequestException{}
  375. secretOutput := &awssm.CreateSecretOutput{
  376. ARN: &arn,
  377. }
  378. externalSecretsTag := []types.Tag{
  379. {
  380. Key: &managedBy,
  381. Value: &externalSecrets,
  382. },
  383. {
  384. Key: ptr.To("taname1"),
  385. Value: ptr.To("tagvalue1"),
  386. },
  387. }
  388. externalSecretsTagFaulty := []types.Tag{
  389. {
  390. Key: &notManagedBy,
  391. Value: &externalSecrets,
  392. },
  393. }
  394. tagSecretOutput := &awssm.DescribeSecretOutput{
  395. ARN: &arn,
  396. Tags: externalSecretsTag,
  397. }
  398. tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
  399. ARN: &arn,
  400. Tags: externalSecretsTagFaulty,
  401. }
  402. initialVersion := "00000000-0000-0000-0000-000000000001"
  403. defaultVersion := "00000000-0000-0000-0000-000000000002"
  404. defaultUpdatedVersion := "00000000-0000-0000-0000-000000000003"
  405. randomUUIDVersion := "c2812e8d-84ce-4858-abec-7163d8ab312b"
  406. randomUUIDVersionIncremented := "c2812e8d-84ce-4858-abec-7163d8ab312c"
  407. unparsableVersion := "IAM UNPARSABLE"
  408. secretValueOutput := &awssm.GetSecretValueOutput{
  409. ARN: &arn,
  410. VersionId: &defaultVersion,
  411. }
  412. secretValueOutput2 := &awssm.GetSecretValueOutput{
  413. ARN: &arn,
  414. SecretBinary: secretValue,
  415. VersionId: &defaultVersion,
  416. }
  417. type params struct {
  418. s string
  419. b []byte
  420. version *string
  421. }
  422. secretValueOutputFrom := func(params params) *awssm.GetSecretValueOutput {
  423. var version *string
  424. if params.version == nil {
  425. version = &defaultVersion
  426. } else {
  427. version = params.version
  428. }
  429. return &awssm.GetSecretValueOutput{
  430. ARN: &arn,
  431. SecretString: &params.s,
  432. SecretBinary: params.b,
  433. VersionId: version,
  434. }
  435. }
  436. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  437. putSecretOutput := &awssm.PutSecretValueOutput{
  438. ARN: &arn,
  439. }
  440. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: ""}
  441. pushSecretDataWithoutSecretKey := fake.PushSecretData{RemoteKey: fakeKey, Property: ""}
  442. pushSecretDataWithMetadata := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  443. Raw: []byte(`{
  444. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  445. "kind": "PushSecretMetadata",
  446. "spec": {
  447. "secretPushFormat": "string"
  448. }
  449. }`)}}
  450. pushSecretDataWithProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "other-fake-property"}
  451. type args struct {
  452. store *esv1.AWSProvider
  453. client fakesm.Client
  454. pushSecretData fake.PushSecretData
  455. }
  456. type want struct {
  457. err error
  458. }
  459. tests := map[string]struct {
  460. reason string
  461. args args
  462. want want
  463. }{
  464. "SetSecretSucceedsWithExistingSecret": {
  465. reason: "a secret can be pushed to aws secrets manager when it already exists",
  466. args: args{
  467. store: makeValidSecretStore().Spec.Provider.AWS,
  468. client: fakesm.Client{
  469. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  470. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  471. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  472. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  473. },
  474. pushSecretData: pushSecretDataWithoutProperty,
  475. },
  476. want: want{
  477. err: nil,
  478. },
  479. },
  480. "SetSecretSucceedsWithoutSecretKey": {
  481. reason: "a secret can be pushed to aws secrets manager without secret key",
  482. args: args{
  483. store: makeValidSecretStore().Spec.Provider.AWS,
  484. client: fakesm.Client{
  485. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  486. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  487. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  488. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  489. },
  490. pushSecretData: pushSecretDataWithoutSecretKey,
  491. },
  492. want: want{
  493. err: nil,
  494. },
  495. },
  496. "SetSecretSucceedsWithExistingSecretAndStringFormat": {
  497. reason: "a secret can be pushed to aws secrets manager when it already exists",
  498. args: args{
  499. store: makeValidSecretStore().Spec.Provider.AWS,
  500. client: fakesm.Client{
  501. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  502. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  503. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  504. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  505. },
  506. pushSecretData: pushSecretDataWithMetadata,
  507. },
  508. want: want{
  509. err: nil,
  510. },
  511. },
  512. "SetSecretSucceedsWithExistingSecretAndKMSKeyAndDescription": {
  513. reason: "a secret can be pushed to aws secrets manager when it already exists",
  514. args: args{
  515. store: makeValidSecretStore().Spec.Provider.AWS,
  516. client: fakesm.Client{
  517. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, &getSecretCorrectErr),
  518. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  519. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  520. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  521. },
  522. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  523. Raw: []byte(`{
  524. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  525. "kind": "PushSecretMetadata",
  526. "spec": {
  527. "kmsKeyID": "bb123123-b2b0-4f60-ac3a-44a13f0e6b6c",
  528. "description": "this is a description"
  529. }
  530. }`)}},
  531. },
  532. want: want{
  533. err: nil,
  534. },
  535. },
  536. "SetSecretSucceedsWithExistingSecretAndAdditionalTags": {
  537. reason: "a secret can be pushed to aws secrets manager when it already exists",
  538. args: args{
  539. store: makeValidSecretStore().Spec.Provider.AWS,
  540. client: fakesm.Client{
  541. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  542. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  543. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  544. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  545. },
  546. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  547. Raw: []byte(`{
  548. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  549. "kind": "PushSecretMetadata",
  550. "spec": {
  551. "tags": {"tagname12": "tagvalue1"}
  552. }
  553. }`)}},
  554. },
  555. want: want{
  556. err: nil,
  557. },
  558. },
  559. "SetSecretSucceedsWithNewSecret": {
  560. reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
  561. args: args{
  562. store: makeValidSecretStore().Spec.Provider.AWS,
  563. client: fakesm.Client{
  564. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  565. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  566. },
  567. pushSecretData: pushSecretDataWithoutProperty,
  568. },
  569. want: want{
  570. err: nil,
  571. },
  572. },
  573. "SetSecretWithPropertySucceedsWithNewSecret": {
  574. 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",
  575. args: args{
  576. store: makeValidSecretStore().Spec.Provider.AWS,
  577. client: fakesm.Client{
  578. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  579. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil, []byte(`{"other-fake-property":"fake-value"}`)),
  580. },
  581. pushSecretData: pushSecretDataWithProperty,
  582. },
  583. want: want{
  584. err: nil,
  585. },
  586. },
  587. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyBinary": {
  588. 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)",
  589. args: args{
  590. store: makeValidSecretStore().Spec.Provider.AWS,
  591. client: fakesm.Client{
  592. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{b: []byte((`{"fake-property":"fake-value"}`))}), nil),
  593. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  594. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  595. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  596. Version: &defaultUpdatedVersion,
  597. }),
  598. },
  599. pushSecretData: pushSecretDataWithProperty,
  600. },
  601. want: want{
  602. err: nil,
  603. },
  604. },
  605. "SetSecretWithPropertySucceedsWithExistingSecretAndRandomUUIDVersion": {
  606. 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",
  607. args: args{
  608. store: makeValidSecretStore().Spec.Provider.AWS,
  609. client: fakesm.Client{
  610. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
  611. b: []byte((`{"fake-property":"fake-value"}`)),
  612. version: &randomUUIDVersion,
  613. }), nil),
  614. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  615. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  616. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  617. Version: &randomUUIDVersionIncremented,
  618. }),
  619. },
  620. pushSecretData: pushSecretDataWithProperty,
  621. },
  622. want: want{
  623. err: nil,
  624. },
  625. },
  626. "SetSecretWithPropertySucceedsWithExistingSecretAndVersionThatCantBeParsed": {
  627. reason: "A manually set secret version doesn't have to be a UUID, we only support UUID secret versions though",
  628. args: args{
  629. store: makeValidSecretStore().Spec.Provider.AWS,
  630. client: fakesm.Client{
  631. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
  632. b: []byte((`{"fake-property":"fake-value"}`)),
  633. version: &unparsableVersion,
  634. }), nil),
  635. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  636. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  637. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  638. Version: &initialVersion,
  639. }),
  640. },
  641. pushSecretData: pushSecretDataWithProperty,
  642. },
  643. want: want{
  644. err: fmt.Errorf("expected secret version in AWS SSM to be a UUID but got '%s'", unparsableVersion),
  645. },
  646. },
  647. "SetSecretWithPropertySucceedsWithExistingSecretAndAbsentVersion": {
  648. reason: "When a secret version is not specified, set it to 1",
  649. args: args{
  650. store: makeValidSecretStore().Spec.Provider.AWS,
  651. client: fakesm.Client{
  652. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  653. ARN: &arn,
  654. SecretBinary: []byte((`{"fake-property":"fake-value"}`)),
  655. }, nil),
  656. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  657. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  658. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  659. Version: &initialVersion,
  660. }),
  661. },
  662. pushSecretData: pushSecretDataWithProperty,
  663. },
  664. want: want{
  665. err: nil,
  666. },
  667. },
  668. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyString": {
  669. 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)",
  670. args: args{
  671. store: makeValidSecretStore().Spec.Provider.AWS,
  672. client: fakesm.Client{
  673. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":"fake-value"}`}), nil),
  674. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  675. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  676. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  677. Version: &defaultUpdatedVersion,
  678. }),
  679. },
  680. pushSecretData: pushSecretDataWithProperty,
  681. },
  682. want: want{
  683. err: nil,
  684. },
  685. },
  686. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyWithDot": {
  687. 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)",
  688. args: args{
  689. store: makeValidSecretStore().Spec.Provider.AWS,
  690. client: fakesm.Client{
  691. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  692. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  693. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  694. SecretBinary: []byte(`{"fake-property":{"fake-property":"fake-value","other-fake-property":"fake-value"}}`),
  695. Version: &defaultUpdatedVersion,
  696. }),
  697. },
  698. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "fake-property.other-fake-property"},
  699. },
  700. want: want{
  701. err: nil,
  702. },
  703. },
  704. "SetSecretWithPropertyFailsExistingNonJsonSecret": {
  705. reason: "setting a pushSecretData property is only supported for json secrets",
  706. args: args{
  707. store: makeValidSecretStore().Spec.Provider.AWS,
  708. client: fakesm.Client{
  709. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `non-json-secret`}), nil),
  710. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  711. },
  712. pushSecretData: pushSecretDataWithProperty,
  713. },
  714. want: want{
  715. err: errors.New("PushSecret for aws secrets manager with a pushSecretData property requires a json secret"),
  716. },
  717. },
  718. "SetSecretCreateSecretFails": {
  719. reason: "CreateSecretWithContext returns an error if it fails",
  720. args: args{
  721. store: makeValidSecretStore().Spec.Provider.AWS,
  722. client: fakesm.Client{
  723. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  724. CreateSecretFn: fakesm.NewCreateSecretFn(nil, noPermission),
  725. },
  726. pushSecretData: pushSecretDataWithoutProperty,
  727. },
  728. want: want{
  729. err: noPermission,
  730. },
  731. },
  732. "SetSecretGetSecretFails": {
  733. reason: "GetSecretValueWithContext returns an error if it fails",
  734. args: args{
  735. store: makeValidSecretStore().Spec.Provider.AWS,
  736. client: fakesm.Client{
  737. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, noPermission),
  738. },
  739. pushSecretData: pushSecretDataWithoutProperty,
  740. },
  741. want: want{
  742. err: noPermission,
  743. },
  744. },
  745. "SetSecretWillNotPushSameSecret": {
  746. reason: "secret with the same value will not be pushed",
  747. args: args{
  748. store: makeValidSecretStore().Spec.Provider.AWS,
  749. client: fakesm.Client{
  750. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput2, nil),
  751. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  752. },
  753. pushSecretData: pushSecretDataWithoutProperty,
  754. },
  755. want: want{
  756. err: nil,
  757. },
  758. },
  759. "SetSecretPutSecretValueFails": {
  760. reason: "PutSecretValueWithContext returns an error if it fails",
  761. args: args{
  762. store: makeValidSecretStore().Spec.Provider.AWS,
  763. client: fakesm.Client{
  764. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  765. PutSecretValueFn: fakesm.NewPutSecretValueFn(nil, noPermission),
  766. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  767. },
  768. pushSecretData: pushSecretDataWithoutProperty,
  769. },
  770. want: want{
  771. err: noPermission,
  772. },
  773. },
  774. "SetSecretWrongGetSecretErrFails": {
  775. reason: "GetSecretValueWithContext errors out when anything except awssm.ErrCodeResourceNotFoundException",
  776. args: args{
  777. store: makeValidSecretStore().Spec.Provider.AWS,
  778. client: fakesm.Client{
  779. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretWrongErr),
  780. },
  781. pushSecretData: pushSecretDataWithoutProperty,
  782. },
  783. want: want{
  784. err: &getSecretWrongErr,
  785. },
  786. },
  787. "SetSecretDescribeSecretFails": {
  788. reason: "secret cannot be described",
  789. args: args{
  790. store: makeValidSecretStore().Spec.Provider.AWS,
  791. client: fakesm.Client{
  792. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  793. DescribeSecretFn: fakesm.NewDescribeSecretFn(nil, noPermission),
  794. },
  795. pushSecretData: pushSecretDataWithoutProperty,
  796. },
  797. want: want{
  798. err: noPermission,
  799. },
  800. },
  801. "SetSecretDoesNotOverwriteUntaggedSecret": {
  802. reason: "secret cannot be described",
  803. args: args{
  804. store: makeValidSecretStore().Spec.Provider.AWS,
  805. client: fakesm.Client{
  806. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  807. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFaulty, nil),
  808. },
  809. pushSecretData: pushSecretDataWithoutProperty,
  810. },
  811. want: want{
  812. err: errors.New("secret not managed by external-secrets"),
  813. },
  814. },
  815. "SetSecretWithPrefix": {
  816. reason: "secret key is properly prefixed when creating a new secret",
  817. args: args{
  818. store: &esv1.AWSProvider{
  819. Service: esv1.AWSServiceSecretsManager,
  820. Region: "eu-west-2",
  821. Prefix: "prefix-",
  822. },
  823. client: fakesm.Client{
  824. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  825. CreateSecretFn: func(ctx context.Context, input *awssm.CreateSecretInput, opts ...func(*awssm.Options)) (*awssm.CreateSecretOutput, error) {
  826. // Verify that the input name has the prefix applied
  827. if *input.Name != "prefix-"+fakeKey {
  828. return nil, fmt.Errorf("expected secret name to be prefixed with 'prefix-', got %s", *input.Name)
  829. }
  830. return secretOutput, nil
  831. },
  832. },
  833. pushSecretData: pushSecretDataWithoutProperty,
  834. },
  835. want: want{
  836. err: nil,
  837. },
  838. },
  839. }
  840. for name, tc := range tests {
  841. t.Run(name, func(t *testing.T) {
  842. sm := SecretsManager{
  843. client: &tc.args.client,
  844. prefix: tc.args.store.Prefix,
  845. }
  846. err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
  847. // Error nil XOR tc.want.err nil
  848. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  849. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  850. }
  851. // if errors are the same type but their contents do not match
  852. if err != nil && tc.want.err != nil {
  853. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  854. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  855. }
  856. }
  857. })
  858. }
  859. }
  860. func TestDeleteSecret(t *testing.T) {
  861. fakeClient := fakesm.Client{}
  862. managed := managedBy
  863. manager := externalSecrets
  864. secretTag := types.Tag{
  865. Key: &managed,
  866. Value: &manager,
  867. }
  868. type args struct {
  869. client fakesm.Client
  870. config esv1.SecretsManager
  871. prefix string
  872. getSecretOutput *awssm.GetSecretValueOutput
  873. describeSecretOutput *awssm.DescribeSecretOutput
  874. deleteSecretOutput *awssm.DeleteSecretOutput
  875. getSecretErr error
  876. describeSecretErr error
  877. deleteSecretErr error
  878. }
  879. type want struct {
  880. err error
  881. }
  882. type testCase struct {
  883. args args
  884. want want
  885. reason string
  886. }
  887. tests := map[string]testCase{
  888. "Deletes Successfully": {
  889. args: args{
  890. client: fakeClient,
  891. config: esv1.SecretsManager{},
  892. getSecretOutput: &awssm.GetSecretValueOutput{},
  893. describeSecretOutput: &awssm.DescribeSecretOutput{
  894. Tags: []types.Tag{secretTag},
  895. },
  896. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  897. getSecretErr: nil,
  898. describeSecretErr: nil,
  899. deleteSecretErr: nil,
  900. },
  901. want: want{
  902. err: nil,
  903. },
  904. reason: "",
  905. },
  906. "Deletes Successfully with ForceDeleteWithoutRecovery": {
  907. args: args{
  908. client: fakeClient,
  909. config: esv1.SecretsManager{
  910. ForceDeleteWithoutRecovery: true,
  911. },
  912. getSecretOutput: &awssm.GetSecretValueOutput{},
  913. describeSecretOutput: &awssm.DescribeSecretOutput{
  914. Tags: []types.Tag{secretTag},
  915. },
  916. deleteSecretOutput: &awssm.DeleteSecretOutput{
  917. DeletionDate: aws.Time(time.Now()),
  918. },
  919. getSecretErr: nil,
  920. describeSecretErr: nil,
  921. deleteSecretErr: nil,
  922. },
  923. want: want{
  924. err: nil,
  925. },
  926. reason: "",
  927. },
  928. "Not Managed by ESO": {
  929. args: args{
  930. client: fakeClient,
  931. config: esv1.SecretsManager{},
  932. getSecretOutput: &awssm.GetSecretValueOutput{},
  933. describeSecretOutput: &awssm.DescribeSecretOutput{
  934. Tags: []types.Tag{},
  935. },
  936. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  937. getSecretErr: nil,
  938. describeSecretErr: nil,
  939. deleteSecretErr: nil,
  940. },
  941. want: want{
  942. err: nil,
  943. },
  944. reason: "",
  945. },
  946. "Invalid Recovery Window": {
  947. args: args{
  948. client: fakesm.Client{},
  949. config: esv1.SecretsManager{
  950. RecoveryWindowInDays: 1,
  951. },
  952. getSecretOutput: &awssm.GetSecretValueOutput{},
  953. describeSecretOutput: &awssm.DescribeSecretOutput{
  954. Tags: []types.Tag{secretTag},
  955. },
  956. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  957. getSecretErr: nil,
  958. describeSecretErr: nil,
  959. deleteSecretErr: nil,
  960. },
  961. want: want{
  962. err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
  963. },
  964. reason: "",
  965. },
  966. "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
  967. args: args{
  968. client: fakesm.Client{},
  969. config: esv1.SecretsManager{
  970. RecoveryWindowInDays: 7,
  971. ForceDeleteWithoutRecovery: true,
  972. },
  973. getSecretOutput: &awssm.GetSecretValueOutput{},
  974. describeSecretOutput: &awssm.DescribeSecretOutput{
  975. Tags: []types.Tag{secretTag},
  976. },
  977. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  978. getSecretErr: nil,
  979. describeSecretErr: nil,
  980. deleteSecretErr: nil,
  981. },
  982. want: want{
  983. err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
  984. },
  985. reason: "",
  986. },
  987. "Failed to get Tags": {
  988. args: args{
  989. client: fakeClient,
  990. config: esv1.SecretsManager{},
  991. getSecretOutput: &awssm.GetSecretValueOutput{},
  992. describeSecretOutput: nil,
  993. deleteSecretOutput: nil,
  994. getSecretErr: nil,
  995. describeSecretErr: errors.New("failed to get tags"),
  996. deleteSecretErr: nil,
  997. },
  998. want: want{
  999. err: errors.New("failed to get tags"),
  1000. },
  1001. reason: "",
  1002. },
  1003. "Secret Not Found": {
  1004. args: args{
  1005. client: fakeClient,
  1006. config: esv1.SecretsManager{},
  1007. getSecretOutput: nil,
  1008. describeSecretOutput: nil,
  1009. deleteSecretOutput: nil,
  1010. getSecretErr: errors.New("not here, sorry dude"),
  1011. describeSecretErr: nil,
  1012. deleteSecretErr: nil,
  1013. },
  1014. want: want{
  1015. err: errors.New("not here, sorry dude"),
  1016. },
  1017. },
  1018. "Not expected AWS error": {
  1019. args: args{
  1020. client: fakeClient,
  1021. config: esv1.SecretsManager{},
  1022. getSecretOutput: nil,
  1023. describeSecretOutput: nil,
  1024. deleteSecretOutput: nil,
  1025. getSecretErr: errors.New("aws unavailable"),
  1026. describeSecretErr: nil,
  1027. deleteSecretErr: nil,
  1028. },
  1029. want: want{
  1030. err: errors.New("aws unavailable"),
  1031. },
  1032. },
  1033. "unexpected error": {
  1034. args: args{
  1035. client: fakeClient,
  1036. config: esv1.SecretsManager{},
  1037. getSecretOutput: nil,
  1038. describeSecretOutput: nil,
  1039. deleteSecretOutput: nil,
  1040. getSecretErr: errors.New("timeout"),
  1041. describeSecretErr: nil,
  1042. deleteSecretErr: nil,
  1043. },
  1044. want: want{
  1045. err: errors.New("timeout"),
  1046. },
  1047. },
  1048. "DeleteWithPrefix": {
  1049. args: args{
  1050. client: fakesm.Client{
  1051. GetSecretValueFn: func(ctx context.Context, input *awssm.GetSecretValueInput, opts ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  1052. // Verify that the input secret ID has the prefix applied
  1053. if *input.SecretId != "my-prefix-"+fakeKey {
  1054. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1055. }
  1056. return &awssm.GetSecretValueOutput{}, nil
  1057. },
  1058. DescribeSecretFn: func(ctx context.Context, input *awssm.DescribeSecretInput, opts ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error) {
  1059. // Verify that the input secret ID has the prefix applied
  1060. if *input.SecretId != "my-prefix-"+fakeKey {
  1061. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1062. }
  1063. return &awssm.DescribeSecretOutput{
  1064. Tags: []types.Tag{secretTag},
  1065. }, nil
  1066. },
  1067. DeleteSecretFn: func(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
  1068. return &awssm.DeleteSecretOutput{}, nil
  1069. },
  1070. },
  1071. config: esv1.SecretsManager{},
  1072. prefix: "my-prefix-",
  1073. getSecretOutput: nil,
  1074. describeSecretOutput: nil,
  1075. deleteSecretOutput: nil,
  1076. getSecretErr: nil,
  1077. describeSecretErr: nil,
  1078. deleteSecretErr: nil,
  1079. },
  1080. want: want{
  1081. err: nil,
  1082. },
  1083. reason: "Verifies that the prefix is correctly applied when deleting a secret",
  1084. },
  1085. }
  1086. for name, tc := range tests {
  1087. t.Run(name, func(t *testing.T) {
  1088. ref := fake.PushSecretData{RemoteKey: fakeKey}
  1089. sm := SecretsManager{
  1090. client: &tc.args.client,
  1091. config: &tc.args.config,
  1092. prefix: tc.args.prefix,
  1093. }
  1094. if tc.args.client.GetSecretValueFn == nil {
  1095. tc.args.client.GetSecretValueFn = fakesm.NewGetSecretValueFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  1096. }
  1097. if tc.args.client.DescribeSecretFn == nil {
  1098. tc.args.client.DescribeSecretFn = fakesm.NewDescribeSecretFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  1099. }
  1100. if tc.args.client.DeleteSecretFn == nil {
  1101. tc.args.client.DeleteSecretFn = fakesm.NewDeleteSecretFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  1102. }
  1103. err := sm.DeleteSecret(context.TODO(), ref)
  1104. t.Logf("DeleteSecret error: %v", err)
  1105. // Error nil XOR tc.want.err nil
  1106. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1107. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1108. }
  1109. // if errors are the same type but their contents do not match
  1110. if err != nil && tc.want.err != nil {
  1111. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1112. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1113. }
  1114. }
  1115. })
  1116. }
  1117. }
  1118. func makeValidSecretStore() *esv1.SecretStore {
  1119. return &esv1.SecretStore{
  1120. ObjectMeta: metav1.ObjectMeta{
  1121. Name: "aws-secret-store",
  1122. Namespace: "default",
  1123. },
  1124. Spec: esv1.SecretStoreSpec{
  1125. Provider: &esv1.SecretStoreProvider{
  1126. AWS: &esv1.AWSProvider{
  1127. Service: esv1.AWSServiceSecretsManager,
  1128. Region: "eu-west-2",
  1129. },
  1130. },
  1131. },
  1132. }
  1133. }
  1134. func getTagSlice() []types.Tag {
  1135. tagKey1 := tagname1
  1136. tagValue1 := tagvalue1
  1137. tagKey2 := tagname2
  1138. tagValue2 := tagvalue2
  1139. return []types.Tag{
  1140. {
  1141. Key: &tagKey1,
  1142. Value: &tagValue1,
  1143. },
  1144. {
  1145. Key: &tagKey2,
  1146. Value: &tagValue2,
  1147. },
  1148. }
  1149. }
  1150. func TestSecretsManagerGetAllSecrets(t *testing.T) {
  1151. ctx := context.Background()
  1152. errBoom := errors.New("boom")
  1153. secretName := "my-secret"
  1154. secretVersion := "AWSCURRENT"
  1155. secretPath := "/path/to/secret"
  1156. secretValue := "secret value"
  1157. secretTags := map[string]string{
  1158. "foo": "bar",
  1159. }
  1160. // Test cases
  1161. testCases := []struct {
  1162. name string
  1163. ref esv1.ExternalSecretFind
  1164. secretName string
  1165. secretVersion string
  1166. secretValue string
  1167. batchGetSecretValueFn func(context.Context, *awssm.BatchGetSecretValueInput, ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error)
  1168. listSecretsFn func(context.Context, *awssm.ListSecretsInput, ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error)
  1169. expectedData map[string][]byte
  1170. expectedError string
  1171. }{
  1172. {
  1173. name: "Matching secrets found",
  1174. ref: esv1.ExternalSecretFind{
  1175. Name: &esv1.FindName{
  1176. RegExp: secretName,
  1177. },
  1178. Path: ptr.To(secretPath),
  1179. },
  1180. secretName: secretName,
  1181. secretVersion: secretVersion,
  1182. secretValue: secretValue,
  1183. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1184. assert.Len(t, input.Filters, 1)
  1185. assert.Equal(t, "name", string(input.Filters[0].Key))
  1186. assert.Equal(t, secretPath, input.Filters[0].Values[0])
  1187. return &awssm.BatchGetSecretValueOutput{
  1188. SecretValues: []types.SecretValueEntry{
  1189. {
  1190. Name: ptr.To(secretName),
  1191. VersionStages: []string{secretVersion},
  1192. SecretBinary: []byte(secretValue),
  1193. },
  1194. },
  1195. }, nil
  1196. },
  1197. expectedData: map[string][]byte{
  1198. secretName: []byte(secretValue),
  1199. },
  1200. expectedError: "",
  1201. },
  1202. {
  1203. name: "Error occurred while fetching secret value",
  1204. ref: esv1.ExternalSecretFind{
  1205. Name: &esv1.FindName{
  1206. RegExp: secretName,
  1207. },
  1208. Path: ptr.To(secretPath),
  1209. },
  1210. secretName: secretName,
  1211. secretVersion: secretVersion,
  1212. secretValue: secretValue,
  1213. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1214. return &awssm.BatchGetSecretValueOutput{
  1215. SecretValues: []types.SecretValueEntry{
  1216. {
  1217. Name: ptr.To(secretName),
  1218. },
  1219. },
  1220. }, errBoom
  1221. },
  1222. expectedData: nil,
  1223. expectedError: errBoom.Error(),
  1224. },
  1225. {
  1226. name: "regexp: error occurred while listing secrets",
  1227. ref: esv1.ExternalSecretFind{
  1228. Name: &esv1.FindName{
  1229. RegExp: secretName,
  1230. },
  1231. },
  1232. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1233. return nil, errBoom
  1234. },
  1235. expectedData: nil,
  1236. expectedError: errBoom.Error(),
  1237. },
  1238. {
  1239. name: "regep: no matching secrets found",
  1240. ref: esv1.ExternalSecretFind{
  1241. Name: &esv1.FindName{
  1242. RegExp: secretName,
  1243. },
  1244. },
  1245. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1246. return &awssm.ListSecretsOutput{
  1247. SecretList: []types.SecretListEntry{
  1248. {
  1249. Name: ptr.To("other-secret"),
  1250. },
  1251. },
  1252. }, nil
  1253. },
  1254. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1255. return &awssm.BatchGetSecretValueOutput{
  1256. SecretValues: []types.SecretValueEntry{
  1257. {
  1258. Name: ptr.To("other-secret"),
  1259. },
  1260. },
  1261. }, nil
  1262. },
  1263. expectedData: make(map[string][]byte),
  1264. expectedError: "",
  1265. },
  1266. {
  1267. name: "invalid regexp",
  1268. ref: esv1.ExternalSecretFind{
  1269. Name: &esv1.FindName{
  1270. RegExp: "[",
  1271. },
  1272. },
  1273. expectedData: nil,
  1274. expectedError: "could not compile find.name.regexp [[]: error parsing regexp: missing closing ]: `[`",
  1275. },
  1276. {
  1277. name: "tags: Matching secrets found",
  1278. ref: esv1.ExternalSecretFind{
  1279. Tags: secretTags,
  1280. },
  1281. secretName: secretName,
  1282. secretVersion: secretVersion,
  1283. secretValue: secretValue,
  1284. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1285. assert.Len(t, input.Filters, 2)
  1286. assert.Equal(t, "tag-key", string(input.Filters[0].Key))
  1287. assert.Equal(t, "foo", input.Filters[0].Values[0])
  1288. assert.Equal(t, "tag-value", string(input.Filters[1].Key))
  1289. assert.Equal(t, "bar", input.Filters[1].Values[0])
  1290. return &awssm.BatchGetSecretValueOutput{
  1291. SecretValues: []types.SecretValueEntry{
  1292. {
  1293. Name: ptr.To(secretName),
  1294. VersionStages: []string{secretVersion},
  1295. SecretBinary: []byte(secretValue),
  1296. },
  1297. },
  1298. }, nil
  1299. },
  1300. expectedData: map[string][]byte{
  1301. secretName: []byte(secretValue),
  1302. },
  1303. expectedError: "",
  1304. },
  1305. {
  1306. name: "tags: error occurred while fetching secret value",
  1307. ref: esv1.ExternalSecretFind{
  1308. Tags: secretTags,
  1309. },
  1310. secretName: secretName,
  1311. secretVersion: secretVersion,
  1312. secretValue: secretValue,
  1313. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1314. return &awssm.BatchGetSecretValueOutput{
  1315. SecretValues: []types.SecretValueEntry{
  1316. {
  1317. Name: ptr.To(secretName),
  1318. VersionStages: []string{secretVersion},
  1319. SecretBinary: []byte(secretValue),
  1320. },
  1321. },
  1322. }, errBoom
  1323. },
  1324. expectedData: nil,
  1325. expectedError: errBoom.Error(),
  1326. },
  1327. {
  1328. name: "tags: error occurred while listing secrets",
  1329. ref: esv1.ExternalSecretFind{
  1330. Tags: secretTags,
  1331. },
  1332. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1333. return nil, errBoom
  1334. },
  1335. expectedData: nil,
  1336. expectedError: errBoom.Error(),
  1337. },
  1338. }
  1339. for _, tc := range testCases {
  1340. t.Run(tc.name, func(t *testing.T) {
  1341. fc := fakesm.NewClient()
  1342. fc.BatchGetSecretValueFn = tc.batchGetSecretValueFn
  1343. fc.ListSecretsFn = tc.listSecretsFn
  1344. sm := SecretsManager{
  1345. client: fc,
  1346. cache: make(map[string]*awssm.GetSecretValueOutput),
  1347. }
  1348. data, err := sm.GetAllSecrets(ctx, tc.ref)
  1349. if err != nil && err.Error() != tc.expectedError {
  1350. t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
  1351. }
  1352. if !reflect.DeepEqual(data, tc.expectedData) {
  1353. t.Errorf("unexpected data: got %v, want %v", data, tc.expectedData)
  1354. }
  1355. })
  1356. }
  1357. }
  1358. func TestSecretsManagerValidate(t *testing.T) {
  1359. type fields struct {
  1360. cfg *aws.Config
  1361. referentAuth bool
  1362. }
  1363. validConfig := &aws.Config{
  1364. Credentials: credentials.NewStaticCredentialsProvider(
  1365. "fake",
  1366. "fake",
  1367. "fake",
  1368. ),
  1369. }
  1370. invalidConfig := &aws.Config{
  1371. Credentials: &FakeCredProvider{
  1372. retrieveFunc: func() (aws.Credentials, error) {
  1373. return aws.Credentials{}, errors.New("invalid credentials")
  1374. },
  1375. },
  1376. }
  1377. tests := []struct {
  1378. name string
  1379. fields fields
  1380. want esv1.ValidationResult
  1381. wantErr bool
  1382. }{
  1383. {
  1384. name: "ReferentAuth should always return unknown",
  1385. fields: fields{
  1386. referentAuth: true,
  1387. },
  1388. want: esv1.ValidationResultUnknown,
  1389. },
  1390. {
  1391. name: "Valid credentials should return ready",
  1392. fields: fields{
  1393. cfg: validConfig,
  1394. },
  1395. want: esv1.ValidationResultReady,
  1396. },
  1397. {
  1398. name: "Invalid credentials should return error",
  1399. fields: fields{
  1400. cfg: invalidConfig,
  1401. },
  1402. want: esv1.ValidationResultError,
  1403. wantErr: true,
  1404. },
  1405. }
  1406. for _, tt := range tests {
  1407. t.Run(tt.name, func(t *testing.T) {
  1408. sm := &SecretsManager{
  1409. cfg: tt.fields.cfg,
  1410. referentAuth: tt.fields.referentAuth,
  1411. }
  1412. got, err := sm.Validate()
  1413. if (err != nil) != tt.wantErr {
  1414. t.Errorf("SecretsManager.Validate() error = %v, wantErr %v", err, tt.wantErr)
  1415. return
  1416. }
  1417. if !reflect.DeepEqual(got, tt.want) {
  1418. t.Errorf("SecretsManager.Validate() = %v, want %v", got, tt.want)
  1419. }
  1420. })
  1421. }
  1422. }
  1423. func TestSecretExists(t *testing.T) {
  1424. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  1425. defaultVersion := "00000000-0000-0000-0000-000000000002"
  1426. secretValueOutput := &awssm.GetSecretValueOutput{
  1427. ARN: &arn,
  1428. VersionId: &defaultVersion,
  1429. }
  1430. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  1431. getSecretCorrectErr := types.ResourceNotFoundException{}
  1432. getSecretWrongErr := types.InvalidRequestException{}
  1433. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: "fake-secret-key", RemoteKey: fakeKey, Property: ""}
  1434. type args struct {
  1435. store *esv1.AWSProvider
  1436. client fakesm.Client
  1437. pushSecretData fake.PushSecretData
  1438. }
  1439. type want struct {
  1440. err error
  1441. wantError bool
  1442. }
  1443. tests := map[string]struct {
  1444. args args
  1445. want want
  1446. }{
  1447. "SecretExistsReturnsTrueForExistingSecret": {
  1448. args: args{
  1449. store: makeValidSecretStore().Spec.Provider.AWS,
  1450. client: fakesm.Client{
  1451. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1452. },
  1453. pushSecretData: pushSecretDataWithoutProperty,
  1454. },
  1455. want: want{
  1456. err: nil,
  1457. wantError: true,
  1458. },
  1459. },
  1460. "SecretExistsReturnsFalseForNonExistingSecret": {
  1461. args: args{
  1462. store: makeValidSecretStore().Spec.Provider.AWS,
  1463. client: fakesm.Client{
  1464. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  1465. },
  1466. pushSecretData: pushSecretDataWithoutProperty,
  1467. },
  1468. want: want{
  1469. err: nil,
  1470. wantError: false,
  1471. },
  1472. },
  1473. "SecretExistsReturnsFalseForErroredSecret": {
  1474. args: args{
  1475. store: makeValidSecretStore().Spec.Provider.AWS,
  1476. client: fakesm.Client{
  1477. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretWrongErr),
  1478. },
  1479. pushSecretData: pushSecretDataWithoutProperty,
  1480. },
  1481. want: want{
  1482. err: &getSecretWrongErr,
  1483. wantError: false,
  1484. },
  1485. },
  1486. }
  1487. for name, tc := range tests {
  1488. t.Run(name, func(t *testing.T) {
  1489. sm := &SecretsManager{
  1490. client: &tc.args.client,
  1491. }
  1492. got, err := sm.SecretExists(context.Background(), tc.args.pushSecretData)
  1493. assert.Equal(
  1494. t,
  1495. tc.want,
  1496. want{
  1497. err: err,
  1498. wantError: got,
  1499. })
  1500. })
  1501. }
  1502. }
  1503. // FakeCredProvider implements the AWS credentials.Provider interface
  1504. // It is used to inject an error into the AWS config to cause a
  1505. // validation error.
  1506. type FakeCredProvider struct {
  1507. retrieveFunc func() (aws.Credentials, error)
  1508. }
  1509. func (f *FakeCredProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
  1510. return f.retrieveFunc()
  1511. }
  1512. func (f *FakeCredProvider) IsExpired() bool {
  1513. return true
  1514. }