forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
import {
|
|
S3Client,
|
|
CreateBucketCommand,
|
|
HeadBucketCommand,
|
|
} from '@aws-sdk/client-s3';
|
|
import { adminRequest } from './admin.mjs';
|
|
import { bootstrapSettings, storagePolicies } from './policies.mjs';
|
|
|
|
const settings = bootstrapSettings();
|
|
const client = new S3Client({
|
|
endpoint: settings.endpoint,
|
|
region: settings.region,
|
|
forcePathStyle: true,
|
|
maxAttempts: 1,
|
|
credentials: settings.credentials,
|
|
});
|
|
const Bucket = settings.bucket;
|
|
try {
|
|
try {
|
|
await client.send(new HeadBucketCommand({ Bucket }), {
|
|
abortSignal: AbortSignal.timeout(30000),
|
|
});
|
|
} catch (error) {
|
|
if (error.$metadata?.httpStatusCode !== 404) throw error;
|
|
try {
|
|
await client.send(new CreateBucketCommand({ Bucket }), {
|
|
abortSignal: AbortSignal.timeout(30000),
|
|
});
|
|
} catch (error) {
|
|
if (error.name !== 'BucketAlreadyOwnedByYou') throw error;
|
|
}
|
|
}
|
|
const policies = storagePolicies(Bucket);
|
|
for (const role of ['api', 'reconciler']) {
|
|
const identity = settings[role];
|
|
const policyName = `${Bucket}-${role}`;
|
|
await adminRequest(
|
|
settings,
|
|
'PUT',
|
|
'add-canned-policy',
|
|
{ name: policyName },
|
|
policies[role],
|
|
);
|
|
await adminRequest(
|
|
settings,
|
|
'PUT',
|
|
'add-user',
|
|
{ accessKey: identity.accessKeyId },
|
|
{ secretKey: identity.secretAccessKey, status: 'enabled' },
|
|
);
|
|
// Replace the direct binding instead of accumulating old policies across bootstraps.
|
|
await adminRequest(settings, 'PUT', 'set-user-or-group-policy', {
|
|
policyName,
|
|
userOrGroup: identity.accessKeyId,
|
|
isGroup: false,
|
|
});
|
|
console.log(`Restricted S3 identity provisioned: ${role}`);
|
|
}
|
|
console.log('Private attachments bucket and restricted identities are ready');
|
|
} finally {
|
|
client.destroy();
|
|
}
|