client.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/provider/conjur/util"
  25. "github.com/external-secrets/external-secrets/pkg/utils"
  26. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  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. func (c *Client) GetConjurClient(ctx context.Context) (SecretsClient, error) {
  46. // if the client is initialized already, return it
  47. if c.client != nil {
  48. return c.client, nil
  49. }
  50. prov, err := util.GetConjurProvider(c.store)
  51. if err != nil {
  52. return nil, err
  53. }
  54. cert, getCertErr := utils.FetchCACertFromSource(ctx, utils.CreateCertOpts{
  55. CABundle: []byte(prov.CABundle),
  56. CAProvider: prov.CAProvider,
  57. StoreKind: c.store.GetKind(),
  58. Namespace: c.namespace,
  59. Client: c.kube,
  60. })
  61. if getCertErr != nil {
  62. return nil, getCertErr
  63. }
  64. config := conjurapi.Config{
  65. ApplianceURL: prov.URL,
  66. SSLCert: string(cert),
  67. // disable credential storage, as it depends on a writable
  68. // file system, which we can't rely on - it would fail.
  69. // see: https://github.com/cyberark/conjur-api-go/issues/183
  70. NetRCPath: "/dev/null",
  71. }
  72. if prov.Auth.APIKey != nil {
  73. return c.conjurClientFromAPIKey(ctx, config, prov)
  74. } else if prov.Auth.Jwt != nil {
  75. return c.conjurClientFromJWT(ctx, config, prov)
  76. } else {
  77. // Should not happen because validate func should catch this
  78. return nil, errors.New("no authentication method provided")
  79. }
  80. }
  81. // PushSecret will write a single secret into the provider.
  82. func (c *Client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  83. // NOT IMPLEMENTED
  84. return nil
  85. }
  86. func (c *Client) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  87. // NOT IMPLEMENTED
  88. return nil
  89. }
  90. func (c *Client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  91. return false, errors.New("not implemented")
  92. }
  93. // Validate validates the provider.
  94. func (c *Client) Validate() (esv1.ValidationResult, error) {
  95. return esv1.ValidationResultReady, nil
  96. }
  97. // Close closes the provider.
  98. func (c *Client) Close(_ context.Context) error {
  99. return nil
  100. }
  101. func (c *Client) conjurClientFromAPIKey(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  102. config.Account = prov.Auth.APIKey.Account
  103. conjUser, secErr := resolvers.SecretKeyRef(
  104. ctx,
  105. c.kube,
  106. c.StoreKind,
  107. c.namespace, prov.Auth.APIKey.UserRef)
  108. if secErr != nil {
  109. return nil, fmt.Errorf(errBadServiceUser, secErr)
  110. }
  111. conjAPIKey, secErr := resolvers.SecretKeyRef(
  112. ctx,
  113. c.kube,
  114. c.StoreKind,
  115. c.namespace,
  116. prov.Auth.APIKey.APIKeyRef)
  117. if secErr != nil {
  118. return nil, fmt.Errorf(errBadServiceAPIKey, secErr)
  119. }
  120. conjur, newClientFromKeyError := c.clientAPI.NewClientFromKey(config,
  121. authn.LoginPair{
  122. Login: conjUser,
  123. APIKey: conjAPIKey,
  124. },
  125. )
  126. if newClientFromKeyError != nil {
  127. return nil, fmt.Errorf(errConjurClient, newClientFromKeyError)
  128. }
  129. c.client = conjur
  130. return conjur, nil
  131. }
  132. func (c *Client) conjurClientFromJWT(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  133. config.AuthnType = "jwt"
  134. config.Account = prov.Auth.Jwt.Account
  135. config.JWTHostID = prov.Auth.Jwt.HostID
  136. config.ServiceID = prov.Auth.Jwt.ServiceID
  137. jwtToken, getJWTError := c.getJWTToken(ctx, prov.Auth.Jwt)
  138. if getJWTError != nil {
  139. return nil, getJWTError
  140. }
  141. config.JWTContent = jwtToken
  142. conjur, clientError := c.clientAPI.NewClientFromJWT(config)
  143. if clientError != nil {
  144. return nil, fmt.Errorf(errConjurClient, clientError)
  145. }
  146. c.client = conjur
  147. return conjur, nil
  148. }