auth_jwt.go 3.2 KB

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