Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
fix: adding validation for study update route (#215)
Browse files Browse the repository at this point in the history
* fix: adding validation for study update route

* doc: updating changelog

* fix: test code cleanup
  • Loading branch information
SanketD92 authored Nov 23, 2020
1 parent 06b2780 commit 9bc2d90
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

All notable changes to this project will be documented in this file.

## [1.4.2] - 2020-11-23

### Added
- fix: Fix a bug on the update study API

We recommend to apply this patch as soon as possible

## [1.4.1] - 2020-11-18

### Added
- fix: Handling policy names for windows envs
- fix: Fix a bug on the create study API

We recommend to apply this patch as soon as possible

## [1.4.0] - 2020-11-13

### Added
- feat: Study Read/Write and Permission propagation (Goofys)
- feat: Read/Only study mounts on AWS Service Catalog based EC2 Windows workspaces

## [1.3.2] - 2020-10-23

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ class EnvironmentMountService extends Service {
const requestedStudyIds = studyInfo.map(study => study.id);

// Retrieve and verify user's study permissions
const studyPermissionService = await this.service('studyPermissionService');
const [studyPermissionService, studyService] = await this.service(['studyPermissionService', 'studyService']);
const storedPermissions = await studyPermissionService.getRequestorPermissions(requestContext);

// If there are no stored permissions, use an empty permissions object
Expand All @@ -806,10 +806,10 @@ class EnvironmentMountService extends Service {
);

// Determine whether any forbidden studies were requested
const allowedStudies = permissions.adminAccess.concat(permissions.readonlyAccess);
const allowedStudies = studyService.getAllowedStudies(permissions);
const forbiddenStudies = _.difference(requestedStudyIds, allowedStudies);

if (forbiddenStudies.length) {
if (!_.isEmpty(forbiddenStudies)) {
throw new Error(`Studies not found: ${forbiddenStudies.join(',')}`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('studyService', () => {
it('should get the correct allowed studies ONLY (admin, R/O, R/W)', async () => {
// BUILD, OPERATE and CHECK
expect(
service._getAllowedStudies({
service.getAllowedStudies({
adminAccess: ['studyA'],
readonlyAccess: ['studyB'],
readwriteAccess: ['studyC'],
Expand Down Expand Up @@ -251,6 +251,46 @@ describe('studyService', () => {
);
});

it('should fail to update resource list of non-Open Data study', async () => {
// BUILD
const dataIpt = {
id: 'newOpenStudy',
category: 'Organization',
resources: [{ arn: 'arn:aws:s3:::someRandomStudyArn' }],
};
service.audit = jest.fn();

// OPERATE
await expect(
service.update(
{ principal: { userRole: 'researcher' }, principalIdentifier: { uid: 'someRandomUserUid' } },
dataIpt,
),
).rejects.toThrow({
message: 'Resources can only be updated for Open Data study category',
});
});

it('should fail to update Open Data study by non-system user', async () => {
// BUILD
const dataIpt = {
id: 'newOpenStudy',
category: 'Open Data',
resources: [{ arn: 'arn:aws:s3:::someRandomStudyArn' }],
};
service.audit = jest.fn();

// OPERATE
await expect(
service.update(
{ principal: { userRole: 'admin' }, principalIdentifier: { uid: 'someRandomUserUid' } },
dataIpt,
),
).rejects.toThrow({
message: 'Only the system can update Open Data studies.',
});
});

it('should try to create the study successfully', async () => {
// BUILD
const dataIpt = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ class StudyService extends Service {
async update(requestContext, rawData) {
const [validationService] = await this.service(['jsonSchemaValidationService']);

if (rawData.category === 'Open Data' && !isSystem(requestContext)) {
throw this.boom.badRequest('Only the system can update Open Data studies.', true);
}

if (rawData.category !== 'Open Data' && !_.isEmpty(rawData.resources)) {
throw this.boom.badRequest('Resources can only be updated for Open Data study category', true);
}

// Validate input
await validationService.ensureValid(rawData, updateSchema);

Expand Down Expand Up @@ -243,7 +251,7 @@ class StudyService extends Service {
return result;
}

_getAllowedStudies(permissions = []) {
getAllowedStudies(permissions = []) {
const adminAccess = permissions.adminAccess || [];
const readonlyAccess = permissions.readonlyAccess || [];
const readwriteAccess = permissions.readwriteAccess || [];
Expand All @@ -269,7 +277,7 @@ class StudyService extends Service {
const permissions = await this.studyPermissionService.getRequestorPermissions(requestContext);
if (permissions) {
// We can't give duplicate keys to the batch get, so ensure that allowedStudies is unique
const allowedStudies = this._getAllowedStudies(permissions);
const allowedStudies = this.getAllowedStudies(permissions);
if (allowedStudies.length) {
const rawResult = await this._getter()
.keys(allowedStudies.map(studyId => ({ id: studyId })))
Expand Down

0 comments on commit 9bc2d90

Please sign in to comment.