Repository navigation
fix(kiro): route enterprise IAM Identity Center accounts to their regional endpoint #3631
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
Changes from 2 commits
99397b4
42ef4d9
eacf04e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -168,6 +168,37 @@ function ensureKiroUsage(state: KiroStreamState) { | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Resolve the AWS region for a Kiro/CodeWhisperer connection. Enterprise AWS IAM Identity | ||||||
| * Center accounts are region-bound: the access token, the Q Developer profile ARN and the | ||||||
| * runtime endpoint must all match the region the IdC instance lives in (e.g. eu-central-1). | ||||||
| * A request signed for one region is rejected by another ("bearer token is invalid"), and a | ||||||
| * regional profileArn sent to us-east-1 fails with "Improperly formed request". Falls back to | ||||||
| * the region embedded in the profileArn, then us-east-1 (the AWS Builder ID default). | ||||||
| */ | ||||||
| export function resolveKiroRegion( | ||||||
| credentials: { providerSpecificData?: unknown } | null | undefined | ||||||
| ): string { | ||||||
| const psd = (credentials?.providerSpecificData || {}) as Record<string, unknown>; | ||||||
| const region = typeof psd.region === "string" ? psd.region.trim() : ""; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The region string retrieved from
Suggested change
|
||||||
| if (region) return region; | ||||||
| const arn = typeof psd.profileArn === "string" ? psd.profileArn : ""; | ||||||
| const match = arn.match(/^arn:aws:codewhisperer:([a-z0-9-]+):/); | ||||||
| return match ? match[1] : "us-east-1"; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * CodeWhisperer/Amazon Q runtime host for a region. us-east-1 keeps the legacy | ||||||
| * codewhisperer.us-east-1 host (AWS Builder ID); other regions use the regional Amazon Q | ||||||
| * endpoint q.{region}.amazonaws.com — codewhisperer.{region}.amazonaws.com does not resolve | ||||||
| * for non-us-east-1 regions. | ||||||
| */ | ||||||
| export function kiroRuntimeHost(region: string): string { | ||||||
| return region === "us-east-1" | ||||||
| ? "https://codewhisperer.us-east-1.amazonaws.com" | ||||||
| : `https://q.${region}.amazonaws.com`; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * KiroExecutor - Executor for Kiro AI (AWS CodeWhisperer) | ||||||
| * Uses AWS CodeWhisperer streaming API with AWS EventStream binary format | ||||||
|
|
@@ -228,7 +259,11 @@ export class KiroExecutor extends BaseExecutor { | |||||
| log, | ||||||
| upstreamExtraHeaders, | ||||||
| }: ExecuteInput) { | ||||||
| const url = this.buildUrl(model, stream, 0); | ||||||
| // Route to the region-specific CodeWhisperer/Amazon Q endpoint. Enterprise IAM Identity | ||||||
| // Center accounts (e.g. eu-central-1) are rejected by the default us-east-1 host; only the | ||||||
| // regional endpoint accepts the region-bound token + profileArn. | ||||||
| const region = resolveKiroRegion(credentials); | ||||||
| const url = `${kiroRuntimeHost(region)}/generateAssistantResponse`; | ||||||
| const headers = this.buildHeaders(credentials, stream); | ||||||
| mergeUpstreamExtraHeaders(headers, upstreamExtraHeaders); | ||||||
| const transformedBody = await this.transformRequest(model, body, stream, credentials); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3005,14 +3005,29 @@ async function getKiroUsage(accessToken?: string, providerSpecificData?: JsonRec | |||||||||||||||||
| return { message: "Kiro connected. Profile ARN not available for quota tracking." }; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Enterprise IAM Identity Center accounts are region-bound: the profileArn, token and | ||||||||||||||||||
| // endpoint must all match the region. Derive the region from the stored region (preferred) | ||||||||||||||||||
| // or the profileArn, then route to the regional Amazon Q endpoint (us-east-1 keeps the | ||||||||||||||||||
| // legacy codewhisperer host; codewhisperer.{region} does not resolve for other regions). | ||||||||||||||||||
| const regionFromArn = | ||||||||||||||||||
| typeof profileArn === "string" | ||||||||||||||||||
| ? profileArn.match(/^arn:aws:codewhisperer:([a-z0-9-]+):/)?.[1] | ||||||||||||||||||
| : undefined; | ||||||||||||||||||
| const region = | ||||||||||||||||||
| (typeof providerSpecificData?.region === "string" && providerSpecificData.region.trim()) || | ||||||||||||||||||
| regionFromArn || | ||||||||||||||||||
| "us-east-1"; | ||||||||||||||||||
|
Comment on lines
+3016
to
+3020
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the executor, the region retrieved from
Suggested change
|
||||||||||||||||||
| const usageBaseUrl = | ||||||||||||||||||
| region === "us-east-1" ? CODEWHISPERER_BASE_URL : `https://q.${region}.amazonaws.com`; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Kiro uses AWS CodeWhisperer GetUsageLimits API | ||||||||||||||||||
| const payload = { | ||||||||||||||||||
| origin: "AI_EDITOR", | ||||||||||||||||||
| profileArn: profileArn, | ||||||||||||||||||
| resourceType: "AGENTIC_REQUEST", | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const response = await fetch(CODEWHISPERER_BASE_URL, { | ||||||||||||||||||
| const response = await fetch(usageBaseUrl, { | ||||||||||||||||||
| method: "POST", | ||||||||||||||||||
| headers: { | ||||||||||||||||||
| Authorization: `Bearer ${accessToken}`, | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,41 @@ export const kiro = { | |
| } | ||
|
|
||
| if (data.accessToken) { | ||
| // Enterprise IAM Identity Center accounts require a region-bound Q Developer profileArn on | ||
| // every CodeWhisperer call; without it AWS returns 403 "User is not authorized to make this | ||
| // call". The device-code flow does not return one, so discover it via ListAvailableProfiles | ||
| // against the same regional endpoint the token was issued for (best-effort; AWS Builder ID | ||
| // accounts legitimately have no profile and this simply yields none). | ||
| let profileArn: string | undefined; | ||
| try { | ||
| const runtimeHost = | ||
| tokenRegion === "us-east-1" | ||
| ? "https://codewhisperer.us-east-1.amazonaws.com" | ||
| : `https://q.${tokenRegion}.amazonaws.com`; | ||
| const profRes = await fetch(`${runtimeHost}/`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/x-amz-json-1.0", | ||
| Accept: "application/json", | ||
| "x-amz-target": "AmazonCodeWhispererService.ListAvailableProfiles", | ||
| Authorization: `Bearer ${data.accessToken}`, | ||
| }, | ||
| body: JSON.stringify({ maxResults: 10 }), | ||
| }); | ||
| if (profRes.ok) { | ||
| const profData = await profRes.json(); | ||
| const profiles = Array.isArray(profData?.profiles) ? profData.profiles : []; | ||
| const matched = | ||
| profiles.find( | ||
| (p: { arn?: string }) => | ||
| typeof p?.arn === "string" && p.arn.includes(`:${tokenRegion}:`) | ||
| ) || profiles[0]; | ||
| if (matched && typeof matched.arn === "string") profileArn = matched.arn; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two issues here:
try {
const normalizedRegion = tokenRegion.toLowerCase();
const runtimeHost =
normalizedRegion === "us-east-1"
? "https://codewhisperer.us-east-1.amazonaws.com"
: `https://q.${normalizedRegion}.amazonaws.com`;
const profRes = await fetch(`${runtimeHost}/`, {
method: "POST",
headers: {
"Content-Type": "application/x-amz-json-1.0",
Accept: "application/json",
"x-amz-target": "AmazonCodeWhispererService.ListAvailableProfiles",
Authorization: `Bearer ${data.accessToken}`
},
body: JSON.stringify({ maxResults: 10 }),
signal: AbortSignal.timeout(10000)
});
if (profRes.ok) {
const profData = await profRes.json();
const profiles = Array.isArray(profData?.profiles) ? profData.profiles : [];
const matched =
profiles.find(
(p: { arn?: string }) =>
typeof p?.arn === "string" && p.arn.includes(`:${normalizedRegion}:`)
) || profiles[0];
if (matched && typeof matched.arn === "string") profileArn = matched.arn;
} |
||
| } catch { | ||
| // Best-effort profile discovery — never block login on it. | ||
| } | ||
|
|
||
| return { | ||
| ok: true, | ||
| data: { | ||
|
|
@@ -111,6 +146,7 @@ export const kiro = { | |
| _clientId: extraData?._clientId, | ||
| _clientSecret: extraData?._clientSecret, | ||
| _region: tokenRegion, | ||
| _profileArn: profileArn, | ||
| }, | ||
| }; | ||
| } | ||
|
|
@@ -131,6 +167,7 @@ export const kiro = { | |
| clientId: tokens._clientId, | ||
| clientSecret: tokens._clientSecret, | ||
| region: tokens._region, | ||
| ...(tokens._profileArn ? { profileArn: tokens._profileArn } : {}), | ||
| }, | ||
| }), | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the Repository Style Guide (Rule 9), tests must be included when changing production code in
open-sse/orsrc/. Please add unit tests (e.g., intests/) to cover the newresolveKiroRegionandkiroRuntimeHostfunctions, verifying correct region resolution and host mapping.References