forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
141 lines
4.9 KiB
JavaScript
141 lines
4.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { randomUUID } from 'node:crypto';
|
|
import * as s3 from '@aws-sdk/client-s3';
|
|
import { adminRequest } from '../infra/rustfs/admin.mjs';
|
|
import { bootstrapSettings } from '../infra/rustfs/policies.mjs';
|
|
|
|
const settings = bootstrapSettings();
|
|
const Bucket = settings.bucket;
|
|
const clients = Object.fromEntries(
|
|
['root', 'api', 'reconciler'].map((role) => [
|
|
role,
|
|
new s3.S3Client({
|
|
endpoint: settings.endpoint,
|
|
region: settings.region,
|
|
forcePathStyle: true,
|
|
maxAttempts: 1,
|
|
credentials: role === 'root' ? settings.credentials : settings[role],
|
|
}),
|
|
]),
|
|
);
|
|
const id = randomUUID();
|
|
const Key = `originals/iam-test/${id}`;
|
|
const outside = `iam-test/${id}`;
|
|
const otherBucket = `iam-test-${id}`;
|
|
const send = (role, Command, args = {}) =>
|
|
clients[role].send(new Command({ Bucket, ...args }), {
|
|
abortSignal: AbortSignal.timeout(30000),
|
|
});
|
|
async function denied(action) {
|
|
await assert.rejects(
|
|
action,
|
|
(error) => error.$metadata?.httpStatusCode === 403 || error.status === 403,
|
|
);
|
|
}
|
|
let failed = false;
|
|
try {
|
|
await send('root', s3.CreateBucketCommand, { Bucket: otherBucket });
|
|
await send('root', s3.PutObjectCommand, { Key: outside, Body: 'private' });
|
|
await send('root', s3.PutObjectCommand, {
|
|
Bucket: otherBucket,
|
|
Key,
|
|
Body: 'private',
|
|
});
|
|
await send('api', s3.PutObjectCommand, { Key, Body: 'fixture' });
|
|
for (const role of ['api', 'reconciler']) {
|
|
await send(role, s3.GetBucketLocationCommand);
|
|
await send(role, s3.HeadObjectCommand, { Key });
|
|
const object = await send(role, s3.GetObjectCommand, { Key });
|
|
assert.equal(await object.Body.transformToString(), 'fixture');
|
|
// RustFS filters ListBuckets to accessible buckets instead of returning 403.
|
|
const visible = await send(role, s3.ListBucketsCommand);
|
|
assert.ok(
|
|
!(visible.Buckets ?? []).some((bucket) => bucket.Name === otherBucket),
|
|
);
|
|
await denied(() => send(role, s3.GetObjectCommand, { Key: outside }));
|
|
await denied(() =>
|
|
send(role, s3.PutObjectCommand, { Key: outside, Body: 'denied' }),
|
|
);
|
|
await denied(() => send(role, s3.DeleteObjectCommand, { Key: outside }));
|
|
await denied(() =>
|
|
send(role, s3.GetObjectCommand, { Bucket: otherBucket, Key }),
|
|
);
|
|
await denied(() => send(role, s3.GetBucketPolicyCommand));
|
|
await denied(() =>
|
|
adminRequest(
|
|
{ ...settings, credentials: settings[role] },
|
|
'GET',
|
|
'list-users',
|
|
),
|
|
);
|
|
await denied(() =>
|
|
adminRequest(
|
|
{ ...settings, credentials: settings[role] },
|
|
'PUT',
|
|
'add-user',
|
|
{ accessKey: `DENIED${id.replaceAll('-', '')}` },
|
|
{ secretKey: randomUUID(), status: 'enabled' },
|
|
),
|
|
);
|
|
console.log(
|
|
`PASS: ${role}: object read and readiness allowed; other prefixes/buckets, bucket policy and admin denied`,
|
|
);
|
|
}
|
|
await denied(() =>
|
|
send('api', s3.ListObjectsV2Command, { Prefix: 'originals/' }),
|
|
);
|
|
await denied(() =>
|
|
send('reconciler', s3.PutObjectCommand, { Key, Body: 'denied' }),
|
|
);
|
|
await denied(() => send('reconciler', s3.ListObjectsV2Command));
|
|
await denied(() =>
|
|
send('reconciler', s3.ListObjectsV2Command, { Prefix: 'iam-test/' }),
|
|
);
|
|
const listed = await send('reconciler', s3.ListObjectsV2Command, {
|
|
Prefix: `originals/iam-test/${id}`,
|
|
});
|
|
assert.ok(listed.Contents.some((object) => object.Key === Key));
|
|
await send('reconciler', s3.ListObjectsV2Command, {
|
|
Prefix: 'originals/',
|
|
MaxKeys: 1,
|
|
});
|
|
await send('reconciler', s3.DeleteObjectCommand, { Key });
|
|
await send('api', s3.PutObjectCommand, { Key, Body: 'fixture' });
|
|
await send('api', s3.DeleteObjectCommand, { Key });
|
|
await assert.rejects(
|
|
() => send('reconciler', s3.HeadObjectCommand, { Key }),
|
|
(error) => error.$metadata?.httpStatusCode === 404,
|
|
'Reconciler must distinguish missing bytes from authorization failures',
|
|
);
|
|
const anonymous = await fetch(`${settings.endpoint}/${Bucket}/${outside}`);
|
|
assert.equal(anonymous.status, 403);
|
|
await anonymous.body?.cancel();
|
|
console.log(
|
|
'PASS: API CRUD without listing; reconciler scoped listing/HEAD/delete without PUT; anonymous access denied',
|
|
);
|
|
} catch (error) {
|
|
failed = true;
|
|
throw error;
|
|
} finally {
|
|
const cleanup = await Promise.allSettled([
|
|
send('root', s3.DeleteObjectCommand, { Key }),
|
|
send('root', s3.DeleteObjectCommand, { Key: outside }),
|
|
send('root', s3.DeleteObjectCommand, { Bucket: otherBucket, Key }),
|
|
]);
|
|
cleanup.push(
|
|
...(await Promise.allSettled([
|
|
send('root', s3.DeleteBucketCommand, { Bucket: otherBucket }),
|
|
])),
|
|
);
|
|
for (const client of Object.values(clients)) client.destroy();
|
|
const errors = cleanup.filter(
|
|
(result) =>
|
|
result.status === 'rejected' &&
|
|
result.reason.$metadata?.httpStatusCode !== 404,
|
|
);
|
|
if (errors.length) {
|
|
console.error(`IAM fixture cleanup failed: ${id}`);
|
|
if (!failed) throw new Error('IAM fixture cleanup failed');
|
|
}
|
|
}
|