auth_jwt.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. authenticationv1 "k8s.io/api/authentication/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  22. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  23. )
  24. const JwtLifespan = 600 // 10 minutes
  25. // getJWTToken retrieves a JWT token either using the TokenRequest API for a specified service account, or from a jwt stored in a k8s secret.
  26. func (c *Client) getJWTToken(ctx context.Context, conjurJWTConfig *esv1.ConjurJWT) (string, error) {
  27. if conjurJWTConfig.ServiceAccountRef != nil {
  28. // Should work for Kubernetes >=v1.22: fetch token via TokenRequest API
  29. jwtToken, err := c.getJwtFromServiceAccountTokenRequest(ctx, *conjurJWTConfig.ServiceAccountRef, nil, JwtLifespan)
  30. if err != nil {
  31. return "", err
  32. }
  33. return jwtToken, nil
  34. } else if conjurJWTConfig.SecretRef != nil {
  35. tokenRef := conjurJWTConfig.SecretRef
  36. if tokenRef.Key == "" {
  37. tokenRef = conjurJWTConfig.SecretRef.DeepCopy()
  38. tokenRef.Key = "token"
  39. }
  40. jwtToken, err := resolvers.SecretKeyRef(
  41. ctx,
  42. c.kube,
  43. c.StoreKind,
  44. c.namespace,
  45. tokenRef)
  46. if err != nil {
  47. return "", err
  48. }
  49. return jwtToken, nil
  50. }
  51. return "", errors.New("missing ServiceAccountRef or SecretRef")
  52. }
  53. // getJwtFromServiceAccountTokenRequest uses the TokenRequest API to get a JWT token for the given service account.
  54. func (c *Client) getJwtFromServiceAccountTokenRequest(ctx context.Context, serviceAccountRef esmeta.ServiceAccountSelector, additionalAud []string, expirationSeconds int64) (string, error) {
  55. audiences := serviceAccountRef.Audiences
  56. if len(additionalAud) > 0 {
  57. audiences = append(audiences, additionalAud...)
  58. }
  59. tokenRequest := &authenticationv1.TokenRequest{
  60. ObjectMeta: metav1.ObjectMeta{
  61. Namespace: c.namespace,
  62. },
  63. Spec: authenticationv1.TokenRequestSpec{
  64. Audiences: audiences,
  65. ExpirationSeconds: &expirationSeconds,
  66. },
  67. }
  68. if (c.StoreKind == esv1.ClusterSecretStoreKind) &&
  69. (serviceAccountRef.Namespace != nil) {
  70. tokenRequest.Namespace = *serviceAccountRef.Namespace
  71. }
  72. tokenResponse, err := c.corev1.ServiceAccounts(tokenRequest.Namespace).CreateToken(ctx, serviceAccountRef.Name, tokenRequest, metav1.CreateOptions{})
  73. if err != nil {
  74. return "", fmt.Errorf(errGetKubeSATokenRequest, serviceAccountRef.Name, err)
  75. }
  76. return tokenResponse.Status.Token, nil
  77. }