secretsmanager_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package secretsmanager
  13. import (
  14. "context"
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "github.com/aws/aws-sdk-go/aws"
  19. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  20. "github.com/google/go-cmp/cmp"
  21. "github.com/stretchr/testify/assert"
  22. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  23. fakesm "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager/fake"
  24. sess "github.com/external-secrets/external-secrets/pkg/provider/aws/session"
  25. )
  26. func TestConstructor(t *testing.T) {
  27. s, err := sess.New("1111", "2222", "foo", "", nil)
  28. assert.Nil(t, err)
  29. c, err := New(s)
  30. assert.Nil(t, err)
  31. assert.NotNil(t, c.client)
  32. }
  33. // test the sm<->aws interface
  34. // make sure correct values are passed and errors are handled accordingly.
  35. func TestGetSecret(t *testing.T) {
  36. fake := &fakesm.Client{}
  37. p := &SecretsManager{
  38. client: fake,
  39. }
  40. for i, row := range []struct {
  41. apiInput *awssm.GetSecretValueInput
  42. apiOutput *awssm.GetSecretValueOutput
  43. rr esv1alpha1.ExternalSecretDataRemoteRef
  44. apiErr error
  45. expectError string
  46. expectedSecret string
  47. }{
  48. {
  49. // good case: default version is set
  50. // key is passed in, output is sent back
  51. apiInput: &awssm.GetSecretValueInput{
  52. SecretId: aws.String("/baz"),
  53. VersionStage: aws.String("AWSCURRENT"),
  54. },
  55. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  56. Key: "/baz",
  57. },
  58. apiOutput: &awssm.GetSecretValueOutput{
  59. SecretString: aws.String("RRRRR"),
  60. },
  61. apiErr: nil,
  62. expectError: "",
  63. expectedSecret: "RRRRR",
  64. },
  65. {
  66. // good case: extract property
  67. apiInput: &awssm.GetSecretValueInput{
  68. SecretId: aws.String("/baz"),
  69. VersionStage: aws.String("AWSCURRENT"),
  70. },
  71. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  72. Key: "/baz",
  73. Property: "/shmoo",
  74. },
  75. apiOutput: &awssm.GetSecretValueOutput{
  76. SecretString: aws.String(`{"/shmoo": "bang"}`),
  77. },
  78. apiErr: nil,
  79. expectError: "",
  80. expectedSecret: "bang",
  81. },
  82. {
  83. // bad case: missing property
  84. apiInput: &awssm.GetSecretValueInput{
  85. SecretId: aws.String("/baz"),
  86. VersionStage: aws.String("AWSCURRENT"),
  87. },
  88. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  89. Key: "/baz",
  90. Property: "INVALPROP",
  91. },
  92. apiOutput: &awssm.GetSecretValueOutput{
  93. SecretString: aws.String(`{"/shmoo": "bang"}`),
  94. },
  95. apiErr: nil,
  96. expectError: "key INVALPROP does not exist in secret",
  97. expectedSecret: "",
  98. },
  99. {
  100. // bad case: extract property failure due to invalid json
  101. apiInput: &awssm.GetSecretValueInput{
  102. SecretId: aws.String("/baz"),
  103. VersionStage: aws.String("AWSCURRENT"),
  104. },
  105. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  106. Key: "/baz",
  107. Property: "INVALPROP",
  108. },
  109. apiOutput: &awssm.GetSecretValueOutput{
  110. SecretString: aws.String(`------`),
  111. },
  112. apiErr: nil,
  113. expectError: "key INVALPROP does not exist in secret",
  114. expectedSecret: "",
  115. },
  116. {
  117. // case: ssm.SecretString may be nil but binary is set
  118. apiInput: &awssm.GetSecretValueInput{
  119. SecretId: aws.String("/baz"),
  120. VersionStage: aws.String("AWSCURRENT"),
  121. },
  122. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  123. Key: "/baz",
  124. },
  125. apiOutput: &awssm.GetSecretValueOutput{
  126. SecretString: nil,
  127. SecretBinary: []byte("yesplease"),
  128. },
  129. apiErr: nil,
  130. expectError: "",
  131. expectedSecret: "yesplease",
  132. },
  133. {
  134. // case: both .SecretString and .SecretBinary is nil
  135. apiInput: &awssm.GetSecretValueInput{
  136. SecretId: aws.String("/baz"),
  137. VersionStage: aws.String("AWSCURRENT"),
  138. },
  139. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  140. Key: "/baz",
  141. },
  142. apiOutput: &awssm.GetSecretValueOutput{
  143. SecretString: nil,
  144. SecretBinary: nil,
  145. },
  146. apiErr: nil,
  147. expectError: "no secret string nor binary for key",
  148. expectedSecret: "",
  149. },
  150. {
  151. // case: secretOut.SecretBinary JSON parsing
  152. apiInput: &awssm.GetSecretValueInput{
  153. SecretId: aws.String("/baz"),
  154. VersionStage: aws.String("AWSCURRENT"),
  155. },
  156. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  157. Key: "/baz",
  158. Property: "foobar.baz",
  159. },
  160. apiOutput: &awssm.GetSecretValueOutput{
  161. SecretString: nil,
  162. SecretBinary: []byte(`{"foobar":{"baz":"nestedval"}}`),
  163. },
  164. apiErr: nil,
  165. expectError: "",
  166. expectedSecret: "nestedval",
  167. },
  168. {
  169. // should pass version
  170. apiInput: &awssm.GetSecretValueInput{
  171. SecretId: aws.String("/foo/bar"),
  172. VersionStage: aws.String("1234"),
  173. },
  174. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  175. Key: "/foo/bar",
  176. Version: "1234",
  177. },
  178. apiOutput: &awssm.GetSecretValueOutput{
  179. SecretString: aws.String("FOOBA!"),
  180. },
  181. apiErr: nil,
  182. expectError: "",
  183. expectedSecret: "FOOBA!",
  184. },
  185. {
  186. // should return err
  187. apiInput: &awssm.GetSecretValueInput{
  188. SecretId: aws.String("/foo/bar"),
  189. VersionStage: aws.String("AWSCURRENT"),
  190. },
  191. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  192. Key: "/foo/bar",
  193. },
  194. apiOutput: &awssm.GetSecretValueOutput{},
  195. apiErr: fmt.Errorf("oh no"),
  196. expectError: "oh no",
  197. },
  198. } {
  199. fake.WithValue(row.apiInput, row.apiOutput, row.apiErr)
  200. out, err := p.GetSecret(context.Background(), row.rr)
  201. if !ErrorContains(err, row.expectError) {
  202. t.Errorf("[%d] unexpected error: %s, expected: '%s'", i, err.Error(), row.expectError)
  203. }
  204. if string(out) != row.expectedSecret {
  205. t.Errorf("[%d] unexpected secret: expected %s, got %s", i, row.expectedSecret, string(out))
  206. }
  207. }
  208. }
  209. func TestGetSecretMap(t *testing.T) {
  210. fake := &fakesm.Client{}
  211. p := &SecretsManager{
  212. client: fake,
  213. }
  214. for i, row := range []struct {
  215. apiInput *awssm.GetSecretValueInput
  216. apiOutput *awssm.GetSecretValueOutput
  217. rr esv1alpha1.ExternalSecretDataRemoteRef
  218. expectedData map[string]string
  219. apiErr error
  220. expectError string
  221. }{
  222. {
  223. // good case: default version & deserialization
  224. apiInput: &awssm.GetSecretValueInput{
  225. SecretId: aws.String("/baz"),
  226. VersionStage: aws.String("AWSCURRENT"),
  227. },
  228. apiOutput: &awssm.GetSecretValueOutput{
  229. SecretString: aws.String(`{"foo":"bar"}`),
  230. },
  231. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  232. Key: "/baz",
  233. },
  234. expectedData: map[string]string{
  235. "foo": "bar",
  236. },
  237. apiErr: nil,
  238. expectError: "",
  239. },
  240. {
  241. // bad case: api error returned
  242. apiInput: &awssm.GetSecretValueInput{
  243. SecretId: aws.String("/baz"),
  244. VersionStage: aws.String("AWSCURRENT"),
  245. },
  246. apiOutput: &awssm.GetSecretValueOutput{
  247. SecretString: aws.String(`{"foo":"bar"}`),
  248. },
  249. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  250. Key: "/baz",
  251. },
  252. expectedData: map[string]string{
  253. "foo": "bar",
  254. },
  255. apiErr: fmt.Errorf("some api err"),
  256. expectError: "some api err",
  257. },
  258. {
  259. // bad case: invalid json
  260. apiInput: &awssm.GetSecretValueInput{
  261. SecretId: aws.String("/baz"),
  262. VersionStage: aws.String("AWSCURRENT"),
  263. },
  264. apiOutput: &awssm.GetSecretValueOutput{
  265. SecretString: aws.String(`-----------------`),
  266. },
  267. rr: esv1alpha1.ExternalSecretDataRemoteRef{
  268. Key: "/baz",
  269. },
  270. expectedData: map[string]string{},
  271. apiErr: nil,
  272. expectError: "unable to unmarshal secret",
  273. },
  274. } {
  275. fake.WithValue(row.apiInput, row.apiOutput, row.apiErr)
  276. out, err := p.GetSecretMap(context.Background(), row.rr)
  277. if !ErrorContains(err, row.expectError) {
  278. t.Errorf("[%d] unexpected error: %s, expected: '%s'", i, err.Error(), row.expectError)
  279. }
  280. if cmp.Equal(out, row.expectedData) {
  281. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", i, row.expectedData, out)
  282. }
  283. }
  284. }
  285. func ErrorContains(out error, want string) bool {
  286. if out == nil {
  287. return want == ""
  288. }
  289. if want == "" {
  290. return false
  291. }
  292. return strings.Contains(out.Error(), want)
  293. }