client.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. config.Account = prov.Auth.APIKey.Account
  69. conjUser, secErr := resolvers.SecretKeyRef(
  70. ctx,
  71. c.kube,
  72. c.StoreKind,
  73. c.namespace, prov.Auth.APIKey.UserRef)
  74. if secErr != nil {
  75. return nil, fmt.Errorf(errBadServiceUser, secErr)
  76. }
  77. conjAPIKey, secErr := resolvers.SecretKeyRef(
  78. ctx,
  79. c.kube,
  80. c.StoreKind,
  81. c.namespace,
  82. prov.Auth.APIKey.APIKeyRef)
  83. if secErr != nil {
  84. return nil, fmt.Errorf(errBadServiceAPIKey, secErr)
  85. }
  86. conjur, newClientFromKeyError := c.clientAPI.NewClientFromKey(config,
  87. authn.LoginPair{
  88. Login: conjUser,
  89. APIKey: conjAPIKey,
  90. },
  91. )
  92. if newClientFromKeyError != nil {
  93. return nil, fmt.Errorf(errConjurClient, newClientFromKeyError)
  94. }
  95. c.client = conjur
  96. return conjur, nil
  97. } else if prov.Auth.Jwt != nil {
  98. config.Account = prov.Auth.Jwt.Account
  99. conjur, clientFromJwtError := c.newClientFromJwt(ctx, config, prov.Auth.Jwt)
  100. if clientFromJwtError != nil {
  101. return nil, fmt.Errorf(errConjurClient, clientFromJwtError)
  102. }
  103. c.client = conjur
  104. return conjur, nil
  105. } else {
  106. // Should not happen because validate func should catch this
  107. return nil, errors.New("no authentication method provided")
  108. }
  109. }
  110. // PushSecret will write a single secret into the provider.
  111. func (c *Client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  112. // NOT IMPLEMENTED
  113. return nil
  114. }
  115. func (c *Client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  116. // NOT IMPLEMENTED
  117. return nil
  118. }
  119. func (c *Client) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  120. return false, errors.New("not implemented")
  121. }
  122. // Validate validates the provider.
  123. func (c *Client) Validate() (esv1beta1.ValidationResult, error) {
  124. return esv1beta1.ValidationResultReady, nil
  125. }
  126. // Close closes the provider.
  127. func (c *Client) Close(_ context.Context) error {
  128. return nil
  129. }