provider.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/runtime/esutils"
  25. "github.com/external-secrets/external-secrets/runtime/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. // Capabilities returns the provider's supported capabilities (ReadWrite).
  41. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  42. return esv1.SecretStoreReadWrite
  43. }
  44. // NewClient constructs a new Keeper Security client with the provided store configuration.
  45. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  46. storeSpec := store.GetSpec()
  47. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.KeeperSecurity == nil {
  48. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  49. }
  50. keeperStore := storeSpec.Provider.KeeperSecurity
  51. clientConfig, err := getKeeperSecurityAuth(ctx, keeperStore, kube, store.GetKind(), namespace)
  52. if err != nil {
  53. return nil, fmt.Errorf(errKeeperSecurityUnableToCreateConfig, err)
  54. }
  55. ksmClientOptions := &ksm.ClientOptions{
  56. Config: ksm.NewMemoryKeyValueStorage(clientConfig),
  57. LogLevel: logger.ErrorLevel,
  58. }
  59. ksmClient := ksm.NewSecretsManager(ksmClientOptions)
  60. client := &Client{
  61. folderID: keeperStore.FolderID,
  62. ksmClient: ksmClient,
  63. }
  64. return client, nil
  65. }
  66. // ValidateStore validates the Keeper Security SecretStore configuration.
  67. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  68. if store == nil {
  69. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  70. }
  71. spc := store.GetSpec()
  72. if spc == nil {
  73. return nil, errors.New(errKeeperSecurityNilSpec)
  74. }
  75. if spc.Provider == nil {
  76. return nil, errors.New(errKeeperSecurityNilSpecProvider)
  77. }
  78. if spc.Provider.KeeperSecurity == nil {
  79. return nil, errors.New(errKeeperSecurityNilSpecProviderKeeperSecurity)
  80. }
  81. // check mandatory fields
  82. config := spc.Provider.KeeperSecurity
  83. if err := esutils.ValidateSecretSelector(store, config.Auth); err != nil {
  84. return nil, fmt.Errorf("error validating secret selector: %w", err)
  85. }
  86. if config.FolderID == "" {
  87. return nil, errors.New(errKeeperSecurityStoreMissingFolderID)
  88. }
  89. return nil, nil
  90. }
  91. func getKeeperSecurityAuth(ctx context.Context, store *esv1.KeeperSecurityProvider, kube kclient.Client, storeKind, namespace string) (string, error) {
  92. return resolvers.SecretKeyRef(
  93. ctx,
  94. kube,
  95. storeKind,
  96. namespace,
  97. &store.Auth)
  98. }
  99. // NewProvider creates a new Provider instance.
  100. func NewProvider() esv1.Provider {
  101. return &Provider{}
  102. }
  103. // ProviderSpec returns the provider specification for registration.
  104. func ProviderSpec() *esv1.SecretStoreProvider {
  105. return &esv1.SecretStoreProvider{
  106. KeeperSecurity: &esv1.KeeperSecurityProvider{},
  107. }
  108. }
  109. // MaintenanceStatus returns the maintenance status of the provider.
  110. func MaintenanceStatus() esv1.MaintenanceStatus {
  111. return esv1.MaintenanceStatusMaintained
  112. }