passworddepot.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright © The ESO Authors
  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 passworddepot implements a SecretStore provider for PasswordDepot.
  14. package passworddepot
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. corev1 "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/types"
  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. )
  26. // Requires PASSWORDDEPOT_TOKEN and PASSWORDDEPOT_PROJECT_ID to be set in environment variables
  27. const (
  28. errPasswordDepotCredSecretName = "credentials are empty"
  29. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  30. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  31. errMissingSAK = "missing credentials while setting auth"
  32. errUninitalizedPasswordDepotProvider = "provider passworddepot is not initialized"
  33. errNotImplemented = "%s not implemented"
  34. )
  35. // Client defines the interface for interacting with the PasswordDepot API.
  36. type Client interface {
  37. GetSecret(database, key string) (SecretEntry, error)
  38. }
  39. // PasswordDepot Provider struct with reference to a PasswordDepot client and a projectID.
  40. type PasswordDepot struct {
  41. client Client
  42. database string
  43. }
  44. // ValidateStore validates the PasswordDepot SecretStore resource configuration.
  45. func (p *PasswordDepot) ValidateStore(esv1.GenericStore) (admission.Warnings, error) {
  46. return nil, nil
  47. }
  48. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  49. func (p *PasswordDepot) Capabilities() esv1.SecretStoreCapabilities {
  50. return esv1.SecretStoreReadOnly
  51. }
  52. // Client for interacting with kubernetes cluster...?
  53. type passwordDepotClient struct {
  54. kube kclient.Client
  55. store *esv1.PasswordDepotProvider
  56. namespace string
  57. storeKind string
  58. }
  59. // Provider represents the PasswordDepot provider configuration.
  60. type Provider struct{}
  61. func (c *passwordDepotClient) getAuth(ctx context.Context) (string, string, error) {
  62. credentialsSecret := &corev1.Secret{}
  63. credentialsSecretName := c.store.Auth.SecretRef.Credentials.Name
  64. if credentialsSecretName == "" {
  65. return "", "", errors.New(errPasswordDepotCredSecretName)
  66. }
  67. objectKey := types.NamespacedName{
  68. Name: credentialsSecretName,
  69. Namespace: c.namespace,
  70. }
  71. // only ClusterStore is allowed to set namespace (and then it's required)
  72. if c.storeKind == esv1.ClusterSecretStoreKind {
  73. if c.store.Auth.SecretRef.Credentials.Namespace == nil {
  74. return "", "", errors.New(errInvalidClusterStoreMissingSAKNamespace)
  75. }
  76. objectKey.Namespace = *c.store.Auth.SecretRef.Credentials.Namespace
  77. }
  78. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  79. if err != nil {
  80. return "", "", fmt.Errorf(errFetchSAKSecret, err)
  81. }
  82. username := credentialsSecret.Data["username"]
  83. password := credentialsSecret.Data["password"]
  84. if (username == nil) || (len(username) == 0 || password == nil) || (len(password) == 0) {
  85. return "", "", errors.New(errMissingSAK)
  86. }
  87. return string(username), string(password), nil
  88. }
  89. // NewClient constructs a new secrets client based on the provided store.
  90. func (p *PasswordDepot) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  91. storeSpec := store.GetSpec()
  92. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.PasswordDepot == nil {
  93. return nil, errors.New("no store type or wrong store type")
  94. }
  95. storeSpecPasswordDepot := storeSpec.Provider.PasswordDepot
  96. cliStore := passwordDepotClient{
  97. kube: kube,
  98. store: storeSpecPasswordDepot,
  99. namespace: namespace,
  100. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  101. }
  102. username, password, err := cliStore.getAuth(ctx)
  103. if err != nil {
  104. return nil, err
  105. }
  106. // Create a new PasswordDepot client using credentials and options
  107. passworddepotClient, err := NewAPI(ctx, storeSpecPasswordDepot.Host, username, password, "8714")
  108. if err != nil {
  109. return nil, err
  110. }
  111. p.client = passworddepotClient
  112. p.database = storeSpecPasswordDepot.Database
  113. return p, nil
  114. }
  115. // SecretExists checks if the secret exists in the PasswordDepot. This method is not implemented
  116. // as PasswordDepot is read-only.
  117. func (p *PasswordDepot) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  118. return false, fmt.Errorf(errNotImplemented, "SecretExists")
  119. }
  120. // Validate performs validation of the PasswordDepot provider configuration.
  121. func (p *PasswordDepot) Validate() (esv1.ValidationResult, error) {
  122. return esv1.ValidationResultReady, nil
  123. }
  124. // PushSecret is not implemented for PasswordDepot as it is read-only.
  125. func (p *PasswordDepot) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  126. return fmt.Errorf(errNotImplemented, "PushSecret")
  127. }
  128. // GetAllSecrets retrieves all secrets from PasswordDepot that match the given criteria.
  129. func (p *PasswordDepot) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  130. return nil, fmt.Errorf(errNotImplemented, "GetAllSecrets")
  131. }
  132. // DeleteSecret is not implemented for PasswordDepot as it is read-only.
  133. func (p *PasswordDepot) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  134. return fmt.Errorf(errNotImplemented, "DeleteSecret")
  135. }
  136. // GetSecret retrieves a secret from PasswordDepot.
  137. func (p *PasswordDepot) GetSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  138. if esutils.IsNil(p.client) {
  139. return nil, errors.New(errUninitalizedPasswordDepotProvider)
  140. }
  141. data, err := p.client.GetSecret(p.database, ref.Key)
  142. if err != nil {
  143. return nil, err
  144. }
  145. mappedData := data.ToMap()
  146. value, ok := mappedData[ref.Property]
  147. if !ok {
  148. return nil, errors.New("key not found in secret data")
  149. }
  150. return value, nil
  151. }
  152. // GetSecretMap retrieves a secret and returns it as a map of key/value pairs.
  153. func (p *PasswordDepot) GetSecretMap(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  154. data, err := p.client.GetSecret(p.database, ref.Key)
  155. if err != nil {
  156. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  157. }
  158. return data.ToMap(), nil
  159. }
  160. // Close implements cleanup operations for the PasswordDepot provider.
  161. func (p *PasswordDepot) Close(_ context.Context) error {
  162. return nil
  163. }
  164. // NewProvider creates a new Provider instance.
  165. func NewProvider() esv1.Provider {
  166. return &PasswordDepot{}
  167. }
  168. // ProviderSpec returns the provider specification for registration.
  169. func ProviderSpec() *esv1.SecretStoreProvider {
  170. return &esv1.SecretStoreProvider{
  171. PasswordDepot: &esv1.PasswordDepotProvider{},
  172. }
  173. }
  174. // MaintenanceStatus returns the maintenance status of the provider.
  175. func MaintenanceStatus() esv1.MaintenanceStatus {
  176. return esv1.MaintenanceStatusMaintained
  177. }