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

Parameterize region and improve error handling #332

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
119 changes: 56 additions & 63 deletions src/main/server/adapter/ce/pna.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ async function pnaEndpoint() {
return "Unable to find framework: " + query["id"];
}

if (this.params.methodType == "DELETE") {
// Remove index file from Competency Explorer registry
const result = await removeFromAws(query["id"]);
return JSON.stringify(result);
}

let match = framework["@id"].match('(.*)(\/data\/)(.*)');
let ceasnEndpointFramework = match && match.length > 1 ? match[1] + "/ceasn/" : undefined;

Expand Down Expand Up @@ -97,14 +91,50 @@ async function pnaEndpoint() {

async function uploadToAws(data, name) {

const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");

const AWS_REGION = process.env.AWS_REGION ? process.env.AWS_REGION : "";
const AWS_BUCKET = process.env.AWS_BUCKET ? process.env.AWS_BUCKET : "";
const AWS_REGION = process.env.AWS_REGION ? process.env.AWS_REGION : "us-east-2";
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID ? process.env.AWS_ACCESS_KEY_ID : "";
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY ? process.env.AWS_SECRET_ACCESS_KEY : "";
let success = false;

try {
if (!name) {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", 'No framework provided');
return {
framework: undefined,
success: false
};
}
if (!AWS_REGION) {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", 'No AWS Region provided');
return {
framework: name,
success: false
};
}
if (!AWS_BUCKET) {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", 'No AWS Bucket provided');
return {
framework: name,
success: false
};
}
if (!AWS_ACCESS_KEY_ID) {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", 'No AWS Access key provided');
return {
framework: name,
success: false
};
}
if (!AWS_SECRET_ACCESS_KEY) {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", 'No AWS Access secret provided');
return {
framework: name,
success: false
};
}
const s3 = new S3Client({
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID,
Expand All @@ -113,66 +143,29 @@ async function uploadToAws(data, name) {
region: AWS_REGION,
});

await s3.send(
new PutObjectCommand({
Bucket: AWS_BUCKET,
Key: name,
Body: data,
ContentType: "application/json"
})
);

const { Body } = await s3.send(
new GetObjectCommand({
Bucket: AWS_BUCKET,
Key: name
})
);

const receipt = await Body.transformToString();

return receipt;

} catch (err) {
return {
framework: name,
error: err
};
}
}

async function removeFromAws(name) {

const { S3Client, DeleteObjectCommand } = require("@aws-sdk/client-s3");

const AWS_BUCKET = process.env.AWS_BUCKET ? process.env.AWS_BUCKET : "";
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID ? process.env.AWS_ACCESS_KEY_ID : "";
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY ? process.env.AWS_SECRET_ACCESS_KEY : "";

try {
const s3 = new S3Client({
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY
},
region: "us-east-2",
const put = new PutObjectCommand({
Bucket: AWS_BUCKET,
Key: name,
Body: data
})
await s3.send(put).then(() => {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.INFO, "pnaUploadOk", name);
success = true;
}).catch((err) => {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", name);
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", err);
success = false;
});

await s3.send(
new DeleteObjectCommand({
Bucket: AWS_BUCKET,
Key: name
})
);

return {
framework: name
};

framework: name,
success: success
};
} catch (err) {
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", name);
global.auditLogger.report(global.auditLogger.LogCategory.ADAPTER, global.auditLogger.Severity.ERROR, "pnaUploadErr", err);
return {
framework: name,
error: err
success: false
};
}
}
Expand Down
Loading