device42.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "time"
  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/pkg/utils"
  25. )
  26. const (
  27. errNotImplemented = "not implemented"
  28. errUninitializedProvider = "unable to get device42 client"
  29. errCredSecretName = "credentials are empty"
  30. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  31. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  32. errMissingSAK = "missing credentials while setting auth"
  33. )
  34. type Client interface {
  35. GetSecret(secretID string) (D42Password, error)
  36. }
  37. // Device42 Provider struct with reference to a Device42 client.
  38. type Device42 struct {
  39. client Client
  40. }
  41. func (p *Device42) ValidateStore(esv1.GenericStore) (admission.Warnings, error) {
  42. return nil, nil
  43. }
  44. func (p *Device42) Capabilities() esv1.SecretStoreCapabilities {
  45. return esv1.SecretStoreReadOnly
  46. }
  47. // Client for interacting with kubernetes.
  48. type device42Client struct {
  49. kube kclient.Client
  50. store *esv1.Device42Provider
  51. namespace string
  52. storeKind string
  53. }
  54. type Provider struct{}
  55. func (c *device42Client) 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(errCredSecretName)
  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 len(username) == 0 || len(password) == 0 {
  79. return "", "", errors.New(errMissingSAK)
  80. }
  81. return string(username), string(password), nil
  82. }
  83. // NewDevice42Provider returns a reference to a new instance of a 'Device42' struct.
  84. func NewDevice42Provider() *Device42 {
  85. return &Device42{}
  86. }
  87. func (p *Device42) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  88. storeSpec := store.GetSpec()
  89. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Device42 == nil {
  90. return nil, errors.New("no store type or wrong store type")
  91. }
  92. storeSpecDevice42 := storeSpec.Provider.Device42
  93. cliStore := device42Client{
  94. kube: kube,
  95. store: storeSpecDevice42,
  96. namespace: namespace,
  97. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  98. }
  99. username, password, err := cliStore.getAuth(ctx)
  100. if err != nil {
  101. return nil, err
  102. }
  103. // Create a new client using credentials and options
  104. p.client = NewAPI(storeSpecDevice42.Host, username, password, "443")
  105. return p, nil
  106. }
  107. func (p *Device42) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  108. return false, errors.New(errNotImplemented)
  109. }
  110. func (p *Device42) Validate() (esv1.ValidationResult, error) {
  111. timeout := 15 * time.Second
  112. url := fmt.Sprintf("https://%s:%s", p.client.(*API).baseURL, p.client.(*API).hostPort)
  113. if err := utils.NetworkValidate(url, timeout); err != nil {
  114. return esv1.ValidationResultError, err
  115. }
  116. return esv1.ValidationResultReady, nil
  117. }
  118. func (p *Device42) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  119. return errors.New(errNotImplemented)
  120. }
  121. func (p *Device42) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  122. return nil, errors.New(errNotImplemented)
  123. }
  124. func (p *Device42) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  125. return errors.New(errNotImplemented)
  126. }
  127. func (p *Device42) GetSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  128. if utils.IsNil(p.client) {
  129. return nil, errors.New(errUninitializedProvider)
  130. }
  131. data, err := p.client.GetSecret(ref.Key)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return []byte(data.Password), nil
  136. }
  137. func (p *Device42) GetSecretMap(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  138. data, err := p.client.GetSecret(ref.Key)
  139. if err != nil {
  140. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  141. }
  142. return data.ToMap(), nil
  143. }
  144. func (p *Device42) Close(_ context.Context) error {
  145. return nil
  146. }
  147. func init() {
  148. esv1.Register(&Device42{}, &esv1.SecretStoreProvider{
  149. Device42: &esv1.Device42Provider{},
  150. }, esv1.MaintenanceStatusNotMaintained)
  151. }