Skip to content

Commit

Permalink
simplify the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyblasczyk committed Dec 3, 2024
1 parent 257e502 commit 428480b
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions packages/api/src/router/workspace-integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import { Permission } from "@ctrlplane/validators/auth";

import { createTRPCRouter, protectedProcedure } from "../trpc";

const iamClient = new IAMClient({
region: "us-east-1",
credentials: defaultProvider(),
});

const stsClient = new STSClient({
region: "us-east-1",
credentials: defaultProvider(),
});

export const integrationsRouter = createTRPCRouter({
google: createTRPCRouter({
createServiceAccount: protectedProcedure
Expand Down Expand Up @@ -167,16 +177,6 @@ export const integrationsRouter = createTRPCRouter({
message: "AWS Role Arn already defined.",
});

const iamClient = new IAMClient({
region: "us-east-1",
credentials: defaultProvider(),
});

const stsClient = new STSClient({
region: "us-east-1",
credentials: defaultProvider(),
});

const { Arn: currentArn, Account: accountId } = await stsClient.send(
new GetCallerIdentityCommand({}),
);
Expand All @@ -190,33 +190,40 @@ export const integrationsRouter = createTRPCRouter({

const isSSORole = currentArn.includes("AWSReservedSSO");

const sanitizedRoleArn = isSSORole
? `arn:aws:iam::${accountId}:role/aws-reserved/sso.amazonaws.com/*/${currentArn.split("/")[1]}`
: `arn:aws:iam::${accountId}:role/${currentArn.split("/")[1]}`;

const roleName = `ctrlplane-${ws.slug}`;

const assumeRolePolicyDocument = {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: {
AWS: isSSORole
? `arn:aws:iam::${accountId}:root`
: sanitizedRoleArn,
},
Action: "sts:AssumeRole",
...(isSSORole && {
Condition: {
ArnLike: {
"aws:PrincipalArn": [sanitizedRoleArn],
const assumeRolePolicyDocument = isSSORole
? {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: {
AWS: `arn:aws:iam::${accountId}:root`,
},
Action: "sts:AssumeRole",
Condition: {
ArnLike: {
"aws:PrincipalArn": [
`arn:aws:iam::${accountId}:role/aws-reserved/sso.amazonaws.com/*/${currentArn.split("/")[1]}`,
],
},
},
},
}),
},
],
};
],
}
: {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: {
AWS: `arn:aws:iam::${accountId}:role/${currentArn.split("/")[1]}`,
},
Action: "sts:AssumeRole",
},
],
};

const createRoleResponse = await iamClient.send(
new CreateRoleCommand({
Expand Down Expand Up @@ -297,11 +304,6 @@ export const integrationsRouter = createTRPCRouter({
message: "AWS Role does not exist.",
});

const iamClient = new IAMClient({
region: "us-east-1",
credentials: defaultProvider(),
});

const roleName = `ctrlplane-${ws.slug}`;

await iamClient.send(
Expand Down

0 comments on commit 428480b

Please sign in to comment.