fake.go 7.4 KB

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