fake.go 7.4 KB

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