secretsmanager_test.go 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package secretsmanager
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "time"
  23. "github.com/aws/aws-sdk-go-v2/aws"
  24. "github.com/aws/aws-sdk-go-v2/credentials"
  25. awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
  26. "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
  27. "github.com/google/go-cmp/cmp"
  28. "github.com/stretchr/testify/assert"
  29. "github.com/stretchr/testify/require"
  30. corev1 "k8s.io/api/core/v1"
  31. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  32. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  33. "k8s.io/utils/ptr"
  34. "sigs.k8s.io/controller-runtime/pkg/client"
  35. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  36. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  37. fakesm "github.com/external-secrets/external-secrets/providers/v1/aws/secretsmanager/fake"
  38. awsutil "github.com/external-secrets/external-secrets/providers/v1/aws/util"
  39. "github.com/external-secrets/external-secrets/runtime/esutils/metadata"
  40. "github.com/external-secrets/external-secrets/runtime/testing/fake"
  41. )
  42. type secretsManagerTestCase struct {
  43. fakeClient *fakesm.Client
  44. apiInput *awssm.GetSecretValueInput
  45. apiOutput *awssm.GetSecretValueOutput
  46. remoteRef *esv1.ExternalSecretDataRemoteRef
  47. apiErr error
  48. expectError string
  49. expectedSecret string
  50. // for testing secretmap
  51. expectedData map[string][]byte
  52. // for testing caching
  53. expectedCounter *int
  54. prefix string
  55. }
  56. const unexpectedErrorString = "[%d] unexpected error: %s, expected: '%s'"
  57. const (
  58. tagname1 = "tagname1"
  59. tagvalue1 = "tagvalue1"
  60. tagname2 = "tagname2"
  61. tagvalue2 = "tagvalue2"
  62. fakeKey = "fake-key"
  63. )
  64. func makeValidSecretsManagerTestCase() *secretsManagerTestCase {
  65. smtc := secretsManagerTestCase{
  66. fakeClient: fakesm.NewClient(),
  67. apiInput: makeValidAPIInput(),
  68. remoteRef: makeValidRemoteRef(),
  69. apiOutput: makeValidAPIOutput(),
  70. apiErr: nil,
  71. expectError: "",
  72. expectedSecret: "",
  73. expectedData: map[string][]byte{},
  74. }
  75. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  76. return &smtc
  77. }
  78. func makeValidRemoteRef() *esv1.ExternalSecretDataRemoteRef {
  79. return &esv1.ExternalSecretDataRemoteRef{
  80. Key: "/baz",
  81. Version: "AWSCURRENT",
  82. }
  83. }
  84. func makeValidAPIInput() *awssm.GetSecretValueInput {
  85. return &awssm.GetSecretValueInput{
  86. SecretId: aws.String("/baz"),
  87. VersionStage: aws.String("AWSCURRENT"),
  88. }
  89. }
  90. func makeValidAPIOutput() *awssm.GetSecretValueOutput {
  91. return &awssm.GetSecretValueOutput{
  92. SecretString: aws.String(""),
  93. }
  94. }
  95. func makeValidGetResourcePolicyOutput() *awssm.GetResourcePolicyOutput {
  96. return &awssm.GetResourcePolicyOutput{
  97. ResourcePolicy: aws.String(`{
  98. "Version": "2012-10-17",
  99. "Statement": [
  100. {
  101. "Sid": "DenyPolicyChangesExceptAdmins",
  102. "Effect": "Deny",
  103. "Principal": "*",
  104. "Action": [
  105. "secretsmanager:PutResourcePolicy",
  106. "secretsmanager:DeleteResourcePolicy",
  107. "secretsmanager:GetResourcePolicy"
  108. ],
  109. "Resource": "*",
  110. "Condition": {
  111. "ArnNotEquals": {
  112. "aws:PrincipalArn": [
  113. "arn:aws:iam::000000000000:root",
  114. "arn:aws:iam::000000000000:role/admin"
  115. ]
  116. }
  117. }
  118. }
  119. ]
  120. }`),
  121. }
  122. }
  123. func makeValidSecretsManagerTestCaseCustom(tweaks ...func(smtc *secretsManagerTestCase)) *secretsManagerTestCase {
  124. smtc := makeValidSecretsManagerTestCase()
  125. for _, fn := range tweaks {
  126. fn(smtc)
  127. }
  128. smtc.fakeClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
  129. return smtc
  130. }
  131. // This case can be shared by both GetSecret and GetSecretMap tests.
  132. // bad case: set apiErr.
  133. var setAPIErr = func(smtc *secretsManagerTestCase) {
  134. smtc.apiErr = errors.New("oh no")
  135. smtc.expectError = "oh no"
  136. }
  137. func TestSecretsManagerResolver(t *testing.T) {
  138. endpointEnvKey := SecretsManagerEndpointEnv
  139. endpointURL := "http://sm.foo"
  140. t.Setenv(endpointEnvKey, endpointURL)
  141. f, err := customEndpointResolver{}.ResolveEndpoint(context.Background(), awssm.EndpointParameters{})
  142. assert.Nil(t, err)
  143. assert.Equal(t, endpointURL, f.URI.String())
  144. }
  145. // test the sm<->aws interface
  146. // make sure correct values are passed and errors are handled accordingly.
  147. func TestSecretsManagerGetSecret(t *testing.T) {
  148. // good case: default version is set
  149. // key is passed in, output is sent back
  150. setSecretString := func(smtc *secretsManagerTestCase) {
  151. smtc.apiOutput.SecretString = aws.String("testtesttest")
  152. smtc.expectedSecret = "testtesttest"
  153. }
  154. // good case: key is passed in with prefix
  155. setSecretStringWithPrefix := func(smtc *secretsManagerTestCase) {
  156. smtc.remoteRef.Key = "secret-key"
  157. smtc.apiInput = &awssm.GetSecretValueInput{
  158. SecretId: aws.String("my-prefix/secret-key"),
  159. VersionStage: aws.String("AWSCURRENT"),
  160. }
  161. smtc.prefix = "my-prefix/"
  162. }
  163. // good case: extract property
  164. // Testing that the property exists in the SecretString
  165. setRemoteRefPropertyExistsInKey := func(smtc *secretsManagerTestCase) {
  166. smtc.remoteRef.Property = "/shmoo"
  167. smtc.apiOutput.SecretString = aws.String(`{"/shmoo": "bang"}`)
  168. smtc.expectedSecret = "bang"
  169. }
  170. // bad case: missing property
  171. setRemoteRefMissingProperty := func(smtc *secretsManagerTestCase) {
  172. smtc.remoteRef.Property = "INVALPROP"
  173. smtc.expectError = "key INVALPROP does not exist in secret"
  174. }
  175. // bad case: extract property failure due to invalid json
  176. setRemoteRefMissingPropertyInvalidJSON := func(smtc *secretsManagerTestCase) {
  177. smtc.remoteRef.Property = "INVALPROP"
  178. smtc.apiOutput.SecretString = aws.String(`------`)
  179. smtc.expectError = "key INVALPROP does not exist in secret"
  180. }
  181. // good case: set .SecretString to nil but set binary with value
  182. setSecretBinaryNotSecretString := func(smtc *secretsManagerTestCase) {
  183. smtc.apiOutput.SecretBinary = []byte("yesplease")
  184. // needs to be set as nil, empty quotes ("") is considered existing
  185. smtc.apiOutput.SecretString = nil
  186. smtc.expectedSecret = "yesplease"
  187. }
  188. // bad case: both .SecretString and .SecretBinary are nil
  189. setSecretBinaryAndSecretStringToNil := func(smtc *secretsManagerTestCase) {
  190. smtc.apiOutput.SecretBinary = nil
  191. smtc.apiOutput.SecretString = nil
  192. smtc.expectError = "no secret string nor binary for key"
  193. }
  194. // good case: secretOut.SecretBinary JSON parsing
  195. setNestedSecretValueJSONParsing := func(smtc *secretsManagerTestCase) {
  196. smtc.apiOutput.SecretString = nil
  197. smtc.apiOutput.SecretBinary = []byte(`{"foobar":{"baz":"nestedval"}}`)
  198. smtc.remoteRef.Property = "foobar.baz"
  199. smtc.expectedSecret = "nestedval"
  200. }
  201. // good case: secretOut.SecretBinary no JSON parsing if name on key
  202. setSecretValueWithDot := func(smtc *secretsManagerTestCase) {
  203. smtc.apiOutput.SecretString = nil
  204. smtc.apiOutput.SecretBinary = []byte(`{"foobar.baz":"nestedval"}`)
  205. smtc.remoteRef.Property = "foobar.baz"
  206. smtc.expectedSecret = "nestedval"
  207. }
  208. // good case: custom version stage set
  209. setCustomVersionStage := func(smtc *secretsManagerTestCase) {
  210. smtc.apiInput.VersionStage = aws.String("1234")
  211. smtc.remoteRef.Version = "1234"
  212. smtc.apiOutput.SecretString = aws.String("FOOBA!")
  213. smtc.expectedSecret = "FOOBA!"
  214. }
  215. // good case: custom version id set
  216. setCustomVersionID := func(smtc *secretsManagerTestCase) {
  217. smtc.apiInput.VersionStage = nil
  218. smtc.apiInput.VersionId = aws.String("1234-5678")
  219. smtc.remoteRef.Version = "uuid/1234-5678"
  220. smtc.apiOutput.SecretString = aws.String("myvalue")
  221. smtc.expectedSecret = "myvalue"
  222. }
  223. fetchMetadata := func(smtc *secretsManagerTestCase) {
  224. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  225. describeSecretOutput := &awssm.DescribeSecretOutput{
  226. Tags: getTagSlice(),
  227. }
  228. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  229. jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
  230. smtc.apiOutput.SecretString = &jsonTags
  231. smtc.expectedSecret = jsonTags
  232. }
  233. fetchMetadataProperty := func(smtc *secretsManagerTestCase) {
  234. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  235. describeSecretOutput := &awssm.DescribeSecretOutput{
  236. Tags: getTagSlice(),
  237. }
  238. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  239. smtc.remoteRef.Property = tagname2
  240. jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
  241. smtc.apiOutput.SecretString = &jsonTags
  242. smtc.expectedSecret = tagvalue2
  243. }
  244. failMetadataWrongProperty := func(smtc *secretsManagerTestCase) {
  245. smtc.remoteRef.MetadataPolicy = esv1.ExternalSecretMetadataPolicyFetch
  246. describeSecretOutput := &awssm.DescribeSecretOutput{
  247. Tags: getTagSlice(),
  248. }
  249. smtc.fakeClient.DescribeSecretFn = fakesm.NewDescribeSecretFn(describeSecretOutput, nil)
  250. smtc.remoteRef.Property = "fail"
  251. jsonTags, _ := awsutil.SecretTagsToJSONString(getTagSlice())
  252. smtc.apiOutput.SecretString = &jsonTags
  253. smtc.expectError = "key fail does not exist in secret /baz"
  254. }
  255. successCases := []*secretsManagerTestCase{
  256. makeValidSecretsManagerTestCase(),
  257. makeValidSecretsManagerTestCaseCustom(setSecretString),
  258. makeValidSecretsManagerTestCaseCustom(setSecretStringWithPrefix),
  259. makeValidSecretsManagerTestCaseCustom(setRemoteRefPropertyExistsInKey),
  260. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingProperty),
  261. makeValidSecretsManagerTestCaseCustom(setRemoteRefMissingPropertyInvalidJSON),
  262. makeValidSecretsManagerTestCaseCustom(setSecretBinaryNotSecretString),
  263. makeValidSecretsManagerTestCaseCustom(setSecretBinaryAndSecretStringToNil),
  264. makeValidSecretsManagerTestCaseCustom(setNestedSecretValueJSONParsing),
  265. makeValidSecretsManagerTestCaseCustom(setSecretValueWithDot),
  266. makeValidSecretsManagerTestCaseCustom(setCustomVersionStage),
  267. makeValidSecretsManagerTestCaseCustom(setCustomVersionID),
  268. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  269. makeValidSecretsManagerTestCaseCustom(fetchMetadata),
  270. makeValidSecretsManagerTestCaseCustom(fetchMetadataProperty),
  271. makeValidSecretsManagerTestCaseCustom(failMetadataWrongProperty),
  272. }
  273. for k, v := range successCases {
  274. sm := SecretsManager{
  275. cache: make(map[string]*awssm.GetSecretValueOutput),
  276. client: v.fakeClient,
  277. prefix: v.prefix,
  278. }
  279. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  280. if !ErrorContains(err, v.expectError) {
  281. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  282. }
  283. if err == nil && string(out) != v.expectedSecret {
  284. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  285. }
  286. }
  287. }
  288. func TestCaching(t *testing.T) {
  289. fakeClient := fakesm.NewClient()
  290. // good case: first call, since we are using the same key, results should be cached and the counter should not go
  291. // over 1
  292. firstCall := func(smtc *secretsManagerTestCase) {
  293. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  294. smtc.remoteRef.Property = "foo"
  295. smtc.expectedSecret = "bar"
  296. smtc.expectedCounter = aws.Int(1)
  297. smtc.fakeClient = fakeClient
  298. }
  299. secondCall := func(smtc *secretsManagerTestCase) {
  300. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
  301. smtc.remoteRef.Property = "bar"
  302. smtc.expectedSecret = "vodka"
  303. smtc.expectedCounter = aws.Int(1)
  304. smtc.fakeClient = fakeClient
  305. }
  306. notCachedCall := func(smtc *secretsManagerTestCase) {
  307. smtc.apiOutput.SecretString = aws.String(`{"sheldon":"bazinga", "bar":"foo"}`)
  308. smtc.remoteRef.Property = "sheldon"
  309. smtc.expectedSecret = "bazinga"
  310. smtc.expectedCounter = aws.Int(2)
  311. smtc.fakeClient = fakeClient
  312. smtc.apiInput.SecretId = aws.String("xyz")
  313. smtc.remoteRef.Key = "xyz" // it should reset the cache since the key is different
  314. }
  315. cachedCases := []*secretsManagerTestCase{
  316. makeValidSecretsManagerTestCaseCustom(firstCall),
  317. makeValidSecretsManagerTestCaseCustom(firstCall),
  318. makeValidSecretsManagerTestCaseCustom(secondCall),
  319. makeValidSecretsManagerTestCaseCustom(notCachedCall),
  320. }
  321. sm := SecretsManager{
  322. cache: make(map[string]*awssm.GetSecretValueOutput),
  323. }
  324. for k, v := range cachedCases {
  325. sm.client = v.fakeClient
  326. out, err := sm.GetSecret(context.Background(), *v.remoteRef)
  327. if !ErrorContains(err, v.expectError) {
  328. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  329. }
  330. if err == nil && string(out) != v.expectedSecret {
  331. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  332. }
  333. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  334. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  335. }
  336. }
  337. }
  338. func TestGetSecretMap(t *testing.T) {
  339. // good case: default version & deserialization
  340. setDeserialization := func(smtc *secretsManagerTestCase) {
  341. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar"}`)
  342. smtc.expectedData["foo"] = []byte("bar")
  343. }
  344. // good case: nested json
  345. setNestedJSON := func(smtc *secretsManagerTestCase) {
  346. smtc.apiOutput.SecretString = aws.String(`{"foobar":{"baz":"nestedval"}}`)
  347. smtc.expectedData["foobar"] = []byte("{\"baz\":\"nestedval\"}")
  348. }
  349. // good case: caching
  350. cachedMap := func(smtc *secretsManagerTestCase) {
  351. smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "plus": "one"}`)
  352. smtc.expectedData["foo"] = []byte("bar")
  353. smtc.expectedData["plus"] = []byte("one")
  354. smtc.expectedCounter = aws.Int(1)
  355. }
  356. // bad case: invalid json
  357. setInvalidJSON := func(smtc *secretsManagerTestCase) {
  358. smtc.apiOutput.SecretString = aws.String(`-----------------`)
  359. smtc.expectError = "unable to unmarshal secret"
  360. }
  361. successCases := []*secretsManagerTestCase{
  362. makeValidSecretsManagerTestCaseCustom(setDeserialization),
  363. makeValidSecretsManagerTestCaseCustom(setNestedJSON),
  364. makeValidSecretsManagerTestCaseCustom(setAPIErr),
  365. makeValidSecretsManagerTestCaseCustom(setInvalidJSON),
  366. makeValidSecretsManagerTestCaseCustom(cachedMap),
  367. }
  368. for k, v := range successCases {
  369. sm := SecretsManager{
  370. cache: make(map[string]*awssm.GetSecretValueOutput),
  371. client: v.fakeClient,
  372. }
  373. out, err := sm.GetSecretMap(context.Background(), *v.remoteRef)
  374. if !ErrorContains(err, v.expectError) {
  375. t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
  376. }
  377. if err == nil && !cmp.Equal(out, v.expectedData) {
  378. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  379. }
  380. if v.expectedCounter != nil && v.fakeClient.ExecutionCounter != *v.expectedCounter {
  381. t.Errorf("[%d] unexpected counter value: expected %d, got %d", k, v.expectedCounter, v.fakeClient.ExecutionCounter)
  382. }
  383. }
  384. }
  385. func ErrorContains(out error, want string) bool {
  386. if out == nil {
  387. return want == ""
  388. }
  389. if want == "" {
  390. return false
  391. }
  392. return strings.Contains(out.Error(), want)
  393. }
  394. func TestSetSecret(t *testing.T) {
  395. managedBy := managedBy
  396. notManagedBy := "not-managed-by"
  397. secretKey := "fake-secret-key"
  398. secretValue := []byte("fake-value")
  399. fakeSecret := &corev1.Secret{
  400. Data: map[string][]byte{
  401. secretKey: secretValue,
  402. },
  403. }
  404. externalSecrets := externalSecrets
  405. noPermission := errors.New("no permission")
  406. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  407. getSecretCorrectErr := types.ResourceNotFoundException{}
  408. getSecretWrongErr := types.InvalidRequestException{}
  409. secretOutput := &awssm.CreateSecretOutput{
  410. ARN: &arn,
  411. }
  412. externalSecretsTag := []types.Tag{
  413. {
  414. Key: &managedBy,
  415. Value: &externalSecrets,
  416. },
  417. {
  418. Key: ptr.To("taname1"),
  419. Value: ptr.To("tagvalue1"),
  420. },
  421. }
  422. externalSecretsTagFaulty := []types.Tag{
  423. {
  424. Key: &notManagedBy,
  425. Value: &externalSecrets,
  426. },
  427. }
  428. tagSecretOutputNoVersions := &awssm.DescribeSecretOutput{
  429. ARN: &arn,
  430. Tags: externalSecretsTag,
  431. }
  432. defaultVersion := "00000000-0000-0000-0000-000000000002"
  433. tagSecretOutput := &awssm.DescribeSecretOutput{
  434. ARN: &arn,
  435. Tags: externalSecretsTag,
  436. VersionIdsToStages: map[string][]string{
  437. defaultVersion: {"AWSCURRENT"},
  438. },
  439. }
  440. tagSecretOutputFaulty := &awssm.DescribeSecretOutput{
  441. ARN: &arn,
  442. Tags: externalSecretsTagFaulty,
  443. }
  444. tagSecretOutputFrom := func(versionId string) *awssm.DescribeSecretOutput {
  445. return &awssm.DescribeSecretOutput{
  446. ARN: &arn,
  447. Tags: externalSecretsTag,
  448. VersionIdsToStages: map[string][]string{
  449. versionId: {"AWSCURRENT"},
  450. },
  451. }
  452. }
  453. initialVersion := "00000000-0000-0000-0000-000000000001"
  454. defaultUpdatedVersion := "6c70d57a-f53d-bf4d-9525-3503dd5abe8c"
  455. randomUUIDVersion := "9d6202c2-c216-433e-a2f0-5836c4f025af"
  456. randomUUIDVersionIncremented := "4346824b-7da1-4d82-addf-dee197fd5d71"
  457. unparsableVersion := "IAM UNPARSABLE"
  458. secretValueOutput := &awssm.GetSecretValueOutput{
  459. ARN: &arn,
  460. VersionId: &defaultVersion,
  461. }
  462. secretValueOutput2 := &awssm.GetSecretValueOutput{
  463. ARN: &arn,
  464. SecretBinary: secretValue,
  465. VersionId: &defaultVersion,
  466. }
  467. blankDescribeSecretOutput := &awssm.DescribeSecretOutput{}
  468. type params struct {
  469. s string
  470. b []byte
  471. version *string
  472. }
  473. secretValueOutputFrom := func(params params) *awssm.GetSecretValueOutput {
  474. var version *string
  475. if params.version == nil {
  476. version = &defaultVersion
  477. } else {
  478. version = params.version
  479. }
  480. return &awssm.GetSecretValueOutput{
  481. ARN: &arn,
  482. SecretString: &params.s,
  483. SecretBinary: params.b,
  484. VersionId: version,
  485. }
  486. }
  487. putSecretOutput := &awssm.PutSecretValueOutput{
  488. ARN: &arn,
  489. }
  490. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: ""}
  491. pushSecretDataWithoutSecretKey := fake.PushSecretData{RemoteKey: fakeKey, Property: ""}
  492. pushSecretDataWithMetadata := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  493. Raw: []byte(`{
  494. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  495. "kind": "PushSecretMetadata",
  496. "spec": {
  497. "secretPushFormat": "string"
  498. }
  499. }`)}}
  500. pushSecretDataWithProperty := fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "other-fake-property"}
  501. type args struct {
  502. store *esv1.AWSProvider
  503. client fakesm.Client
  504. pushSecretData fake.PushSecretData
  505. newUUID string
  506. kubeclient client.Client
  507. }
  508. type want struct {
  509. err error
  510. }
  511. tests := map[string]struct {
  512. reason string
  513. args args
  514. want want
  515. }{
  516. "SetSecretSucceedsWithExistingSecret": {
  517. reason: "a secret can be pushed to aws secrets manager when it already exists",
  518. args: args{
  519. store: makeValidSecretStore().Spec.Provider.AWS,
  520. client: fakesm.Client{
  521. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  522. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  523. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  524. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  525. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  526. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  527. },
  528. pushSecretData: pushSecretDataWithoutProperty,
  529. },
  530. want: want{
  531. err: nil,
  532. },
  533. },
  534. "SetSecretSucceedsWithExistingSecretButNoSecretVersionsWithoutProperty": {
  535. reason: "a secret can be pushed to aws secrets manager when it already exists but has no secret versions",
  536. args: args{
  537. store: makeValidSecretStore().Spec.Provider.AWS,
  538. client: fakesm.Client{
  539. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputNoVersions, nil),
  540. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  541. SecretBinary: []byte(`fake-value`),
  542. Version: aws.String(initialVersion),
  543. }),
  544. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  545. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  546. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  547. },
  548. pushSecretData: pushSecretDataWithoutProperty,
  549. },
  550. want: want{
  551. err: nil,
  552. },
  553. },
  554. "SetSecretSucceedsWithExistingSecretButNoSecretVersionsWithProperty": {
  555. reason: "a secret can be pushed to aws secrets manager when it already exists but has no secret versions",
  556. args: args{
  557. store: makeValidSecretStore().Spec.Provider.AWS,
  558. client: fakesm.Client{
  559. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputNoVersions, nil),
  560. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  561. SecretBinary: []byte(`{"other-fake-property":"fake-value"}`),
  562. Version: aws.String(initialVersion),
  563. }),
  564. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  565. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  566. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  567. },
  568. pushSecretData: pushSecretDataWithProperty,
  569. },
  570. want: want{
  571. err: nil,
  572. },
  573. },
  574. "SetSecretSucceedsWithoutSecretKey": {
  575. reason: "a secret can be pushed to aws secrets manager without secret key",
  576. args: args{
  577. store: makeValidSecretStore().Spec.Provider.AWS,
  578. client: fakesm.Client{
  579. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  580. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  581. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  582. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  583. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  584. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  585. },
  586. pushSecretData: pushSecretDataWithoutSecretKey,
  587. },
  588. want: want{
  589. err: nil,
  590. },
  591. },
  592. "SetSecretSucceedsWithExistingSecretAndStringFormat": {
  593. reason: "a secret can be pushed to aws secrets manager when it already exists",
  594. args: args{
  595. store: makeValidSecretStore().Spec.Provider.AWS,
  596. client: fakesm.Client{
  597. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  598. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  599. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  600. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  601. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  602. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  603. },
  604. pushSecretData: pushSecretDataWithMetadata,
  605. },
  606. want: want{
  607. err: nil,
  608. },
  609. },
  610. "SetSecretSucceedsWithExistingSecretAndKMSKeyAndDescription": {
  611. reason: "a secret can be pushed to aws secrets manager when it already exists",
  612. args: args{
  613. store: makeValidSecretStore().Spec.Provider.AWS,
  614. client: fakesm.Client{
  615. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, &getSecretCorrectErr),
  616. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  617. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  618. },
  619. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  620. Raw: []byte(`{
  621. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  622. "kind": "PushSecretMetadata",
  623. "spec": {
  624. "kmsKeyID": "bb123123-b2b0-4f60-ac3a-44a13f0e6b6c",
  625. "description": "this is a description"
  626. }
  627. }`)}},
  628. },
  629. want: want{
  630. err: &getSecretCorrectErr,
  631. },
  632. },
  633. "SetSecretSucceedsWithExistingSecretAndAdditionalTags": {
  634. reason: "a secret can be pushed to aws secrets manager when it already exists",
  635. args: args{
  636. store: makeValidSecretStore().Spec.Provider.AWS,
  637. client: fakesm.Client{
  638. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  639. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  640. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  641. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  642. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  643. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  644. },
  645. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  646. Raw: []byte(`{
  647. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  648. "kind": "PushSecretMetadata",
  649. "spec": {
  650. "tags": {"tagname12": "tagvalue1"}
  651. }
  652. }`)}},
  653. },
  654. want: want{
  655. err: nil,
  656. },
  657. },
  658. "SetSecretSucceedsWithNewSecret": {
  659. reason: "a secret can be pushed to aws secrets manager if it doesn't already exist",
  660. args: args{
  661. store: makeValidSecretStore().Spec.Provider.AWS,
  662. client: fakesm.Client{
  663. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  664. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil),
  665. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil),
  666. },
  667. pushSecretData: pushSecretDataWithoutProperty,
  668. },
  669. want: want{
  670. err: nil,
  671. },
  672. },
  673. "SetSecretWithPropertySucceedsWithNewSecret": {
  674. 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",
  675. args: args{
  676. store: makeValidSecretStore().Spec.Provider.AWS,
  677. client: fakesm.Client{
  678. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  679. CreateSecretFn: fakesm.NewCreateSecretFn(secretOutput, nil, []byte(`{"other-fake-property":"fake-value"}`)),
  680. },
  681. pushSecretData: pushSecretDataWithProperty,
  682. },
  683. want: want{
  684. err: nil,
  685. },
  686. },
  687. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyBinary": {
  688. 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)",
  689. args: args{
  690. newUUID: defaultUpdatedVersion,
  691. store: makeValidSecretStore().Spec.Provider.AWS,
  692. client: fakesm.Client{
  693. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{b: []byte((`{"fake-property":"fake-value"}`))}), nil),
  694. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  695. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  696. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  697. Version: &defaultUpdatedVersion,
  698. }),
  699. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  700. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  701. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  702. },
  703. pushSecretData: pushSecretDataWithProperty,
  704. },
  705. want: want{
  706. err: nil,
  707. },
  708. },
  709. "SetSecretWithPropertySucceedsWithExistingSecretAndRandomUUIDVersion": {
  710. 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",
  711. args: args{
  712. store: makeValidSecretStore().Spec.Provider.AWS,
  713. newUUID: randomUUIDVersionIncremented,
  714. client: fakesm.Client{
  715. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
  716. b: []byte((`{"fake-property":"fake-value"}`)),
  717. version: &randomUUIDVersion,
  718. }), nil),
  719. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFrom(randomUUIDVersion), nil),
  720. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  721. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  722. Version: &randomUUIDVersionIncremented,
  723. }),
  724. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  725. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  726. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  727. },
  728. pushSecretData: pushSecretDataWithProperty,
  729. },
  730. want: want{
  731. err: nil,
  732. },
  733. },
  734. "SetSecretWithPropertySucceedsWithExistingSecretAndVersionThatCantBeParsed": {
  735. reason: "A manually set secret version doesn't have to be a UUID",
  736. args: args{
  737. newUUID: unparsableVersion,
  738. store: makeValidSecretStore().Spec.Provider.AWS,
  739. client: fakesm.Client{
  740. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{
  741. b: []byte((`{"fake-property":"fake-value"}`)),
  742. version: &unparsableVersion,
  743. }), nil),
  744. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  745. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  746. SecretBinary: []byte((`fake-value`)),
  747. Version: &unparsableVersion,
  748. }),
  749. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  750. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  751. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  752. },
  753. pushSecretData: pushSecretDataWithoutProperty,
  754. },
  755. want: want{
  756. err: nil,
  757. },
  758. },
  759. "SetSecretWithPropertySucceedsWithExistingSecretAndAbsentVersion": {
  760. reason: "When a secret version is not specified, set it to 1",
  761. args: args{
  762. newUUID: initialVersion,
  763. store: makeValidSecretStore().Spec.Provider.AWS,
  764. client: fakesm.Client{
  765. GetSecretValueFn: fakesm.NewGetSecretValueFn(&awssm.GetSecretValueOutput{
  766. ARN: &arn,
  767. SecretBinary: []byte((`{"fake-property":"fake-value"}`)),
  768. }, nil),
  769. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  770. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  771. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  772. Version: &initialVersion,
  773. }),
  774. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  775. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  776. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  777. },
  778. pushSecretData: pushSecretDataWithProperty,
  779. },
  780. want: want{
  781. err: nil,
  782. },
  783. },
  784. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyString": {
  785. 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)",
  786. args: args{
  787. newUUID: defaultUpdatedVersion,
  788. store: makeValidSecretStore().Spec.Provider.AWS,
  789. client: fakesm.Client{
  790. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":"fake-value"}`}), nil),
  791. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  792. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  793. SecretBinary: []byte(`{"fake-property":"fake-value","other-fake-property":"fake-value"}`),
  794. Version: &defaultUpdatedVersion,
  795. }),
  796. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  797. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  798. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  799. },
  800. pushSecretData: pushSecretDataWithProperty,
  801. },
  802. want: want{
  803. err: nil,
  804. },
  805. },
  806. "SetSecretWithPropertySucceedsWithExistingSecretAndNewPropertyWithDot": {
  807. 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)",
  808. args: args{
  809. newUUID: defaultUpdatedVersion,
  810. store: makeValidSecretStore().Spec.Provider.AWS,
  811. client: fakesm.Client{
  812. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  813. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  814. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil, fakesm.ExpectedPutSecretValueInput{
  815. SecretBinary: []byte(`{"fake-property":{"fake-property":"fake-value","other-fake-property":"fake-value"}}`),
  816. Version: &defaultUpdatedVersion,
  817. }),
  818. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  819. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  820. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  821. },
  822. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "fake-property.other-fake-property"},
  823. },
  824. want: want{
  825. err: nil,
  826. },
  827. },
  828. "SetSecretWithPropertyFailsExistingNonJsonSecret": {
  829. reason: "setting a pushSecretData property is only supported for json secrets",
  830. args: args{
  831. store: makeValidSecretStore().Spec.Provider.AWS,
  832. client: fakesm.Client{
  833. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `non-json-secret`}), nil),
  834. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  835. },
  836. pushSecretData: pushSecretDataWithProperty,
  837. },
  838. want: want{
  839. err: errors.New("PushSecret for aws secrets manager with a pushSecretData property requires a json secret"),
  840. },
  841. },
  842. "SetSecretCreateSecretFails": {
  843. reason: "CreateSecretWithContext returns an error if it fails",
  844. args: args{
  845. store: makeValidSecretStore().Spec.Provider.AWS,
  846. client: fakesm.Client{
  847. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretCorrectErr),
  848. CreateSecretFn: fakesm.NewCreateSecretFn(nil, noPermission),
  849. },
  850. pushSecretData: pushSecretDataWithoutProperty,
  851. },
  852. want: want{
  853. err: noPermission,
  854. },
  855. },
  856. "SetSecretGetSecretFails": {
  857. reason: "GetSecretValueWithContext returns an error if it fails",
  858. args: args{
  859. store: makeValidSecretStore().Spec.Provider.AWS,
  860. client: fakesm.Client{
  861. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, noPermission),
  862. },
  863. pushSecretData: pushSecretDataWithoutProperty,
  864. },
  865. want: want{
  866. err: noPermission,
  867. },
  868. },
  869. "SetSecretWillNotPushSameSecret": {
  870. reason: "secret with the same value will not be pushed",
  871. args: args{
  872. store: makeValidSecretStore().Spec.Provider.AWS,
  873. client: fakesm.Client{
  874. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput2, nil),
  875. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  876. },
  877. pushSecretData: pushSecretDataWithoutProperty,
  878. },
  879. want: want{
  880. err: nil,
  881. },
  882. },
  883. "SetSecretPutSecretValueFails": {
  884. reason: "PutSecretValueWithContext returns an error if it fails",
  885. args: args{
  886. store: makeValidSecretStore().Spec.Provider.AWS,
  887. client: fakesm.Client{
  888. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  889. PutSecretValueFn: fakesm.NewPutSecretValueFn(nil, noPermission),
  890. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  891. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  892. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  893. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  894. },
  895. pushSecretData: pushSecretDataWithoutProperty,
  896. },
  897. want: want{
  898. err: noPermission,
  899. },
  900. },
  901. "SetSecretWrongGetSecretErrFails": {
  902. reason: "DescribeSecret errors out when anything except awssm.ErrCodeResourceNotFoundException",
  903. args: args{
  904. store: makeValidSecretStore().Spec.Provider.AWS,
  905. client: fakesm.Client{
  906. DescribeSecretFn: fakesm.NewDescribeSecretFn(blankDescribeSecretOutput, &getSecretWrongErr),
  907. },
  908. pushSecretData: pushSecretDataWithoutProperty,
  909. },
  910. want: want{
  911. err: &getSecretWrongErr,
  912. },
  913. },
  914. "SetSecretDescribeSecretFails": {
  915. reason: "secret cannot be described",
  916. args: args{
  917. store: makeValidSecretStore().Spec.Provider.AWS,
  918. client: fakesm.Client{
  919. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  920. DescribeSecretFn: fakesm.NewDescribeSecretFn(nil, noPermission),
  921. },
  922. pushSecretData: pushSecretDataWithoutProperty,
  923. },
  924. want: want{
  925. err: noPermission,
  926. },
  927. },
  928. "SetSecretDoesNotOverwriteUntaggedSecret": {
  929. reason: "secret cannot be described",
  930. args: args{
  931. store: makeValidSecretStore().Spec.Provider.AWS,
  932. client: fakesm.Client{
  933. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  934. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutputFaulty, nil),
  935. },
  936. pushSecretData: pushSecretDataWithoutProperty,
  937. },
  938. want: want{
  939. err: errors.New("secret not managed by external-secrets"),
  940. },
  941. },
  942. "PatchSecretTags": {
  943. reason: "secret key is configured with tags to remove and add",
  944. args: args{
  945. store: &esv1.AWSProvider{
  946. Service: esv1.AWSServiceSecretsManager,
  947. Region: "eu-west-2",
  948. },
  949. client: fakesm.Client{
  950. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutputFrom(params{s: `{"fake-property":{"fake-property":"fake-value"}}`}), nil),
  951. DescribeSecretFn: fakesm.NewDescribeSecretFn(&awssm.DescribeSecretOutput{
  952. ARN: &arn,
  953. Tags: []types.Tag{
  954. {Key: &managedBy, Value: &externalSecrets},
  955. {Key: ptr.To("team"), Value: ptr.To("paradox")},
  956. },
  957. }, nil),
  958. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  959. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  960. assert.Len(t, input.Tags, 2)
  961. assert.Contains(t, input.Tags, types.Tag{Key: &managedBy, Value: &externalSecrets})
  962. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("env"), Value: ptr.To("sandbox")})
  963. }),
  964. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
  965. assert.Len(t, input.TagKeys, 1)
  966. assert.Equal(t, []string{"team"}, input.TagKeys)
  967. assert.NotContains(t, input.TagKeys, managedBy)
  968. }),
  969. DeleteResourcePolicyFn: fakesm.NewDeleteResourcePolicyFn(&awssm.DeleteResourcePolicyOutput{}, nil),
  970. },
  971. pushSecretData: fake.PushSecretData{SecretKey: secretKey, RemoteKey: fakeKey, Property: "", Metadata: &apiextensionsv1.JSON{
  972. Raw: []byte(`{
  973. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  974. "kind": "PushSecretMetadata",
  975. "spec": {
  976. "secretPushFormat": "string",
  977. "tags": {
  978. "env": "sandbox"
  979. }
  980. }
  981. }`)}},
  982. },
  983. want: want{
  984. err: nil,
  985. },
  986. },
  987. "SetSecretWithExistingNonChangingResourcePolicy": {
  988. reason: "sync an existing secret without syncing resource policy that has no change",
  989. args: args{
  990. store: makeValidSecretStore().Spec.Provider.AWS,
  991. client: fakesm.Client{
  992. // NO call to PutResourcePolicy
  993. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  994. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  995. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  996. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  997. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  998. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(makeValidGetResourcePolicyOutput(), nil),
  999. },
  1000. pushSecretData: fake.PushSecretData{
  1001. SecretKey: secretKey,
  1002. RemoteKey: fakeKey,
  1003. Property: "",
  1004. Metadata: &apiextensionsv1.JSON{
  1005. Raw: []byte(`{
  1006. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1007. "kind": "PushSecretMetadata",
  1008. "spec": {
  1009. "secretPushFormat": "string",
  1010. "resourcePolicy": {
  1011. "blockPublicPolicy": true,
  1012. "policySourceRef": {
  1013. "kind": "ConfigMap",
  1014. "name": "resource-policy",
  1015. "key": "policy.json"
  1016. }
  1017. }
  1018. }
  1019. }`),
  1020. },
  1021. },
  1022. kubeclient: clientfake.NewFakeClient(&corev1.ConfigMap{
  1023. ObjectMeta: metav1.ObjectMeta{
  1024. Name: "resource-policy",
  1025. },
  1026. // Create a policy that does not match object order of the
  1027. // existing one
  1028. Data: map[string]string{
  1029. "policy.json": `
  1030. {
  1031. "Version": "2012-10-17",
  1032. "Statement": [
  1033. {
  1034. "Resource": "*",
  1035. "Effect": "Deny",
  1036. "Principal": "*",
  1037. "Action": [
  1038. "secretsmanager:PutResourcePolicy",
  1039. "secretsmanager:DeleteResourcePolicy",
  1040. "secretsmanager:GetResourcePolicy"
  1041. ],
  1042. "Condition": {
  1043. "ArnNotEquals": {
  1044. "aws:PrincipalArn": [
  1045. "arn:aws:iam::000000000000:root",
  1046. "arn:aws:iam::000000000000:role/admin"
  1047. ]
  1048. }
  1049. },
  1050. "Sid": "DenyPolicyChangesExceptAdmins"
  1051. }
  1052. ]
  1053. }
  1054. `,
  1055. },
  1056. }),
  1057. },
  1058. want: want{
  1059. err: nil,
  1060. },
  1061. },
  1062. "SetSecretWithExistingChangingResourcePolicy": {
  1063. reason: "sync an existing secret and the resource policy when it has changes",
  1064. args: args{
  1065. store: makeValidSecretStore().Spec.Provider.AWS,
  1066. client: fakesm.Client{
  1067. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1068. PutSecretValueFn: fakesm.NewPutSecretValueFn(putSecretOutput, nil),
  1069. DescribeSecretFn: fakesm.NewDescribeSecretFn(tagSecretOutput, nil),
  1070. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil),
  1071. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil),
  1072. GetResourcePolicyFn: fakesm.NewGetResourcePolicyFn(makeValidGetResourcePolicyOutput(), nil),
  1073. // Call to PutResourcePolicy since policy does not match
  1074. PutResourcePolicyFn: fakesm.NewPutResourcePolicyFn(&awssm.PutResourcePolicyOutput{}, nil),
  1075. },
  1076. pushSecretData: fake.PushSecretData{
  1077. SecretKey: secretKey,
  1078. RemoteKey: fakeKey,
  1079. Property: "",
  1080. Metadata: &apiextensionsv1.JSON{
  1081. Raw: []byte(`{
  1082. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1083. "kind": "PushSecretMetadata",
  1084. "spec": {
  1085. "secretPushFormat": "string",
  1086. "resourcePolicy": {
  1087. "blockPublicPolicy": true,
  1088. "policySourceRef": {
  1089. "kind": "ConfigMap",
  1090. "name": "resource-policy",
  1091. "key": "policy.json"
  1092. }
  1093. }
  1094. }
  1095. }`),
  1096. },
  1097. },
  1098. kubeclient: clientfake.NewFakeClient(&corev1.ConfigMap{
  1099. ObjectMeta: metav1.ObjectMeta{
  1100. Name: "resource-policy",
  1101. },
  1102. // Create a policy that does not match object order of the
  1103. // existing one
  1104. Data: map[string]string{
  1105. "policy.json": `
  1106. {
  1107. "Version": "2012-10-17",
  1108. "Statement": [
  1109. {
  1110. "Resource": "*",
  1111. "Effect": "Deny",
  1112. "Principal": "*",
  1113. "Action": [
  1114. "secretsmanager:PutResourcePolicy",
  1115. "secretsmanager:DeleteResourcePolicy",
  1116. "secretsmanager:GetResourcePolicy"
  1117. ],
  1118. "Condition": {
  1119. "ArnNotEquals": {
  1120. "aws:PrincipalArn": [
  1121. "arn:aws:iam::000000000000:root",
  1122. "arn:aws:iam::000000000000:role/sudo"
  1123. ]
  1124. }
  1125. },
  1126. "Sid": "DenyPolicyChangesExceptAdmins"
  1127. }
  1128. ]
  1129. }
  1130. `,
  1131. },
  1132. }),
  1133. },
  1134. want: want{
  1135. err: nil,
  1136. },
  1137. },
  1138. }
  1139. for name, tc := range tests {
  1140. t.Run(name, func(t *testing.T) {
  1141. sm := SecretsManager{
  1142. client: &tc.args.client,
  1143. prefix: tc.args.store.Prefix,
  1144. newUUID: func() string { return tc.args.newUUID },
  1145. kube: tc.args.kubeclient,
  1146. }
  1147. err := sm.PushSecret(context.Background(), fakeSecret, tc.args.pushSecretData)
  1148. // Error nil XOR tc.want.err nil
  1149. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1150. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1151. }
  1152. // if errors are the same type but their contents do not match
  1153. if err != nil && tc.want.err != nil {
  1154. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1155. t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1156. }
  1157. }
  1158. })
  1159. }
  1160. }
  1161. func TestDeleteSecret(t *testing.T) {
  1162. fakeClient := fakesm.Client{}
  1163. managed := managedBy
  1164. manager := externalSecrets
  1165. secretTag := types.Tag{
  1166. Key: &managed,
  1167. Value: &manager,
  1168. }
  1169. type args struct {
  1170. client fakesm.Client
  1171. config esv1.SecretsManager
  1172. prefix string
  1173. getSecretOutput *awssm.GetSecretValueOutput
  1174. describeSecretOutput *awssm.DescribeSecretOutput
  1175. deleteSecretOutput *awssm.DeleteSecretOutput
  1176. getSecretErr error
  1177. describeSecretErr error
  1178. deleteSecretErr error
  1179. }
  1180. type want struct {
  1181. err error
  1182. }
  1183. type testCase struct {
  1184. args args
  1185. want want
  1186. reason string
  1187. }
  1188. tests := map[string]testCase{
  1189. "Deletes Successfully": {
  1190. args: args{
  1191. client: fakeClient,
  1192. config: esv1.SecretsManager{},
  1193. getSecretOutput: &awssm.GetSecretValueOutput{},
  1194. describeSecretOutput: &awssm.DescribeSecretOutput{
  1195. Tags: []types.Tag{secretTag},
  1196. },
  1197. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1198. getSecretErr: nil,
  1199. describeSecretErr: nil,
  1200. deleteSecretErr: nil,
  1201. },
  1202. want: want{
  1203. err: nil,
  1204. },
  1205. reason: "",
  1206. },
  1207. "Deletes Successfully with ForceDeleteWithoutRecovery": {
  1208. args: args{
  1209. client: fakeClient,
  1210. config: esv1.SecretsManager{
  1211. ForceDeleteWithoutRecovery: true,
  1212. },
  1213. getSecretOutput: &awssm.GetSecretValueOutput{},
  1214. describeSecretOutput: &awssm.DescribeSecretOutput{
  1215. Tags: []types.Tag{secretTag},
  1216. },
  1217. deleteSecretOutput: &awssm.DeleteSecretOutput{
  1218. DeletionDate: aws.Time(time.Now()),
  1219. },
  1220. getSecretErr: nil,
  1221. describeSecretErr: nil,
  1222. deleteSecretErr: nil,
  1223. },
  1224. want: want{
  1225. err: nil,
  1226. },
  1227. reason: "",
  1228. },
  1229. "Not Managed by ESO": {
  1230. args: args{
  1231. client: fakeClient,
  1232. config: esv1.SecretsManager{},
  1233. getSecretOutput: &awssm.GetSecretValueOutput{},
  1234. describeSecretOutput: &awssm.DescribeSecretOutput{
  1235. Tags: []types.Tag{},
  1236. },
  1237. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1238. getSecretErr: nil,
  1239. describeSecretErr: nil,
  1240. deleteSecretErr: nil,
  1241. },
  1242. want: want{
  1243. err: nil,
  1244. },
  1245. reason: "",
  1246. },
  1247. "Invalid Recovery Window": {
  1248. args: args{
  1249. client: fakesm.Client{},
  1250. config: esv1.SecretsManager{
  1251. RecoveryWindowInDays: 1,
  1252. },
  1253. getSecretOutput: &awssm.GetSecretValueOutput{},
  1254. describeSecretOutput: &awssm.DescribeSecretOutput{
  1255. Tags: []types.Tag{secretTag},
  1256. },
  1257. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1258. getSecretErr: nil,
  1259. describeSecretErr: nil,
  1260. deleteSecretErr: nil,
  1261. },
  1262. want: want{
  1263. err: errors.New("invalid DeleteSecretInput: RecoveryWindowInDays must be between 7 and 30 days"),
  1264. },
  1265. reason: "",
  1266. },
  1267. "RecoveryWindowInDays is supplied with ForceDeleteWithoutRecovery": {
  1268. args: args{
  1269. client: fakesm.Client{},
  1270. config: esv1.SecretsManager{
  1271. RecoveryWindowInDays: 7,
  1272. ForceDeleteWithoutRecovery: true,
  1273. },
  1274. getSecretOutput: &awssm.GetSecretValueOutput{},
  1275. describeSecretOutput: &awssm.DescribeSecretOutput{
  1276. Tags: []types.Tag{secretTag},
  1277. },
  1278. deleteSecretOutput: &awssm.DeleteSecretOutput{},
  1279. getSecretErr: nil,
  1280. describeSecretErr: nil,
  1281. deleteSecretErr: nil,
  1282. },
  1283. want: want{
  1284. err: errors.New("invalid DeleteSecretInput: ForceDeleteWithoutRecovery conflicts with RecoveryWindowInDays"),
  1285. },
  1286. reason: "",
  1287. },
  1288. "Failed to get Tags": {
  1289. args: args{
  1290. client: fakeClient,
  1291. config: esv1.SecretsManager{},
  1292. getSecretOutput: &awssm.GetSecretValueOutput{},
  1293. describeSecretOutput: nil,
  1294. deleteSecretOutput: nil,
  1295. getSecretErr: nil,
  1296. describeSecretErr: errors.New("failed to get tags"),
  1297. deleteSecretErr: nil,
  1298. },
  1299. want: want{
  1300. err: errors.New("failed to get tags"),
  1301. },
  1302. reason: "",
  1303. },
  1304. "Secret Not Found": {
  1305. args: args{
  1306. client: fakeClient,
  1307. config: esv1.SecretsManager{},
  1308. getSecretOutput: nil,
  1309. describeSecretOutput: nil,
  1310. deleteSecretOutput: nil,
  1311. getSecretErr: errors.New("not here, sorry dude"),
  1312. describeSecretErr: nil,
  1313. deleteSecretErr: nil,
  1314. },
  1315. want: want{
  1316. err: errors.New("not here, sorry dude"),
  1317. },
  1318. },
  1319. "Not expected AWS error": {
  1320. args: args{
  1321. client: fakeClient,
  1322. config: esv1.SecretsManager{},
  1323. getSecretOutput: nil,
  1324. describeSecretOutput: nil,
  1325. deleteSecretOutput: nil,
  1326. getSecretErr: errors.New("aws unavailable"),
  1327. describeSecretErr: nil,
  1328. deleteSecretErr: nil,
  1329. },
  1330. want: want{
  1331. err: errors.New("aws unavailable"),
  1332. },
  1333. },
  1334. "unexpected error": {
  1335. args: args{
  1336. client: fakeClient,
  1337. config: esv1.SecretsManager{},
  1338. getSecretOutput: nil,
  1339. describeSecretOutput: nil,
  1340. deleteSecretOutput: nil,
  1341. getSecretErr: errors.New("timeout"),
  1342. describeSecretErr: nil,
  1343. deleteSecretErr: nil,
  1344. },
  1345. want: want{
  1346. err: errors.New("timeout"),
  1347. },
  1348. },
  1349. "DeleteWithPrefix": {
  1350. args: args{
  1351. client: fakesm.Client{
  1352. GetSecretValueFn: func(ctx context.Context, input *awssm.GetSecretValueInput, opts ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  1353. // Verify that the input secret ID has the prefix applied
  1354. if *input.SecretId != "my-prefix-"+fakeKey {
  1355. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1356. }
  1357. return &awssm.GetSecretValueOutput{}, nil
  1358. },
  1359. DescribeSecretFn: func(ctx context.Context, input *awssm.DescribeSecretInput, opts ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error) {
  1360. // Verify that the input secret ID has the prefix applied
  1361. if *input.SecretId != "my-prefix-"+fakeKey {
  1362. return nil, fmt.Errorf("expected secret name to be prefixed with 'my-prefix-', got %s", *input.SecretId)
  1363. }
  1364. return &awssm.DescribeSecretOutput{
  1365. Tags: []types.Tag{secretTag},
  1366. }, nil
  1367. },
  1368. DeleteSecretFn: func(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
  1369. return &awssm.DeleteSecretOutput{}, nil
  1370. },
  1371. },
  1372. config: esv1.SecretsManager{},
  1373. prefix: "my-prefix-",
  1374. getSecretOutput: nil,
  1375. describeSecretOutput: nil,
  1376. deleteSecretOutput: nil,
  1377. getSecretErr: nil,
  1378. describeSecretErr: nil,
  1379. deleteSecretErr: nil,
  1380. },
  1381. want: want{
  1382. err: nil,
  1383. },
  1384. reason: "Verifies that the prefix is correctly applied when deleting a secret",
  1385. },
  1386. }
  1387. for name, tc := range tests {
  1388. t.Run(name, func(t *testing.T) {
  1389. ref := fake.PushSecretData{RemoteKey: fakeKey}
  1390. sm := SecretsManager{
  1391. client: &tc.args.client,
  1392. config: &tc.args.config,
  1393. prefix: tc.args.prefix,
  1394. }
  1395. if tc.args.client.GetSecretValueFn == nil {
  1396. tc.args.client.GetSecretValueFn = fakesm.NewGetSecretValueFn(tc.args.getSecretOutput, tc.args.getSecretErr)
  1397. }
  1398. if tc.args.client.DescribeSecretFn == nil {
  1399. tc.args.client.DescribeSecretFn = fakesm.NewDescribeSecretFn(tc.args.describeSecretOutput, tc.args.describeSecretErr)
  1400. }
  1401. if tc.args.client.DeleteSecretFn == nil {
  1402. tc.args.client.DeleteSecretFn = fakesm.NewDeleteSecretFn(tc.args.deleteSecretOutput, tc.args.deleteSecretErr)
  1403. }
  1404. err := sm.DeleteSecret(context.TODO(), ref)
  1405. t.Logf("DeleteSecret error: %v", err)
  1406. // Error nil XOR tc.want.err nil
  1407. if ((err == nil) || (tc.want.err == nil)) && !((err == nil) && (tc.want.err == nil)) {
  1408. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, err)
  1409. }
  1410. // if errors are the same type but their contents do not match
  1411. if err != nil && tc.want.err != nil {
  1412. if !strings.Contains(err.Error(), tc.want.err.Error()) {
  1413. t.Errorf("\nTesting DeleteSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error got nil", name, tc.reason, tc.want.err)
  1414. }
  1415. }
  1416. })
  1417. }
  1418. }
  1419. func makeValidSecretStore() *esv1.SecretStore {
  1420. return &esv1.SecretStore{
  1421. ObjectMeta: metav1.ObjectMeta{
  1422. Name: "aws-secret-store",
  1423. Namespace: "default",
  1424. },
  1425. Spec: esv1.SecretStoreSpec{
  1426. Provider: &esv1.SecretStoreProvider{
  1427. AWS: &esv1.AWSProvider{
  1428. Service: esv1.AWSServiceSecretsManager,
  1429. Region: "eu-west-2",
  1430. },
  1431. },
  1432. },
  1433. }
  1434. }
  1435. func getTagSlice() []types.Tag {
  1436. tagKey1 := tagname1
  1437. tagValue1 := tagvalue1
  1438. tagKey2 := tagname2
  1439. tagValue2 := tagvalue2
  1440. return []types.Tag{
  1441. {
  1442. Key: &tagKey1,
  1443. Value: &tagValue1,
  1444. },
  1445. {
  1446. Key: &tagKey2,
  1447. Value: &tagValue2,
  1448. },
  1449. }
  1450. }
  1451. func TestSecretsManagerGetAllSecrets(t *testing.T) {
  1452. ctx := context.Background()
  1453. errBoom := errors.New("boom")
  1454. secretName := "my-secret"
  1455. secretVersion := "AWSCURRENT"
  1456. secretPath := "/path/to/secret"
  1457. secretValue := "secret value"
  1458. secretTags := map[string]string{
  1459. "foo": "bar",
  1460. }
  1461. // Test cases
  1462. testCases := []struct {
  1463. name string
  1464. ref esv1.ExternalSecretFind
  1465. secretName string
  1466. secretVersion string
  1467. secretValue string
  1468. batchGetSecretValueFn func(context.Context, *awssm.BatchGetSecretValueInput, ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error)
  1469. listSecretsFn func(context.Context, *awssm.ListSecretsInput, ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error)
  1470. getSecretValueFn func(context.Context, *awssm.GetSecretValueInput, ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error)
  1471. expectedData map[string][]byte
  1472. expectedError string
  1473. }{
  1474. {
  1475. name: "Matching secrets found",
  1476. ref: esv1.ExternalSecretFind{
  1477. Name: &esv1.FindName{
  1478. RegExp: secretName,
  1479. },
  1480. Path: ptr.To(secretPath),
  1481. },
  1482. secretName: secretName,
  1483. secretVersion: secretVersion,
  1484. secretValue: secretValue,
  1485. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1486. assert.Len(t, input.Filters, 1)
  1487. assert.Equal(t, "name", string(input.Filters[0].Key))
  1488. assert.Equal(t, secretPath, input.Filters[0].Values[0])
  1489. return &awssm.BatchGetSecretValueOutput{
  1490. SecretValues: []types.SecretValueEntry{
  1491. {
  1492. Name: ptr.To(secretName),
  1493. VersionStages: []string{secretVersion},
  1494. SecretBinary: []byte(secretValue),
  1495. },
  1496. },
  1497. }, nil
  1498. },
  1499. expectedData: map[string][]byte{
  1500. secretName: []byte(secretValue),
  1501. },
  1502. expectedError: "",
  1503. },
  1504. {
  1505. name: "Error occurred while fetching secret value",
  1506. ref: esv1.ExternalSecretFind{
  1507. Name: &esv1.FindName{
  1508. RegExp: secretName,
  1509. },
  1510. Path: ptr.To(secretPath),
  1511. },
  1512. secretName: secretName,
  1513. secretVersion: secretVersion,
  1514. secretValue: secretValue,
  1515. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1516. return &awssm.BatchGetSecretValueOutput{
  1517. SecretValues: []types.SecretValueEntry{
  1518. {
  1519. Name: ptr.To(secretName),
  1520. },
  1521. },
  1522. }, errBoom
  1523. },
  1524. expectedData: nil,
  1525. expectedError: errBoom.Error(),
  1526. },
  1527. {
  1528. name: "regexp: error occurred while listing secrets",
  1529. ref: esv1.ExternalSecretFind{
  1530. Name: &esv1.FindName{
  1531. RegExp: secretName,
  1532. },
  1533. },
  1534. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1535. return nil, errBoom
  1536. },
  1537. expectedData: nil,
  1538. expectedError: errBoom.Error(),
  1539. },
  1540. {
  1541. name: "regep: no matching secrets found",
  1542. ref: esv1.ExternalSecretFind{
  1543. Name: &esv1.FindName{
  1544. RegExp: secretName,
  1545. },
  1546. },
  1547. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1548. return &awssm.ListSecretsOutput{
  1549. SecretList: []types.SecretListEntry{
  1550. {
  1551. Name: ptr.To("other-secret"),
  1552. },
  1553. },
  1554. }, nil
  1555. },
  1556. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1557. return &awssm.BatchGetSecretValueOutput{
  1558. SecretValues: []types.SecretValueEntry{
  1559. {
  1560. Name: ptr.To("other-secret"),
  1561. },
  1562. },
  1563. }, nil
  1564. },
  1565. expectedData: make(map[string][]byte),
  1566. expectedError: "",
  1567. },
  1568. {
  1569. name: "invalid regexp",
  1570. ref: esv1.ExternalSecretFind{
  1571. Name: &esv1.FindName{
  1572. RegExp: "[",
  1573. },
  1574. },
  1575. expectedData: nil,
  1576. expectedError: "could not compile find.name.regexp [[]: error parsing regexp: missing closing ]: `[`",
  1577. },
  1578. {
  1579. name: "tags: Matching secrets found",
  1580. ref: esv1.ExternalSecretFind{
  1581. Tags: secretTags,
  1582. },
  1583. secretName: secretName,
  1584. secretVersion: secretVersion,
  1585. secretValue: secretValue,
  1586. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1587. assert.Len(t, input.Filters, 2)
  1588. assert.Equal(t, "tag-key", string(input.Filters[0].Key))
  1589. assert.Equal(t, "foo", input.Filters[0].Values[0])
  1590. assert.Equal(t, "tag-value", string(input.Filters[1].Key))
  1591. assert.Equal(t, "bar", input.Filters[1].Values[0])
  1592. return &awssm.BatchGetSecretValueOutput{
  1593. SecretValues: []types.SecretValueEntry{
  1594. {
  1595. Name: ptr.To(secretName),
  1596. VersionStages: []string{secretVersion},
  1597. SecretBinary: []byte(secretValue),
  1598. },
  1599. },
  1600. }, nil
  1601. },
  1602. expectedData: map[string][]byte{
  1603. secretName: []byte(secretValue),
  1604. },
  1605. expectedError: "",
  1606. },
  1607. {
  1608. name: "name and tags: matching secrets found",
  1609. ref: esv1.ExternalSecretFind{
  1610. Name: &esv1.FindName{
  1611. RegExp: secretName,
  1612. },
  1613. Tags: secretTags,
  1614. },
  1615. listSecretsFn: func(_ context.Context, input *awssm.ListSecretsInput, _ ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  1616. allSecrets := []types.SecretListEntry{
  1617. {
  1618. Name: ptr.To(secretName),
  1619. Tags: []types.Tag{
  1620. {Key: ptr.To("foo"), Value: ptr.To("bar")},
  1621. },
  1622. },
  1623. {
  1624. Name: ptr.To(fmt.Sprintf("%ssomeothertext", secretName)),
  1625. },
  1626. {
  1627. Name: ptr.To("unmatched-secret"),
  1628. Tags: []types.Tag{
  1629. {Key: ptr.To("foo"), Value: ptr.To("bar")},
  1630. },
  1631. },
  1632. }
  1633. filtered := make([]types.SecretListEntry, 0, len(allSecrets))
  1634. for _, secret := range allSecrets {
  1635. exclude := false
  1636. tagMap := map[string]string{}
  1637. for _, t := range secret.Tags {
  1638. if t.Key != nil && t.Value != nil {
  1639. tagMap[*t.Key] = *t.Value
  1640. }
  1641. }
  1642. for _, f := range input.Filters {
  1643. switch f.Key {
  1644. case types.FilterNameStringTypeName:
  1645. if secret.Name != nil {
  1646. for _, v := range f.Values {
  1647. if strings.Contains(*secret.Name, v) {
  1648. exclude = true
  1649. break
  1650. }
  1651. }
  1652. }
  1653. case types.FilterNameStringTypeTagKey:
  1654. for _, v := range f.Values {
  1655. if tagMap[v] == "" {
  1656. exclude = true
  1657. break
  1658. }
  1659. }
  1660. case types.FilterNameStringTypeDescription,
  1661. types.FilterNameStringTypeTagValue,
  1662. types.FilterNameStringTypePrimaryRegion,
  1663. types.FilterNameStringTypeOwningService,
  1664. types.FilterNameStringTypeAll:
  1665. continue
  1666. }
  1667. }
  1668. if !exclude {
  1669. filtered = append(filtered, secret)
  1670. }
  1671. }
  1672. return &awssm.ListSecretsOutput{SecretList: filtered}, nil
  1673. },
  1674. getSecretValueFn: func(_ context.Context, input *awssm.GetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  1675. if *input.SecretId == secretName {
  1676. return &awssm.GetSecretValueOutput{
  1677. Name: ptr.To(secretName),
  1678. VersionStages: []string{secretVersion},
  1679. SecretBinary: []byte(secretValue),
  1680. }, nil
  1681. }
  1682. if *input.SecretId == "unmatched-secret" {
  1683. return &awssm.GetSecretValueOutput{
  1684. Name: ptr.To("unmatched-secret"),
  1685. VersionStages: []string{secretVersion},
  1686. SecretBinary: []byte("othervalue"),
  1687. }, nil
  1688. }
  1689. return &awssm.GetSecretValueOutput{
  1690. Name: ptr.To(fmt.Sprintf("%ssomeothertext", secretName)),
  1691. VersionStages: []string{secretVersion},
  1692. SecretBinary: []byte("someothervalue"),
  1693. }, nil
  1694. },
  1695. expectedData: map[string][]byte{
  1696. secretName: []byte(secretValue),
  1697. },
  1698. expectedError: "",
  1699. },
  1700. {
  1701. name: "tags: error occurred while fetching secret value",
  1702. ref: esv1.ExternalSecretFind{
  1703. Tags: secretTags,
  1704. },
  1705. secretName: secretName,
  1706. secretVersion: secretVersion,
  1707. secretValue: secretValue,
  1708. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1709. return &awssm.BatchGetSecretValueOutput{
  1710. SecretValues: []types.SecretValueEntry{
  1711. {
  1712. Name: ptr.To(secretName),
  1713. VersionStages: []string{secretVersion},
  1714. SecretBinary: []byte(secretValue),
  1715. },
  1716. },
  1717. }, errBoom
  1718. },
  1719. expectedData: nil,
  1720. expectedError: errBoom.Error(),
  1721. },
  1722. {
  1723. name: "tags: error occurred while listing secrets",
  1724. ref: esv1.ExternalSecretFind{
  1725. Tags: secretTags,
  1726. },
  1727. batchGetSecretValueFn: func(_ context.Context, input *awssm.BatchGetSecretValueInput, _ ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  1728. return nil, errBoom
  1729. },
  1730. expectedData: nil,
  1731. expectedError: errBoom.Error(),
  1732. },
  1733. }
  1734. for _, tc := range testCases {
  1735. t.Run(tc.name, func(t *testing.T) {
  1736. fc := fakesm.NewClient()
  1737. fc.BatchGetSecretValueFn = tc.batchGetSecretValueFn
  1738. fc.ListSecretsFn = tc.listSecretsFn
  1739. fc.GetSecretValueFn = tc.getSecretValueFn
  1740. sm := SecretsManager{
  1741. client: fc,
  1742. cache: make(map[string]*awssm.GetSecretValueOutput),
  1743. }
  1744. data, err := sm.GetAllSecrets(ctx, tc.ref)
  1745. if err != nil && err.Error() != tc.expectedError {
  1746. t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
  1747. }
  1748. if !reflect.DeepEqual(data, tc.expectedData) {
  1749. t.Errorf("unexpected data: got %v, want %v", data, tc.expectedData)
  1750. }
  1751. })
  1752. }
  1753. }
  1754. func TestSecretsManagerValidate(t *testing.T) {
  1755. type fields struct {
  1756. cfg *aws.Config
  1757. referentAuth bool
  1758. }
  1759. validConfig := &aws.Config{
  1760. Credentials: credentials.NewStaticCredentialsProvider(
  1761. "fake",
  1762. "fake",
  1763. "fake",
  1764. ),
  1765. }
  1766. invalidConfig := &aws.Config{
  1767. Credentials: &FakeCredProvider{
  1768. retrieveFunc: func() (aws.Credentials, error) {
  1769. return aws.Credentials{}, errors.New("invalid credentials")
  1770. },
  1771. },
  1772. }
  1773. tests := []struct {
  1774. name string
  1775. fields fields
  1776. want esv1.ValidationResult
  1777. wantErr bool
  1778. }{
  1779. {
  1780. name: "ReferentAuth should always return unknown",
  1781. fields: fields{
  1782. referentAuth: true,
  1783. },
  1784. want: esv1.ValidationResultUnknown,
  1785. },
  1786. {
  1787. name: "Valid credentials should return ready",
  1788. fields: fields{
  1789. cfg: validConfig,
  1790. },
  1791. want: esv1.ValidationResultReady,
  1792. },
  1793. {
  1794. name: "Invalid credentials should return error",
  1795. fields: fields{
  1796. cfg: invalidConfig,
  1797. },
  1798. want: esv1.ValidationResultError,
  1799. wantErr: true,
  1800. },
  1801. }
  1802. for _, tt := range tests {
  1803. t.Run(tt.name, func(t *testing.T) {
  1804. sm := &SecretsManager{
  1805. cfg: tt.fields.cfg,
  1806. referentAuth: tt.fields.referentAuth,
  1807. }
  1808. got, err := sm.Validate()
  1809. if (err != nil) != tt.wantErr {
  1810. t.Errorf("SecretsManager.Validate() error = %v, wantErr %v", err, tt.wantErr)
  1811. return
  1812. }
  1813. if !reflect.DeepEqual(got, tt.want) {
  1814. t.Errorf("SecretsManager.Validate() = %v, want %v", got, tt.want)
  1815. }
  1816. })
  1817. }
  1818. }
  1819. func TestSecretExists(t *testing.T) {
  1820. arn := "arn:aws:secretsmanager:us-east-1:702902267788:secret:foo-bar5-Robbgh"
  1821. defaultVersion := "00000000-0000-0000-0000-000000000002"
  1822. secretValueOutput := &awssm.GetSecretValueOutput{
  1823. ARN: &arn,
  1824. VersionId: &defaultVersion,
  1825. }
  1826. blankSecretValueOutput := &awssm.GetSecretValueOutput{}
  1827. getSecretCorrectErr := types.ResourceNotFoundException{}
  1828. getSecretWrongErr := types.InvalidRequestException{}
  1829. pushSecretDataWithoutProperty := fake.PushSecretData{SecretKey: "fake-secret-key", RemoteKey: fakeKey, Property: ""}
  1830. type args struct {
  1831. store *esv1.AWSProvider
  1832. client fakesm.Client
  1833. pushSecretData fake.PushSecretData
  1834. }
  1835. type want struct {
  1836. err error
  1837. wantError bool
  1838. }
  1839. tests := map[string]struct {
  1840. args args
  1841. want want
  1842. }{
  1843. "SecretExistsReturnsTrueForExistingSecret": {
  1844. args: args{
  1845. store: makeValidSecretStore().Spec.Provider.AWS,
  1846. client: fakesm.Client{
  1847. GetSecretValueFn: fakesm.NewGetSecretValueFn(secretValueOutput, nil),
  1848. },
  1849. pushSecretData: pushSecretDataWithoutProperty,
  1850. },
  1851. want: want{
  1852. err: nil,
  1853. wantError: true,
  1854. },
  1855. },
  1856. "SecretExistsReturnsFalseForNonExistingSecret": {
  1857. args: args{
  1858. store: makeValidSecretStore().Spec.Provider.AWS,
  1859. client: fakesm.Client{
  1860. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretCorrectErr),
  1861. },
  1862. pushSecretData: pushSecretDataWithoutProperty,
  1863. },
  1864. want: want{
  1865. err: nil,
  1866. wantError: false,
  1867. },
  1868. },
  1869. "SecretExistsReturnsFalseForErroredSecret": {
  1870. args: args{
  1871. store: makeValidSecretStore().Spec.Provider.AWS,
  1872. client: fakesm.Client{
  1873. GetSecretValueFn: fakesm.NewGetSecretValueFn(blankSecretValueOutput, &getSecretWrongErr),
  1874. },
  1875. pushSecretData: pushSecretDataWithoutProperty,
  1876. },
  1877. want: want{
  1878. err: &getSecretWrongErr,
  1879. wantError: false,
  1880. },
  1881. },
  1882. }
  1883. for name, tc := range tests {
  1884. t.Run(name, func(t *testing.T) {
  1885. sm := &SecretsManager{
  1886. client: &tc.args.client,
  1887. }
  1888. got, err := sm.SecretExists(context.Background(), tc.args.pushSecretData)
  1889. assert.Equal(
  1890. t,
  1891. tc.want,
  1892. want{
  1893. err: err,
  1894. wantError: got,
  1895. })
  1896. })
  1897. }
  1898. }
  1899. func TestConstructMetadataWithDefaults(t *testing.T) {
  1900. tests := []struct {
  1901. name string
  1902. input *apiextensionsv1.JSON
  1903. expected *metadata.PushSecretMetadata[PushSecretMetadataSpec]
  1904. expectError bool
  1905. }{
  1906. {
  1907. name: "Valid metadata with multiple fields",
  1908. input: &apiextensionsv1.JSON{Raw: []byte(`{
  1909. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1910. "kind": "PushSecretMetadata",
  1911. "spec": {
  1912. "description": "test description",
  1913. "secretPushFormat":"string",
  1914. "kmsKeyID": "custom-kms-key",
  1915. "tags": {
  1916. "customKey": "customValue"
  1917. },
  1918. }
  1919. }`)},
  1920. expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
  1921. APIVersion: "kubernetes.external-secrets.io/v1alpha1",
  1922. Kind: "PushSecretMetadata",
  1923. Spec: PushSecretMetadataSpec{
  1924. Description: "test description",
  1925. SecretPushFormat: "string",
  1926. KMSKeyID: "custom-kms-key",
  1927. Tags: map[string]string{
  1928. "customKey": "customValue",
  1929. managedBy: externalSecrets,
  1930. },
  1931. },
  1932. },
  1933. },
  1934. {
  1935. name: "Empty metadata, defaults applied",
  1936. input: nil,
  1937. expected: &metadata.PushSecretMetadata[PushSecretMetadataSpec]{
  1938. Spec: PushSecretMetadataSpec{
  1939. Description: fmt.Sprintf("secret '%s:%s'", managedBy, externalSecrets),
  1940. SecretPushFormat: "binary",
  1941. KMSKeyID: "alias/aws/secretsmanager",
  1942. Tags: map[string]string{
  1943. managedBy: externalSecrets,
  1944. },
  1945. },
  1946. },
  1947. },
  1948. {
  1949. name: "Added default metadata with 'managed-by' tag",
  1950. input: &apiextensionsv1.JSON{Raw: []byte(`{
  1951. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  1952. "kind": "PushSecretMetadata",
  1953. "spec": {
  1954. "tags": {
  1955. "managed-by": "external-secrets",
  1956. "customKey": "customValue"
  1957. },
  1958. }
  1959. }`)},
  1960. expected: nil,
  1961. expectError: true,
  1962. },
  1963. {
  1964. name: "Invalid metadata format",
  1965. input: &apiextensionsv1.JSON{Raw: []byte(`invalid-json`)},
  1966. expected: nil,
  1967. expectError: true,
  1968. },
  1969. {
  1970. name: "Metadata with 'managed-by' tag specified",
  1971. input: &apiextensionsv1.JSON{Raw: []byte(`{"tags":{"managed-by":"invalid"}}`)},
  1972. expected: nil,
  1973. expectError: true,
  1974. },
  1975. }
  1976. for _, tt := range tests {
  1977. t.Run(tt.name, func(t *testing.T) {
  1978. result, err := (&SecretsManager{}).constructMetadataWithDefaults(tt.input)
  1979. if tt.expectError {
  1980. assert.Error(t, err)
  1981. } else {
  1982. assert.NoError(t, err)
  1983. assert.Equal(t, tt.expected, result)
  1984. }
  1985. })
  1986. }
  1987. }
  1988. func TestComputeTagsToUpdate(t *testing.T) {
  1989. tests := []struct {
  1990. name string
  1991. tags map[string]string
  1992. metaTags map[string]string
  1993. expected []types.Tag
  1994. modified bool
  1995. }{
  1996. {
  1997. name: "No tags to update",
  1998. tags: map[string]string{
  1999. "key1": "value1",
  2000. "key2": "value2",
  2001. },
  2002. metaTags: map[string]string{
  2003. "key1": "value1",
  2004. "key2": "value2",
  2005. },
  2006. expected: []types.Tag{
  2007. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2008. {Key: ptr.To("key2"), Value: ptr.To("value2")},
  2009. },
  2010. modified: false,
  2011. },
  2012. {
  2013. name: "No tags to update as managed-by tag is ignored",
  2014. tags: map[string]string{
  2015. "key1": "value1",
  2016. "key2": "value2",
  2017. },
  2018. metaTags: map[string]string{
  2019. "key1": "value1",
  2020. "key2": "value2",
  2021. managedBy: externalSecrets,
  2022. },
  2023. expected: []types.Tag{
  2024. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2025. {Key: ptr.To("key2"), Value: ptr.To("value2")},
  2026. {Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)},
  2027. },
  2028. modified: false,
  2029. },
  2030. {
  2031. name: "Add new tag",
  2032. tags: map[string]string{
  2033. "key1": "value1",
  2034. },
  2035. metaTags: map[string]string{
  2036. "key1": "value1",
  2037. "key2": "value2",
  2038. },
  2039. expected: []types.Tag{
  2040. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2041. {Key: ptr.To("key2"), Value: ptr.To("value2")},
  2042. },
  2043. modified: true,
  2044. },
  2045. {
  2046. name: "Update existing tag value",
  2047. tags: map[string]string{
  2048. "key1": "value1",
  2049. },
  2050. metaTags: map[string]string{
  2051. "key1": "newValue",
  2052. },
  2053. expected: []types.Tag{
  2054. {Key: ptr.To("key1"), Value: ptr.To("newValue")},
  2055. },
  2056. modified: true,
  2057. },
  2058. {
  2059. name: "Empty tags and metaTags",
  2060. tags: map[string]string{},
  2061. metaTags: map[string]string{},
  2062. expected: []types.Tag{},
  2063. modified: false,
  2064. },
  2065. {
  2066. name: "Empty tags with non-empty metaTags",
  2067. tags: map[string]string{},
  2068. metaTags: map[string]string{
  2069. "key1": "value1",
  2070. },
  2071. expected: []types.Tag{
  2072. {Key: ptr.To("key1"), Value: ptr.To("value1")},
  2073. },
  2074. modified: true,
  2075. },
  2076. }
  2077. for _, tt := range tests {
  2078. t.Run(tt.name, func(t *testing.T) {
  2079. result, modified := computeTagsToUpdate(tt.tags, tt.metaTags)
  2080. assert.ElementsMatch(t, tt.expected, result)
  2081. assert.Equal(t, tt.modified, modified)
  2082. })
  2083. }
  2084. }
  2085. func TestPatchTags(t *testing.T) {
  2086. type call struct {
  2087. untagCalled bool
  2088. tagCalled bool
  2089. }
  2090. tests := []struct {
  2091. name string
  2092. existingTags map[string]string
  2093. metaTags map[string]string
  2094. expectUntag bool
  2095. expectTag bool
  2096. assertsTag func(input *awssm.TagResourceInput)
  2097. assertsUntag func(input *awssm.UntagResourceInput)
  2098. }{
  2099. {
  2100. name: "no changes",
  2101. existingTags: map[string]string{"a": "1"},
  2102. metaTags: map[string]string{"a": "1"},
  2103. expectUntag: false,
  2104. expectTag: false,
  2105. assertsTag: func(input *awssm.TagResourceInput) {
  2106. assert.Fail(t, "Expected TagResource to not be called")
  2107. },
  2108. assertsUntag: func(input *awssm.UntagResourceInput) {
  2109. assert.Fail(t, "Expected UntagResource to not be called")
  2110. },
  2111. },
  2112. {
  2113. name: "update tag value",
  2114. existingTags: map[string]string{"a": "1"},
  2115. metaTags: map[string]string{"a": "2"},
  2116. expectUntag: false,
  2117. expectTag: true,
  2118. assertsTag: func(input *awssm.TagResourceInput) {
  2119. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)})
  2120. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("a"), Value: ptr.To("2")})
  2121. },
  2122. assertsUntag: func(input *awssm.UntagResourceInput) {
  2123. assert.Fail(t, "Expected UntagResource to not be called")
  2124. },
  2125. },
  2126. {
  2127. name: "remove tag",
  2128. existingTags: map[string]string{"a": "1", "b": "2"},
  2129. metaTags: map[string]string{"a": "1"},
  2130. expectUntag: true,
  2131. expectTag: false,
  2132. assertsTag: func(input *awssm.TagResourceInput) {
  2133. assert.Fail(t, "Expected TagResource to not be called")
  2134. },
  2135. assertsUntag: func(input *awssm.UntagResourceInput) {
  2136. assert.Equal(t, []string{"b"}, input.TagKeys)
  2137. },
  2138. },
  2139. {
  2140. name: "add tags",
  2141. existingTags: map[string]string{"a": "1"},
  2142. metaTags: map[string]string{"a": "1", "b": "2"},
  2143. expectUntag: false,
  2144. expectTag: true,
  2145. assertsTag: func(input *awssm.TagResourceInput) {
  2146. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To(managedBy), Value: ptr.To(externalSecrets)})
  2147. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("a"), Value: ptr.To("1")})
  2148. assert.Contains(t, input.Tags, types.Tag{Key: ptr.To("b"), Value: ptr.To("2")})
  2149. },
  2150. assertsUntag: func(input *awssm.UntagResourceInput) {
  2151. assert.Fail(t, "Expected UntagResource to not be called")
  2152. },
  2153. },
  2154. }
  2155. for _, tt := range tests {
  2156. t.Run(tt.name, func(t *testing.T) {
  2157. calls := call{}
  2158. fakeClient := &fakesm.Client{
  2159. TagResourceFn: fakesm.NewTagResourceFn(&awssm.TagResourceOutput{}, nil, func(input *awssm.TagResourceInput) {
  2160. tt.assertsTag(input)
  2161. calls.tagCalled = true
  2162. }),
  2163. UntagResourceFn: fakesm.NewUntagResourceFn(&awssm.UntagResourceOutput{}, nil, func(input *awssm.UntagResourceInput) {
  2164. tt.assertsUntag(input)
  2165. calls.untagCalled = true
  2166. }),
  2167. }
  2168. sm := &SecretsManager{client: fakeClient}
  2169. metaMap := map[string]interface{}{
  2170. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  2171. "kind": "PushSecretMetadata",
  2172. "spec": map[string]interface{}{
  2173. "description": "adding managed-by tag explicitly",
  2174. "tags": tt.metaTags,
  2175. },
  2176. }
  2177. raw, err := json.Marshal(metaMap)
  2178. require.NoError(t, err)
  2179. meta := &apiextensionsv1.JSON{Raw: raw}
  2180. secretId := "secret"
  2181. err = sm.patchTags(context.Background(), meta, &secretId, tt.existingTags)
  2182. require.NoError(t, err)
  2183. assert.Equal(t, tt.expectUntag, calls.untagCalled)
  2184. assert.Equal(t, tt.expectTag, calls.tagCalled)
  2185. })
  2186. }
  2187. }
  2188. // FakeCredProvider implements the AWS credentials.Provider interface
  2189. // It is used to inject an error into the AWS config to cause a
  2190. // validation error.
  2191. type FakeCredProvider struct {
  2192. retrieveFunc func() (aws.Credentials, error)
  2193. }
  2194. func (f *FakeCredProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
  2195. return f.retrieveFunc()
  2196. }
  2197. func (f *FakeCredProvider) IsExpired() bool {
  2198. return true
  2199. }