client.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 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. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. conjurutil "github.com/external-secrets/external-secrets/providers/v1/conjur/util"
  24. "github.com/external-secrets/external-secrets/runtime/esutils"
  25. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  26. )
  27. var (
  28. errConjurClient = "cannot setup new Conjur client: %w"
  29. errBadServiceUser = "could not get Auth.Apikey.UserRef: %w"
  30. errBadServiceAPIKey = "could not get Auth.Apikey.ApiKeyRef: %w"
  31. errBadClientCert = "could not get Auth.Cert.ClientCertRef: %w"
  32. errBadClientKey = "could not get Auth.Cert.ClientKeyRef: %w"
  33. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  34. errSecretKeyFmt = "cannot find secret data for key: %q"
  35. )
  36. // Client is a provider for Conjur.
  37. type Client struct {
  38. StoreKind string
  39. kube client.Client
  40. store esv1.GenericStore
  41. namespace string
  42. corev1 typedcorev1.CoreV1Interface
  43. clientAPI SecretsClientFactory
  44. client SecretsClient
  45. }
  46. // GetConjurClient returns an authenticated Conjur client.
  47. // If a client is already initialized, it returns the existing client.
  48. // Otherwise, it creates a new client based on the authentication method specified.
  49. func (c *Client) GetConjurClient(ctx context.Context) (SecretsClient, error) {
  50. // if the client is initialized already, return it
  51. if c.client != nil {
  52. return c.client, nil
  53. }
  54. prov, err := conjurutil.GetConjurProvider(c.store)
  55. if err != nil {
  56. return nil, err
  57. }
  58. cert, getCertErr := esutils.FetchCACertFromSource(ctx, esutils.CreateCertOpts{
  59. CABundle: []byte(prov.CABundle),
  60. CAProvider: prov.CAProvider,
  61. StoreKind: c.store.GetKind(),
  62. Namespace: c.namespace,
  63. Client: c.kube,
  64. })
  65. if getCertErr != nil {
  66. return nil, getCertErr
  67. }
  68. config := conjurapi.Config{
  69. ApplianceURL: prov.URL,
  70. SSLCert: string(cert),
  71. // disable credential storage, as it depends on a writable
  72. // file system, which we can't rely on - it would fail.
  73. CredentialStorage: conjurapi.CredentialStorageNone,
  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. if prov.Auth.Cert != nil {
  82. return c.conjurClientFromCert(ctx, config, prov)
  83. }
  84. // Should not happen because validate func should catch this
  85. return nil, errors.New("no authentication method provided")
  86. }
  87. // DeleteSecret removes a secret from the provider.
  88. func (c *Client) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  89. return errors.New("deleting secrets is not implemented for the Conjur provider")
  90. }
  91. // SecretExists checks if a secret exists in the provider.
  92. func (c *Client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  93. return false, errors.New("not implemented")
  94. }
  95. // Validate validates the provider configuration.
  96. func (c *Client) Validate() (esv1.ValidationResult, error) {
  97. return esv1.ValidationResultReady, nil
  98. }
  99. // Close closes the provider.
  100. func (c *Client) Close(_ context.Context) error {
  101. return nil
  102. }
  103. // conjurClientFromAPIKey creates a new Conjur client using API key authentication.
  104. func (c *Client) conjurClientFromAPIKey(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  105. config.Account = prov.Auth.APIKey.Account
  106. conjUser, secErr := resolvers.SecretKeyRef(
  107. ctx,
  108. c.kube,
  109. c.StoreKind,
  110. c.namespace, prov.Auth.APIKey.UserRef)
  111. if secErr != nil {
  112. return nil, fmt.Errorf(errBadServiceUser, secErr)
  113. }
  114. conjAPIKey, secErr := resolvers.SecretKeyRef(
  115. ctx,
  116. c.kube,
  117. c.StoreKind,
  118. c.namespace,
  119. prov.Auth.APIKey.APIKeyRef)
  120. if secErr != nil {
  121. return nil, fmt.Errorf(errBadServiceAPIKey, secErr)
  122. }
  123. conjur, newClientFromKeyError := c.clientAPI.NewClientFromKey(config,
  124. authn.LoginPair{
  125. Login: conjUser,
  126. APIKey: conjAPIKey,
  127. },
  128. )
  129. if newClientFromKeyError != nil {
  130. return nil, fmt.Errorf(errConjurClient, newClientFromKeyError)
  131. }
  132. c.client = conjur
  133. return conjur, nil
  134. }
  135. func (c *Client) conjurClientFromJWT(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  136. config.AuthnType = "jwt"
  137. config.Account = prov.Auth.Jwt.Account
  138. config.JWTHostID = prov.Auth.Jwt.HostID
  139. config.ServiceID = prov.Auth.Jwt.ServiceID
  140. jwtToken, getJWTError := c.getJWTToken(ctx, prov.Auth.Jwt)
  141. if getJWTError != nil {
  142. return nil, getJWTError
  143. }
  144. config.JWTContent = jwtToken
  145. conjur, clientError := c.clientAPI.NewClientFromJWT(config)
  146. if clientError != nil {
  147. return nil, fmt.Errorf(errConjurClient, clientError)
  148. }
  149. c.client = conjur
  150. return conjur, nil
  151. }
  152. func (c *Client) conjurClientFromCert(ctx context.Context, config conjurapi.Config, prov *esv1.ConjurProvider) (SecretsClient, error) {
  153. config.AuthnType = "cert"
  154. config.Account = prov.Auth.Cert.Account
  155. config.ServiceID = prov.Auth.Cert.ServiceID
  156. config.CertHostID = prov.Auth.Cert.HostID
  157. clientCert, secErr := resolvers.SecretKeyRef(
  158. ctx,
  159. c.kube,
  160. c.StoreKind,
  161. c.namespace, prov.Auth.Cert.ClientCertRef)
  162. if secErr != nil {
  163. return nil, fmt.Errorf(errBadClientCert, secErr)
  164. }
  165. config.ClientCert = clientCert
  166. clientKey, secErr := resolvers.SecretKeyRef(
  167. ctx,
  168. c.kube,
  169. c.StoreKind,
  170. c.namespace,
  171. prov.Auth.Cert.ClientKeyRef)
  172. if secErr != nil {
  173. return nil, fmt.Errorf(errBadClientKey, secErr)
  174. }
  175. config.ClientCertKey = clientKey
  176. conjur, clientError := c.clientAPI.NewClientFromCert(config)
  177. if clientError != nil {
  178. return nil, fmt.Errorf(errConjurClient, clientError)
  179. }
  180. c.client = conjur
  181. return conjur, nil
  182. }