device42.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "github.com/external-secrets/external-secrets/pkg/utils"
  24. )
  25. const (
  26. errNotImplemented = "not implemented"
  27. errUninitializedProvider = "unable to get device42 client"
  28. errCredSecretName = "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. )
  33. type Client interface {
  34. GetSecret(secretID string) (D42Password, error)
  35. }
  36. // Device42 Provider struct with reference to a Device42 client.
  37. type Device42 struct {
  38. client Client
  39. }
  40. func (p *Device42) ValidateStore(esv1beta1.GenericStore) (admission.Warnings, error) {
  41. return nil, nil
  42. }
  43. func (p *Device42) Capabilities() esv1beta1.SecretStoreCapabilities {
  44. return esv1beta1.SecretStoreReadOnly
  45. }
  46. // Client for interacting with kubernetes.
  47. type device42Client struct {
  48. kube kclient.Client
  49. store *esv1beta1.Device42Provider
  50. namespace string
  51. storeKind string
  52. }
  53. type Provider struct{}
  54. func (c *device42Client) getAuth(ctx context.Context) (string, string, error) {
  55. credentialsSecret := &corev1.Secret{}
  56. credentialsSecretName := c.store.Auth.SecretRef.Credentials.Name
  57. if credentialsSecretName == "" {
  58. return "", "", errors.New(errCredSecretName)
  59. }
  60. objectKey := types.NamespacedName{
  61. Name: credentialsSecretName,
  62. Namespace: c.namespace,
  63. }
  64. // only ClusterStore is allowed to set namespace (and then it's required)
  65. if c.storeKind == esv1beta1.ClusterSecretStoreKind {
  66. if c.store.Auth.SecretRef.Credentials.Namespace == nil {
  67. return "", "", errors.New(errInvalidClusterStoreMissingSAKNamespace)
  68. }
  69. objectKey.Namespace = *c.store.Auth.SecretRef.Credentials.Namespace
  70. }
  71. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  72. if err != nil {
  73. return "", "", fmt.Errorf(errFetchSAKSecret, err)
  74. }
  75. username := credentialsSecret.Data["username"]
  76. password := credentialsSecret.Data["password"]
  77. if len(username) == 0 || len(password) == 0 {
  78. return "", "", errors.New(errMissingSAK)
  79. }
  80. return string(username), string(password), nil
  81. }
  82. // NewDevice42Provider returns a reference to a new instance of a 'Device42' struct.
  83. func NewDevice42Provider() *Device42 {
  84. return &Device42{}
  85. }
  86. func (p *Device42) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  87. storeSpec := store.GetSpec()
  88. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Device42 == nil {
  89. return nil, errors.New("no store type or wrong store type")
  90. }
  91. storeSpecDevice42 := storeSpec.Provider.Device42
  92. cliStore := device42Client{
  93. kube: kube,
  94. store: storeSpecDevice42,
  95. namespace: namespace,
  96. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  97. }
  98. username, password, err := cliStore.getAuth(ctx)
  99. if err != nil {
  100. return nil, err
  101. }
  102. // Create a new client using credentials and options
  103. p.client = NewAPI(storeSpecDevice42.Host, username, password, "443")
  104. return p, nil
  105. }
  106. func (p *Device42) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  107. return false, errors.New(errNotImplemented)
  108. }
  109. func (p *Device42) Validate() (esv1beta1.ValidationResult, error) {
  110. timeout := 15 * time.Second
  111. url := fmt.Sprintf("https://%s:%s", p.client.(*API).baseURL, p.client.(*API).hostPort)
  112. if err := utils.NetworkValidate(url, timeout); err != nil {
  113. return esv1beta1.ValidationResultError, err
  114. }
  115. return esv1beta1.ValidationResultReady, nil
  116. }
  117. func (p *Device42) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  118. return errors.New(errNotImplemented)
  119. }
  120. func (p *Device42) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  121. return nil, errors.New(errNotImplemented)
  122. }
  123. func (p *Device42) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  124. return errors.New(errNotImplemented)
  125. }
  126. func (p *Device42) GetSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  127. if utils.IsNil(p.client) {
  128. return nil, errors.New(errUninitializedProvider)
  129. }
  130. data, err := p.client.GetSecret(ref.Key)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return []byte(data.Password), nil
  135. }
  136. func (p *Device42) GetSecretMap(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  137. data, err := p.client.GetSecret(ref.Key)
  138. if err != nil {
  139. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  140. }
  141. return data.ToMap(), nil
  142. }
  143. func (p *Device42) Close(_ context.Context) error {
  144. return nil
  145. }
  146. func init() {
  147. esv1beta1.Register(&Device42{}, &esv1beta1.SecretStoreProvider{
  148. Device42: &esv1beta1.Device42Provider{},
  149. })
  150. }