Skip to content

Commit

Permalink
Merge pull request #322 from center-for-threat-informed-defense/feature/
Browse files Browse the repository at this point in the history
#311-import-assets

Add support for importing assets.
  • Loading branch information
ElJocko authored Oct 31, 2023
2 parents c4861c1 + be1753f commit 6b06166
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/assets-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports.retrieveById = async function(req, res) {
}

try {
const assets = await assetsService.retrieveById(req.params.stixId, options);
const assets = await assetsService.retrieveByIdAsync(req.params.stixId, options);
if (assets.length === 0) {
return res.status(404).send('Asset not found.');
}
Expand Down
67 changes: 66 additions & 1 deletion app/services/assets-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,72 @@ exports.retrieveAll = async function(options) {
}
};

exports.retrieveById = async function(stixId, options) {
exports.retrieveById = function(stixId, options, callback) {
// versions=all Retrieve all assets with the stixId
// versions=latest Retrieve the asset with the latest modified date for this stixId

if (!stixId) {
const error = new Error(errors.missingParameter);
error.parameterName = 'stixId';
return callback(error);
}

if (options.versions === 'all') {
Asset.find({'stix.id': stixId})
.sort('-stix.modified')
.lean()
.exec(function (err, assets) {
if (err) {
if (err.name === 'CastError') {
const error = new Error(errors.badlyFormattedParameter);
error.parameterName = 'stixId';
return callback(error);
}
else {
return callback(err);
}
}
else {
identitiesService.addCreatedByAndModifiedByIdentitiesToAll(assets)
.then(() => callback(null, assets));
}
});
}
else if (options.versions === 'latest') {
Asset.findOne({ 'stix.id': stixId })
.sort('-stix.modified')
.lean()
.exec(function(err, asset) {
if (err) {
if (err.name === 'CastError') {
const error = new Error(errors.badlyFormattedParameter);
error.parameterName = 'stixId';
return callback(error);
}
else {
return callback(err);
}
}
else {
// Note: document is null if not found
if (asset) {
identitiesService.addCreatedByAndModifiedByIdentities(asset)
.then(() => callback(null, [ asset ]));
}
else {
return callback(null, []);
}
}
});
}
else {
const error = new Error(errors.invalidQueryStringParameter);
error.parameterName = 'versions';
return callback(error);
}
};

exports.retrieveByIdAsync = async function(stixId, options) {
// versions=all Retrieve all versions of the asset with the stixId
// versions=latest Retrieve the asset with the latest modified date for this stixId

Expand Down
4 changes: 4 additions & 0 deletions app/services/collection-bundles-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const uuid = require('uuid');
const util = require('util');
const semver = require('semver');

const assetsService = require('../services/assets-service');
const collectionsService = require('../services/collections-service');
const techniquesService = require('../services/techniques-service');
const tacticsService = require('../services/tactics-service');
Expand Down Expand Up @@ -306,6 +307,9 @@ exports.importBundle = function(collection, data, options, callback) {
else if (importObject.type === 'x-mitre-data-component') {
service = dataComponentsService;
}
else if (importObject.type === 'x-mitre-asset') {
service = assetsService;
}

if (service) {
// Retrieve all the objects with the same stix ID
Expand Down

0 comments on commit 6b06166

Please sign in to comment.