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: request grant confirmation for config updates #39

Draft
wants to merge 4 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
15 changes: 14 additions & 1 deletion backend/src/controllers/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getS3AndParams,
streamToString
} from '../services/utils.js'
import { S3FileNotFoundError } from '../services/errors.js'
import { MissingGrantError, S3FileNotFoundError } from '../services/errors.js'

export const getDefault = async (_: Request, res: Response) => {
try {
Expand Down Expand Up @@ -36,6 +36,12 @@ export const saveUserConfig = async (req: Request, res: Response) => {
try {
// existing config
const s3data = await s3.send(new GetObjectCommand(params))

// if file exists, we need to verify ownership
if (!data.variableHoldingSomeConfirmation) {
throw new MissingGrantError('Grant confirmation is required')
}

// Convert the file stream to a string
fileContentString = await streamToString(
s3data.Body as NodeJS.ReadableStream
Expand All @@ -44,9 +50,16 @@ export const saveUserConfig = async (req: Request, res: Response) => {
const err = error as Error
if (err.name === 'NoSuchKey') {
// file / config not found, continue with defaults
} else if (err.name === 'MissingGrant') {
res.status(200).send({
grantRequired: true,
message: 'Grant confirmation is required'
})
return
} else {
console.log(error)
res.status(500).send('An error occurred while fetching data')
return
}
}

Expand Down
7 changes: 7 additions & 0 deletions backend/src/services/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ export class S3FileNotFoundError extends Error {
this.name = 'NoSuchKey' // Set the error name
}
}

export class MissingGrantError extends Error {
constructor(message: string) {
super(message)
this.name = 'MissingGrant'
}
}
Loading
Loading