Skip to content

Commit

Permalink
Merge pull request #1643 from balena-io/ab77/patch
Browse files Browse the repository at this point in the history
better handling of exceptions from AWS/S3
  • Loading branch information
flowzone-app[bot] authored May 23, 2024
2 parents 9c91596 + ced3815 commit 11388b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/services/open-balena-api.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Description=open-balena-api
Requires=confd.service
After=confd.service
StartLimitIntervalSec=0

[Service]
StandardOutput=journal+console
Expand All @@ -11,7 +12,6 @@ EnvironmentFile=/usr/src/app/config/env
ExecStart=/usr/src/app/entry.sh
Restart=always
SyslogIdentifier=api
StartLimitIntervalSec=0

[Install]
WantedBy=basic.target
32 changes: 27 additions & 5 deletions src/features/device-types/storage/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ function createS3Client() {

const s3Client = createS3Client();

function logUnauthenticatedWarning(
clientS3: UnauthenticatedS3Facade | AWSWrapper.AWS.S3,
pathS3: string,
err: any,
): void {
if (
clientS3 instanceof UnauthenticatedS3Facade &&
[401, 403].includes(err.statusCode)
) {
console.warn(
`${err.code} (${err.statusCode}): ${pathS3} belongs to a private device type or has incorrect permissions`,
);
}
}

async function getFileInfo(s3Path: string) {
const req = s3Client.headObject({
Bucket: S3_BUCKET,
Expand All @@ -66,11 +81,16 @@ async function getFileInfo(s3Path: string) {
}

export async function getFile(s3Path: string) {
const req = s3Client.getObject({
Bucket: S3_BUCKET,
Key: s3Path,
});
return await req.promise();
try {
const req = s3Client.getObject({
Bucket: S3_BUCKET,
Key: s3Path,
});
return await req.promise();
} catch (err) {
// catch errors for private device types when running unauthenticated
logUnauthenticatedWarning(s3Client, s3Path, err);
}
}

export async function getFolderSize(
Expand Down Expand Up @@ -128,6 +148,8 @@ export async function fileExists(s3Path: string): Promise<boolean> {
await getFileInfo(s3Path);
return true;
} catch (err) {
// catch errors for private device types when running unauthenticated
logUnauthenticatedWarning(s3Client, s3Path, err);
if (err.statusCode === 404) {
return false;
}
Expand Down

0 comments on commit 11388b0

Please sign in to comment.