fake.go 7.2 KB

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