fake.go 7.7 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. "context"
  16. "errors"
  17. "fmt"
  18. "time"
  19. awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
  20. "github.com/google/go-cmp/cmp"
  21. "github.com/google/go-cmp/cmp/cmpopts"
  22. "k8s.io/utils/ptr"
  23. )
  24. // Client implements the aws secretsmanager interface.
  25. type Client struct {
  26. ExecutionCounter int
  27. valFn map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)
  28. CreateSecretFn CreateSecretFn
  29. GetSecretValueFn GetSecretValueFn
  30. PutSecretValueFn PutSecretValueFn
  31. DescribeSecretFn DescribeSecretFn
  32. DeleteSecretFn DeleteSecretFn
  33. ListSecretsFn ListSecretsFn
  34. BatchGetSecretValueFn BatchGetSecretValueFn
  35. }
  36. type CreateSecretFn func(context.Context, *awssm.CreateSecretInput, ...func(*awssm.Options)) (*awssm.CreateSecretOutput, error)
  37. type GetSecretValueFn func(context.Context, *awssm.GetSecretValueInput, ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error)
  38. type PutSecretValueFn func(context.Context, *awssm.PutSecretValueInput, ...func(*awssm.Options)) (*awssm.PutSecretValueOutput, error)
  39. type DescribeSecretFn func(context.Context, *awssm.DescribeSecretInput, ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error)
  40. type DeleteSecretFn func(context.Context, *awssm.DeleteSecretInput, ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error)
  41. type ListSecretsFn func(context.Context, *awssm.ListSecretsInput, ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error)
  42. type BatchGetSecretValueFn func(context.Context, *awssm.BatchGetSecretValueInput, ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error)
  43. func (sm Client) CreateSecret(ctx context.Context, input *awssm.CreateSecretInput, options ...func(*awssm.Options)) (*awssm.CreateSecretOutput, error) {
  44. return sm.CreateSecretFn(ctx, input, options...)
  45. }
  46. func NewCreateSecretFn(output *awssm.CreateSecretOutput, err error, expectedSecretBinary ...[]byte) CreateSecretFn {
  47. return func(ctx context.Context, actualInput *awssm.CreateSecretInput, options ...func(*awssm.Options)) (*awssm.CreateSecretOutput, error) {
  48. if *actualInput.ClientRequestToken != "00000000-0000-0000-0000-000000000001" {
  49. return nil, errors.New("expected the version to be 1 at creation")
  50. }
  51. if len(expectedSecretBinary) == 1 {
  52. if bytes.Equal(actualInput.SecretBinary, expectedSecretBinary[0]) {
  53. return output, err
  54. }
  55. return nil, fmt.Errorf("expected secret to be '%s' but was '%s'", string(expectedSecretBinary[0]), string(actualInput.SecretBinary))
  56. }
  57. return output, err
  58. }
  59. }
  60. func (sm Client) DeleteSecret(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
  61. return sm.DeleteSecretFn(ctx, input, opts...)
  62. }
  63. func NewDeleteSecretFn(output *awssm.DeleteSecretOutput, err error) DeleteSecretFn {
  64. return func(ctx context.Context, input *awssm.DeleteSecretInput, opts ...func(*awssm.Options)) (*awssm.DeleteSecretOutput, error) {
  65. if input.ForceDeleteWithoutRecovery != nil && *input.ForceDeleteWithoutRecovery {
  66. output.DeletionDate = ptr.To(time.Now())
  67. }
  68. return output, err
  69. }
  70. }
  71. func NewGetSecretValueFn(output *awssm.GetSecretValueOutput, err error) GetSecretValueFn {
  72. return func(ctx context.Context, input *awssm.GetSecretValueInput, options ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  73. return output, err
  74. }
  75. }
  76. func (sm Client) PutSecretValue(ctx context.Context, input *awssm.PutSecretValueInput, options ...func(*awssm.Options)) (*awssm.PutSecretValueOutput, error) {
  77. return sm.PutSecretValueFn(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 NewPutSecretValueFn(output *awssm.PutSecretValueOutput, err error, expectedInput ...ExpectedPutSecretValueInput) PutSecretValueFn {
  107. return func(ctx context.Context, actualInput *awssm.PutSecretValueInput, actualOptions ...func(*awssm.Options)) (*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) DescribeSecret(ctx context.Context, input *awssm.DescribeSecretInput, options ...func(*awssm.Options)) (*awssm.DescribeSecretOutput, error) {
  118. return sm.DescribeSecretFn(ctx, input, options...)
  119. }
  120. func NewDescribeSecretFn(output *awssm.DescribeSecretOutput, err error) DescribeSecretFn {
  121. return func(ctx context.Context, input *awssm.DescribeSecretInput, options ...func(*awssm.Options)) (*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(ctx context.Context, in *awssm.GetSecretValueInput, options ...func(*awssm.Options)) (*awssm.GetSecretValueOutput, error) {
  132. // check if there's a direct fake function for this input
  133. if sm.GetSecretValueFn != nil {
  134. return sm.GetSecretValueFn(ctx, in, options...)
  135. }
  136. sm.ExecutionCounter++
  137. if entry, found := sm.valFn[sm.cacheKeyForInput(in)]; found {
  138. return entry(in)
  139. }
  140. return nil, errors.New("test case not found")
  141. }
  142. func (sm *Client) ListSecrets(ctx context.Context, input *awssm.ListSecretsInput, options ...func(*awssm.Options)) (*awssm.ListSecretsOutput, error) {
  143. return sm.ListSecretsFn(ctx, input, options...)
  144. }
  145. func (sm *Client) BatchGetSecretValue(ctx context.Context, in *awssm.BatchGetSecretValueInput, options ...func(*awssm.Options)) (*awssm.BatchGetSecretValueOutput, error) {
  146. return sm.BatchGetSecretValueFn(ctx, in, options...)
  147. }
  148. func (sm *Client) cacheKeyForInput(in *awssm.GetSecretValueInput) string {
  149. var secretID, versionID string
  150. if in.SecretId != nil {
  151. secretID = *in.SecretId
  152. }
  153. if in.VersionId != nil {
  154. versionID = *in.VersionId
  155. }
  156. return fmt.Sprintf("%s#%s", secretID, versionID)
  157. }
  158. func (sm *Client) WithValue(in *awssm.GetSecretValueInput, val *awssm.GetSecretValueOutput, err error) {
  159. sm.valFn[sm.cacheKeyForInput(in)] = func(paramIn *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  160. if !cmp.Equal(paramIn, in, cmpopts.IgnoreUnexported(awssm.GetSecretValueInput{})) {
  161. return nil, errors.New("unexpected test argument")
  162. }
  163. return val, err
  164. }
  165. }