Skip to content

Commit

Permalink
Minor cleanup and formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Sheriff committed Jan 24, 2024
1 parent 225273b commit d721767
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 77 deletions.
6 changes: 1 addition & 5 deletions app/controllers/software-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const softwareService = require('../services/software-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError, MissingPropertyError, PropertyNotAllowedError } = require('../exceptions');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError, PropertyNotAllowedError } = require('../exceptions');

exports.retrieveAll = async function(req, res) {
const options = {
Expand Down Expand Up @@ -113,10 +113,6 @@ exports.create = async function(req, res) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create software. Duplicate stix.id and stix.modified properties.');
}
else if (err instanceof MissingPropertyError) {
logger.warn(`Unable to create software, missing property ${ err.propertyName }`);
return res.status(400).send(`Unable to create software, missing property ${ err.propertyName }`);
}
else if (err instanceof PropertyNotAllowedError) {
logger.warn(`Unable to create software, property ${ err.propertyName } is not allowed`);
return res.status(400).send(`Unable to create software, property ${ err.propertyName } is not allowed`);
Expand Down
8 changes: 0 additions & 8 deletions app/exceptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ class NotImplementedError extends CustomError {
}
}

class MissingPropertyError extends CustomError {
constructor(propertyName, options) {
super(`Unable to create software, missing property ${propertyName}`, options);
}
}

class PropertyNotAllowedError extends CustomError {
constructor(propertyName, options) {
super(`Unable to create software, property ${propertyName} is not allowed`, options);
Expand Down Expand Up @@ -126,8 +120,6 @@ module.exports = {
IdentityServiceError,
TechniquesServiceError,
TacticsServiceError,

MissingPropertyError,
PropertyNotAllowedError,

InvalidTypeError,
Expand Down
4 changes: 1 addition & 3 deletions app/repository/software-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const BaseRepository = require('./_base.repository');
const Software = require('../models/software-model');

class SoftwareRepository extends BaseRepository {

}
class SoftwareRepository extends BaseRepository { }

module.exports = new SoftwareRepository(Software);
2 changes: 0 additions & 2 deletions app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,9 @@ class BaseService extends AbstractService {
callback = arguments[arguments.length - 1];
}


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


// eslint-disable-next-line no-useless-catch
try {
Expand Down
121 changes: 62 additions & 59 deletions app/services/software-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,84 @@ const systemConfigurationService = require('./system-configuration-service');
const attackObjectsService = require('./attack-objects-service');
const config = require('../config/config');

const { PropertyNotAllowedError} = require('../exceptions');
const { PropertyNotAllowedError, InvalidTypeError } = require('../exceptions');

const BaseService = require('./_base.service');
const SoftwareRepository = require('../repository/software-repository');
const softwareRepository = require('../repository/software-repository');

class SoftwareService extends BaseService {

async create(data, options, callback) {
// This function handles two use cases:
// 1. This is a completely new object. Create a new object and generate the stix.id if not already
// provided. Set both stix.created_by_ref and stix.x_mitre_modified_by_ref to the organization identity.
// 2. This is a new version of an existing object. Create a new object with the specified id.
// Set stix.x_mitre_modified_by_ref to the organization identity.

// is_family defaults to true for malware, not allowed for tools
try {
if (data.stix && data.stix.type === 'malware' && typeof data.stix.is_family !== 'boolean') {
data.stix.is_family = true;
}
else if (data.stix && data.stix.type === 'tool' && data.stix.is_family !== undefined) {
throw new PropertyNotAllowedError;
}
try {
if (data?.stix?.type !== 'malware' && data?.stix?.type !== 'tool') {
throw new InvalidTypeError();

Check warning on line 24 in app/services/software-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/software-service.js#L24

Added line #L24 was not covered by tests
}

options = options || {};
if (!options.import) {
// Set the ATT&CK Spec Version
data.stix.x_mitre_attack_spec_version = data.stix.x_mitre_attack_spec_version ?? config.app.attackSpecVersion;

// Record the user account that created the object
if (options.userAccountId) {
data.workspace.workflow.created_by_user_account = options.userAccountId;
if (data.stix && data.stix.type === 'malware' && typeof data.stix.is_family !== 'boolean') {
data.stix.is_family = true;
}
else if (data.stix && data.stix.type === 'tool' && data.stix.is_family !== undefined) {
throw new PropertyNotAllowedError();
}

// Set the default marking definitions
await attackObjectsService.setDefaultMarkingDefinitions(data);
options = options || {};
if (!options.import) {
// Set the ATT&CK Spec Version
data.stix.x_mitre_attack_spec_version = data.stix.x_mitre_attack_spec_version ?? config.app.attackSpecVersion;

// Get the organization identity
const organizationIdentityRef = await systemConfigurationService.retrieveOrganizationIdentityRef();

// Check for an existing object
let existingObject;
if (data.stix.id) {
existingObject = await this.repository.retrieveOneById(data.stix.id);
}
// Record the user account that created the object
if (options.userAccountId) {
data.workspace.workflow.created_by_user_account = options.userAccountId;
}

if (existingObject) {
// New version of an existing object
// Only set the x_mitre_modified_by_ref property
data.stix.x_mitre_modified_by_ref = organizationIdentityRef;
}
else {
// New object
// Assign a new STIX id if not already provided
if (!data.stix.id) {
// const stixIdPrefix = getStixIdPrefixFromModel(this.model.modelName, data.stix.type);
data.stix.id = `${data.stix.type}--${uuid.v4()}`;
}
// Set the default marking definitions
await attackObjectsService.setDefaultMarkingDefinitions(data);

// Set the created_by_ref and x_mitre_modified_by_ref properties
data.stix.created_by_ref = organizationIdentityRef;
data.stix.x_mitre_modified_by_ref = organizationIdentityRef;
}
}
const res = await this.repository.save(data);
if (callback) {
return callback(null, res);
}
return res;
} catch (err) {
if (callback) {
return callback(err);
}
throw err;
}
}
// Get the organization identity
const organizationIdentityRef = await systemConfigurationService.retrieveOrganizationIdentityRef();

// Check for an existing object
let existingObject;
if (data.stix.id) {
existingObject = await this.repository.retrieveOneById(data.stix.id);
}

if (existingObject) {
// New version of an existing object
// Only set the x_mitre_modified_by_ref property
data.stix.x_mitre_modified_by_ref = organizationIdentityRef;
}
else {
// New object
// Assign a new STIX id if not already provided
if (!data.stix.id) {
// const stixIdPrefix = getStixIdPrefixFromModel(this.model.modelName, data.stix.type);
data.stix.id = `${data.stix.type}--${uuid.v4()}`;
}

// Set the created_by_ref and x_mitre_modified_by_ref properties
data.stix.created_by_ref = organizationIdentityRef;
data.stix.x_mitre_modified_by_ref = organizationIdentityRef;
}
}
const res = await this.repository.save(data);
if (callback) {
return callback(null, res);

Check warning on line 76 in app/services/software-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/software-service.js#L76

Added line #L76 was not covered by tests
}
return res;
} catch (err) {
if (callback) {
return callback(err);
}
throw err;
}
}
}

module.exports = new SoftwareService(null, SoftwareRepository);
module.exports = new SoftwareService(null, softwareRepository);

0 comments on commit d721767

Please sign in to comment.