Skip to content

Commit

Permalink
Delay setting AWS credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Jun 17, 2023
1 parent cf48e3a commit 2a47cb3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
5 changes: 5 additions & 0 deletions app/handlers/handle-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
v4 as uuidv4,
} from "uuid";

import {
initializeAwsIfNeeded,
} from "../lib/aws.js";
import {
BLITLINE_APP_ID,
S3_BUCKET,
Expand Down Expand Up @@ -68,6 +71,8 @@ const getBeforeKey = (generatedId, extension) => `before/${shortDateString()}/${
const getAfterKey = (beforeKey) => beforeKey.replace(/^before\//, "after/");

const getSignedS3Url = async (key, imageContentType) => {
initializeAwsIfNeeded();

// const clientFilename = (query.filename || "");
// TODO: save original client file name.
// const metadata = {
Expand Down
25 changes: 25 additions & 0 deletions app/lib/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import aws from "aws-sdk";

import {
AWS_ACCESS_KEY_ID,
AWS_REGION,
AWS_SECRET_ACCESS_KEY,
} from "./configuration.js";

const initializeAws = () => {
aws.config.update({
accessKeyId: AWS_ACCESS_KEY_ID,
region: AWS_REGION,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
signatureVersion: "v4",
});
};

const hasInitialized = false;

export const initializeAwsIfNeeded = () => {
// TODO: dynamically determine if the provided access and secret keys are valid.
if (!hasInitialized) {
initializeAws();
}
};
18 changes: 0 additions & 18 deletions app/web.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import aws from "aws-sdk";
import express from "express";
import helmet from "helmet";
import morgan from "morgan";
import st from "st";

import handleUpload from "./handlers/handle-upload.js";
import {
AWS_ACCESS_KEY,
AWS_REGION,
AWS_SECRET_KEY,
http as httpConfiguration,
https as httpsConfiguration,
S3_BUCKET,
Expand All @@ -23,17 +19,6 @@ import {
resolvePathFromProjectRoot,
} from "./lib/resolve-path.js";

const initializeAws = () => {
aws.config.update({
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_KEY,
});
aws.config.update({
region: AWS_REGION,
signatureVersion: "v4",
});
};

const createExpressApp = (siteRootPath) => {
const app = express();

Expand All @@ -49,7 +34,6 @@ const createExpressApp = (siteRootPath) => {
maxAge: 15_724_800_000,
}));
app.use(helmet.contentSecurityPolicy({

directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
"connect-src": [
Expand Down Expand Up @@ -78,8 +62,6 @@ const createExpressApp = (siteRootPath) => {
};

const startWebServer = () => {
initializeAws();

// Path to static resources like index.html, css etcetera
const siteRootPath = resolvePathFromProjectRoot(...siteRootRelativePath.split("/"));

Expand Down

0 comments on commit 2a47cb3

Please sign in to comment.