forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
88 lines
2.4 KiB
JavaScript
88 lines
2.4 KiB
JavaScript
export function storagePolicies(bucket) {
|
|
const bucketArn = `arn:aws:s3:::${bucket}`;
|
|
const objectsArn = `${bucketArn}/originals/*`;
|
|
const base = [
|
|
{
|
|
Effect: 'Allow',
|
|
Action: ['s3:GetBucketLocation'],
|
|
Resource: [bucketArn],
|
|
},
|
|
];
|
|
return {
|
|
api: {
|
|
Version: '2012-10-17',
|
|
Statement: [
|
|
...base,
|
|
{
|
|
Effect: 'Allow',
|
|
Action: ['s3:GetObject', 's3:PutObject', 's3:DeleteObject'],
|
|
Resource: [objectsArn, `${bucketArn}/staging/*`],
|
|
},
|
|
],
|
|
},
|
|
reconciler: {
|
|
Version: '2012-10-17',
|
|
Statement: [
|
|
...base,
|
|
{
|
|
Effect: 'Allow',
|
|
Action: ['s3:ListBucket'],
|
|
Resource: [bucketArn],
|
|
Condition: {
|
|
StringLike: { 's3:prefix': ['originals/*', 'staging/*'] },
|
|
},
|
|
},
|
|
{
|
|
Effect: 'Allow',
|
|
Action: ['s3:GetObject', 's3:DeleteObject'],
|
|
Resource: [objectsArn, `${bucketArn}/staging/*`],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
}
|
|
|
|
export function bootstrapSettings(env = process.env) {
|
|
const settings = {
|
|
endpoint: env.S3_ENDPOINT ?? 'http://s3:9000',
|
|
region: env.S3_REGION ?? 'us-east-1',
|
|
bucket: env.S3_BUCKET ?? 'messenger-attachments',
|
|
credentials: {
|
|
accessKeyId: env.S3_ACCESS_KEY_ID,
|
|
secretAccessKey: env.S3_SECRET_ACCESS_KEY,
|
|
},
|
|
api: {
|
|
accessKeyId: env.S3_API_ACCESS_KEY_ID ?? 'LABAPI',
|
|
secretAccessKey:
|
|
env.S3_API_SECRET_ACCESS_KEY ?? 'local_api_storage_password',
|
|
},
|
|
reconciler: {
|
|
accessKeyId: env.S3_RECONCILER_ACCESS_KEY_ID ?? 'LABRECONCILER',
|
|
secretAccessKey:
|
|
env.S3_RECONCILER_SECRET_ACCESS_KEY ??
|
|
'local_reconciler_storage_password',
|
|
},
|
|
};
|
|
const identities = [settings.credentials, settings.api, settings.reconciler];
|
|
for (const identity of identities) {
|
|
if (
|
|
!identity.accessKeyId ||
|
|
!identity.secretAccessKey ||
|
|
identity.secretAccessKey.length < 8
|
|
) {
|
|
throw new Error(
|
|
'All S3 identities require an access key and a secret of at least 8 characters',
|
|
);
|
|
}
|
|
}
|
|
if (
|
|
new Set(identities.map((identity) => identity.accessKeyId)).size !== 3 ||
|
|
new Set(identities.map((identity) => identity.secretAccessKey)).size !== 3
|
|
) {
|
|
throw new Error(
|
|
'Root, API and reconciler identities must have distinct access and secret keys',
|
|
);
|
|
}
|
|
return settings;
|
|
}
|