provider.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 keepersecurity
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. ksm "github.com/keeper-security/secrets-manager-go/core"
  18. "github.com/keeper-security/secrets-manager-go/core/logger"
  19. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  20. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  24. )
  25. const (
  26. errKeeperSecurityUnableToCreateConfig = "unable to create valid KeeperSecurity config: %w"
  27. errKeeperSecurityStore = "received invalid KeeperSecurity SecretStore resource: %s"
  28. errKeeperSecurityNilSpec = "nil spec"
  29. errKeeperSecurityNilSpecProvider = "nil spec.provider"
  30. errKeeperSecurityNilSpecProviderKeeperSecurity = "nil spec.provider.keepersecurity"
  31. errKeeperSecurityStoreMissingAuth = "missing: spec.provider.keepersecurity.auth"
  32. errKeeperSecurityStoreMissingFolderID = "missing: spec.provider.keepersecurity.folderID"
  33. )
  34. // Provider implements the necessary NewClient() and ValidateStore() funcs.
  35. type Provider struct{}
  36. // https://github.com/external-secrets/external-secrets/issues/644
  37. var _ esv1beta1.SecretsClient = &Client{}
  38. var _ esv1beta1.Provider = &Provider{}
  39. func init() {
  40. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  41. KeeperSecurity: &esv1beta1.KeeperSecurityProvider{},
  42. })
  43. }
  44. func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
  45. return esv1beta1.SecretStoreReadWrite
  46. }
  47. // NewClient constructs a GCP Provider.
  48. func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  49. storeSpec := store.GetSpec()
  50. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.KeeperSecurity == nil {
  51. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  52. }
  53. keeperStore := storeSpec.Provider.KeeperSecurity
  54. clientConfig, err := getKeeperSecurityAuth(ctx, keeperStore, kube, store.GetKind(), namespace)
  55. if err != nil {
  56. return nil, fmt.Errorf(errKeeperSecurityUnableToCreateConfig, err)
  57. }
  58. ksmClientOptions := &ksm.ClientOptions{
  59. Config: ksm.NewMemoryKeyValueStorage(clientConfig),
  60. LogLevel: logger.ErrorLevel,
  61. }
  62. ksmClient := ksm.NewSecretsManager(ksmClientOptions)
  63. client := &Client{
  64. folderID: keeperStore.FolderID,
  65. ksmClient: ksmClient,
  66. }
  67. return client, nil
  68. }
  69. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
  70. if store == nil {
  71. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  72. }
  73. spc := store.GetSpec()
  74. if spc == nil {
  75. return nil, errors.New(errKeeperSecurityNilSpec)
  76. }
  77. if spc.Provider == nil {
  78. return nil, errors.New(errKeeperSecurityNilSpecProvider)
  79. }
  80. if spc.Provider.KeeperSecurity == nil {
  81. return nil, errors.New(errKeeperSecurityNilSpecProviderKeeperSecurity)
  82. }
  83. // check mandatory fields
  84. config := spc.Provider.KeeperSecurity
  85. if err := utils.ValidateSecretSelector(store, config.Auth); err != nil {
  86. return nil, errors.New(errKeeperSecurityStoreMissingAuth)
  87. }
  88. if config.FolderID == "" {
  89. return nil, errors.New(errKeeperSecurityStoreMissingFolderID)
  90. }
  91. return nil, nil
  92. }
  93. func getKeeperSecurityAuth(ctx context.Context, store *esv1beta1.KeeperSecurityProvider, kube kclient.Client, storeKind, namespace string) (string, error) {
  94. return resolvers.SecretKeyRef(
  95. ctx,
  96. kube,
  97. storeKind,
  98. namespace,
  99. &store.Auth)
  100. }