Configure AWS Workload Identity
Authenticate AWS workloads to Keycard with an IAM-issued identity token.
An AWS workload gets a short-lived token for its IAM role from AWS STS, with your Zone as its audience. It sends the token to Keycard along with the Client ID of the Application credential it authenticates as. Keycard checks the token’s signature with your account’s public keys, then checks its audience, subject, and claims. This works on any compute with an IAM role, such as Lambda, ECS, EC2, and EKS pods.
Prerequisites
Section titled “Prerequisites”- Familiarity with the concepts in Workload Identity, and with AWS’s outbound identity federation docs
- A Keycard Zone and an Application for the workload
- Your Zone issuer URL, from Settings → Connection in Keycard Console
- The Keycard CLI, signed in with
keycard auth signin --org <org-id> - An IAM role for the workload, and permission to manage IAM in its account
Enable outbound identity federation
Section titled “Enable outbound identity federation”Outbound identity federation is off by default. Enable it once per AWS account and note the issuer URL it returns:
aws iam enable-outbound-web-identity-federationThe IssuerIdentifier looks like https://<issuer-id>.tokens.sts.global.api.aws. To look it up later, run aws iam get-outbound-web-identity-federation-info.
Register the AWS account as a Provider
Section titled “Register the AWS account as a Provider”Each AWS account has its own issuer. Create one Provider per account:
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{ "name": "AWS <account-id>", "identifier": "https://<issuer-id>.tokens.sts.global.api.aws", "protocols": { "oauth2": { "issuer": "https://<issuer-id>.tokens.sts.global.api.aws" } }}'Keycard discovers the account’s signing keys from the issuer.
Create a token credential
Section titled “Create a token credential”-
Add the credential to the Application
Terminal window keycard agent api /zones/<zone-id>/application-credentials --org <org-id> -X POST -d '{"type": "token","version": 2,"application_id": "<application-id>","provider_id": "<aws-provider-id>","subject": "arn:aws:iam::<account-id>:role/<role-name>"}'Field Value subjectThe ARN of the role that requests the token. If the role has a path, include it: arn:aws:iam::<account-id>:role/<path>/<role-name>.allowed_audienceOmitted, so it defaults to your Zone issuer URL. The workload requests its token with that audience. authentication_conditionOptional. The issuer already limits tokens to your account. For an example token and every claim AWS includes, see AWS’s Understanding token claims.
-
Copy the credential’s Client ID
The response’s
identifieris the credential’s Client ID. The workload sends it asclient_id. It is not a secret.
Configure the workload
Section titled “Configure the workload”-
Allow the role to request identity tokens
Attach this policy to the workload’s role. It limits the role to ES384-signed tokens for your Zone:
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": "sts:GetWebIdentityToken","Resource": "*","Condition": {"ForAllValues:StringEquals": {"sts:IdentityTokenAudience": "<keycard-issuer-url>"},"Null": {"sts:IdentityTokenAudience": "false"},"StringEquals": {"sts:SigningAlgorithm": "ES384"}}}]} -
Set environment variables
Set these on the workload, for example in the Lambda configuration or the ECS task definition:
Variable Value KEYCARD_ISSUERYour Zone issuer URL KEYCARD_CLIENT_IDThe credential’s Client ID KEYCARD_RESOURCE_URLThe Resource the workload needs a token for -
Request an AWS token and exchange it
Request a token with your Zone issuer URL as the audience and
ES384as the signing algorithm. Keycard also acceptsRS256. Then send it asclient_assertion, with the credential’s Client ID asclient_id, in aclient_credentialsgrant:Terminal window pip install boto3 keycardai-oauthimport osimport boto3from keycardai.oauth import Client, GrantTypeaws_token = boto3.client("sts", region_name=os.environ["AWS_REGION"]).get_web_identity_token(Audience=[os.environ["KEYCARD_ISSUER"]],SigningAlgorithm="ES384",)["WebIdentityToken"]with Client(os.environ["KEYCARD_ISSUER"]) as client:response = client.client_credentials_grant(client_assertion=aws_token,client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,client_id=os.environ["KEYCARD_CLIENT_ID"],resource=os.environ["KEYCARD_RESOURCE_URL"],)access_token = response.access_tokenTerminal window npm install @aws-sdk/client-sts @keycardai/oauthimport { GetWebIdentityTokenCommand, STSClient } from "@aws-sdk/client-sts";import { ClientCredentialsClient } from "@keycardai/oauth";const { WebIdentityToken: awsToken } = await new STSClient({}).send(new GetWebIdentityTokenCommand({Audience: [process.env.KEYCARD_ISSUER!],SigningAlgorithm: "ES384",}),);const client = new ClientCredentialsClient(process.env.KEYCARD_ISSUER!);const response = await client.requestToken({clientAssertion: awsToken!,clientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",clientId: process.env.KEYCARD_CLIENT_ID!,resource: process.env.KEYCARD_RESOURCE_URL!,});const accessToken = response.accessToken;Terminal window go get github.com/aws/aws-sdk-go-v2/config github.com/aws/aws-sdk-go-v2/service/sts github.com/keycardai/go-sdk/oauthcfg, err := config.LoadDefaultConfig(ctx)if err != nil {return err}out, err := sts.NewFromConfig(cfg).GetWebIdentityToken(ctx, &sts.GetWebIdentityTokenInput{Audience: []string{os.Getenv("KEYCARD_ISSUER")},SigningAlgorithm: aws.String("ES384"),})if err != nil {return err}awsToken := aws.ToString(out.WebIdentityToken)client := oauth.NewClientCredentialsClient(os.Getenv("KEYCARD_ISSUER"))response, err := client.RequestToken(ctx, oauth.ClientCredentialsRequest{ClientID: os.Getenv("KEYCARD_CLIENT_ID"),ClientAssertion: awsToken,ClientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",Resource: os.Getenv("KEYCARD_RESOURCE_URL"),})if err != nil {return err}accessToken := response.AccessTokenTerminal window bundle add aws-sdk-corebundle add keycardai-oauthrequire "aws-sdk-core"require "keycardai/oauth"aws_token = Aws::STS::Client.new.get_web_identity_token(audience: [ENV.fetch("KEYCARD_ISSUER")],signing_algorithm: "ES384").web_identity_tokenclient = Keycardai::OAuth::ClientCredentialsClient.new(issuer: ENV.fetch("KEYCARD_ISSUER"))response = client.request_token(client_id: ENV.fetch("KEYCARD_CLIENT_ID"),client_assertion: aws_token,client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",resource: ENV.fetch("KEYCARD_RESOURCE_URL"))access_token = response.access_tokenTerminal window AWS_TOKEN=$(aws sts get-web-identity-token \--audience "$KEYCARD_ISSUER" \--signing-algorithm ES384 \--query WebIdentityToken --output text)curl -sS "$KEYCARD_ISSUER/oauth/2/token" \-d grant_type=client_credentials \-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \--data-urlencode "client_assertion=$AWS_TOKEN" \--data-urlencode "client_id=$KEYCARD_CLIENT_ID" \--data-urlencode "resource=$KEYCARD_RESOURCE_URL"
Verify
Section titled “Verify”Run the workload. The exchange returns an access token, and a credentials:issue event shows on the Application’s Activity tab in Keycard Console.
Troubleshooting
Section titled “Troubleshooting”Keycard returns invalid_client with Invalid client assertion when the signature, audience, subject, or condition doesn’t match. The Audit Log records details on the failure. Decode the token with the workload’s role to compare its claims against the credential:
aws sts get-web-identity-token --audience "$KEYCARD_ISSUER" --signing-algorithm ES384 \ --query WebIdentityToken --output text \ | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub}'OutboundWebIdentityFederationDisabledExceptionfrom AWS: outbound identity federation isn’t enabled in the account the role belongs to. Runaws iam enable-outbound-web-identity-federationthere.issdoesn’t match the Provider: the role is in a different account than the Provider. Register a Provider for that account’s issuer.subdoesn’t match: the subject is the role ARN, including any path. It is not theassumed-rolesession ARN.