fake.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "context"
  15. "fmt"
  16. "sigs.k8s.io/controller-runtime/pkg/client"
  17. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  18. "github.com/external-secrets/external-secrets/pkg/provider"
  19. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  20. )
  21. var (
  22. errNotFound = fmt.Errorf("secret value not found")
  23. errMissingStore = fmt.Errorf("missing store provider")
  24. errMissingFakeProvider = fmt.Errorf("missing store provider fake")
  25. )
  26. type Provider struct {
  27. config *esv1alpha1.FakeProvider
  28. }
  29. func (p *Provider) NewClient(ctx context.Context, store esv1alpha1.GenericStore, kube client.Client, namespace string) (provider.SecretsClient, error) {
  30. cfg, err := getProvider(store)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return &Provider{
  35. config: cfg,
  36. }, nil
  37. }
  38. func getProvider(store esv1alpha1.GenericStore) (*esv1alpha1.FakeProvider, error) {
  39. if store == nil {
  40. return nil, errMissingStore
  41. }
  42. spc := store.GetSpec()
  43. if spc == nil || spc.Provider == nil || spc.Provider.Fake == nil {
  44. return nil, errMissingFakeProvider
  45. }
  46. return spc.Provider.Fake, nil
  47. }
  48. // GetSecret returns a single secret from the provider.
  49. func (p *Provider) GetSecret(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) ([]byte, error) {
  50. for _, data := range p.config.Data {
  51. if data.Key == ref.Key && data.Version == ref.Version {
  52. return []byte(data.Value), nil
  53. }
  54. }
  55. return nil, errNotFound
  56. }
  57. // GetSecretMap returns multiple k/v pairs from the provider.
  58. func (p *Provider) GetSecretMap(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  59. for _, data := range p.config.Data {
  60. if data.Key != ref.Key || data.Version != ref.Version || data.ValueMap == nil {
  61. continue
  62. }
  63. return convertMap(data.ValueMap), nil
  64. }
  65. return nil, errNotFound
  66. }
  67. func convertMap(in map[string]string) map[string][]byte {
  68. m := make(map[string][]byte)
  69. for k, v := range in {
  70. m[k] = []byte(v)
  71. }
  72. return m
  73. }
  74. func (p *Provider) Close(ctx context.Context) error {
  75. return nil
  76. }
  77. func init() {
  78. schema.Register(&Provider{}, &esv1alpha1.SecretStoreProvider{
  79. Fake: &esv1alpha1.FakeProvider{},
  80. })
  81. }