client.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "fmt"
  16. "github.com/cyberark/conjur-api-go/conjurapi"
  17. "github.com/cyberark/conjur-api-go/conjurapi/authn"
  18. corev1 "k8s.io/api/core/v1"
  19. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. "github.com/external-secrets/external-secrets/pkg/provider/conjur/util"
  23. "github.com/external-secrets/external-secrets/pkg/utils"
  24. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  25. )
  26. var (
  27. errConjurClient = "cannot setup new Conjur client: %w"
  28. errBadServiceUser = "could not get Auth.Apikey.UserRef: %w"
  29. errBadServiceAPIKey = "could not get Auth.Apikey.ApiKeyRef: %w"
  30. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  31. errSecretKeyFmt = "cannot find secret data for key: %q"
  32. )
  33. // Client is a provider for Conjur.
  34. type Client struct {
  35. StoreKind string
  36. kube client.Client
  37. store esv1beta1.GenericStore
  38. namespace string
  39. corev1 typedcorev1.CoreV1Interface
  40. clientAPI SecretsClientFactory
  41. client SecretsClient
  42. }
  43. func (c *Client) GetConjurClient(ctx context.Context) (SecretsClient, error) {
  44. // if the client is initialized already, return it
  45. if c.client != nil {
  46. return c.client, nil
  47. }
  48. prov, err := util.GetConjurProvider(c.store)
  49. if err != nil {
  50. return nil, err
  51. }
  52. cert, getCertErr := utils.FetchCACertFromSource(ctx, utils.CreateCertOpts{
  53. CABundle: []byte(prov.CABundle),
  54. CAProvider: prov.CAProvider,
  55. StoreKind: c.store.GetKind(),
  56. Namespace: c.namespace,
  57. Client: c.kube,
  58. })
  59. if getCertErr != nil {
  60. return nil, getCertErr
  61. }
  62. config := conjurapi.Config{
  63. ApplianceURL: prov.URL,
  64. SSLCert: string(cert),
  65. }
  66. if prov.Auth.APIKey != nil {
  67. config.Account = prov.Auth.APIKey.Account
  68. conjUser, secErr := resolvers.SecretKeyRef(
  69. ctx,
  70. c.kube,
  71. c.StoreKind,
  72. c.namespace, prov.Auth.APIKey.UserRef)
  73. if secErr != nil {
  74. return nil, fmt.Errorf(errBadServiceUser, secErr)
  75. }
  76. conjAPIKey, secErr := resolvers.SecretKeyRef(
  77. ctx,
  78. c.kube,
  79. c.StoreKind,
  80. c.namespace,
  81. prov.Auth.APIKey.APIKeyRef)
  82. if secErr != nil {
  83. return nil, fmt.Errorf(errBadServiceAPIKey, secErr)
  84. }
  85. conjur, newClientFromKeyError := c.clientAPI.NewClientFromKey(config,
  86. authn.LoginPair{
  87. Login: conjUser,
  88. APIKey: conjAPIKey,
  89. },
  90. )
  91. if newClientFromKeyError != nil {
  92. return nil, fmt.Errorf(errConjurClient, newClientFromKeyError)
  93. }
  94. c.client = conjur
  95. return conjur, nil
  96. } else if prov.Auth.Jwt != nil {
  97. config.Account = prov.Auth.Jwt.Account
  98. conjur, clientFromJwtError := c.newClientFromJwt(ctx, config, prov.Auth.Jwt)
  99. if clientFromJwtError != nil {
  100. return nil, fmt.Errorf(errConjurClient, clientFromJwtError)
  101. }
  102. c.client = conjur
  103. return conjur, nil
  104. } else {
  105. // Should not happen because validate func should catch this
  106. return nil, fmt.Errorf("no authentication method provided")
  107. }
  108. }
  109. // PushSecret will write a single secret into the provider.
  110. func (c *Client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  111. // NOT IMPLEMENTED
  112. return nil
  113. }
  114. func (c *Client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  115. // NOT IMPLEMENTED
  116. return nil
  117. }
  118. func (c *Client) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  119. return false, fmt.Errorf("not implemented")
  120. }
  121. // Validate validates the provider.
  122. func (c *Client) Validate() (esv1beta1.ValidationResult, error) {
  123. return esv1beta1.ValidationResultReady, nil
  124. }
  125. // Close closes the provider.
  126. func (c *Client) Close(_ context.Context) error {
  127. return nil
  128. }