Skip to content
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

Outside m25 failed deliveries processor generate json #2054

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion handlers/failed-national-deliveries-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": "esbuild --bundle --platform=node --target=node18 --outfile=dist/index.js src/index.ts"
},
"devDependencies": {
"esbuild": "^0.19.2"
"esbuild": "^0.19.2",
"aws-sdk": "^2.1450.0"
},
"dependencies": {
"csv-parse": "^5.4.1",
Expand Down
32 changes: 32 additions & 0 deletions handlers/failed-national-deliveries-processor/src/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { STS } from "aws-sdk";
import {
CredentialProviderChain,
Credentials,
EnvironmentCredentials,
SharedIniFileCredentials,
} from 'aws-sdk/lib/core';

export function credentialProvider(profile: string): CredentialProviderChain {
return new CredentialProviderChain([
(): Credentials => new SharedIniFileCredentials({profile}),
(): Credentials => new EnvironmentCredentials('AWS'),
]);
}

export function assumeRole(roleToAssume: string): Promise<Credentials> {
const sts = new STS({apiVersion: '2011-06-15'});
return sts.assumeRole({
RoleArn: roleToAssume,
RoleSessionName: 'tracker',
}).promise().then((data) => {
if (data.Credentials) {
return new Credentials({
accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey,
sessionToken: data.Credentials.SessionToken
})
} else {
return Promise.reject("Could not get credentials from AWS")
}
})
}
25 changes: 19 additions & 6 deletions handlers/failed-national-deliveries-processor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import {Request, FileRow} from "../src/types.ts";
import {S3Ops} from "../src/s3.ts";
import {getFileContents, getFailedDeliveryRowsFromFileContents} from "./FileIO.js";
import {generateRequestsFromFailedDeliveryRows} from "./RequestBuilder.js";
// import { S3Ops } from 'common/aws/s3';


export const main = async (): Promise<string> => {

const allFileRows = await getFileContents();
const failedDeliveryRows : FileRow[] = getFailedDeliveryRowsFromFileContents(allFileRows);
// const s3Client = new S3Ops();

const s3Client = new S3Ops('AWS_REGION');
console.log('s3Client:',s3Client);
// await s3Client.putObject(
// config.bucketName,
// 'snyk/allrepos.json',
// allReposAndSnykProjects,
// );

// const allFileRows = await getFileContents();
// const failedDeliveryRows : FileRow[] = getFailedDeliveryRowsFromFileContents(allFileRows);

const requests : Request[] = generateRequestsFromFailedDeliveryRows(failedDeliveryRows);
// const requests : Request[] = generateRequestsFromFailedDeliveryRows(failedDeliveryRows);

requests.forEach(
request => console.log('request: '+ JSON.stringify(request))
);
// requests.forEach(
// request => console.log('request: '+ JSON.stringify(request))
// );

return Promise.resolve('');
};
Expand Down
61 changes: 61 additions & 0 deletions handlers/failed-national-deliveries-processor/src/s3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { S3 } from 'aws-sdk';
import type { GetObjectRequest, PutObjectRequest } from 'aws-sdk/clients/s3';
import { credentialProvider } from 'common/aws/credentials';

export class S3Ops {
private readonly s3: S3;

constructor(region: string) {
// this.s3 = new S3({ credentialProvider: credentialProvider('deployTools'), region });
console.log('HELLO!');
}



async greeting(){
console.log('HELLO!');
}

async getObject(params: GetObjectRequest): Promise<string> {
const response = await this.s3.getObject(params).promise();
const body = response.Body?.toString();

if (!body) {
throw new Error(`s3://${params.Bucket}/${params.Key} is empty`);
}

return body;
}

async putObject(
bucketName: string,
key: string,
body: unknown,
): Promise<void> {
if (!shouldOutput) {
console.info(
`Skipping output. Would have put the following data into the following s3 bucket: ${key}`,
);
console.info(JSON.stringify(body, null, 2));
return Promise.resolve();
}

try {
const params: PutObjectRequest = {
Bucket: bucketName,
Key: key,
Body: JSON.stringify(body),
ContentType: 'application/json; charset=utf-8',
ACL: 'private',
};

await this.s3.upload(params).promise();
} catch (e) {
if (e instanceof Error) {
console.error(`Error uploading item to s3: ${e.message}`);
} else {
console.error(e);
}
}
}
}