All files / aws/src source.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 7/7
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                  8x   8x               1x       3x                         2x         2x       2x   2x                         2x   1x             1x                            
import { SourceModule, KeyInfo, getEnv, PartiallyRequired } from '@refreshly/core';
import { AccessKey, createAwsAccessKey, deleteAwsAccessKey } from './aws/access-keys';
 
export class AWSSourceModule extends SourceModule {
  private options: PartiallyRequired<Omit<AWSSourceModule.Options, keyof SourceModule.Options>, 'key' | 'secretKey'>;
 
  private accessKey?: AccessKey;
 
  constructor({ targets, prefix, key, secretKey, ...options }: AWSSourceModule.Options) {
    super({ targets, prefix });
 
    this.options = {
      ...options,
      key: getEnv('key', key, 'AWS_ACCESS_KEY_ID'),
      secretKey: getEnv('secretKey', secretKey, 'AWS_SECRET_ACCESS_KEY'),
    };
  }
 
  get name(): string {
    return 'aws';
  }
 
  get originalKeyInfos(): KeyInfo[] {
    return [
      {
        name: 'AWS_ACCESS_KEY_ID',
        value: this.options.key,
      },
      {
        name: 'AWS_SECRET_ACCESS_KEY',
        value: this.options.secretKey,
      },
    ];
  }
 
  async source(): Promise<KeyInfo[]> {
    const accessKey = await createAwsAccessKey({
      key: this.options.key,
      secretKey: this.options.secretKey,
    });
 
    this.accessKey = accessKey;
 
    // Why the hell do I have to arbitrarily wait?
    // What is propagating on Amazon's backend that results in the token not being immediately usable?
    await new Promise((resolve) => setTimeout(resolve, 7000));
 
    return [
      {
        name: 'AWS_ACCESS_KEY_ID',
        value: accessKey.key,
      },
      {
        name: 'AWS_SECRET_ACCESS_KEY',
        value: accessKey.secretKey,
      },
    ];
  }
 
  async revert(): Promise<void> {
    if (!this.accessKey) return;
 
    await deleteAwsAccessKey({
      key: this.accessKey.key,
      secretKey: this.accessKey.secretKey,
    });
  }
 
  async cleanup(): Promise<void> {
    await deleteAwsAccessKey({
      key: this.options.key,
      secretKey: this.options.secretKey,
    });
  }
}
 
export namespace AWSSourceModule {
  export type Options = {
    key?: string;
    secretKey?: string;
    prefix?: string;
  } & SourceModule.Options;
}