client.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "github.com/cyberark/conjur-api-go/conjurapi"
  19. "github.com/cyberark/conjur-api-go/conjurapi/authn"
  20. corev1 "k8s.io/api/core/v1"
  21. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  22. "sigs.k8s.io/controller-runtime/pkg/client"
  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. "github.com/external-secrets/external-secrets/pkg/provider/conjur/util"
  27. )
  28. var (
  29. errConjurClient = "cannot setup new Conjur client: %w"
  30. errBadServiceUser = "could not get Auth.Apikey.UserRef: %w"
  31. errBadServiceAPIKey = "could not get Auth.Apikey.ApiKeyRef: %w"
  32. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  33. errSecretKeyFmt = "cannot find secret data for key: %q"
  34. )
  35. // Client is a provider for Conjur.
  36. type Client struct {
  37. StoreKind string
  38. kube client.Client
  39. store esv1.GenericStore
  40. namespace string
  41. corev1 typedcorev1.CoreV1Interface
  42. clientAPI SecretsClientFactory
  43. client SecretsClient
  44. }
  45. // GetConjurClient returns an authenticated Conjur client.
  46. // If a client is already initialized, it returns the existing client.
  47. // Otherwise, it creates a new client based on the authentication method specified.
  48. func (c *Client) GetConjurClient(ctx context.Context) (SecretsClient, error) {
  49. // if the client is initialized already, return it
  50. if c.client != nil {
  51. return c.client, nil
  52. }
  53. prov, err := conjurutil.GetConjurProvider(c.store)
  54. if err != nil {
  55. return nil, err
  56. }
  57. cert, getCertErr := esutils.FetchCACertFromSource(ctx, esutils.CreateCertOpts{
  58. CABundle: []byte(prov.CABundle),
  59. CAProvider: prov.CAProvider,
  60. StoreKind: c.store.GetKind(),
  61. Namespace: c.namespace,
  62. Client: c.kube,
  63. })
  64. if getCertErr != nil {
  65. return nil, getCertErr
  66. }
  67. config := conjurapi.Config{
  68. ApplianceURL: prov.URL,
  69. SSLCert: string(cert),
  70. // disable credential storage, as it depends on a writable
  71. // file system, which we can't rely on - it would fail.
  72. // see: https://github.com/cyberark/conjur-api-go/issues/183
  73. NetRCPath: "/dev/null",
  74. }
  75. if prov.Auth.APIKey != nil {
  76. return c.conjurClientFromAPIKey(ctx, config, prov)
  77. }
  78. if prov.Auth.Jwt != nil {
  79. return c.conjurClientFromJWT(ctx, config, prov)
  80. }
  81. // Should not happen because validate func should catch this
  82. return nil, errors.New("no authentication method provided")
  83. }
  84. // PushSecret will write a single secret into the provider.
  85. func (c *Client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  86. // NOT IMPLEMENTED
  87. return nil
  88. }
  89. // DeleteSecret removes a secret from the provider.
  90. func (c *Client) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  91. // NOT IMPLEMENTED
  92. return nil
  93. }
  94. // SecretExists checks if a secret exists in the provider.
  95. func (c *Client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  96. return false, errors.New("not implemented")
  97. }
  98. // Validate validates the provider configuration.
  99. func (c *Client) Validate() (esv1.ValidationResult, error) {
  100. return esv1.ValidationResultReady, nil
  101. }
  102. // Close closes the provider.
  103. func (c *Client) Close(_ context.Context) error {
  104. return nil
  105. }
  106. // conjurClientFromAPIKey creates a new Conjur client using API key authentication.
  107. func (c *Client) conjurClientFromAPIKey(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  108. config.Account = prov.Auth.APIKey.Account
  109. conjUser, secErr := resolvers.SecretKeyRef(
  110. ctx,
  111. c.kube,
  112. c.StoreKind,
  113. c.namespace, prov.Auth.APIKey.UserRef)
  114. if secErr != nil {
  115. return nil, fmt.Errorf(errBadServiceUser, secErr)
  116. }
  117. conjAPIKey, secErr := resolvers.SecretKeyRef(
  118. ctx,
  119. c.kube,
  120. c.StoreKind,
  121. c.namespace,
  122. prov.Auth.APIKey.APIKeyRef)
  123. if secErr != nil {
  124. return nil, fmt.Errorf(errBadServiceAPIKey, secErr)
  125. }
  126. conjur, newClientFromKeyError := c.clientAPI.NewClientFromKey(config,
  127. authn.LoginPair{
  128. Login: conjUser,
  129. APIKey: conjAPIKey,
  130. },
  131. )
  132. if newClientFromKeyError != nil {
  133. return nil, fmt.Errorf(errConjurClient, newClientFromKeyError)
  134. }
  135. c.client = conjur
  136. return conjur, nil
  137. }
  138. func (c *Client) conjurClientFromJWT(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  139. config.AuthnType = "jwt"
  140. config.Account = prov.Auth.Jwt.Account
  141. config.JWTHostID = prov.Auth.Jwt.HostID
  142. config.ServiceID = prov.Auth.Jwt.ServiceID
  143. jwtToken, getJWTError := c.getJWTToken(ctx, prov.Auth.Jwt)
  144. if getJWTError != nil {
  145. return nil, getJWTError
  146. }
  147. config.JWTContent = jwtToken
  148. conjur, clientError := c.clientAPI.NewClientFromJWT(config)
  149. if clientError != nil {
  150. return nil, fmt.Errorf(errConjurClient, clientError)
  151. }
  152. c.client = conjur
  153. return conjur, nil
  154. }