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