fake.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 fake
  13. import (
  14. "bytes"
  15. "errors"
  16. "fmt"
  17. "time"
  18. "github.com/aws/aws-sdk-go/aws"
  19. "github.com/aws/aws-sdk-go/aws/request"
  20. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  21. "github.com/google/go-cmp/cmp"
  22. )
  23. // Client implements the aws secretsmanager interface.
  24. type Client struct {
  25. ExecutionCounter int
  26. valFn map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)
  27. CreateSecretWithContextFn CreateSecretWithContextFn
  28. GetSecretValueWithContextFn GetSecretValueWithContextFn
  29. PutSecretValueWithContextFn PutSecretValueWithContextFn
  30. DescribeSecretWithContextFn DescribeSecretWithContextFn
  31. DeleteSecretWithContextFn DeleteSecretWithContextFn
  32. ListSecretsFn ListSecretsFn
  33. BatchGetSecretValueWithContextFn BatchGetSecretValueWithContextFn
  34. }
  35. type CreateSecretWithContextFn func(aws.Context, *awssm.CreateSecretInput, ...request.Option) (*awssm.CreateSecretOutput, error)
  36. type GetSecretValueWithContextFn func(aws.Context, *awssm.GetSecretValueInput, ...request.Option) (*awssm.GetSecretValueOutput, error)
  37. type PutSecretValueWithContextFn func(aws.Context, *awssm.PutSecretValueInput, ...request.Option) (*awssm.PutSecretValueOutput, error)
  38. type DescribeSecretWithContextFn func(aws.Context, *awssm.DescribeSecretInput, ...request.Option) (*awssm.DescribeSecretOutput, error)
  39. type DeleteSecretWithContextFn func(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (*awssm.DeleteSecretOutput, error)
  40. type ListSecretsFn func(ctx aws.Context, input *awssm.ListSecretsInput, opts ...request.Option) (*awssm.ListSecretsOutput, error)
  41. type BatchGetSecretValueWithContextFn func(aws.Context, *awssm.BatchGetSecretValueInput, ...request.Option) (*awssm.BatchGetSecretValueOutput, error)
  42. func (sm Client) CreateSecretWithContext(ctx aws.Context, input *awssm.CreateSecretInput, options ...request.Option) (*awssm.CreateSecretOutput, error) {
  43. return sm.CreateSecretWithContextFn(ctx, input, options...)
  44. }
  45. func NewCreateSecretWithContextFn(output *awssm.CreateSecretOutput, err error, expectedSecretBinary ...[]byte) CreateSecretWithContextFn {
  46. return func(ctx aws.Context, actualInput *awssm.CreateSecretInput, options ...request.Option) (*awssm.CreateSecretOutput, error) {
  47. if *actualInput.ClientRequestToken != "00000000-0000-0000-0000-000000000001" {
  48. return nil, errors.New("expected the version to be 1 at creation")
  49. }
  50. if len(expectedSecretBinary) == 1 {
  51. if bytes.Equal(actualInput.SecretBinary, expectedSecretBinary[0]) {
  52. return output, err
  53. }
  54. return nil, fmt.Errorf("expected secret to be '%s' but was '%s'", string(expectedSecretBinary[0]), string(actualInput.SecretBinary))
  55. }
  56. return output, err
  57. }
  58. }
  59. func (sm Client) DeleteSecretWithContext(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (*awssm.DeleteSecretOutput, error) {
  60. return sm.DeleteSecretWithContextFn(ctx, input, opts...)
  61. }
  62. func NewDeleteSecretWithContextFn(output *awssm.DeleteSecretOutput, err error) DeleteSecretWithContextFn {
  63. return func(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (*awssm.DeleteSecretOutput, error) {
  64. if input.ForceDeleteWithoutRecovery != nil && *input.ForceDeleteWithoutRecovery {
  65. output.SetDeletionDate(time.Now())
  66. }
  67. return output, err
  68. }
  69. }
  70. func (sm Client) GetSecretValueWithContext(ctx aws.Context, input *awssm.GetSecretValueInput, options ...request.Option) (*awssm.GetSecretValueOutput, error) {
  71. return sm.GetSecretValueWithContextFn(ctx, input, options...)
  72. }
  73. func NewGetSecretValueWithContextFn(output *awssm.GetSecretValueOutput, err error) GetSecretValueWithContextFn {
  74. return func(aws.Context, *awssm.GetSecretValueInput, ...request.Option) (*awssm.GetSecretValueOutput, error) {
  75. return output, err
  76. }
  77. }
  78. func (sm Client) PutSecretValueWithContext(ctx aws.Context, input *awssm.PutSecretValueInput, options ...request.Option) (*awssm.PutSecretValueOutput, error) {
  79. return sm.PutSecretValueWithContextFn(ctx, input, options...)
  80. }
  81. type ExpectedPutSecretValueInput struct {
  82. SecretBinary []byte
  83. Version *string
  84. }
  85. func (e ExpectedPutSecretValueInput) assertEquals(actualInput *awssm.PutSecretValueInput) error {
  86. errSecretBinary := e.assertSecretBinary(actualInput)
  87. if errSecretBinary != nil {
  88. return errSecretBinary
  89. }
  90. errSecretVersion := e.assertVersion(actualInput)
  91. if errSecretVersion != nil {
  92. return errSecretVersion
  93. }
  94. return nil
  95. }
  96. func (e ExpectedPutSecretValueInput) assertSecretBinary(actualInput *awssm.PutSecretValueInput) error {
  97. if e.SecretBinary != nil && !bytes.Equal(actualInput.SecretBinary, e.SecretBinary) {
  98. return fmt.Errorf("expected secret to be '%s' but was '%s'", string(e.SecretBinary), string(actualInput.SecretBinary))
  99. }
  100. return nil
  101. }
  102. func (e ExpectedPutSecretValueInput) assertVersion(actualInput *awssm.PutSecretValueInput) error {
  103. if e.Version != nil && (*actualInput.ClientRequestToken != *e.Version) {
  104. return fmt.Errorf("expected version to be '%s', but was '%s'", *e.Version, *actualInput.ClientRequestToken)
  105. }
  106. return nil
  107. }
  108. func NewPutSecretValueWithContextFn(output *awssm.PutSecretValueOutput, err error, expectedInput ...ExpectedPutSecretValueInput) PutSecretValueWithContextFn {
  109. return func(actualContext aws.Context, actualInput *awssm.PutSecretValueInput, actualOptions ...request.Option) (*awssm.PutSecretValueOutput, error) {
  110. if len(expectedInput) == 1 {
  111. assertErr := expectedInput[0].assertEquals(actualInput)
  112. if assertErr != nil {
  113. return nil, assertErr
  114. }
  115. }
  116. return output, err
  117. }
  118. }
  119. func (sm Client) DescribeSecretWithContext(ctx aws.Context, input *awssm.DescribeSecretInput, options ...request.Option) (*awssm.DescribeSecretOutput, error) {
  120. return sm.DescribeSecretWithContextFn(ctx, input, options...)
  121. }
  122. func NewDescribeSecretWithContextFn(output *awssm.DescribeSecretOutput, err error) DescribeSecretWithContextFn {
  123. return func(aws.Context, *awssm.DescribeSecretInput, ...request.Option) (*awssm.DescribeSecretOutput, error) {
  124. return output, err
  125. }
  126. }
  127. // NewClient init a new fake client.
  128. func NewClient() *Client {
  129. return &Client{
  130. valFn: make(map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)),
  131. }
  132. }
  133. func (sm *Client) GetSecretValue(in *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  134. sm.ExecutionCounter++
  135. if entry, found := sm.valFn[sm.cacheKeyForInput(in)]; found {
  136. return entry(in)
  137. }
  138. return nil, errors.New("test case not found")
  139. }
  140. func (sm *Client) ListSecrets(input *awssm.ListSecretsInput) (*awssm.ListSecretsOutput, error) {
  141. return sm.ListSecretsFn(nil, input)
  142. }
  143. func (sm *Client) BatchGetSecretValueWithContext(_ aws.Context, in *awssm.BatchGetSecretValueInput, _ ...request.Option) (*awssm.BatchGetSecretValueOutput, error) {
  144. return sm.BatchGetSecretValueWithContextFn(nil, in)
  145. }
  146. func (sm *Client) cacheKeyForInput(in *awssm.GetSecretValueInput) string {
  147. var secretID, versionID string
  148. if in.SecretId != nil {
  149. secretID = *in.SecretId
  150. }
  151. if in.VersionId != nil {
  152. versionID = *in.VersionId
  153. }
  154. return fmt.Sprintf("%s#%s", secretID, versionID)
  155. }
  156. func (sm *Client) WithValue(in *awssm.GetSecretValueInput, val *awssm.GetSecretValueOutput, err error) {
  157. sm.valFn[sm.cacheKeyForInput(in)] = func(paramIn *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  158. if !cmp.Equal(paramIn, in) {
  159. return nil, errors.New("unexpected test argument")
  160. }
  161. return val, err
  162. }
  163. }