provider.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 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/pkg/esutils"
  25. "github.com/external-secrets/external-secrets/pkg/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. func init() {
  40. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  41. Fortanix: &esv1.FortanixProvider{},
  42. }, esv1.MaintenanceStatusMaintained)
  43. }
  44. // Capabilities returns the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  45. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  46. return esv1.SecretStoreReadOnly
  47. }
  48. // NewClient creates a new Fortanix Key Management client.
  49. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kubeclient.Client, namespace string) (esv1.SecretsClient, error) {
  50. config, err := getConfig(store)
  51. if err != nil {
  52. return nil, err
  53. }
  54. apiKey, err := resolvers.SecretKeyRef(ctx, kube, store.GetKind(), namespace, config.APIKey.SecretRef)
  55. if err != nil {
  56. return nil, fmt.Errorf(errCannotResolveSecretKeyRef, err)
  57. }
  58. sdkmsClient := sdkms.Client{
  59. HTTPClient: http.DefaultClient,
  60. Auth: sdkms.APIKey(apiKey),
  61. Endpoint: config.APIURL,
  62. }
  63. return &client{
  64. sdkms: sdkmsClient,
  65. }, nil
  66. }
  67. // ValidateStore validates the Fortanix Key Management store configuration.
  68. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  69. _, err := getConfig(store)
  70. return nil, err
  71. }
  72. func getConfig(store esv1.GenericStore) (*esv1.FortanixProvider, error) {
  73. if store == nil {
  74. return nil, errors.New(errStoreIsNil)
  75. }
  76. spec := store.GetSpec()
  77. if spec == nil || spec.Provider == nil || spec.Provider.Fortanix == nil {
  78. return nil, errors.New(errNoStoreTypeOrWrongStoreType)
  79. }
  80. config := spec.Provider.Fortanix
  81. if config.APIURL == "" {
  82. config.APIURL = "https://sdkms.fortanix.com"
  83. }
  84. err := validateSecretStoreRef(store, config.APIKey)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return config, nil
  89. }
  90. func validateSecretStoreRef(store esv1.GenericStore, ref *esv1.FortanixProviderSecretRef) error {
  91. if ref == nil {
  92. return errors.New(errAPIKeyIsRequired)
  93. }
  94. if ref.SecretRef == nil {
  95. return errors.New(errAPIKeySecretRefIsRequired)
  96. }
  97. if ref.SecretRef.Name == "" {
  98. return errors.New(errAPIKeySecretRefNameIsRequired)
  99. }
  100. if ref.SecretRef.Key == "" {
  101. return errors.New(errAPIKeySecretRefKeyIsRequired)
  102. }
  103. return esutils.ValidateReferentSecretSelector(store, *ref.SecretRef)
  104. }