Skip to content

Commit

Permalink
Validate default bucket configuration
Browse files Browse the repository at this point in the history
Check for conflict config > database on startup
Let getBucket() throw if objectStorage not in config
  • Loading branch information
TimCsaky committed Sep 8, 2023
1 parent ed3494a commit 0407769
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
7 changes: 7 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const QueueManager = require('./src/components/queueManager');
const { getAppAuthMode, getGitRevision } = require('./src/components/utils');
const DataConnection = require('./src/db/dataConnection');
const v1Router = require('./src/routes/v1');
const { readUnique } = require('./src/services/bucket');

const dataConnection = new DataConnection();
const queueManager = new QueueManager();
Expand Down Expand Up @@ -206,6 +207,12 @@ function initializeConnections() {
if (state.connections.data) {
log.info('DataConnection Reachable', { function: 'initializeConnections' });
}
if (config.has('objectStorage')) {
readUnique(config.get('objectStorage')).then(() => {
log.error('Default bucket cannot also exist in database', { function: 'initializeConnections' });
fatalErrorHandler();
}).catch(() => { });
}
})
.catch(error => {
log.error(`Initialization failed: Database OK = ${state.connections.data}`, { function: 'initializeConnections' });
Expand Down
2 changes: 1 addition & 1 deletion app/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"objectStorage": {
"accessKeyId": "OBJECTSTORAGE_ACCESSKEYID",
"bucket": "OBJECTSTORAGE_BUCKET",
"defaultTempExpiresIn": "OBJECTSTORAGE_TEMP_EXPIRESIN",
"endpoint": "OBJECTSTORAGE_ENDPOINT",
"key": "OBJECTSTORAGE_KEY",
"secretAccessKey": "OBJECTSTORAGE_SECRETACCESSKEY"
},
"server": {
"bodyLimit": "SERVER_BODYLIMIT",
"defaultTempExpiresIn": "SERVER_TEMP_EXPIRESIN",
"hardReset": "SERVER_HARDRESET",
"logFile": "SERVER_LOGFILE",
"logLevel": "SERVER_LOGLEVEL",
Expand Down
4 changes: 1 addition & 3 deletions app/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
"poolMax": "10",
"username": "app"
},
"objectStorage": {
"defaultTempExpiresIn": "300"
},
"server": {
"bodyLimit": "30mb",
"defaultTempExpiresIn": "300",
"logLevel": "http",
"maxRetries": "3",
"port": "3000"
Expand Down
36 changes: 18 additions & 18 deletions app/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,33 @@ const utils = {
* @throws If there are no records found with `bucketId` and `throwable` is true
*/
async getBucket(bucketId = undefined, throwable = false) {
const data = {
accessKeyId: config.get('objectStorage.accessKeyId'),
bucket: config.get('objectStorage.bucket'),
endpoint: config.get('objectStorage.endpoint'),
key: config.get('objectStorage.key'),
region: DEFAULTREGION,
secretAccessKey: config.get('objectStorage.secretAccessKey')
};

if (bucketId) {
// Function scoped import to avoid circular dependencies
const { bucketService } = require('../services');

try {
const data = { region: DEFAULTREGION };
try {
if (bucketId) {
// Function scoped import to avoid circular dependencies
const { bucketService } = require('../services');
const bucketData = await bucketService.read(bucketId);

data.accessKeyId = bucketData.accessKeyId;
data.bucket = bucketData.bucket;
data.endpoint = bucketData.endpoint;
data.key = bucketData.key;
data.secretAccessKey = bucketData.secretAccessKey;
if (bucketData.region) data.region = bucketData.region;
} catch (err) {
log.warn(err.message, { function: 'getBucket' });
if (throwable) throw new Problem(404, { details: err.message });
} else if (config.has('objectStorage')) {
data.accessKeyId = config.get('objectStorage.accessKeyId');
data.bucket = config.get('objectStorage.bucket');
data.endpoint = config.get('objectStorage.endpoint');
data.key = config.get('objectStorage.key');
data.secretAccessKey = config.get('objectStorage.secretAccessKey');
if (config.has('objectStorage.region')) data.region = config.get('objectStorage.region');
} else {
throw new Error('Unable to get bucket');
}
} catch (err) {
log.warn(err.message, { function: 'getBucket' });
if (throwable) throw new Problem(404, { details: err.message });
}

return data;
},

Expand Down
1 change: 1 addition & 0 deletions app/src/services/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const service = {
throw err;
}
},

};

module.exports = service;
2 changes: 1 addition & 1 deletion app/src/services/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const utils = require('../components/utils');
const DELIMITER = '/';

// Get app configuration
const defaultTempExpiresIn = parseInt(config.get('objectStorage.defaultTempExpiresIn'), 10);
const defaultTempExpiresIn = parseInt(config.get('server.defaultTempExpiresIn'), 10);

/**
* The Core S3 Object Storage Service
Expand Down

0 comments on commit 0407769

Please sign in to comment.