provider.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "net/url"
  17. ksm "github.com/keeper-security/secrets-manager-go/core"
  18. "github.com/keeper-security/secrets-manager-go/core/logger"
  19. v1 "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  22. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  23. smmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  24. "github.com/external-secrets/external-secrets/pkg/utils"
  25. )
  26. const (
  27. errKeeperSecurityUnableToCreateConfig = "unable to create valid KeeperSecurity config: %w"
  28. errKeeperSecurityStore = "received invalid KeeperSecurity SecretStore resource: %s"
  29. errKeeperSecurityNilSpec = "nil spec"
  30. errKeeperSecurityNilSpecProvider = "nil spec.provider"
  31. errKeeperSecurityNilSpecProviderKeeperSecurity = "nil spec.provider.keepersecurity"
  32. errKeeperSecurityStoreMissingAuth = "missing: spec.provider.keepersecurity.auth"
  33. errKeeperSecurityStoreMissingAppKey = "missing: spec.provider.keepersecurity.auth.appKeySecretRef %w"
  34. errKeeperSecurityStoreMissingAppOwnerPublicKey = "missing: spec.provider.keepersecurity.auth.appOwnerPublicKeySecretRef %w"
  35. errKeeperSecurityStoreMissingClientID = "missing: spec.provider.keepersecurity.auth.clientIdSecretRef %w"
  36. errKeeperSecurityStoreMissingPrivateKey = "missing: spec.provider.keepersecurity.auth.privateKeySecretRef %w"
  37. errKeeperSecurityStoreMissingServerPublicKeyID = "missing: spec.provider.keepersecurity.auth.serverPublicKeyIDSecretRef %w"
  38. errKeeperSecurityStoreInvalidConnectHost = "unable to parse URL: spec.provider.keepersecurity.connectHost: %w"
  39. errInvalidClusterStoreMissingK8sSecretNamespace = "invalid ClusterSecretStore: missing KeeperSecurity k8s Auth Secret Namespace"
  40. errFetchK8sSecret = "could not fetch k8s Secret: %w"
  41. errMissingK8sSecretKey = "missing Secret key: %s"
  42. )
  43. // Provider implements the necessary NewClient() and ValidateStore() funcs.
  44. type Provider struct{}
  45. // https://github.com/external-secrets/external-secrets/issues/644
  46. var _ esv1beta1.SecretsClient = &Client{}
  47. var _ esv1beta1.Provider = &Provider{}
  48. func init() {
  49. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  50. KeeperSecurity: &esv1beta1.KeeperSecurityProvider{},
  51. })
  52. }
  53. func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
  54. return esv1beta1.SecretStoreReadWrite
  55. }
  56. // NewClient constructs a GCP Provider.
  57. func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  58. storeSpec := store.GetSpec()
  59. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.KeeperSecurity == nil {
  60. return nil, fmt.Errorf(errKeeperSecurityStore, store)
  61. }
  62. keeperStore := storeSpec.Provider.KeeperSecurity
  63. isClusterKind := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  64. clientConfig, err := getKeeperSecurityConfig(ctx, keeperStore, kube, isClusterKind, namespace)
  65. if err != nil {
  66. return nil, fmt.Errorf(errKeeperSecurityUnableToCreateConfig, err)
  67. }
  68. ksmClientOptions := &ksm.ClientOptions{
  69. Config: ksm.NewMemoryKeyValueStorage(clientConfig),
  70. LogLevel: logger.ErrorLevel,
  71. }
  72. ksmClient := ksm.NewSecretsManager(ksmClientOptions)
  73. client := &Client{
  74. folderID: keeperStore.FolderID,
  75. ksmClient: ksmClient,
  76. }
  77. return client, nil
  78. }
  79. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) error {
  80. if store == nil {
  81. return fmt.Errorf(errKeeperSecurityStore, store)
  82. }
  83. spc := store.GetSpec()
  84. if spc == nil {
  85. return fmt.Errorf(errKeeperSecurityNilSpec)
  86. }
  87. if spc.Provider == nil {
  88. return fmt.Errorf(errKeeperSecurityNilSpecProvider)
  89. }
  90. if spc.Provider.KeeperSecurity == nil {
  91. return fmt.Errorf(errKeeperSecurityNilSpecProviderKeeperSecurity)
  92. }
  93. // check mandatory fields
  94. config := spc.Provider.KeeperSecurity
  95. // check valid URL
  96. if _, err := url.Parse(config.Hostname); err != nil {
  97. return fmt.Errorf(errKeeperSecurityStoreInvalidConnectHost, err)
  98. }
  99. if config.Auth == nil {
  100. return fmt.Errorf(errKeeperSecurityStoreMissingAuth)
  101. }
  102. if err := utils.ValidateSecretSelector(store, config.Auth.AppKey); err != nil {
  103. return fmt.Errorf(errKeeperSecurityStoreMissingAppKey, err)
  104. }
  105. if err := utils.ValidateSecretSelector(store, config.Auth.AppOwnerPublicKey); err != nil {
  106. return fmt.Errorf(errKeeperSecurityStoreMissingAppOwnerPublicKey, err)
  107. }
  108. if err := utils.ValidateSecretSelector(store, config.Auth.PrivateKey); err != nil {
  109. return fmt.Errorf(errKeeperSecurityStoreMissingPrivateKey, err)
  110. }
  111. if err := utils.ValidateSecretSelector(store, config.Auth.ClientID); err != nil {
  112. return fmt.Errorf(errKeeperSecurityStoreMissingClientID, err)
  113. }
  114. if err := utils.ValidateSecretSelector(store, config.Auth.ServerPublicKeyID); err != nil {
  115. return fmt.Errorf(errKeeperSecurityStoreMissingServerPublicKeyID, err)
  116. }
  117. return nil
  118. }
  119. func getKeeperSecurityConfig(ctx context.Context, store *esv1beta1.KeeperSecurityProvider, kube kclient.Client, isClusterKind bool, namespace string) (map[string]string, error) {
  120. auth := store.Auth
  121. apiKey, err := getAuthParameter(ctx, auth.AppKey, kube, isClusterKind, namespace)
  122. if err != nil {
  123. return nil, err
  124. }
  125. appOwnerPublicKey, err := getAuthParameter(ctx, auth.AppOwnerPublicKey, kube, isClusterKind, namespace)
  126. if err != nil {
  127. return nil, err
  128. }
  129. clientID, err := getAuthParameter(ctx, auth.ClientID, kube, isClusterKind, namespace)
  130. if err != nil {
  131. return nil, err
  132. }
  133. privateKey, err := getAuthParameter(ctx, auth.PrivateKey, kube, isClusterKind, namespace)
  134. if err != nil {
  135. return nil, err
  136. }
  137. serverPublicKeyID, err := getAuthParameter(ctx, auth.ServerPublicKeyID, kube, isClusterKind, namespace)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return map[string]string{
  142. "appKey": apiKey,
  143. "appOwnerPublicKey": appOwnerPublicKey,
  144. "clientId": clientID,
  145. "hostname": store.Hostname,
  146. "privateKey": privateKey,
  147. "serverPublicKeyID": serverPublicKeyID,
  148. }, nil
  149. }
  150. func getAuthParameter(ctx context.Context, param smmeta.SecretKeySelector, kube kclient.Client, isClusterKind bool, namespace string) (string, error) {
  151. credentialsSecret := &v1.Secret{}
  152. credentialsSecretName := param.Name
  153. objectKey := types.NamespacedName{
  154. Name: credentialsSecretName,
  155. Namespace: namespace,
  156. }
  157. // only ClusterStore is allowed to set namespace (and then it's required)
  158. if isClusterKind {
  159. if credentialsSecretName != "" && param.Namespace == nil {
  160. return "", fmt.Errorf(errInvalidClusterStoreMissingK8sSecretNamespace)
  161. } else if credentialsSecretName != "" {
  162. objectKey.Namespace = *param.Namespace
  163. }
  164. }
  165. err := kube.Get(ctx, objectKey, credentialsSecret)
  166. if err != nil {
  167. return "", fmt.Errorf(errFetchK8sSecret, err)
  168. }
  169. data := credentialsSecret.Data[param.Key]
  170. if (data == nil) || (len(data) == 0) {
  171. return "", fmt.Errorf(errMissingK8sSecretKey, param.Key)
  172. }
  173. return string(data), nil
  174. }