Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

extract region only

Maidul Islam0a28ac4a7d4f
diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts
index 4622bc5093..f430b41495 100644
--- a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts
+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts
@@ -42,6 +42,31 @@ type TIdentityAwsAuthServiceFactoryDep = {
export type TIdentityAwsAuthServiceFactory = ReturnType<typeof identityAwsAuthServiceFactory>;
+const awsRegionFromHeader = (authorizationHeader: string): string | null => {
+ // https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html
+ // The Authorization header takes the following form.
+ // Authorization: AWS4-HMAC-SHA256
+ // Credential=AKIAIOSFODNN7EXAMPLE/20230719/us-east-1/sts/aws4_request,
+ // SignedHeaders=content-length;content-type;host;x-amz-date,
+ // Signature=fe5f80f77d5fa3beca038a248ff027d0445342fe2855ddc963176630326f1024
+ //
+ // The credential is in the form of "<your-access-key-id>/<date>/<aws-region>/<aws-service>/aws4_request"
+ try {
+ const fields = authorizationHeader.split(" ");
+ for (const field of fields) {
+ if (field.startsWith("Credential=")) {
+ const parts = field.split("/");
+ if (parts.length >= 3) {
+ return parts[2];
+ }
+ }
+ }
+ } catch {
+ return null;
+ }
+ return null;
+};
+
export const identityAwsAuthServiceFactory = ({
identityAccessTokenDAL,
identityAwsAuthDAL,
@@ -58,7 +83,10 @@ export const identityAwsAuthServiceFactory = ({
const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityAwsAuth.identityId });
const headers: TAwsGetCallerIdentityHeaders = JSON.parse(Buffer.from(iamRequestHeaders, "base64").toString());
- const body: string = Buffer.from(iamRequestBody, "base64").toString();
+ const body: string = Buffer.from(iamRequestBody, "base64").toString();
+
+ const region = headers.Authorization ? awsRegionFromHeader(headers.Authorization) : null;
+ const url = region ? `https://sts.${region}.amazonaws.com` : identityAwsAuth.stsEndpoint;
const {
data: {
@@ -68,7 +96,7 @@ export const identityAwsAuthServiceFactory = ({
}
}: { data: TGetCallerIdentityResponse } = await axios({
method: iamHttpRequestMethod,
- url: headers?.Host ? `https://${headers.Host}` : identityAwsAuth.stsEndpoint,
+ url,
headers,
data: body
});