client.go 6.6 KB

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