-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fetchIdToken
fails with a 400 error when using the Compute
client on GKE with Workload Identity
#1305
Comments
@sjaq thanks for digging into this, this is a somewhat fascinating bug. @bojeil-google is this a known issue with the workload identify server, I'm wondering if we should open an internal bug. |
This is a known limitation. We have filed an internal feature request for this (@bcoe FYI: b/196280129) |
Thanks for looking into this! Is this something that I need to work around in my implementation or something I can expect an update of the library for? Just want to prevent any unnecessary workaround in my codebase... Seems like an easy fix to reorder the query arguments in the library itself? |
Hey @sjaq, we will implement it the same way I am describing above (it is not a temporary fix or hack). So you can do it on your own if you can't wait. We want to eventually do it but we have to align across languages (we need to support in several languages and not just Node.js), so it may take time to get to it given the many projects in our pipeline at the moment. |
Thanks @bojeil-google, I'll implement a workaround for the time being then. Not entirely sure how to get the service account email properly when running with workload identity though, could you point me in the right direction? |
There is a
|
Thanks @bojeil-google, but that method seems to be unavailable for the I've used the following implementation for now, which is far from ideal considering it has to do multiple requests to collect all the information and the service account needs the async function getImpersonatedToken(
client: Client,
{ serviceAccount, audience }: TokenOptions
): Promise<string> {
const response = await client.request<any>({
method: 'POST',
url: `https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${serviceAccount}:generateIdToken`,
data: {
audience,
includeEmail: true
}
});
const token = response?.data?.token;
if (typeof token === 'string') {
return token;
} else {
throw new Error('Failed to establish secure API communication, invalid response');
}
}
export async function getIdToken(audience: string, serviceAccount?: string): Promise<string> {
const client = await auth.getClient();
if (client instanceof JWT || client instanceof Compute || client instanceof UserRefreshClient) {
if (serviceAccount) {
return getImpersonatedToken(client, { serviceAccount, audience: audience });
} else if (client instanceof Compute || client instanceof JWT) {
const token = await client.getAccessToken();
if (!token.token) {
throw new Error(
'Failed to establish secure API communication, could not fetch access token from Compute instance'
);
}
const info = await client.getTokenInfo(token.token);
if (!info.email) {
throw new Error(
'Failed to establish secure API communication, could not fetch service account email from Compute instance'
);
}
return getImpersonatedToken(client, { serviceAccount: info.email, audience: audience });
}
}
throw new Error('Failed to establish secure API communication, invalid client type received');
} |
@sjaq I'm glad you found a workaround, but I agree this is ugly (sorry that you have to do this). @bojeil-google and I will work together to get a real fix out to you as soon as we can, we'll keep this bug open until then. |
I am not sure why you are using a |
@bojeil-google I'm using workload identity as described here to run workloads that have a provided Google Service Account, when I use the nodejs client I'm provided with a |
@sjaq I'm glad you've found a workaround that's doing the trick for you. It seems like a nice feature would be if you could somehow upgrade a Compute client to a WorkLoadIdentityCompute client? Leaving this open as a feature request. |
Environment details
google-auth-library
version: 7.9.2Steps to reproduce
Cause of issue
After some debugging what is going wrong it seems the Workload Identity Metadata server only accepts the
audience
query parameter if it is the first argument.The text was updated successfully, but these errors were encountered: