forked from mtusi2026/mtusi2026lab4
feat(storage): add RustFS attachments and presigned uploads
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
import {
|
||||
S3Client,
|
||||
PutObjectCommand,
|
||||
CopyObjectCommand,
|
||||
GetObjectCommand,
|
||||
DeleteObjectCommand,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { bootstrapSettings } from '../infra/rustfs/policies.mjs';
|
||||
|
||||
const config = bootstrapSettings();
|
||||
const client = new S3Client({
|
||||
endpoint: config.endpoint,
|
||||
region: config.region,
|
||||
forcePathStyle: true,
|
||||
credentials: config.api,
|
||||
maxAttempts: 1,
|
||||
});
|
||||
const chat = randomUUID();
|
||||
const source = `staging/${chat}/${randomUUID()}`;
|
||||
const invalid = `staging/${chat}/${randomUUID()}`;
|
||||
const original = `originals/${chat}/${randomUUID()}`;
|
||||
const data = Buffer.from('conditional direct upload fixture');
|
||||
const checksum = (value) => createHash('sha256').update(value).digest('base64');
|
||||
const send = (Command, value) =>
|
||||
client.send(new Command({ Bucket: config.bucket, ...value }), {
|
||||
abortSignal: AbortSignal.timeout(15000),
|
||||
});
|
||||
try {
|
||||
const put = await send(PutObjectCommand, {
|
||||
Key: source,
|
||||
Body: data,
|
||||
ContentType: 'image/png',
|
||||
IfNoneMatch: '*',
|
||||
ChecksumSHA256: checksum(data),
|
||||
});
|
||||
await assert.rejects(
|
||||
() =>
|
||||
send(PutObjectCommand, {
|
||||
Key: source,
|
||||
Body: data,
|
||||
IfNoneMatch: '*',
|
||||
ChecksumSHA256: checksum(data),
|
||||
}),
|
||||
(error) => error.$metadata?.httpStatusCode === 412,
|
||||
'Pinned RustFS must enforce create-only PUT',
|
||||
);
|
||||
await assert.rejects(
|
||||
() =>
|
||||
send(PutObjectCommand, {
|
||||
Key: invalid,
|
||||
Body: data,
|
||||
ChecksumSHA256: Buffer.alloc(32).toString('base64'),
|
||||
}),
|
||||
(error) => error.$metadata?.httpStatusCode === 400,
|
||||
'Pinned RustFS must validate the uploaded checksum',
|
||||
);
|
||||
await assert.rejects(
|
||||
() =>
|
||||
send(CopyObjectCommand, {
|
||||
Key: original,
|
||||
CopySource: `${config.bucket}/${source}`,
|
||||
CopySourceIfMatch: '"wrong"',
|
||||
}),
|
||||
(error) => error.$metadata?.httpStatusCode === 412,
|
||||
'Pinned RustFS must reject changed copy sources',
|
||||
);
|
||||
await send(CopyObjectCommand, {
|
||||
Key: original,
|
||||
CopySource: `${config.bucket}/${source}`,
|
||||
CopySourceIfMatch: put.ETag,
|
||||
});
|
||||
const value = await send(GetObjectCommand, { Key: original });
|
||||
assert.equal(await value.Body.transformToString(), data.toString());
|
||||
const anonymous = await fetch(
|
||||
`${config.endpoint}/${config.bucket}/${original}`,
|
||||
);
|
||||
assert.equal(anonymous.status, 403);
|
||||
await anonymous.body?.cancel();
|
||||
console.log(
|
||||
'PASS: RustFS 1.0.4 enforces If-None-Match, SHA-256 and conditional server-side copy',
|
||||
);
|
||||
} finally {
|
||||
await Promise.all(
|
||||
[source, invalid, original].map((Key) =>
|
||||
send(DeleteObjectCommand, { Key }),
|
||||
),
|
||||
);
|
||||
client.destroy();
|
||||
}
|
||||
Reference in New Issue
Block a user