provider_schema_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 v1beta1
  13. import (
  14. "context"
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. "sigs.k8s.io/controller-runtime/pkg/client"
  18. )
  19. type PP struct{}
  20. const shouldBeRegistered = "provider should be registered"
  21. func (p *PP) Capabilities() SecretStoreCapabilities {
  22. return SecretStoreReadOnly
  23. }
  24. // New constructs a SecretsManager Provider.
  25. func (p *PP) NewClient(_ context.Context, _ GenericStore, _ client.Client, _ string) (SecretsClient, error) {
  26. return p, nil
  27. }
  28. // PushSecret writes a single secret into a provider.
  29. func (p *PP) PushSecret(_ context.Context, _ []byte, _ PushRemoteRef) error {
  30. return nil
  31. }
  32. // DeleteSecret deletes a single secret from a provider.
  33. func (p *PP) DeleteSecret(_ context.Context, _ PushRemoteRef) error {
  34. return nil
  35. }
  36. // GetSecret returns a single secret from the provider.
  37. func (p *PP) GetSecret(_ context.Context, _ ExternalSecretDataRemoteRef) ([]byte, error) {
  38. return []byte("NOOP"), nil
  39. }
  40. // GetSecretMap returns multiple k/v pairs from the provider.
  41. func (p *PP) GetSecretMap(_ context.Context, _ ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  42. return map[string][]byte{}, nil
  43. }
  44. // Empty GetAllSecrets.
  45. func (p *PP) GetAllSecrets(_ context.Context, _ ExternalSecretFind) (map[string][]byte, error) {
  46. // TO be implemented
  47. return map[string][]byte{}, nil
  48. }
  49. func (p *PP) Close(_ context.Context) error {
  50. return nil
  51. }
  52. func (p *PP) Validate() (ValidationResult, error) {
  53. return ValidationResultReady, nil
  54. }
  55. func (p *PP) ValidateStore(_ GenericStore) error {
  56. return nil
  57. }
  58. // TestRegister tests if the Register function
  59. // (1) panics if it tries to register something invalid
  60. // (2) stores the correct provider.
  61. func TestRegister(t *testing.T) {
  62. tbl := []struct {
  63. test string
  64. name string
  65. expPanic bool
  66. expExists bool
  67. provider *SecretStoreProvider
  68. }{
  69. {
  70. test: "should panic when given an invalid provider",
  71. name: "aws",
  72. expPanic: true,
  73. expExists: false,
  74. provider: &SecretStoreProvider{},
  75. },
  76. {
  77. test: "should register an correct provider",
  78. name: "aws",
  79. expExists: false,
  80. provider: &SecretStoreProvider{
  81. AWS: &AWSProvider{
  82. Service: AWSServiceSecretsManager,
  83. },
  84. },
  85. },
  86. {
  87. test: "should panic if already exists",
  88. name: "aws",
  89. expPanic: true,
  90. expExists: true,
  91. provider: &SecretStoreProvider{
  92. AWS: &AWSProvider{
  93. Service: AWSServiceSecretsManager,
  94. },
  95. },
  96. },
  97. }
  98. for i := range tbl {
  99. row := tbl[i]
  100. t.Run(row.test, func(t *testing.T) {
  101. runTest(t,
  102. row.name,
  103. row.provider,
  104. row.expPanic,
  105. )
  106. })
  107. }
  108. }
  109. func runTest(t *testing.T, name string, provider *SecretStoreProvider, expPanic bool) {
  110. testProvider := &PP{}
  111. secretStore := &SecretStore{
  112. Spec: SecretStoreSpec{
  113. Provider: provider,
  114. },
  115. }
  116. if expPanic {
  117. defer func() {
  118. if r := recover(); r == nil {
  119. t.Errorf("Register should panic")
  120. }
  121. }()
  122. }
  123. Register(testProvider, secretStore.Spec.Provider)
  124. p1, ok := GetProviderByName(name)
  125. assert.True(t, ok, shouldBeRegistered)
  126. assert.Equal(t, testProvider, p1)
  127. p2, err := GetProvider(secretStore)
  128. assert.Nil(t, err)
  129. assert.Equal(t, testProvider, p2)
  130. }
  131. // ForceRegister is used by other tests, we should ensure it works as expected.
  132. func TestForceRegister(t *testing.T) {
  133. testProvider := &PP{}
  134. provider := &SecretStoreProvider{
  135. AWS: &AWSProvider{
  136. Service: AWSServiceParameterStore,
  137. },
  138. }
  139. secretStore := &SecretStore{
  140. Spec: SecretStoreSpec{
  141. Provider: provider,
  142. },
  143. }
  144. ForceRegister(testProvider, &SecretStoreProvider{
  145. AWS: &AWSProvider{
  146. Service: AWSServiceParameterStore,
  147. },
  148. })
  149. p1, ok := GetProviderByName("aws")
  150. assert.True(t, ok, shouldBeRegistered)
  151. assert.Equal(t, testProvider, p1)
  152. p2, err := GetProvider(secretStore)
  153. assert.Nil(t, err)
  154. assert.Equal(t, testProvider, p2)
  155. }
  156. func TestRegisterGCP(t *testing.T) {
  157. p, ok := GetProviderByName("gcpsm")
  158. assert.Nil(t, p)
  159. assert.False(t, ok, "provider should not be registered")
  160. testProvider := &PP{}
  161. secretStore := &SecretStore{
  162. Spec: SecretStoreSpec{
  163. Provider: &SecretStoreProvider{
  164. GCPSM: &GCPSMProvider{},
  165. },
  166. },
  167. }
  168. ForceRegister(testProvider, secretStore.Spec.Provider)
  169. p1, ok := GetProviderByName("gcpsm")
  170. assert.True(t, ok, shouldBeRegistered)
  171. assert.Equal(t, testProvider, p1)
  172. p2, err := GetProvider(secretStore)
  173. assert.Nil(t, err)
  174. assert.Equal(t, testProvider, p2)
  175. }