provider.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright © The ESO Authors
  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 fortanix provides a Fortanix provider implementation.
  14. package fortanix
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "net/http"
  20. "github.com/fortanix/sdkms-client-go/sdkms"
  21. kubeclient "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/runtime/esutils"
  25. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  26. )
  27. // Provider implements provider interface for Fortanix Key Management.
  28. type Provider struct{}
  29. const (
  30. errCannotResolveSecretKeyRef = "cannot resolve secret key ref: %w"
  31. errStoreIsNil = "store is nil"
  32. errNoStoreTypeOrWrongStoreType = "no store type or wrong store type"
  33. errAPIKeyIsRequired = "apiKey is required"
  34. errAPIKeySecretRefIsRequired = "apiKey.secretRef is required"
  35. errAPIKeySecretRefNameIsRequired = "apiKey.secretRef.name is required"
  36. errAPIKeySecretRefKeyIsRequired = "apiKey.secretRef.key is required"
  37. )
  38. var _ esv1.Provider = &Provider{}
  39. // Capabilities returns the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  40. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  41. return esv1.SecretStoreReadOnly
  42. }
  43. // NewClient creates a new Fortanix Key Management client.
  44. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kubeclient.Client, namespace string) (esv1.SecretsClient, error) {
  45. config, err := getConfig(store)
  46. if err != nil {
  47. return nil, err
  48. }
  49. apiKey, err := resolvers.SecretKeyRef(ctx, kube, store.GetKind(), namespace, config.APIKey.SecretRef)
  50. if err != nil {
  51. return nil, fmt.Errorf(errCannotResolveSecretKeyRef, err)
  52. }
  53. sdkmsClient := sdkms.Client{
  54. HTTPClient: http.DefaultClient,
  55. Auth: sdkms.APIKey(apiKey),
  56. Endpoint: config.APIURL,
  57. }
  58. return &client{
  59. sdkms: sdkmsClient,
  60. }, nil
  61. }
  62. // ValidateStore validates the Fortanix Key Management store configuration.
  63. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  64. _, err := getConfig(store)
  65. return nil, err
  66. }
  67. func getConfig(store esv1.GenericStore) (*esv1.FortanixProvider, error) {
  68. if store == nil {
  69. return nil, errors.New(errStoreIsNil)
  70. }
  71. spec := store.GetSpec()
  72. if spec == nil || spec.Provider == nil || spec.Provider.Fortanix == nil {
  73. return nil, errors.New(errNoStoreTypeOrWrongStoreType)
  74. }
  75. config := spec.Provider.Fortanix
  76. if config.APIURL == "" {
  77. config.APIURL = "https://sdkms.fortanix.com"
  78. }
  79. err := validateSecretStoreRef(store, config.APIKey)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return config, nil
  84. }
  85. func validateSecretStoreRef(store esv1.GenericStore, ref *esv1.FortanixProviderSecretRef) error {
  86. if ref == nil {
  87. return errors.New(errAPIKeyIsRequired)
  88. }
  89. if ref.SecretRef == nil {
  90. return errors.New(errAPIKeySecretRefIsRequired)
  91. }
  92. if ref.SecretRef.Name == "" {
  93. return errors.New(errAPIKeySecretRefNameIsRequired)
  94. }
  95. if ref.SecretRef.Key == "" {
  96. return errors.New(errAPIKeySecretRefKeyIsRequired)
  97. }
  98. return esutils.ValidateReferentSecretSelector(store, *ref.SecretRef)
  99. }
  100. // NewProvider creates a new Provider instance.
  101. func NewProvider() esv1.Provider {
  102. return &Provider{}
  103. }
  104. // ProviderSpec returns the provider specification for registration.
  105. func ProviderSpec() *esv1.SecretStoreProvider {
  106. return &esv1.SecretStoreProvider{
  107. Fortanix: &esv1.FortanixProvider{},
  108. }
  109. }
  110. // MaintenanceStatus returns the maintenance status of the provider.
  111. func MaintenanceStatus() esv1.MaintenanceStatus {
  112. return esv1.MaintenanceStatusMaintained
  113. }