client.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package conjur
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "github.com/cyberark/conjur-api-go/conjurapi"
  18. "github.com/cyberark/conjur-api-go/conjurapi/authn"
  19. corev1 "k8s.io/api/core/v1"
  20. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  23. "github.com/external-secrets/external-secrets/pkg/provider/conjur/util"
  24. "github.com/external-secrets/external-secrets/pkg/utils"
  25. "github.com/external-secrets/external-secrets/pkg/utils/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. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  32. errSecretKeyFmt = "cannot find secret data for key: %q"
  33. )
  34. // Client is a provider for Conjur.
  35. type Client struct {
  36. StoreKind string
  37. kube client.Client
  38. store esv1beta1.GenericStore
  39. namespace string
  40. corev1 typedcorev1.CoreV1Interface
  41. clientAPI SecretsClientFactory
  42. client SecretsClient
  43. }
  44. func (c *Client) GetConjurClient(ctx context.Context) (SecretsClient, error) {
  45. // if the client is initialized already, return it
  46. if c.client != nil {
  47. return c.client, nil
  48. }
  49. prov, err := util.GetConjurProvider(c.store)
  50. if err != nil {
  51. return nil, err
  52. }
  53. cert, getCertErr := utils.FetchCACertFromSource(ctx, utils.CreateCertOpts{
  54. CABundle: []byte(prov.CABundle),
  55. CAProvider: prov.CAProvider,
  56. StoreKind: c.store.GetKind(),
  57. Namespace: c.namespace,
  58. Client: c.kube,
  59. })
  60. if getCertErr != nil {
  61. return nil, getCertErr
  62. }
  63. config := conjurapi.Config{
  64. ApplianceURL: prov.URL,
  65. SSLCert: string(cert),
  66. }
  67. if prov.Auth.APIKey != nil {
  68. return c.conjurClientFromAPIKey(ctx, config, prov)
  69. } else if prov.Auth.Jwt != nil {
  70. return c.conjurClientFromJWT(ctx, config, prov)
  71. } else {
  72. // Should not happen because validate func should catch this
  73. return nil, errors.New("no authentication method provided")
  74. }
  75. }
  76. // PushSecret will write a single secret into the provider.
  77. func (c *Client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  78. // NOT IMPLEMENTED
  79. return nil
  80. }
  81. func (c *Client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  82. // NOT IMPLEMENTED
  83. return nil
  84. }
  85. func (c *Client) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  86. return false, errors.New("not implemented")
  87. }
  88. // Validate validates the provider.
  89. func (c *Client) Validate() (esv1beta1.ValidationResult, error) {
  90. return esv1beta1.ValidationResultReady, nil
  91. }
  92. // Close closes the provider.
  93. func (c *Client) Close(_ context.Context) error {
  94. return nil
  95. }
  96. func (c *Client) conjurClientFromAPIKey(ctx context.Context, config conjurapi.Config, prov *esv1beta1.ConjurProvider) (SecretsClient, error) {
  97. config.Account = prov.Auth.APIKey.Account
  98. conjUser, secErr := resolvers.SecretKeyRef(
  99. ctx,
  100. c.kube,
  101. c.StoreKind,
  102. c.namespace, prov.Auth.APIKey.UserRef)
  103. if secErr != nil {
  104. return nil, fmt.Errorf(errBadServiceUser, secErr)
  105. }
  106. conjAPIKey, secErr := resolvers.SecretKeyRef(
  107. ctx,
  108. c.kube,
  109. c.StoreKind,
  110. c.namespace,
  111. prov.Auth.APIKey.APIKeyRef)
  112. if secErr != nil {
  113. return nil, fmt.Errorf(errBadServiceAPIKey, secErr)
  114. }
  115. conjur, newClientFromKeyError := c.clientAPI.NewClientFromKey(config,
  116. authn.LoginPair{
  117. Login: conjUser,
  118. APIKey: conjAPIKey,
  119. },
  120. )
  121. if newClientFromKeyError != nil {
  122. return nil, fmt.Errorf(errConjurClient, newClientFromKeyError)
  123. }
  124. c.client = conjur
  125. return conjur, nil
  126. }
  127. func (c *Client) conjurClientFromJWT(ctx context.Context, config conjurapi.Config, prov *esv1beta1.ConjurProvider) (SecretsClient, error) {
  128. config.AuthnType = "jwt"
  129. config.Account = prov.Auth.Jwt.Account
  130. config.JWTHostID = prov.Auth.Jwt.HostID
  131. config.ServiceID = prov.Auth.Jwt.ServiceID
  132. jwtToken, getJWTError := c.getJWTToken(ctx, prov.Auth.Jwt)
  133. if getJWTError != nil {
  134. return nil, getJWTError
  135. }
  136. config.JWTContent = jwtToken
  137. conjur, clientError := c.clientAPI.NewClientFromJWT(config)
  138. if clientError != nil {
  139. return nil, fmt.Errorf(errConjurClient, clientError)
  140. }
  141. c.client = conjur
  142. return conjur, nil
  143. }