provider.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. v1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. )
  24. const (
  25. errKeeperSecurityUnableToCreateConfig = "unable to create valid KeeperSecurity config: %w"
  26. errKeeperSecurityStore = "received invalid KeeperSecurity SecretStore resource: %s"
  27. errKeeperSecurityNilSpec = "nil spec"
  28. errKeeperSecurityNilSpecProvider = "nil spec.provider"
  29. errKeeperSecurityNilSpecProviderKeeperSecurity = "nil spec.provider.keepersecurity"
  30. errKeeperSecurityStoreMissingAuth = "missing: spec.provider.keepersecurity.auth"
  31. errKeeperSecurityStoreMissingFolderID = "missing: spec.provider.keepersecurity.folderID"
  32. errInvalidClusterStoreMissingK8sSecretNamespace = "invalid ClusterSecretStore: missing KeeperSecurity k8s Auth Secret Namespace"
  33. errFetchK8sSecret = "could not fetch k8s Secret: %w"
  34. errMissingK8sSecretKey = "missing Secret key: %s"
  35. )
  36. // Provider implements the necessary NewClient() and ValidateStore() funcs.
  37. type Provider struct{}
  38. // https://github.com/external-secrets/external-secrets/issues/644
  39. var _ esv1beta1.SecretsClient = &Client{}
  40. var _ esv1beta1.Provider = &Provider{}
  41. func init() {
  42. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  43. KeeperSecurity: &esv1beta1.KeeperSecurityProvider{},
  44. })
  45. }
  46. func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
  47. return esv1beta1.SecretStoreReadWrite
  48. }
  49. // NewClient constructs a GCP Provider.
  50. func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.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. isClusterKind := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  57. clientConfig, err := getKeeperSecurityAuth(ctx, keeperStore, kube, isClusterKind, namespace)
  58. if err != nil {
  59. return nil, fmt.Errorf(errKeeperSecurityUnableToCreateConfig, err)
  60. }
  61. ksmClientOptions := &ksm.ClientOptions{
  62. Config: ksm.NewMemoryKeyValueStorage(clientConfig),
  63. LogLevel: logger.ErrorLevel,
  64. }
  65. ksmClient := ksm.NewSecretsManager(ksmClientOptions)
  66. client := &Client{
  67. folderID: keeperStore.FolderID,
  68. ksmClient: ksmClient,
  69. }
  70. return client, nil
  71. }
  72. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) error {
  73. if store == nil {
  74. return fmt.Errorf(errKeeperSecurityStore, store)
  75. }
  76. spc := store.GetSpec()
  77. if spc == nil {
  78. return fmt.Errorf(errKeeperSecurityNilSpec)
  79. }
  80. if spc.Provider == nil {
  81. return fmt.Errorf(errKeeperSecurityNilSpecProvider)
  82. }
  83. if spc.Provider.KeeperSecurity == nil {
  84. return fmt.Errorf(errKeeperSecurityNilSpecProviderKeeperSecurity)
  85. }
  86. // check mandatory fields
  87. config := spc.Provider.KeeperSecurity
  88. if err := utils.ValidateSecretSelector(store, config.Auth); err != nil {
  89. return fmt.Errorf(errKeeperSecurityStoreMissingAuth)
  90. }
  91. if config.FolderID == "" {
  92. return fmt.Errorf(errKeeperSecurityStoreMissingFolderID)
  93. }
  94. return nil
  95. }
  96. func getKeeperSecurityAuth(ctx context.Context, store *esv1beta1.KeeperSecurityProvider, kube kclient.Client, isClusterKind bool, namespace string) (string, error) {
  97. auth := store.Auth
  98. credentialsSecret := &v1.Secret{}
  99. credentialsSecretName := auth.Name
  100. objectKey := types.NamespacedName{
  101. Name: credentialsSecretName,
  102. Namespace: namespace,
  103. }
  104. // only ClusterStore is allowed to set namespace (and then it's required)
  105. if isClusterKind {
  106. if credentialsSecretName != "" && auth.Namespace == nil {
  107. return "", fmt.Errorf(errInvalidClusterStoreMissingK8sSecretNamespace)
  108. } else if credentialsSecretName != "" {
  109. objectKey.Namespace = *auth.Namespace
  110. }
  111. }
  112. err := kube.Get(ctx, objectKey, credentialsSecret)
  113. if err != nil {
  114. return "", fmt.Errorf(errFetchK8sSecret, err)
  115. }
  116. data := credentialsSecret.Data[auth.Key]
  117. if (data == nil) || (len(data) == 0) {
  118. return "", fmt.Errorf(errMissingK8sSecretKey, auth.Key)
  119. }
  120. return string(data), nil
  121. }