Skip to content

Commit

Permalink
Merge pull request #1511 from RoadieHQ/21550-allow-specifying-runtime…
Browse files Browse the repository at this point in the history
…-region-for-aws

Allow specifying runtime region for AWS resources
  • Loading branch information
Xantier authored Jul 29, 2024
2 parents 76926a1 + a011dbd commit fd5e557
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-geckos-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/catalog-backend-module-aws': minor
---

Add the possibility to define region on runtime when running the provider
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class AWSDynamoDbTableProvider extends AWSEntityProvider {
}
const groups = await this.getGroups();

const defaultAnnotations = await this.buildDefaultAnnotations();
const defaultAnnotations = await this.buildDefaultAnnotations(this.region);
const ddb = await this.getDdb();
this.logger.info(
`Retrieving all DynamoDB tables for account ${this.accountId}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ export class AWSEC2Provider extends AWSEntityProvider {
return `aws-ec2-provider-${this.accountId}-${this.providerId ?? 0}`;
}

private async getEc2() {
private async getEc2(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new EC2({ credentials, region: this.region })
? new EC2({ credentials, region: discoveryRegion })
: new EC2(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
const discoveryRegion = region ?? this.region;
if (!this.connection) {
throw new Error('Not initialized');
}
Expand All @@ -77,9 +78,9 @@ export class AWSEC2Provider extends AWSEntityProvider {
this.logger.info(`Providing ec2 resources from aws: ${this.accountId}`);
const ec2Resources: ResourceEntity[] = [];

const ec2 = await this.getEc2();
const ec2 = await this.getEc2(discoveryRegion);

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const instances = await ec2.describeInstances({
Filters: [{ Name: 'instance-state-name', Values: ['running'] }],
Expand All @@ -89,7 +90,7 @@ export class AWSEC2Provider extends AWSEntityProvider {
if (reservation.Instances) {
for (const instance of reservation.Instances) {
const instanceId = instance.InstanceId;
const arn = `arn:aws:ec2:${this.region}:${this.accountId}:instance/${instanceId}`;
const arn = `arn:aws:ec2:${discoveryRegion}:${this.accountId}:instance/${instanceId}`;
const consoleLink = new ARN(arn).consoleLink;
const resource: ResourceEntity = {
kind: 'Resource',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,30 @@ export class AWSEKSClusterProvider extends AWSEntityProvider {
return `aws-eks-cluster-${this.accountId}-${this.providerId ?? 0}`;
}

private async getEks() {
private async getEks(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new EKS({ credentials, region: this.region })
? new EKS({ credentials, region: discoveryRegion })
: new EKS(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
}
const discoveryRegion = region ?? this.region;
const groups = await this.getGroups();

this.logger.info(
`Providing eks cluster resources from aws: ${this.accountId}`,
);
const eksResources: ResourceEntity[] = [];

const eks = await this.getEks();
const eks = await this.getEks(discoveryRegion);

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const paginatorConfig = {
client: eks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export abstract class AWSEntityProvider implements EntityProvider {
protected readonly labelValueMapper: LabelValueMapper | undefined;

public abstract getProviderName(): string;
public abstract run(region?: string): Promise<void>;

protected constructor(
account: AccountConfig,
Expand Down Expand Up @@ -126,12 +127,12 @@ export abstract class AWSEntityProvider implements EntityProvider {
this.connection = connection;
}

protected async buildDefaultAnnotations() {
protected async buildDefaultAnnotations(region: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
const sts = this.useTemporaryCredentials
? new STS({ credentials: credentials, region: this.region })
? new STS({ credentials: credentials, region: region })
: new STS(credentials);

const account = await sts.getCallerIdentity({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,30 @@ export class AWSIAMRoleProvider extends AWSEntityProvider {
return `aws-iam-role-${this.accountId}-${this.providerId ?? 0}`;
}

private async getIam() {
private async getIam(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new IAM({ credentials, region: this.region })
? new IAM({ credentials, region: discoveryRegion })
: new IAM(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
}
const discoveryRegion = region ?? this.region;
const groups = await this.getGroups();

this.logger.info(
`Providing iam role resources from aws: ${this.accountId}`,
);
const roleResources: ResourceEntity[] = [];

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const iam = await this.getIam();
const iam = await this.getIam(discoveryRegion);

const paginatorConfig = {
client: iam,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ export class AWSIAMUserProvider extends AWSEntityProvider {
return `aws-iam-user-${this.accountId}-${this.providerId ?? 0}`;
}

private async getIam() {
private async getIam(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new IAM({ credentials, region: this.region })
? new IAM({ credentials, region: discoveryRegion })
: new IAM(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
}

const discoveryRegion = region ?? this.region;
this.logger.info(
`Providing iam user resources from aws: ${this.accountId}`,
);
const userResources: UserEntity[] = [];

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const iam = await this.getIam();
const iam = await this.getIam(discoveryRegion);

const paginatorConfig = {
client: iam,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ export class AWSLambdaFunctionProvider extends AWSEntityProvider {
return `aws-lambda-function-${this.accountId}-${this.providerId ?? 0}`;
}

private async getLambda() {
private async getLambda(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new Lambda({ credentials, region: this.region })
? new Lambda({ credentials, region: discoveryRegion })
: new Lambda(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
}
const discoveryRegion = region ?? this.region;
const groups = await this.getGroups();

this.logger.info(
Expand All @@ -84,9 +85,9 @@ export class AWSLambdaFunctionProvider extends AWSEntityProvider {

const lambdaComponents: ResourceEntity[] = [];

const lambda = await this.getLambda();
const lambda = await this.getLambda(discoveryRegion);

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const paginatorConfig = {
client: lambda,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class AWSOrganizationAccountsProvider extends AWSEntityProvider {

const organizationsClient = await this.getOrganizationsClient();

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(this.region);

const paginatorConfig = {
client: organizationsClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,27 @@ export class AWSRDSProvider extends AWSEntityProvider {
return `aws-rds-provider-${this.accountId}-${this.providerId ?? 0}`;
}

private async getRdsClient() {
private async getRdsClient(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new RDS({ credentials, region: this.region })
? new RDS({ credentials, region: discoveryRegion })
: new RDS(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
}

const discoveryRegion = region ?? this.region;
const groups = await this.getGroups();
this.logger.info(`Providing RDS resources from aws: ${this.accountId}`);
const rdsResources: ResourceEntity[] = [];

const rdsClient = await this.getRdsClient();
const rdsClient = await this.getRdsClient(discoveryRegion);

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const paginatorConfig = {
client: rdsClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,30 @@ export class AWSS3BucketProvider extends AWSEntityProvider {
return `aws-s3-bucket-${this.accountId}-${this.providerId ?? 0}`;
}

private async getS3() {
private async getS3(discoveryRegion: string) {
const credentials = this.useTemporaryCredentials
? this.getCredentials()
: await this.getCredentialsProvider();
return this.useTemporaryCredentials
? new S3({ credentials, region: this.region })
? new S3({ credentials, region: discoveryRegion })
: new S3(credentials);
}

async run(): Promise<void> {
async run(region?: string): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
}
const discoveryRegion = region ?? this.region;
const groups = await this.getGroups();

this.logger.info(
`Providing s3 bucket resources from aws: ${this.accountId}`,
);
const s3Resources: ResourceEntity[] = [];

const s3 = await this.getS3();
const s3 = await this.getS3(discoveryRegion);

const defaultAnnotations = this.buildDefaultAnnotations();
const defaultAnnotations = this.buildDefaultAnnotations(discoveryRegion);

const buckets = await s3.listBuckets({});

Expand Down

0 comments on commit fd5e557

Please sign in to comment.