Skip to content

Commit

Permalink
Remove identity service functions that duplicate base service functions.
Browse files Browse the repository at this point in the history
Remove calls to repository.model functions and use the repository functions instead.
  • Loading branch information
Jack Sheriff committed Jan 29, 2024
1 parent 98b014d commit 311bfba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 229 deletions.
3 changes: 0 additions & 3 deletions app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ class BaseService extends AbstractService {
return callback(identityError);
}
throw identityError;

}

const paginatedResults = BaseService.paginate(options, results);
if (callback) {
return callback(null, paginatedResults);
}
return paginatedResults;

}

async retrieveById(stixId, options, callback) {
Expand Down Expand Up @@ -200,7 +198,6 @@ class BaseService extends AbstractService {
const document = await this.repository.retrieveOneByVersion(stixId, modified);

if (!document) {
console.log('** NOT FOUND');
if (callback) {
return callback(null, null);
}
Expand Down
239 changes: 15 additions & 224 deletions app/services/identities-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = require('../config/config');
const userAccountsService = require('./user-accounts-service');
const identitiesRepository = require('../repository/identities-repository');
const BaseService = require('./_base.service');
const { DuplicateIdError, MissingParameterError, InvalidQueryStringParameterError, DatabaseError, IdentityServiceError } = require('../exceptions');
const { InvalidTypeError } = require('../exceptions');

const errors = {
missingParameter: 'Missing required parameter',
Expand All @@ -17,6 +17,8 @@ const errors = {
};
exports.errors = errors;

const identityType = 'identity';

class IdentitiesService extends BaseService {

async addCreatedByAndModifiedByIdentitiesToAll(attackObjects) {
Expand All @@ -27,205 +29,15 @@ class IdentitiesService extends BaseService {
await this.addCreatedByAndModifiedByIdentities(attackObject, identityCache, userAccountCache);
}
}

async retrieveById(stixId, options, callback) {
if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}

if (!stixId) {
const err = new MissingParameterError({ parameterName: 'stixId' });
if (callback) {
return callback(err);
}
throw err;
}

try {
if (options.versions === 'all') {
const documents = await this.repository.retrieveAllById(stixId);

try {
await this.addCreatedByAndModifiedByIdentitiesToAll(documents);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
cause: err
});
if (callback) {
return callback(identityError);
}
throw identityError;
}
if (callback) {
return callback(null, documents);
}
return documents;

} else if (options.versions === 'latest') {
const document = await this.repository.retrieveLatestByStixId(stixId);

if (document) {
try {
await this.addCreatedByAndModifiedByIdentities(document);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
cause: err
});
if (callback) {
return callback(identityError);
}
throw identityError;
}
if (callback) {
return callback(null, [document]);
}
return [document];
} else {
if (callback) {
return callback(null, []);
}
return [];
}

} else {
const err = new InvalidQueryStringParameterError({ parameterName: 'versions' });
if (callback) {
return callback(err);
}
throw err;
}
} catch (err) {
if (callback) {
return callback(err);
}
throw err; // Let the DatabaseError bubble up
}
}


async retrieveVersionById(stixId, modified, callback) {
if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
if (!stixId) {
const err = new MissingParameterError({ parameterName: 'stixId' });
if (callback) {
return callback(err);
}
throw err;
}

if (!modified) {
const err = new MissingParameterError({ parameterName: 'modified' });
if (callback) {
return callback(err);
}
throw err;
}

// eslint-disable-next-line no-useless-catch
try {
const document = await this.repository.retrieveOneByVersion(stixId, modified);

if (!document) {
console.log('** NOT FOUND');
if (callback) {
return callback(null, null);
}
return null;
} else {
try {
await this.addCreatedByAndModifiedByIdentities(document);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
cause: err
});
if (callback) {
return callback(identityError);
}
throw identityError;
}
if (callback) {
return callback(null, document);
}
return document;
}
} catch (err) {
if (callback) {
return callback(err);
}
throw err; // Let the DatabaseError bubble up
}
}


