device42.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 device42
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "time"
  18. corev1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  22. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  23. esmeta "github.com/external-secrets/external-secrets/apis/meta/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) ApplyReferent(spec kclient.Object, _ esmeta.ReferentCallOrigin, _ string) (kclient.Object, error) {
  42. return spec, nil
  43. }
  44. func (p *Device42) Convert(_ esv1beta1.GenericStore) (kclient.Object, error) {
  45. return nil, nil
  46. }
  47. func (p *Device42) NewClientFromObj(_ context.Context, _ kclient.Object, _ kclient.Client, _ string) (esv1beta1.SecretsClient, error) {
  48. return nil, fmt.Errorf("not implemented")
  49. }
  50. func (p *Device42) ValidateStore(esv1beta1.GenericStore) (admission.Warnings, error) {
  51. return nil, nil
  52. }
  53. func (p *Device42) Capabilities() esv1beta1.SecretStoreCapabilities {
  54. return esv1beta1.SecretStoreReadOnly
  55. }
  56. // Client for interacting with kubernetes.
  57. type device42Client struct {
  58. kube kclient.Client
  59. store *esv1beta1.Device42Provider
  60. namespace string
  61. storeKind string
  62. }
  63. type Provider struct{}
  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 == esv1beta1.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. // NewDevice42Provider returns a reference to a new instance of a 'Device42' struct.
  93. func NewDevice42Provider() *Device42 {
  94. return &Device42{}
  95. }
  96. func (p *Device42) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  97. storeSpec := store.GetSpec()
  98. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Device42 == nil {
  99. return nil, errors.New("no store type or wrong store type")
  100. }
  101. storeSpecDevice42 := storeSpec.Provider.Device42
  102. cliStore := device42Client{
  103. kube: kube,
  104. store: storeSpecDevice42,
  105. namespace: namespace,
  106. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  107. }
  108. username, password, err := cliStore.getAuth(ctx)
  109. if err != nil {
  110. return nil, err
  111. }
  112. // Create a new client using credentials and options
  113. p.client = NewAPI(storeSpecDevice42.Host, username, password, "443")
  114. return p, nil
  115. }
  116. func (p *Device42) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  117. return false, errors.New(errNotImplemented)
  118. }
  119. func (p *Device42) Validate() (esv1beta1.ValidationResult, error) {
  120. timeout := 15 * time.Second
  121. url := fmt.Sprintf("https://%s:%s", p.client.(*API).baseURL, p.client.(*API).hostPort)
  122. if err := utils.NetworkValidate(url, timeout); err != nil {
  123. return esv1beta1.ValidationResultError, err
  124. }
  125. return esv1beta1.ValidationResultReady, nil
  126. }
  127. func (p *Device42) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  128. return errors.New(errNotImplemented)
  129. }
  130. func (p *Device42) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  131. return nil, errors.New(errNotImplemented)
  132. }
  133. func (p *Device42) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  134. return errors.New(errNotImplemented)
  135. }
  136. func (p *Device42) GetSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  137. if utils.IsNil(p.client) {
  138. return nil, errors.New(errUninitializedProvider)
  139. }
  140. data, err := p.client.GetSecret(ref.Key)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return []byte(data.Password), nil
  145. }
  146. func (p *Device42) GetSecretMap(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  147. data, err := p.client.GetSecret(ref.Key)
  148. if err != nil {
  149. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  150. }
  151. return data.ToMap(), nil
  152. }
  153. func (p *Device42) Close(_ context.Context) error {
  154. return nil
  155. }
  156. func init() {
  157. esv1beta1.Register(&Device42{}, &esv1beta1.SecretStoreProvider{
  158. Device42: &esv1beta1.Device42Provider{},
  159. })
  160. }