|
|
@@ -16,6 +16,7 @@ package vault
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
@@ -35,13 +36,13 @@ import (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- defaultAWSRegion = "us-east-1"
|
|
|
- defaultAWSAuthMountPath = "aws"
|
|
|
- errIrsaTokenEnvVarNotFoundOnPod = "expected env variable: %s not found on controller's pod"
|
|
|
- errIrsaTokenFileNotFoundOnPod = "web ddentity token file not found at %s location: %w"
|
|
|
- errIrsaTokenFileNotReadable = "could not read the web identity token from the file %s: %w"
|
|
|
- errIrsaTokenNotValidJWT = "could not parse web identity token available at %s. not a valid jwt?: %w"
|
|
|
- errPodInfoNotFoundOnToken = "could not find pod identity info on token %s: %w"
|
|
|
+ defaultAWSRegion = "us-east-1"
|
|
|
+ defaultAWSAuthMountPath = "aws"
|
|
|
+ errNoAWSAuthMethodFound = "no AWS authentication method found: expected either IRSA or Pod Identity"
|
|
|
+ errIrsaTokenFileNotFoundOnPod = "web identity token file not found at %s location: %w"
|
|
|
+ errIrsaTokenFileNotReadable = "could not read the web identity token from the file %s: %w"
|
|
|
+ errIrsaTokenNotValidJWT = "could not parse web identity token available at %s. not a valid jwt?: %w"
|
|
|
+ errIrsaTokenNotValidClaims = "could not find pod identity info on token %s"
|
|
|
)
|
|
|
|
|
|
func setIamAuthToken(ctx context.Context, v *client, jwtProvider util.JwtProviderFactory, assumeRoler vaultiamauth.STSProvider) (bool, error) {
|
|
|
@@ -60,14 +61,9 @@ func setIamAuthToken(ctx context.Context, v *client, jwtProvider util.JwtProvide
|
|
|
func (c *client) requestTokenWithIamAuth(ctx context.Context, iamAuth *esv1.VaultIamAuth, isClusterKind bool, k kclient.Client, n string, jwtProvider util.JwtProviderFactory, assumeRoler vaultiamauth.STSProvider) error {
|
|
|
jwtAuth := iamAuth.JWTAuth
|
|
|
secretRefAuth := iamAuth.SecretRef
|
|
|
- regionAWS := defaultAWSRegion
|
|
|
- awsAuthMountPath := defaultAWSAuthMountPath
|
|
|
- if iamAuth.Region != "" {
|
|
|
- regionAWS = iamAuth.Region
|
|
|
- }
|
|
|
- if iamAuth.Path != "" {
|
|
|
- awsAuthMountPath = iamAuth.Path
|
|
|
- }
|
|
|
+ regionAWS := c.getRegionOrDefault(iamAuth.Region)
|
|
|
+ awsAuthMountPath := c.getAuthMountPathOrDefault(iamAuth.Path)
|
|
|
+
|
|
|
var creds *credentials.Credentials
|
|
|
var err error
|
|
|
if jwtAuth != nil { // use credentials from a sa explicitly defined and referenced. Highest preference is given to this method/configuration.
|
|
|
@@ -86,43 +82,7 @@ func (c *client) requestTokenWithIamAuth(ctx context.Context, iamAuth *esv1.Vaul
|
|
|
// Neither of jwtAuth or secretRefAuth defined. Last preference.
|
|
|
// Default to controller pod's identity
|
|
|
if jwtAuth == nil && secretRefAuth == nil {
|
|
|
- // Checking if controller pod's service account is IRSA enabled and Web Identity token is available on pod
|
|
|
- tokenFile, ok := os.LookupEnv(vaultiamauth.AWSWebIdentityTokenFileEnvVar)
|
|
|
- if !ok {
|
|
|
- return fmt.Errorf(errIrsaTokenEnvVarNotFoundOnPod, vaultiamauth.AWSWebIdentityTokenFileEnvVar) // No Web Identity(IRSA) token found on pod
|
|
|
- }
|
|
|
-
|
|
|
- // IRSA enabled service account, let's check that the jwt token filemount and file exists
|
|
|
- if _, err := os.Stat(tokenFile); err != nil {
|
|
|
- return fmt.Errorf(errIrsaTokenFileNotFoundOnPod, tokenFile, err)
|
|
|
- }
|
|
|
-
|
|
|
- // everything looks good so far, let's fetch the jwt token from AWS_WEB_IDENTITY_TOKEN_FILE
|
|
|
- jwtByte, err := os.ReadFile(filepath.Clean(tokenFile))
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf(errIrsaTokenFileNotReadable, tokenFile, err)
|
|
|
- }
|
|
|
-
|
|
|
- // let's parse the jwt token
|
|
|
- parser := jwt.NewParser(jwt.WithoutClaimsValidation())
|
|
|
-
|
|
|
- token, _, err := parser.ParseUnverified(string(jwtByte), jwt.MapClaims{})
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf(errIrsaTokenNotValidJWT, tokenFile, err) // JWT token parser error
|
|
|
- }
|
|
|
-
|
|
|
- var ns string
|
|
|
- var sa string
|
|
|
-
|
|
|
- // let's fetch the namespace and serviceaccount from parsed jwt token
|
|
|
- if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
|
|
- ns = claims["kubernetes.io"].(map[string]any)["namespace"].(string)
|
|
|
- sa = claims["kubernetes.io"].(map[string]any)["serviceaccount"].(map[string]any)["name"].(string)
|
|
|
- } else {
|
|
|
- return fmt.Errorf(errPodInfoNotFoundOnToken, tokenFile, err)
|
|
|
- }
|
|
|
-
|
|
|
- creds, err = vaultiamauth.CredsFromControllerServiceAccount(ctx, sa, ns, regionAWS, k, jwtProvider)
|
|
|
+ creds, err = c.getControllerPodCredentials(ctx, regionAWS, k, jwtProvider)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -183,3 +143,88 @@ func (c *client) requestTokenWithIamAuth(ctx context.Context, iamAuth *esv1.Vaul
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (c *client) getRegionOrDefault(region string) string {
|
|
|
+ if region != "" {
|
|
|
+ return region
|
|
|
+ }
|
|
|
+ return defaultAWSRegion
|
|
|
+}
|
|
|
+
|
|
|
+func (c *client) getAuthMountPathOrDefault(path string) string {
|
|
|
+ if path != "" {
|
|
|
+ return path
|
|
|
+ }
|
|
|
+ return defaultAWSAuthMountPath
|
|
|
+}
|
|
|
+
|
|
|
+func (c *client) getControllerPodCredentials(ctx context.Context, region string, k kclient.Client, jwtProvider util.JwtProviderFactory) (*credentials.Credentials, error) {
|
|
|
+ // First try IRSA (Web Identity Token) - checking if controller pod's service account is IRSA enabled
|
|
|
+ tokenFile := os.Getenv(vaultiamauth.AWSWebIdentityTokenFileEnvVar)
|
|
|
+ if tokenFile != "" {
|
|
|
+ logger.V(1).Info("using IRSA token for authentication")
|
|
|
+ return c.getCredsFromIRSAToken(ctx, tokenFile, region, k, jwtProvider)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check for Pod Identity environment variables.
|
|
|
+ podIdentityURI := os.Getenv(vaultiamauth.AWSContainerCredentialsFullURIEnvVar)
|
|
|
+
|
|
|
+ if podIdentityURI != "" {
|
|
|
+ logger.V(1).Info("using Pod Identity for authentication")
|
|
|
+ // Return nil to let AWS SDK v1 container credential provider handle Pod Identity automatically
|
|
|
+ return nil, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ // No IRSA or Pod Identity found.
|
|
|
+ return nil, errors.New(errNoAWSAuthMethodFound)
|
|
|
+}
|
|
|
+
|
|
|
+func (c *client) getCredsFromIRSAToken(ctx context.Context, tokenFile, region string, k kclient.Client, jwtProvider util.JwtProviderFactory) (*credentials.Credentials, error) {
|
|
|
+ // IRSA enabled service account, let's check that the jwt token filemount and file exists
|
|
|
+ if _, err := os.Stat(filepath.Clean(tokenFile)); err != nil {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenFileNotFoundOnPod, tokenFile, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // everything looks good so far, let's fetch the jwt token from AWS_WEB_IDENTITY_TOKEN_FILE
|
|
|
+ jwtByte, err := os.ReadFile(filepath.Clean(tokenFile))
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenFileNotReadable, tokenFile, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // let's parse the jwt token
|
|
|
+ parser := jwt.NewParser(jwt.WithoutClaimsValidation())
|
|
|
+
|
|
|
+ token, _, err := parser.ParseUnverified(string(jwtByte), jwt.MapClaims{})
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenNotValidJWT, tokenFile, err) // JWT token parser error
|
|
|
+ }
|
|
|
+
|
|
|
+ var ns string
|
|
|
+ var sa string
|
|
|
+
|
|
|
+ // let's fetch the namespace and serviceaccount from parsed jwt token
|
|
|
+ claims, ok := token.Claims.(jwt.MapClaims)
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
|
|
|
+ }
|
|
|
+
|
|
|
+ k8s, ok := claims["kubernetes.io"].(map[string]any)
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
|
|
|
+ }
|
|
|
+
|
|
|
+ ns, ok = k8s["namespace"].(string)
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
|
|
|
+ }
|
|
|
+ saMap, ok := k8s["serviceaccount"].(map[string]any)
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
|
|
|
+ }
|
|
|
+ sa, ok = saMap["name"].(string)
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
|
|
|
+ }
|
|
|
+
|
|
|
+ return vaultiamauth.CredsFromControllerServiceAccount(ctx, sa, ns, region, k, jwtProvider)
|
|
|
+}
|