Skip to content

Commit

Permalink
♻️ Improve function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Jul 2, 2024
1 parent 3f6bf45 commit 883f059
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/utils/AwskmsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ interface AwsS3StorageConfig {
const { AWS_S3_STORAGE_CONFIG } = process.env;
const storageConfig: AwsS3StorageConfig = AWS_S3_STORAGE_CONFIG ? JSON.parse(AWS_S3_STORAGE_CONFIG) : undefined;

async function downloadEncryptedKey(config: KeyConfig): Promise<Uint8Array | undefined> {
const command = new GetObjectCommand({
Bucket: storageConfig.bucket,
Key: storageConfig.key,
});
const client = new S3Client({
region: config.region,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
});
try {
const response = await client.send(command);
// The Body object also has 'transformToByteArray' and 'transformToWebStream' methods.
return response.Body?.transformToByteArray();
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
}

export function getAwskmsConfig(keys: string[]): KeyConfig[] {
let configOverride: AwskmsConfig = {};
if (process.env.AWSKMS_CONFIG) {
Expand All @@ -48,28 +70,6 @@ export function getAwskmsConfig(keys: string[]): KeyConfig[] {
return keyConfigs;
}

export const downloadEncryptedKey = async (config: KeyConfig): Promise<Uint8Array | undefined> => {
const command = new GetObjectCommand({
Bucket: storageConfig.bucket,
Key: storageConfig.key,
});
const client = new S3Client({
region: config.region,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
});
try {
const response = await client.send(command);
// The Body object also has 'transformToByteArray' and 'transformToWebStream' methods.
return response.Body?.transformToByteArray();
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
};

export async function retrieveAwskmsKeys(awskmsConfigs: KeyConfig[]): Promise<string[]> {
return await Promise.all(
awskmsConfigs.map(async (config) => {
Expand Down

0 comments on commit 883f059

Please sign in to comment.