Skip to content

Commit

Permalink
Validate default bucket configuration on startup
Browse files Browse the repository at this point in the history
disable default bucket by removing objectStorage from config
  • Loading branch information
TimCsaky committed Sep 7, 2023
1 parent ed3494a commit 005257d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
10 changes: 10 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 { checkDefaultBucket } = require('./src/services/bucket');

const dataConnection = new DataConnection();
const queueManager = new QueueManager();
Expand Down Expand Up @@ -206,6 +207,15 @@ function initializeConnections() {
if (state.connections.data) {
log.info('DataConnection Reachable', { function: 'initializeConnections' });
}
if (config.has('objectStorage')) {
checkDefaultBucket(config.get('objectStorage')).then(result => {
if (result) {
log.error('Default bucket cannot also exist in database.', { function: 'initializeConnections' });
process.exitCode = 1;
shutdown();
}
});
}
})
.catch(error => {
log.error(`Initialization failed: Database OK = ${state.connections.data}`, { function: 'initializeConnections' });
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');;

Check failure on line 91 in app/src/components/utils.js

View workflow job for this annotation

GitHub Actions / Unit Tests (14.x)

Unnecessary semicolon

Check failure on line 91 in app/src/components/utils.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

Unnecessary semicolon

Check failure on line 91 in app/src/components/utils.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x)

Unnecessary semicolon

Check failure on line 91 in app/src/components/utils.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

Unnecessary semicolon

Check failure on line 91 in app/src/components/utils.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x)

Unnecessary semicolon
} 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
19 changes: 19 additions & 0 deletions app/src/services/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ const service = {
throw err;
}
},

/**
* @function checkDefaultBucket
* Checks if the bucket defined in the COMS configuration matches a bucket in the database
* @param {object} params an object containing bucket attributes
* @returns {boolean} True if default bucket configuration is valid,
* false if default bucket exists in database
*/
checkDefaultBucket(params) {
// check for matching bucket in database
return Bucket.query()
.first()
.where({
'bucket': params.bucket,
'endpoint': params.endpoint,
'key': params.key
});
},

};

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 005257d

Please sign in to comment.