device42.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 device42 implements a provider for Device42 password management.
  14. package device42
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "time"
  20. corev1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  23. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. "github.com/external-secrets/external-secrets/runtime/esutils"
  26. )
  27. const (
  28. errNotImplemented = "not implemented"
  29. errUninitializedProvider = "unable to get device42 client"
  30. errCredSecretName = "credentials are empty"
  31. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  32. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  33. errMissingSAK = "missing credentials while setting auth"
  34. )
  35. // Client defines the interface for interacting with Device42 passwords.
  36. type Client interface {
  37. GetSecret(secretID string) (D42Password, error)
  38. }
  39. // Device42 implements the Provider interface for Device42.
  40. type Device42 struct {
  41. client Client
  42. }
  43. // ValidateStore validates the Device42 provider configuration.
  44. func (p *Device42) ValidateStore(esv1.GenericStore) (admission.Warnings, error) {
  45. return nil, nil
  46. }
  47. // Capabilities returns the provider's supported capabilities (ReadOnly).
  48. func (p *Device42) Capabilities() esv1.SecretStoreCapabilities {
  49. return esv1.SecretStoreReadOnly
  50. }
  51. // Client for interacting with kubernetes.
  52. type device42Client struct {
  53. kube kclient.Client
  54. store *esv1.Device42Provider
  55. namespace string
  56. storeKind string
  57. }
  58. // Provider implements the external-secrets provider for Device42.
  59. type Provider struct{}
  60. // NewDevice42Provider returns a reference to a new instance of a 'Device42' struct.
  61. func NewDevice42Provider() *Device42 {
  62. return &Device42{}
  63. }
  64. func (c *device42Client) getAuth(ctx context.Context) (string, string, error) {
  65. credentialsSecret := &corev1.Secret{}
  66. credentialsSecretName := c.store.Auth.SecretRef.Credentials.Name
  67. if credentialsSecretName == "" {
  68. return "", "", errors.New(errCredSecretName)
  69. }
  70. objectKey := types.NamespacedName{
  71. Name: credentialsSecretName,
  72. Namespace: c.namespace,
  73. }
  74. // only ClusterStore is allowed to set namespace (and then it's required)
  75. if c.storeKind == esv1.ClusterSecretStoreKind {
  76. if c.store.Auth.SecretRef.Credentials.Namespace == nil {
  77. return "", "", errors.New(errInvalidClusterStoreMissingSAKNamespace)
  78. }
  79. objectKey.Namespace = *c.store.Auth.SecretRef.Credentials.Namespace
  80. }
  81. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  82. if err != nil {
  83. return "", "", fmt.Errorf(errFetchSAKSecret, err)
  84. }
  85. username := credentialsSecret.Data["username"]
  86. password := credentialsSecret.Data["password"]
  87. if len(username) == 0 || len(password) == 0 {
  88. return "", "", errors.New(errMissingSAK)
  89. }
  90. return string(username), string(password), nil
  91. }
  92. // NewClient creates a new Device42 client.
  93. func (p *Device42) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  94. storeSpec := store.GetSpec()
  95. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Device42 == nil {
  96. return nil, errors.New("no store type or wrong store type")
  97. }
  98. storeSpecDevice42 := storeSpec.Provider.Device42
  99. cliStore := device42Client{
  100. kube: kube,
  101. store: storeSpecDevice42,
  102. namespace: namespace,
  103. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  104. }
  105. username, password, err := cliStore.getAuth(ctx)
  106. if err != nil {
  107. return nil, err
  108. }
  109. // Create a new client using credentials and options
  110. p.client = NewAPI(storeSpecDevice42.Host, username, password, "443")
  111. return p, nil
  112. }
  113. // SecretExists checks if a secret exists in Device42.
  114. func (p *Device42) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  115. return false, errors.New(errNotImplemented)
  116. }
  117. // Validate validates the Device42 provider configuration.
  118. func (p *Device42) Validate() (esv1.ValidationResult, error) {
  119. timeout := 15 * time.Second
  120. url := fmt.Sprintf("https://%s:%s", p.client.(*API).baseURL, p.client.(*API).hostPort)
  121. if err := esutils.NetworkValidate(url, timeout); err != nil {
  122. return esv1.ValidationResultError, err
  123. }
  124. return esv1.ValidationResultReady, nil
  125. }
  126. // PushSecret creates or updates a secret in Device42.
  127. func (p *Device42) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  128. return errors.New(errNotImplemented)
  129. }
  130. // GetAllSecrets retrieves multiple secrets from Device42.
  131. func (p *Device42) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  132. return nil, errors.New(errNotImplemented)
  133. }
  134. // DeleteSecret removes a secret from Device42.
  135. func (p *Device42) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  136. return errors.New(errNotImplemented)
  137. }
  138. // GetSecret retrieves a secret from Device42.
  139. func (p *Device42) GetSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  140. if esutils.IsNil(p.client) {
  141. return nil, errors.New(errUninitializedProvider)
  142. }
  143. data, err := p.client.GetSecret(ref.Key)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return []byte(data.Password), nil
  148. }
  149. // GetSecretMap retrieves a secret from Device42 and returns it as a map.
  150. func (p *Device42) GetSecretMap(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  151. data, err := p.client.GetSecret(ref.Key)
  152. if err != nil {
  153. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  154. }
  155. return data.ToMap(), nil
  156. }
  157. // Close implements cleanup operations for the Device42 client.
  158. func (p *Device42) Close(_ context.Context) error {
  159. return nil
  160. }
  161. // NewProvider creates a new Provider instance.
  162. func NewProvider() esv1.Provider {
  163. return &Device42{}
  164. }
  165. // ProviderSpec returns the provider specification for registration.
  166. func ProviderSpec() *esv1.SecretStoreProvider {
  167. return &esv1.SecretStoreProvider{
  168. Device42: &esv1.Device42Provider{},
  169. }
  170. }
  171. // MaintenanceStatus returns the maintenance status of the provider.
  172. func MaintenanceStatus() esv1.MaintenanceStatus {
  173. return esv1.MaintenanceStatusNotMaintained
  174. }