provider.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package keepersecurity implements a provider for Keeper Security secrets management service
  14. package keepersecurity
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. ksm "github.com/keeper-security/secrets-manager-go/core"
  20. "github.com/keeper-security/secrets-manager-go/core/logger"
  21. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  22. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  23. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. "github.com/external-secrets/external-secrets/pkg/esutils"
  25. "github.com/external-secrets/external-secrets/pkg/esutils/resolvers"
  26. )
  27. const (
  28. errKeeperSecurityUnableToCreateConfig = "unable to create valid KeeperSecurity config: %w"
  29. errKeeperSecurityStore = "received invalid KeeperSecurity SecretStore resource: %s"
  30. errKeeperSecurityNilSpec = "nil spec"
  31. errKeeperSecurityNilSpecProvider = "nil spec.provider"
  32. errKeeperSecurityNilSpecProviderKeeperSecurity = "nil spec.provider.keepersecurity"
  33. errKeeperSecurityStoreMissingFolderID = "missing: spec.provider.keepersecurity.folderID"
  34. )
  35. // Provider implements the necessary NewClient() and ValidateStore() funcs for Keeper Security.
  36. type Provider struct{}
  37. // https://github.com/external-secrets/external-secrets/issues/644
  38. var _ esv1.SecretsClient = &Client{}
  39. var _ esv1.Provider = &Provider{}
  40. func init() {
  41. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  42. KeeperSecurity: &esv1.KeeperSecurityProvider{},
  43. }, esv1.MaintenanceStatusMaintained)
  44. }
  45. // Capabilities returns the provider's supported capabilities (ReadWrite).
  46. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  47. return esv1.SecretStoreReadWrite
  48. }
  49. // NewClient constructs a new Keeper Security client with the provided store configuration.
  50. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  51. storeSpec := store.GetSpec()
  52. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.KeeperSecurity == nil {
  53. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  54. }
  55. keeperStore := storeSpec.Provider.KeeperSecurity
  56. clientConfig, err := getKeeperSecurityAuth(ctx, keeperStore, kube, store.GetKind(), namespace)
  57. if err != nil {
  58. return nil, fmt.Errorf(errKeeperSecurityUnableToCreateConfig, err)
  59. }
  60. ksmClientOptions := &ksm.ClientOptions{
  61. Config: ksm.NewMemoryKeyValueStorage(clientConfig),
  62. LogLevel: logger.ErrorLevel,
  63. }
  64. ksmClient := ksm.NewSecretsManager(ksmClientOptions)
  65. client := &Client{
  66. folderID: keeperStore.FolderID,
  67. ksmClient: ksmClient,
  68. }
  69. return client, nil
  70. }
  71. // ValidateStore validates the Keeper Security SecretStore configuration.
  72. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  73. if store == nil {
  74. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  75. }
  76. spc := store.GetSpec()
  77. if spc == nil {
  78. return nil, errors.New(errKeeperSecurityNilSpec)
  79. }
  80. if spc.Provider == nil {
  81. return nil, errors.New(errKeeperSecurityNilSpecProvider)
  82. }
  83. if spc.Provider.KeeperSecurity == nil {
  84. return nil, errors.New(errKeeperSecurityNilSpecProviderKeeperSecurity)
  85. }
  86. // check mandatory fields
  87. config := spc.Provider.KeeperSecurity
  88. if err := esutils.ValidateSecretSelector(store, config.Auth); err != nil {
  89. return nil, fmt.Errorf("error validating secret selector: %w", err)
  90. }
  91. if config.FolderID == "" {
  92. return nil, errors.New(errKeeperSecurityStoreMissingFolderID)
  93. }
  94. return nil, nil
  95. }
  96. func getKeeperSecurityAuth(ctx context.Context, store *esv1.KeeperSecurityProvider, kube kclient.Client, storeKind, namespace string) (string, error) {
  97. return resolvers.SecretKeyRef(
  98. ctx,
  99. kube,
  100. storeKind,
  101. namespace,
  102. &store.Auth)
  103. }