Skip to content

Commit 90f39ee

Browse files
authored
Check pActiveLib validity (MRA-836) (#361)
* Add allowedLibs check for pActiveLibrary (MRA-836) * Add .env* to .gitignore * 3.4.1-alpha.5
1 parent 47a132b commit 90f39ee

File tree

9 files changed

+25
-12
lines changed

9 files changed

+25
-12
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ coverage
44
dist
55
.nyc_output
66
.vscode
7-
.env
7+
.env*
88
tmp/

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Melinda REST API for ILS integration
2222
| REQUIRE_AUTH_FOR_READ | No | false |
2323
| REQUIRE_KVP_FOR_WRITE | No | false |
2424
| DEFAULT_ACCEPT | No | application/json |
25-
| FIX_TYPES | No | UNDEL,DELET
25+
| FIX_TYPES | No | UNDEL,DELET |
26+
| ALLOWED_LIBS | No | [] |
2627

2728
### ApiDoc
2829
https://bib-rest.api.melinda.kansalliskirjasto.fi/swagger/

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"url": "[email protected]:natlibfi/melinda-rest-api-http.git"
1515
},
1616
"license": "MIT",
17-
"version": "3.4.1-alpha.3",
17+
"version": "3.4.1-alpha.5",
1818
"main": "dist/index.js",
1919
"engines": {
2020
"node": ">=18"

src/app.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default async function ({
1515
sruUrl, amqpUrl, mongoUri,
1616
pollWaitTime, recordType,
1717
requireAuthForRead, requireKVPForWrite,
18-
fixTypes
18+
fixTypes, allowedLibs
1919
}) {
2020
const logger = createLogger();
2121
const server = await initExpress();
@@ -46,11 +46,11 @@ export default async function ({
4646
}));
4747

4848
app.use(passport.initialize());
49-
app.use('/bulk', passport.authenticate('melinda', {session: false}), await createBulkRouter({mongoUri, amqpUrl, recordType})); // Must be here to avoid bodyparser
49+
app.use('/bulk', passport.authenticate('melinda', {session: false}), await createBulkRouter({mongoUri, amqpUrl, recordType, allowedLibs})); // Must be here to avoid bodyparser
5050
app.use(bodyParser.text({limit: '5MB', type: '*/*'}));
5151
app.use('/apidoc', createApiDocRouter());
5252
app.use('/logs', passport.authenticate('melinda', {session: false}), await createLogsRouter({mongoUri}));
53-
app.use('/', await createPrioRouter({sruUrl, amqpUrl, mongoUri, pollWaitTime, recordType, requireAuthForRead, requireKVPForWrite, fixTypes}));
53+
app.use('/', await createPrioRouter({sruUrl, amqpUrl, mongoUri, pollWaitTime, recordType, requireAuthForRead, requireKVPForWrite, fixTypes, allowedLibs}));
5454
app.use(handleError);
5555

5656
return app.listen(httpPort, () => logger.info(`Started Melinda REST API for ${recordType} records in port ${httpPort}`));

src/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ export const CONTENT_TYPES = [
3737
export const DEFAULT_ACCEPT = readEnvironmentVariable('DEFAULT_ACCEPT', {defaultValue: 'application/json'});
3838

3939
export const fixTypes = readEnvironmentVariable('FIX_TYPES', {defaultValue: ['DELET', 'UNDEL']});
40+
41+
// We default allowedLibs to empty array for backwards compatibility, as it is anyways checked in aleph-record-load-api
42+
export const allowedLibs = readEnvironmentVariable('ALLOWED_LIBS', {defaultValue: []});

src/interfaces/bulk.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {CONTENT_TYPES} from '../config';
3333
import {generateQuery, generateShowParams} from './utils';
3434
// import {inspect} from 'util';
3535

36-
export default async function ({mongoUri, amqpUrl}) {
36+
export default async function ({mongoUri, amqpUrl, allowedLibs}) {
3737
const logger = createLogger();
3838
const mongoOperator = await mongoFactory(mongoUri, 'bulk');
3939
const amqpOperator = await amqpFactory(amqpUrl, true);
@@ -266,9 +266,16 @@ export default async function ({mongoUri, amqpUrl}) {
266266
return recordStatuses;
267267
}
268268

269+
// eslint-disable-next-line max-statements
269270
function validateQueryParams(queryParams) {
270271
logger.silly(`bulk/validateQueryParams: queryParams: ${JSON.stringify(queryParams)}`);
271272

273+
// Note: for backwards compatibility, if we have default empty allowedLibs, we do note check lib here (aleph-record-load-api handles it later)
274+
if (queryParams.pActiveLibrary && allowedLibs.length > 0 && !allowedLibs.includes(queryParams.pActiveLibrary)) {
275+
logger.debug(`Invalid pActiveLibrary parameter '${queryParams.pActiveLibrary} - not included in ${JSON.stringify(allowedLibs)}`);
276+
throw new HttpError(httpStatus.BAD_REQUEST, `Invalid pActiveLibrary parameter '${queryParams.pActiveLibrary}'`);
277+
}
278+
272279
if (queryParams.pOldNew && queryParams.pActiveLibrary) {
273280
const {pOldNew} = queryParams;
274281

@@ -277,6 +284,7 @@ export default async function ({mongoUri, amqpUrl}) {
277284
throw new HttpError(httpStatus.BAD_REQUEST, `Invalid pOldNew query parameter '${pOldNew}'. (Valid values: OLD/NEW)`);
278285
}
279286

287+
// DEVELOP: if we want to use FIX operation for bulk, we'll need to handle this choice differently
280288
const operation = pOldNew === 'NEW' ? OPERATIONS.CREATE : OPERATIONS.UPDATE;
281289

282290
const recordLoadParams = {

src/routes/bulk.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import {authorizeKVPOnly, checkId, checkContentType} from './routeUtils';
1010
import {checkQueryParams} from './queryUtils';
1111
import {inspect} from 'util';
1212

13-
export default async function ({mongoUri, amqpUrl, recordType}) {
13+
export default async function ({mongoUri, amqpUrl, recordType, allowedLibs}) {
1414
const logger = createLogger();
1515

1616
const OPERATION_TYPES = [OPERATIONS.CREATE, OPERATIONS.UPDATE];
17-
const Service = await createService({mongoUri, amqpUrl});
17+
const Service = await createService({mongoUri, amqpUrl, allowedLibs});
1818

1919
return new Router()
2020
.use(authorizeKVPOnly)
@@ -31,6 +31,7 @@ export default async function ({mongoUri, amqpUrl, recordType}) {
3131
async function create(req, res, next) {
3232
try {
3333
logger.silly('routes/Bulk create');
34+
// DEVELOP: why we pass req.user.id here?
3435
const {operation, recordLoadParams, noStream, operationSettings} = Service.validateQueryParams(req.query, req.user.id);
3536

3637
// We have match and merge settings just for bib records in validator

src/routes/prio.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {CONTENT_TYPES, DEFAULT_ACCEPT} from '../config';
1414
import {checkQueryParams} from './queryUtils';
1515

1616
// eslint-disable-next-line no-unused-vars
17-
export default async ({sruUrl, amqpUrl, mongoUri, pollWaitTime, recordType, requireAuthForRead, requireKVPForWrite, fixTypes}) => {
17+
export default async ({sruUrl, amqpUrl, mongoUri, pollWaitTime, recordType, requireAuthForRead, requireKVPForWrite, fixTypes, allowedLibs}) => {
1818
const logger = createLogger();
1919
//const apiDoc = fs.readFileSync(path.join(__dirname, '..', 'api.yaml'), 'utf8');
2020
const Service = await createService({

0 commit comments

Comments
 (0)