-
Notifications
You must be signed in to change notification settings - Fork 18
feat: AWS VCP Scanner #275
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89f71cb
feat: AWS VCP Scanner
zacharyblasczyk 96c48a7
fix aws link, name, and awsDialog save
zacharyblasczyk 9296830
format fix
zacharyblasczyk 54723e7
refactor name
zacharyblasczyk c6ddf0f
Update apps/webservice/src/app/[workspaceSlug]/(app)/(resources)/reso…
zacharyblasczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| import type { Vpc } from "@aws-sdk/client-ec2"; | ||
| import type { STSClient } from "@aws-sdk/client-sts"; | ||
| import type { ResourceProviderAws, Workspace } from "@ctrlplane/db/schema"; | ||
| import type { CloudVPCV1 } from "@ctrlplane/validators/resources"; | ||
| import { | ||
| DescribeRegionsCommand, | ||
| DescribeSubnetsCommand, | ||
| DescribeVpcsCommand, | ||
| } from "@aws-sdk/client-ec2"; | ||
| import _ from "lodash"; | ||
| import { isPresent } from "ts-is-present"; | ||
|
|
||
| import { logger } from "@ctrlplane/logger"; | ||
| import { ReservedMetadataKey } from "@ctrlplane/validators/conditions"; | ||
|
|
||
| import type { AwsCredentials } from "./aws.js"; | ||
| import { omitNullUndefined } from "../../utils.js"; | ||
| import { assumeRole, assumeWorkspaceRole } from "./aws.js"; | ||
|
|
||
| const log = logger.child({ label: "resource-scan/aws/vpc" }); | ||
|
|
||
| const convertVpcToCloudResource = ( | ||
| accountId: string, | ||
| region: string, | ||
| vpc: Vpc, | ||
| subnets: { | ||
| name: string; | ||
| region: string; | ||
| cidr: string; | ||
| type: "public" | "private"; | ||
| availabilityZone?: string; | ||
| }[] = [], | ||
| ): CloudVPCV1 => { | ||
| const partition = region.startsWith("us-gov-") ? "aws-us-gov" : "aws"; | ||
| const appUrl = `https://${ | ||
| partition === "aws-us-gov" | ||
| ? `console.${region}.${partition}` | ||
| : "console.aws.amazon" | ||
| }.com/vpcconsole/home?region=${region}#vpcs:search=${vpc.VpcId}`; | ||
|
|
||
| const name = vpc.Tags?.find((tag) => tag.Key === "Name")?.Value ?? vpc.VpcId!; | ||
|
|
||
| return { | ||
| name, | ||
| identifier: `aws/${accountId}/vpc/${vpc.VpcId}`, | ||
| version: "cloud/v1", | ||
| kind: "VPC", | ||
| config: { | ||
| name, | ||
| id: vpc.VpcId!, | ||
| provider: "aws", | ||
| region, | ||
| accountId: accountId, | ||
| cidr: vpc.CidrBlock, | ||
| subnets, | ||
| secondaryCidrs: vpc.CidrBlockAssociationSet?.filter( | ||
| (assoc) => assoc.CidrBlock !== vpc.CidrBlock, | ||
| ).map((assoc) => ({ | ||
| cidr: assoc.CidrBlock ?? "", | ||
| state: assoc.CidrBlockState?.State?.toLowerCase() ?? "", | ||
| })), | ||
| }, | ||
| metadata: omitNullUndefined({ | ||
| [ReservedMetadataKey.ExternalId]: vpc.VpcId, | ||
| [ReservedMetadataKey.Links]: JSON.stringify({ "AWS Console": appUrl }), | ||
| "aws/region": region, | ||
| "aws/state": vpc.State, | ||
| "aws/is-default": vpc.IsDefault, | ||
| "aws/dhcp-options-id": vpc.DhcpOptionsId, | ||
| "aws/instance-tenancy": vpc.InstanceTenancy, | ||
| ...(vpc.Tags?.reduce( | ||
| (acc, tag) => ({ | ||
| ...acc, | ||
| [`aws/tag/${tag.Key}`]: tag.Value, | ||
| }), | ||
| {}, | ||
| ) ?? {}), | ||
| }), | ||
| }; | ||
| }; | ||
|
|
||
| const getAwsRegions = async (credentials: AwsCredentials) => | ||
| credentials | ||
| .ec2() | ||
| .send(new DescribeRegionsCommand({})) | ||
| .then(({ Regions = [] }) => Regions.map((region) => region.RegionName)); | ||
|
|
||
| const getVpcs = async (client: AwsCredentials, region: string) => { | ||
| const ec2Client = client.ec2(region); | ||
| const { Vpcs = [] } = await ec2Client.send(new DescribeVpcsCommand({})); | ||
| return Vpcs; | ||
| }; | ||
|
|
||
| const getSubnets = async ( | ||
| client: AwsCredentials, | ||
| region: string, | ||
| vpcId: string, | ||
| ): Promise< | ||
| { | ||
| name: string; | ||
| region: string; | ||
| cidr: string; | ||
| type: "public" | "private"; | ||
| availabilityZone: string; | ||
| }[] | ||
| > => { | ||
| const ec2Client = client.ec2(region); | ||
| const { Subnets = [] } = await ec2Client.send( | ||
| new DescribeSubnetsCommand({ | ||
| Filters: [{ Name: "vpc-id", Values: [vpcId] }], | ||
| }), | ||
| ); | ||
| return Subnets.map((subnet) => ({ | ||
| name: subnet.SubnetId ?? "", | ||
| region, | ||
| cidr: subnet.CidrBlock ?? "", | ||
| type: subnet.MapPublicIpOnLaunch ? "public" : "private", | ||
| availabilityZone: subnet.AvailabilityZone ?? "", | ||
| })); | ||
| }; | ||
|
|
||
| const createVpcScannerForRegion = ( | ||
| client: AwsCredentials, | ||
| customerRoleArn: string, | ||
| ) => { | ||
| const accountId = /arn:aws:iam::(\d+):/.exec(customerRoleArn)?.[1]; | ||
| if (accountId == null) throw new Error("Missing account ID"); | ||
|
|
||
| return async (region: string) => { | ||
| const vpcs = await getVpcs(client, region); | ||
| log.info( | ||
| `Found ${vpcs.length} VPCs for ${customerRoleArn} in region ${region}`, | ||
| ); | ||
|
|
||
| const vpcResources = await Promise.all( | ||
| vpcs.map(async (vpc) => { | ||
| if (!vpc.VpcId) return null; | ||
| const subnets = await getSubnets(client, region, vpc.VpcId); | ||
| return convertVpcToCloudResource(accountId, region, vpc, subnets); | ||
| }), | ||
| ); | ||
|
|
||
| return vpcResources.filter(isPresent); | ||
| }; | ||
| }; | ||
|
|
||
| const scanVpcsByAssumedRole = async ( | ||
| workspaceClient: STSClient, | ||
| customerRoleArn: string, | ||
| ) => { | ||
| const client = await assumeRole(workspaceClient, customerRoleArn); | ||
| const regions = await getAwsRegions(client); | ||
|
|
||
| log.info( | ||
| `Scanning ${regions.length} AWS regions for VPCs in account ${customerRoleArn}`, | ||
| ); | ||
|
|
||
| const regionalVpcScanner = createVpcScannerForRegion(client, customerRoleArn); | ||
|
|
||
| return _.chain(regions) | ||
| .filter(isPresent) | ||
| .map(regionalVpcScanner) | ||
| .thru((promises) => Promise.all(promises)) | ||
| .value() | ||
| .then((results) => results.flat()); | ||
| }; | ||
|
|
||
| export const getVpcResources = async ( | ||
| workspace: Workspace, | ||
| config: ResourceProviderAws, | ||
| ) => { | ||
| const { awsRoleArn: workspaceRoleArn } = workspace; | ||
| if (workspaceRoleArn == null) return []; | ||
|
|
||
| log.info( | ||
| `Scanning for VPCs with assumed role arns ${config.awsRoleArns.join(", ")} using role ${workspaceRoleArn}`, | ||
| { | ||
| workspaceId: workspace.id, | ||
| config, | ||
| workspaceRoleArn, | ||
| }, | ||
| ); | ||
|
|
||
| const credentials = await assumeWorkspaceRole(workspaceRoleArn); | ||
| const workspaceStsClient = credentials.sts(); | ||
|
|
||
| const resources = config.importVpc | ||
| ? await _.chain(config.awsRoleArns) | ||
| .map((customerRoleArn) => | ||
| scanVpcsByAssumedRole(workspaceStsClient, customerRoleArn), | ||
| ) | ||
| .thru((promises) => Promise.all(promises)) | ||
| .value() | ||
| .then((results) => results.flat()) | ||
| .then((resources) => | ||
| resources.map((resource) => ({ | ||
| ...resource, | ||
| workspaceId: workspace.id, | ||
| providerId: config.resourceProviderId, | ||
| })), | ||
| ) | ||
| : []; | ||
|
|
||
| log.info(`Found ${resources.length} VPC resources`); | ||
|
|
||
| return resources; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.