auth_jwt.go 3.0 KB

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