2024-11-08 05:21:56 -05:00
|
|
|
const fs = require('node:fs')
|
|
|
|
const Path = require('node:path')
|
|
|
|
const crypto = require('node:crypto')
|
2024-11-08 03:32:09 -05:00
|
|
|
const {
|
|
|
|
RootKeyEncryptionKey,
|
|
|
|
} = require('@overleaf/object-persistor/src/PerProjectEncryptedS3Persistor')
|
2020-02-12 06:01:51 -05:00
|
|
|
|
2024-11-15 05:56:53 -05:00
|
|
|
const AWS_S3_USER_FILES_STORAGE_CLASS =
|
|
|
|
process.env.AWS_S3_USER_FILES_STORAGE_CLASS
|
|
|
|
|
2020-02-12 06:01:51 -05:00
|
|
|
// use functions to get a fresh copy, not a reference, each time
|
2024-10-31 08:08:26 -04:00
|
|
|
function s3BaseConfig() {
|
2020-02-12 06:01:51 -05:00
|
|
|
return {
|
|
|
|
endpoint: process.env.AWS_S3_ENDPOINT,
|
|
|
|
pathStyle: true,
|
2021-07-13 07:04:46 -04:00
|
|
|
partSize: 100 * 1024 * 1024,
|
2024-11-08 03:33:22 -05:00
|
|
|
ca: [fs.readFileSync('/certs/public.crt')],
|
2024-10-31 08:08:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function s3Config() {
|
|
|
|
return {
|
|
|
|
key: process.env.AWS_ACCESS_KEY_ID,
|
|
|
|
secret: process.env.AWS_SECRET_ACCESS_KEY,
|
|
|
|
...s3BaseConfig(),
|
2020-02-12 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:32:09 -05:00
|
|
|
const S3SSECKeys = [
|
|
|
|
new RootKeyEncryptionKey(
|
|
|
|
crypto.generateKeySync('aes', { length: 256 }).export(),
|
|
|
|
Buffer.alloc(32)
|
|
|
|
),
|
|
|
|
]
|
2024-11-08 03:31:44 -05:00
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
function s3SSECConfig() {
|
|
|
|
return {
|
|
|
|
...s3Config(),
|
2024-11-08 03:31:44 -05:00
|
|
|
ignoreErrorsFromDEKReEncryption: false,
|
|
|
|
automaticallyRotateDEKEncryption: true,
|
2024-11-08 03:32:24 -05:00
|
|
|
dataEncryptionKeyBucketName: process.env.AWS_S3_USER_FILES_DEK_BUCKET_NAME,
|
2024-11-08 03:32:09 -05:00
|
|
|
pathToProjectFolder(_bucketName, path) {
|
|
|
|
const [projectFolder] = path.match(/^[a-f0-9]+\//)
|
|
|
|
return projectFolder
|
2024-11-08 03:31:26 -05:00
|
|
|
},
|
2024-11-08 03:32:09 -05:00
|
|
|
async getRootKeyEncryptionKeys() {
|
2024-11-08 03:31:44 -05:00
|
|
|
return S3SSECKeys
|
2024-11-08 03:31:26 -05:00
|
|
|
},
|
2024-11-15 05:56:53 -05:00
|
|
|
storageClass: {
|
|
|
|
[process.env.AWS_S3_USER_FILES_BUCKET_NAME]:
|
|
|
|
AWS_S3_USER_FILES_STORAGE_CLASS,
|
|
|
|
},
|
2024-11-08 03:31:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 06:51:22 -05:00
|
|
|
function s3ConfigDefaultProviderCredentials() {
|
|
|
|
return {
|
2024-10-31 08:08:26 -04:00
|
|
|
...s3BaseConfig(),
|
2024-02-21 06:51:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 06:01:51 -05:00
|
|
|
function s3Stores() {
|
|
|
|
return {
|
|
|
|
user_files: process.env.AWS_S3_USER_FILES_BUCKET_NAME,
|
|
|
|
template_files: process.env.AWS_S3_TEMPLATE_FILES_BUCKET_NAME,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gcsConfig() {
|
|
|
|
return {
|
2020-03-13 12:14:06 -04:00
|
|
|
endpoint: {
|
|
|
|
apiEndpoint: process.env.GCS_API_ENDPOINT,
|
2021-07-13 07:04:46 -04:00
|
|
|
projectId: 'fake',
|
2020-03-13 12:14:06 -04:00
|
|
|
},
|
2021-10-27 05:50:03 -04:00
|
|
|
directoryKeyRegex: /^[0-9a-fA-F]{24}\/[0-9a-fA-F]{24}/,
|
2020-03-13 12:14:06 -04:00
|
|
|
unlockBeforeDelete: false, // fake-gcs does not support this
|
2021-07-13 07:04:46 -04:00
|
|
|
deletedBucketSuffix: '-deleted',
|
2020-02-12 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gcsStores() {
|
|
|
|
return {
|
|
|
|
user_files: process.env.GCS_USER_FILES_BUCKET_NAME,
|
|
|
|
template_files: process.env.GCS_TEMPLATE_FILES_BUCKET_NAME,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fsStores() {
|
|
|
|
return {
|
|
|
|
user_files: Path.resolve(__dirname, '../../../user_files'),
|
2021-07-13 07:04:46 -04:00
|
|
|
template_files: Path.resolve(__dirname, '../../../template_files'),
|
2020-02-12 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fallbackStores(primaryConfig, fallbackConfig) {
|
|
|
|
return {
|
|
|
|
[primaryConfig.user_files]: fallbackConfig.user_files,
|
2021-07-13 07:04:46 -04:00
|
|
|
[primaryConfig.template_files]: fallbackConfig.template_files,
|
2020-02-12 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 08:08:26 -04:00
|
|
|
const BackendSettings = {
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_01_FSPersistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 'fs',
|
2021-07-13 07:04:46 -04:00
|
|
|
stores: fsStores(),
|
2020-02-12 06:01:51 -05:00
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_01_S3Persistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 's3',
|
|
|
|
s3: s3Config(),
|
2021-07-13 07:04:46 -04:00
|
|
|
stores: s3Stores(),
|
2020-02-12 06:01:51 -05:00
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_01_S3PersistorDefaultProviderCredentials: {
|
2024-02-21 06:51:22 -05:00
|
|
|
backend: 's3',
|
|
|
|
s3: s3ConfigDefaultProviderCredentials(),
|
|
|
|
stores: s3Stores(),
|
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_01_GcsPersistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 'gcs',
|
|
|
|
gcs: gcsConfig(),
|
2021-07-13 07:04:46 -04:00
|
|
|
stores: gcsStores(),
|
2020-02-12 06:01:51 -05:00
|
|
|
},
|
2024-11-08 03:31:26 -05:00
|
|
|
SHARD_01_PerProjectEncryptedS3Persistor: {
|
|
|
|
backend: 's3SSEC',
|
|
|
|
s3SSEC: s3SSECConfig(),
|
|
|
|
stores: s3Stores(),
|
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_02_FallbackS3ToFSPersistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 's3',
|
|
|
|
s3: s3Config(),
|
|
|
|
stores: s3Stores(),
|
|
|
|
fallback: {
|
|
|
|
backend: 'fs',
|
2021-07-13 07:04:46 -04:00
|
|
|
buckets: fallbackStores(s3Stores(), fsStores()),
|
|
|
|
},
|
2020-02-12 06:01:51 -05:00
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_02_FallbackFSToS3Persistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 'fs',
|
|
|
|
s3: s3Config(),
|
|
|
|
stores: fsStores(),
|
|
|
|
fallback: {
|
|
|
|
backend: 's3',
|
2021-07-13 07:04:46 -04:00
|
|
|
buckets: fallbackStores(fsStores(), s3Stores()),
|
|
|
|
},
|
2020-02-12 06:01:51 -05:00
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_03_FallbackGcsToS3Persistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 'gcs',
|
|
|
|
gcs: gcsConfig(),
|
|
|
|
stores: gcsStores(),
|
|
|
|
s3: s3Config(),
|
|
|
|
fallback: {
|
|
|
|
backend: 's3',
|
2021-07-13 07:04:46 -04:00
|
|
|
buckets: fallbackStores(gcsStores(), s3Stores()),
|
|
|
|
},
|
2020-02-12 06:01:51 -05:00
|
|
|
},
|
2024-07-12 05:46:33 -04:00
|
|
|
SHARD_03_FallbackS3ToGcsPersistor: {
|
2020-02-12 06:01:51 -05:00
|
|
|
backend: 's3',
|
|
|
|
// can use the same bucket names for gcs and s3 (in tests)
|
|
|
|
stores: s3Stores(),
|
|
|
|
s3: s3Config(),
|
|
|
|
gcs: gcsConfig(),
|
|
|
|
fallback: {
|
|
|
|
backend: 'gcs',
|
2021-07-13 07:04:46 -04:00
|
|
|
buckets: fallbackStores(s3Stores(), gcsStores()),
|
|
|
|
},
|
|
|
|
},
|
2020-02-12 06:01:51 -05:00
|
|
|
}
|
2024-07-12 05:46:33 -04:00
|
|
|
|
|
|
|
function checkForUnexpectedTestFile() {
|
|
|
|
const awareOfSharding = [
|
|
|
|
'FilestoreApp.js',
|
|
|
|
'FilestoreTests.js',
|
|
|
|
'TestConfig.js',
|
|
|
|
'TestHelper.js',
|
|
|
|
]
|
|
|
|
for (const file of fs.readdirSync(__dirname).sort()) {
|
|
|
|
if (!awareOfSharding.includes(file)) {
|
|
|
|
throw new Error(
|
|
|
|
`Found new test file ${file}: All tests must be aware of the SHARD_ prefix.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkForUnexpectedTestFile()
|
2024-10-31 08:08:26 -04:00
|
|
|
|
|
|
|
module.exports = {
|
2024-11-15 05:56:53 -05:00
|
|
|
AWS_S3_USER_FILES_STORAGE_CLASS,
|
2024-10-31 08:08:26 -04:00
|
|
|
BackendSettings,
|
|
|
|
s3Config,
|
2024-11-08 03:31:44 -05:00
|
|
|
s3SSECConfig,
|
2024-10-31 08:08:26 -04:00
|
|
|
}
|