device42.go 5.6 KB

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