passworddepot.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  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. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  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(esv1beta1.GenericStore) (admission.Warnings, error) {
  42. return nil, nil
  43. }
  44. func (p *PasswordDepot) Capabilities() esv1beta1.SecretStoreCapabilities {
  45. return esv1beta1.SecretStoreReadOnly
  46. }
  47. // Client for interacting with kubernetes cluster...?
  48. type passwordDepotClient struct {
  49. kube kclient.Client
  50. store *esv1beta1.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 "", "", fmt.Errorf(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 == esv1beta1.ClusterSecretStoreKind {
  67. if c.store.Auth.SecretRef.Credentials.Namespace == nil {
  68. return "", "", fmt.Errorf(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 "", "", fmt.Errorf(errMissingSAK)
  80. }
  81. return string(username), string(password), nil
  82. }
  83. // Function newPasswordDepotProvider returns a reference to a new instance of a 'PasswordDepot' struct.
  84. func NewPasswordDepotProvider() *PasswordDepot {
  85. return &PasswordDepot{}
  86. }
  87. // Method on PasswordDepot Provider to set up client with credentials and populate projectID.
  88. func (p *PasswordDepot) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  89. storeSpec := store.GetSpec()
  90. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.PasswordDepot == nil {
  91. return nil, fmt.Errorf("no store type or wrong store type")
  92. }
  93. storeSpecPasswordDepot := storeSpec.Provider.PasswordDepot
  94. cliStore := passwordDepotClient{
  95. kube: kube,
  96. store: storeSpecPasswordDepot,
  97. namespace: namespace,
  98. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  99. }
  100. username, password, err := cliStore.getAuth(ctx)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Create a new PasswordDepot client using credentials and options
  105. passworddepotClient, err := NewAPI(ctx, storeSpecPasswordDepot.Host, username, password, "8714")
  106. if err != nil {
  107. return nil, err
  108. }
  109. p.client = passworddepotClient
  110. p.database = storeSpecPasswordDepot.Database
  111. return p, nil
  112. }
  113. func (p *PasswordDepot) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  114. return false, fmt.Errorf("not implemented")
  115. }
  116. func (p *PasswordDepot) Validate() (esv1beta1.ValidationResult, error) {
  117. return 0, nil
  118. }
  119. func (p *PasswordDepot) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  120. return fmt.Errorf("not implemented")
  121. }
  122. func (p *PasswordDepot) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  123. return nil, fmt.Errorf("GetAllSecrets not implemented")
  124. }
  125. func (p *PasswordDepot) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  126. return fmt.Errorf("not implemented")
  127. }
  128. func (p *PasswordDepot) GetSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  129. if utils.IsNil(p.client) {
  130. return nil, fmt.Errorf(errUninitalizedPasswordDepotProvider)
  131. }
  132. data, err := p.client.GetSecret(p.database, ref.Key)
  133. if err != nil {
  134. return nil, err
  135. }
  136. mappedData := data.ToMap()
  137. value, ok := mappedData[ref.Property]
  138. if !ok {
  139. return nil, errors.New("key not found in secret data")
  140. }
  141. return value, nil
  142. }
  143. func (p *PasswordDepot) GetSecretMap(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  144. data, err := p.client.GetSecret(p.database, ref.Key)
  145. if err != nil {
  146. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  147. }
  148. return data.ToMap(), nil
  149. }
  150. func (p *PasswordDepot) Close(_ context.Context) error {
  151. return nil
  152. }
  153. func init() {
  154. esv1beta1.Register(&PasswordDepot{}, &esv1beta1.SecretStoreProvider{
  155. PasswordDepot: &esv1beta1.PasswordDepotProvider{},
  156. })
  157. }