async retrieveAll(options, callback) {
if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}

let results;
try {
results = await this.repository.retrieveAll(options);
} catch (err) {
const databaseError = new DatabaseError(err); // Let the DatabaseError buddle up
if (callback) {
return callback(databaseError);
}
throw databaseError;
}

try {
await this.addCreatedByAndModifiedByIdentitiesToAll(results[0].documents);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
cause: err
});
if (callback) {
return callback(identityError);
}
throw identityError;

}

const paginatedResults = BaseService.paginate(options, results);
if (callback) {
return callback(null, paginatedResults);
}
return paginatedResults;

}

async deleteVersionById(stixId, stixModified) {
if (!stixId) {
throw new MissingParameterError;
}

if (!stixModified) {
throw new MissingParameterError;
}

const identity = await this.repository.model.findOneAndRemove({ 'stix.id': stixId, 'stix.modified': stixModified }).exec();

// Note: identity is null if not found
return identity || null;

}



createIsAsync = true;

async create(data, options) {
// 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.
// 2. This is a new version of an existing object. Create a new object with the specified id.
// Do not set the created_by_ref or x_mitre_modified_by_ref properties.
// This function overrides the base class create() because
// 1. It does not set the created_by_ref or x_mitre_modified_by_ref properties
// 2. It does not check for an existing identity object

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

options = options || {};
if (!options.import) {
Expand All @@ -245,28 +57,8 @@ class IdentitiesService extends BaseService {
}

// Save the document in the database
try {
const savedIdentity = await this.repository.save(data);
return savedIdentity;
}
catch (err) {
if (err.name === 'MongoServerError' && err.code === 11000) {
throw new DuplicateIdError;
}
else {
throw err;
}
}
}

async getLatest(stixId) {
const identity = await this.repository.model
.findOne({ 'stix.id': stixId })
.sort('-stix.modified')
.lean()
.exec();

return identity;
const savedIdentity = await this.repository.save(data);
return savedIdentity;
}

async addCreatedByIdentity(attackObject, cache) {
Expand All @@ -282,7 +74,7 @@ class IdentitiesService extends BaseService {
// No cache or not found in cache
try {
// eslint-disable-next-line require-atomic-updates
const identityObject = await this.getLatest(attackObject.stix.created_by_ref);
const identityObject = await this.repository.retrieveLatestByStixId(attackObject.stix.created_by_ref);
attackObject.created_by_identity = identityObject;

if (cache) {
Expand All @@ -308,7 +100,7 @@ class IdentitiesService extends BaseService {
// No cache or not found in cache
try {
// eslint-disable-next-line require-atomic-updates
const identityObject = await this.getLatest(attackObject.stix.x_mitre_modified_by_ref);
const identityObject = await this.repository.retrieveLatestByStixId(attackObject.stix.x_mitre_modified_by_ref);
attackObject.modified_by_identity = identityObject;

if (cache) {
Expand Down Expand Up @@ -356,7 +148,6 @@ class IdentitiesService extends BaseService {
await IdentitiesService.addCreatedByUserAccountWithCache(attackObject, userAccountCache);
}
}

}

module.exports = new IdentitiesService('identity', identitiesRepository);
module.exports = new IdentitiesService(identityType, identitiesRepository);
2 changes: 0 additions & 2 deletions app/tests/config/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ describe('App Configuration', function () {
});

it('The static marking definitions should be created', function(done) {
console.log("Calling retrieveall");
const options = {};
markingDefinitionsService.retrieveAll(options, function(err, markingDefinitions) {
if (err) {
done(err);
}
else {
console.log("succeeded!");
// We expect to get two marking definitions
expect(markingDefinitions).toBeDefined();
expect(Array.isArray(markingDefinitions)).toBe(true);
Expand Down

0 comments on commit 311bfba

Please sign in to comment.