I am getting this error when calling AssumeRoleWithWebIdentityCommand from a firebase function.
Error: {
Type: 'Sender',
Code: 'InvalidIdentityToken',
Message: 'Incorrect token audience',
}
I have configured a Service account, assigned it to the firebase function, and am attempting to assume the aws role like so.
const config = {
policyName: "Firebase-Access-Dynamo",
roleArn: "arn:aws:iam::---aws_account_id--:role/Firebase-Role",
projectID: "---gcp_project_id----",
serviceAccountName: "aws-bridge",
};
async function getGoogleIdToken() {
const idc = await new GoogleAuth().getIdTokenClient(config.policyName);
const { Authorization } = await idc.getRequestHeaders();
return Authorization.replace(/^Bearer\s+/i, "");
}
exports.beforeusercreate = beforeUserCreated(
{
serviceAccount: `${config.serviceAccountName}@${config.projectID}.iam.gserviceaccount.com`,
},
async () => {
const sts = new STSClient({ region: "us-east-1" });
const { Credentials } = await sts.send(
new AssumeRoleWithWebIdentityCommand({
RoleArn: config.roleArn,
RoleSessionName: `gcp-${Date.now()}`,
WebIdentityToken: await getGoogleIdToken(),
})
);
if (!Credentials) {
throw new Error("no credential");
}
....
}
);
In terraform I have configured the google service account like so:
resource "google_service_account" "aws_bridge" {
account_id = "aws-bridge"
display_name = "AWS Bridge (Firebase v2)"
}
#...some more stuff
locals {
gcp_trust_policy = "Firebase-Access-Dynamo"
}
resource "aws_iam_openid_connect_provider" "google" {
url = "https://accounts.google.com"
client_id_list = [local.gcp_trust_policy]
}
resource "aws_iam_role" "firebase_access" {
name = "Firebase-Role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow",
Principal = { Federated = "accounts.google.com" },
Action = "sts:AssumeRoleWithWebIdentity",
Condition = {
StringEquals = {
"accounts.google.com:aud": google_service_account.aws_bridge.unique_id,
"accounts.google.com:sub": google_service_account.aws_bridge.unique_id,
"accounts.google.com:oaud": local.gcp_trust_policy
}
}
}]
})
}
resource "aws_iam_role_policy" "dynamo_access" {
name = local.gcp_trust_policy
role = aws_iam_role.firebase_access.id
policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow",
Action = [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
"dynamodb:Query"
],
Resource = [
aws_dynamodb_table.architect.arn,
"${aws_dynamodb_table.architect.arn}/index/*"
]
}]
})
}
I have a lot of other terraform modules, I extracted the ones that I think pertain to my question. I'm hoping it's something obvious.
I have tried some suggestions from ChatGPT that haven't helped. Finding examples for this subject is difficult.