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

feat(log-viewer-webui): Refactor S3Manager into a fastify plugin. #689

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 21 additions & 9 deletions components/log-viewer-webui/server/src/S3Manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fastifyPlugin from "fastify-plugin";

import {
GetObjectCommand,
S3Client,
Expand All @@ -14,17 +16,22 @@ const PRE_SIGNED_URL_EXPIRY_TIME_SECONDS = 3600;
* Class to manage Simple Storage Service (S3) objects.
*/
class S3Manager {
#s3Client;
#s3Client = null;

/**
* @param {string} region
* @param {object} props
* @param {string | null} props.region
*/
constructor (
region,
) {
this.#s3Client = new S3Client({
region: region,
});
constructor ({region}) {
if (null !== region) {
this.#s3Client = new S3Client({
region: region,
});
}
}

isEnabled () {
return null !== this.#s3Client;
}

/**
Expand All @@ -35,6 +42,9 @@ class S3Manager {
* @throws {Error} If a pre-signed URL couldn't be generated.
*/
async getPreSignedUrl (s3UriString) {
if (false === this.isEnabled()) {
throw new Error("pre-signed URL cannot be generated when S3Manager is not enabled");
}
const s3Uri = new URL(s3UriString);
const command = new GetObjectCommand({
Bucket: s3Uri.hostname,
Expand All @@ -55,4 +65,6 @@ class S3Manager {
}
}

export default S3Manager;
export default fastifyPlugin(async (app, options) => {
await app.decorate("s3Manager", new S3Manager(options));
});
2 changes: 2 additions & 0 deletions components/log-viewer-webui/server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DbManager from "./DbManager.js";
import exampleRoutes from "./routes/example.js";
import queryRoutes from "./routes/query.js";
import staticRoutes from "./routes/static.js";
import S3Manager from "./S3Manager.js";


/**
Expand Down Expand Up @@ -41,6 +42,7 @@ const app = async ({
port: settings.MongoDbPort,
},
});
await server.register(S3Manager, {region: settings.StreamFilesS3Region});
}

await server.register(staticRoutes);
Expand Down
25 changes: 10 additions & 15 deletions components/log-viewer-webui/server/src/routes/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ import {StatusCodes} from "http-status-codes";

import settings from "../../settings.json" with {type: "json"};
import {EXTRACT_JOB_TYPES} from "../DbManager.js";
import S3Manager from "../S3Manager.js";


const S3_MANAGER = (
null === settings.StreamFilesS3PathPrefix ||
null === settings.StreamFilesS3Region
) ?
null :
new S3Manager(settings.StreamFilesS3Region);


/**
* Submits a stream extraction job and returns the metadata of the extracted stream.
*
* @param {object} props
* @param {import("fastify").FastifyInstance | {dbManager: DbManager}} props.fastify
* @param {import("fastify").FastifyInstance |
* {dbManager: DbManager} |
* {s3Manager: S3Manager}} props.fastify
* @param {EXTRACT_JOB_TYPES} props.jobType
* @param {number} props.logEventIdx
* @param {string} props.streamId
Expand Down Expand Up @@ -63,7 +56,9 @@ const extractStreamAndGetMetadata = async ({
/**
* Creates query routes.
*
* @param {import("fastify").FastifyInstance | {dbManager: DbManager}} fastify
* @param {import("fastify").FastifyInstance |
* {dbManager: DbManager} |
* {s3Manager: S3Manager}} fastify
* @param {import("fastify").FastifyPluginOptions} options
* @return {Promise<void>}
*/
Expand Down Expand Up @@ -96,12 +91,12 @@ const routes = async (fastify, options) => {
});
}

if (null === S3_MANAGER) {
streamMetadata.path = `/streams/${streamMetadata.path}`;
} else {
streamMetadata.path = await S3_MANAGER.getPreSignedUrl(
if (fastify.s3Manager.isEnabled()) {
streamMetadata.path = await fastify.s3Manager.getPreSignedUrl(
`s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}`
);
} else {
streamMetadata.path = `/streams/${streamMetadata.path}`;
Comment on lines +94 to +99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure s3Manager is defined before use

Accessing fastify.s3Manager without checking if it is defined could lead to runtime errors, especially in environments where s3Manager is not registered (e.g., when NODE_ENV === "test"). Modify the condition to check for the existence of s3Manager before calling isEnabled().

Apply this diff to prevent potential null reference errors:

-if (fastify.s3Manager.isEnabled()) {
+if (fastify.s3Manager && fastify.s3Manager.isEnabled()) {
     streamMetadata.path = await fastify.s3Manager.getPreSignedUrl(
         `s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}`
     );
 } else {
     streamMetadata.path = `/streams/${streamMetadata.path}`;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (fastify.s3Manager.isEnabled()) {
streamMetadata.path = await fastify.s3Manager.getPreSignedUrl(
`s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}`
);
} else {
streamMetadata.path = `/streams/${streamMetadata.path}`;
if (fastify.s3Manager && fastify.s3Manager.isEnabled()) {
streamMetadata.path = await fastify.s3Manager.getPreSignedUrl(
`s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}`
);
} else {
streamMetadata.path = `/streams/${streamMetadata.path}`;

}

return streamMetadata;
Expand Down
Loading