forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
92 lines
2.7 KiB
JavaScript
92 lines
2.7 KiB
JavaScript
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();
|
|
}
|