passworddepot.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 passworddepot
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. corev1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/types"
  19. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  20. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. )
  24. // Requires PASSWORDDEPOT_TOKEN and PASSWORDDEPOT_PROJECT_ID to be set in environment variables
  25. const (
  26. errPasswordDepotCredSecretName = "credentials are empty"
  27. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  28. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  29. errMissingSAK = "missing credentials while setting auth"
  30. errUninitalizedPasswordDepotProvider = "provider passworddepot is not initialized"
  31. errNotImplemented = "%s not implemented"
  32. )
  33. type Client interface {
  34. GetSecret(database, key string) (SecretEntry, error)
  35. }
  36. // PasswordDepot Provider struct with reference to a PasswordDepot client and a projectID.
  37. type PasswordDepot struct {
  38. client Client
  39. database string
  40. }
  41. func (p *PasswordDepot) ValidateStore(esv1.GenericStore) (admission.Warnings, error) {
  42. return nil, nil
  43. }
  44. func (p *PasswordDepot) Capabilities() esv1.SecretStoreCapabilities {
  45. return esv1.SecretStoreReadOnly
  46. }
  47. // Client for interacting with kubernetes cluster...?
  48. type passwordDepotClient struct {
  49. kube kclient.Client
  50. store *esv1.PasswordDepotProvider
  51. namespace string
  52. storeKind string
  53. }
  54. type Provider struct{}
  55. func (c *passwordDepotClient) getAuth(ctx context.Context) (string, string, error) {
  56. credentialsSecret := &corev1.Secret{}
  57. credentialsSecretName := c.store.Auth.SecretRef.Credentials.Name
  58. if credentialsSecretName == "" {
  59. return "", "", errors.New(errPasswordDepotCredSecretName)
  60. }
  61. objectKey := types.NamespacedName{
  62. Name: credentialsSecretName,
  63. Namespace: c.namespace,
  64. }
  65. // only ClusterStore is allowed to set namespace (and then it's required)
  66. if c.storeKind == esv1.ClusterSecretStoreKind {
  67. if c.store.Auth.SecretRef.Credentials.Namespace == nil {
  68. return "", "", errors.New(errInvalidClusterStoreMissingSAKNamespace)
  69. }
  70. objectKey.Namespace = *c.store.Auth.SecretRef.Credentials.Namespace
  71. }
  72. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  73. if err != nil {
  74. return "", "", fmt.Errorf(errFetchSAKSecret, err)
  75. }
  76. username := credentialsSecret.Data["username"]
  77. password := credentialsSecret.Data["password"]
  78. if (username == nil) || (len(username) == 0 || password == nil) || (len(password) == 0) {
  79. return "", "", errors.New(errMissingSAK)
  80. }
  81. return string(username), string(password), nil
  82. }
  83. // NewClient Method on PasswordDepot Provider to set up client with credentials and populate projectID.
  84. func (p *PasswordDepot) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  85. storeSpec := store.GetSpec()
  86. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.PasswordDepot == nil {
  87. return nil, errors.New("no store type or wrong store type")
  88. }
  89. storeSpecPasswordDepot := storeSpec.Provider.PasswordDepot
  90. cliStore := passwordDepotClient{
  91. kube: kube,
  92. store: storeSpecPasswordDepot,
  93. namespace: namespace,
  94. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  95. }
  96. username, password, err := cliStore.getAuth(ctx)
  97. if err != nil {
  98. return nil, err
  99. }
  100. // Create a new PasswordDepot client using credentials and options
  101. passworddepotClient, err := NewAPI(ctx, storeSpecPasswordDepot.Host, username, password, "8714")
  102. if err != nil {
  103. return nil, err
  104. }
  105. p.client = passworddepotClient
  106. p.database = storeSpecPasswordDepot.Database
  107. return p, nil
  108. }
  109. func (p *PasswordDepot) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  110. return false, fmt.Errorf(errNotImplemented, "SecretExists")
  111. }
  112. func (p *PasswordDepot) Validate() (esv1.ValidationResult, error) {
  113. return 0, nil
  114. }
  115. func (p *PasswordDepot) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  116. return fmt.Errorf(errNotImplemented, "PushSecret")
  117. }
  118. func (p *PasswordDepot) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  119. return nil, fmt.Errorf(errNotImplemented, "GetAllSecrets")
  120. }
  121. func (p *PasswordDepot) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  122. return fmt.Errorf(errNotImplemented, "DeleteSecret")
  123. }
  124. func (p *PasswordDepot) GetSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  125. if utils.IsNil(p.client) {
  126. return nil, errors.New(errUninitalizedPasswordDepotProvider)
  127. }
  128. data, err := p.client.GetSecret(p.database, ref.Key)
  129. if err != nil {
  130. return nil, err
  131. }
  132. mappedData := data.ToMap()
  133. value, ok := mappedData[ref.Property]
  134. if !ok {
  135. return nil, errors.New("key not found in secret data")
  136. }
  137. return value, nil
  138. }
  139. func (p *PasswordDepot) GetSecretMap(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  140. data, err := p.client.GetSecret(p.database, ref.Key)
  141. if err != nil {
  142. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  143. }
  144. return data.ToMap(), nil
  145. }
  146. func (p *PasswordDepot) Close(_ context.Context) error {
  147. return nil
  148. }
  149. func init() {
  150. esv1.Register(&PasswordDepot{}, &esv1.SecretStoreProvider{
  151. PasswordDepot: &esv1.PasswordDepotProvider{},
  152. }, esv1.MaintenanceStatusMaintained)
  153. }