Skip to content

Commit

Permalink
Add 'type' member variable to BaseService and integrate object STIX t…
Browse files Browse the repository at this point in the history
…ype validation check to 'create' method
  • Loading branch information
seansica committed Nov 6, 2023
1 parent aacfee4 commit a5fc4e7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/controllers/groups-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ exports.create = async function(req, res) {
// Create the group
try {

const group = await groupsService.createGroup(groupData, options);
const group = await groupsService.create(groupData, options);
logger.debug('Success: Created group with id ' + group.stix.id);
return res.status(201).send(group);
}
Expand Down
11 changes: 9 additions & 2 deletions app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const config = require('../config/config');
const { DatabaseError,
IdentityServiceError,
MissingParameterError,
InvalidQueryStringParameterError } = require('../exceptions');
InvalidQueryStringParameterError,
InvalidTypeError } = require('../exceptions');
const AbstractService = require('./_abstract.service');

class BaseService extends AbstractService {

constructor(repository) {
constructor(type, repository) {
super();
this.type = type;
this.repository = repository;
}

Expand Down Expand Up @@ -216,6 +218,11 @@ class BaseService extends AbstractService {
if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}

if (data?.stix?.type !== this.type) {
throw new InvalidTypeError();
}

// eslint-disable-next-line no-useless-catch
try {
// This function handles two use cases:
Expand Down
3 changes: 1 addition & 2 deletions app/services/assets-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const assetsRepository = require('../repository/assets-repository');

const BaseService = require('./_base.service');


class AssetsService extends BaseService { }

module.exports = new AssetsService(assetsRepository);
module.exports = new AssetsService('x-mitre-asset', assetsRepository);
18 changes: 2 additions & 16 deletions app/services/groups-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@

const BaseService = require('./_base.service');
const groupsRepository = require('../repository/groups-repository');
const { InvalidTypeError } = require('../exceptions');

class GroupsService extends BaseService {
class GroupsService extends BaseService { }

createGroup(data, options) {

// Overrides the base method for groups to inject an additional
// logic check to verify that the creation request is not for an
// intrusion-set, which is an unsupported/invalid use case.

if (data.stix.type !== 'intrusion-set') {
throw new InvalidTypeError();
}
return this.create(data, options);
}
}

module.exports = new GroupsService(groupsRepository);
module.exports = new GroupsService('intrusion-set', groupsRepository);
6 changes: 3 additions & 3 deletions app/services/matrices-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const BaseService = require('./_base.service');

class MatrixService extends BaseService {

constructor(repository) {
super(repository);
constructor(type, repository) {
super(type, repository);

this.retrieveTacticById = null;
this.retrieveTechniquesForTactic = null;
Expand Down Expand Up @@ -115,4 +115,4 @@ class MatrixService extends BaseService {
}
}

module.exports = new MatrixService(matrixRepository);
module.exports = new MatrixService('x-mitre-matrix', matrixRepository);
2 changes: 1 addition & 1 deletion app/tests/api/groups/groups.query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async function loadGroups(groups) {
}

// eslint-disable-next-line no-await-in-loop
await groupsService.createGroup(group, { import: false, userAccountId: group.userAccountId });
await groupsService.create(group, { import: false, userAccountId: group.userAccountId });
}
}

Expand Down

0 comments on commit a5fc4e7

Please sign in to comment.