Skip to content

Commit

Permalink
fix: resolved issue with nested options clashing
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Aug 8, 2023
1 parent 86b7739 commit 772cead
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
48 changes: 23 additions & 25 deletions packages/aws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { SourceModule, KeyInfo, getEnv, prefix } from '@refreshly/core';
import * as assert from 'assert';

class AWSSourceModule extends SourceModule {
#options: AWSSourceModule.Options;
#accessKey?: AccessKey;
protected declare options: AWSSourceModule.Options;
private accessKey?: AccessKey;

constructor({ targets, key, secretKey, ...options }: AWSSourceModule.RawOptions) {
super({ targets });
constructor({ key, secretKey, ...options }: AWSSourceModule.Options) {
super();

this.#options = {
this.options = {
...options,
key: getEnv('key', key, 'AWS_ACCESS_KEY_ID'),
secretKey: getEnv('secretKey', secretKey, 'AWS_SECRET_ACCESS_KEY'),
Expand All @@ -20,11 +20,11 @@ class AWSSourceModule extends SourceModule {
return [
{
name: 'AWS_ACCESS_KEY_ID',
value: this.#options.key,
value: this.options.key,
},
{
name: 'AWS_SECRET_ACCESS_KEY',
value: this.#options.secretKey,
value: this.options.secretKey,
},
];
}
Expand All @@ -33,8 +33,8 @@ class AWSSourceModule extends SourceModule {
const client = new IAMClient({
region: 'us-east-1',
credentials: {
accessKeyId: this.#options.key,
secretAccessKey: this.#options.secretKey,
accessKeyId: this.options.key,
secretAccessKey: this.options.secretKey,
},
});

Expand All @@ -44,54 +44,54 @@ class AWSSourceModule extends SourceModule {
})
);

this.#accessKey = AccessKey;
this.accessKey = AccessKey;

return [
{
name: prefix(this.#options.prefix, 'AWS_ACCESS_KEY_ID'),
value: this.#accessKey.AccessKeyId,
name: prefix(this.options.prefix, 'AWS_ACCESS_KEY_ID'),
value: this.accessKey.AccessKeyId,
},
{
name: prefix(this.#options.prefix, 'AWS_SECRET_ACCESS_KEY'),
value: this.#accessKey.SecretAccessKey,
name: prefix(this.options.prefix, 'AWS_SECRET_ACCESS_KEY'),
value: this.accessKey.SecretAccessKey,
},
];
}

async revert(): Promise<void> {
if (!this.#accessKey) return;
if (!this.accessKey) return;

// Retain the old key and cleanup the new key
const client = new IAMClient({
region: 'us-east-1',
credentials: {
accessKeyId: this.#options.key,
secretAccessKey: this.#options.secretKey,
accessKeyId: this.options.key,
secretAccessKey: this.options.secretKey,
},
});

await client.send(
new DeleteAccessKeyCommand({
AccessKeyId: this.#accessKey.AccessKeyId,
AccessKeyId: this.accessKey.AccessKeyId,
})
);
}

async cleanup(): Promise<void> {
assert.ok(this.#accessKey);
assert.ok(this.accessKey);

// Retain the new key and cleanup the old key
const client = new IAMClient({
region: 'us-east-1',
credentials: {
accessKeyId: this.#accessKey.AccessKeyId,
secretAccessKey: this.#accessKey.SecretAccessKey,
accessKeyId: this.accessKey.AccessKeyId,
secretAccessKey: this.accessKey.SecretAccessKey,
},
});

await client.send(
new DeleteAccessKeyCommand({
AccessKeyId: this.#options.key,
AccessKeyId: this.options.key,
})
);
}
Expand All @@ -103,9 +103,7 @@ namespace AWSSourceModule {
secretKey?: string;
prefix?: string;
user: string;
};

export type RawOptions = Options & SourceModule.Options;
} & SourceModule.Options;
}

export const AWS = {
Expand Down
16 changes: 9 additions & 7 deletions packages/core/@types/source-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import { KeyInfo } from './key-info';
import { ITargetModule } from './target-module';

export abstract class SourceModule {
#options: SourceModule.Options;

constructor(options: SourceModule.Options) {
this.#options = options;
}
protected options: SourceModule.Options;

abstract get originalKeyInfos(): KeyInfo[];

async exec(): Promise<void> {
const keyInfos = await this.source();

if (this.options.targets.length === 0) {
console.error('Please provide a list of targets');

return await this.revert();
}

try {
await Promise.all(
this.#options.targets.map(async (target) => {
this.options.targets.map(async (target) => {
await target.target(keyInfos);
})
);
Expand All @@ -27,7 +29,7 @@ export abstract class SourceModule {
console.log(`Error detected, reverting to previous state...`);

await Promise.all(
this.#options.targets.map(async (target) => {
this.options.targets.map(async (target) => {
await target.revert(this.originalKeyInfos);
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { KeyInfo, Refreshly, SourceModule, getEnv, prefix } from '../index';

describe('@refreshly/core', () => {
describe('func(Refreshly)', () => {
it('should ...', async () => {
it('should execute all of the source modules', async () => {
class FakeModule extends SourceModule {
get originalKeyInfos(): KeyInfo[] {
return [];
Expand Down

0 comments on commit 772cead

Please sign in to comment.