provider.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 conjur provides a Conjur provider for External Secrets.
  14. package conjur
  15. import (
  16. "context"
  17. "k8s.io/client-go/kubernetes"
  18. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  19. "sigs.k8s.io/controller-runtime/pkg/client"
  20. ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. )
  23. // Provider implements the External Secrets provider interface for Conjur.
  24. // It facilitates creation of Conjur clients and manages their lifecycle.
  25. type Provider struct {
  26. NewConjurProvider func(context context.Context, store esv1.GenericStore, kube client.Client, namespace string, corev1 typedcorev1.CoreV1Interface, clientApi SecretsClientFactory) (esv1.SecretsClient, error)
  27. }
  28. // NewClient creates a new Conjur client using the provided store configuration.
  29. // It sets up necessary Kubernetes clients and creates a new Conjur provider instance.
  30. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  31. // controller-runtime/client does not support TokenRequest or other subresource APIs
  32. // so we need to construct our own client and use it to create a TokenRequest
  33. restCfg, err := ctrlcfg.GetConfig()
  34. if err != nil {
  35. return nil, err
  36. }
  37. clientset, err := kubernetes.NewForConfig(restCfg)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return p.NewConjurProvider(ctx, store, kube, namespace, clientset.CoreV1(), &ClientAPIImpl{})
  42. }
  43. // Capabilities returns the provider's supported capabilities.
  44. // Conjur provider supports read-only access to secrets.
  45. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  46. return esv1.SecretStoreReadOnly
  47. }
  48. // newConjurProvider creates and returns a new Conjur client with the specified configuration.
  49. func newConjurProvider(_ context.Context, store esv1.GenericStore, kube client.Client, namespace string, corev1 typedcorev1.CoreV1Interface, clientAPI SecretsClientFactory) (esv1.SecretsClient, error) {
  50. return &Client{
  51. StoreKind: store.GetObjectKind().GroupVersionKind().Kind,
  52. store: store,
  53. kube: kube,
  54. namespace: namespace,
  55. corev1: corev1,
  56. clientAPI: clientAPI,
  57. }, nil
  58. }
  59. func init() {
  60. esv1.Register(&Provider{
  61. NewConjurProvider: newConjurProvider,
  62. }, &esv1.SecretStoreProvider{
  63. Conjur: &esv1.ConjurProvider{},
  64. }, esv1.MaintenanceStatusMaintained)
  65. }