This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed-taxii-data.js
101 lines (81 loc) · 3.54 KB
/
seed-taxii-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const
mongoose = require('mongoose'),
config = require('./configs'),
ModelFactory = require('./lib/model-factory'),
ApiRoot = require('./models/apiroot'),
Collection = require('./models/collection'),
Role = require('./models/role'),
Account = require('./models/account'),
fs = require('fs'),
util = require('util');
const readFile = util.promisify(fs.readFile);
// @TODO - need to make sure seeding only happens if taxii2config db doesn't exist
const seedTaxiiData = async () => {
let seedDir = __dirname + '/seed-data/';
let basicJsonInsert = async function(fileName, BaseModel, additionalFields = null) {
let filePath = seedDir + fileName;
try {
let importData = await readFile(filePath, {encoding: 'utf8'});
let importJson = JSON.parse(importData);
let insertData = [];
// get rid of this hack, quick & dirty to get data in
if(additionalFields) {
insertData.push(Object.assign(additionalFields, importJson[0]));
} else {
insertData = importJson;
}
console.log("Importing from: ", fileName);
let objectResult = await BaseModel.insertMany(insertData);
} catch(err) {
console.log(err);
}
}
let importEnterpriseAttack = async function(apiRootName, collectionId) {
let file = __dirname + '/seed-data/enterprise-attack.json';
try {
let importData = await readFile(file, {encoding: 'utf8'});
let importJson = JSON.parse(importData);
//console.log('Import Data Example: ', importJson[1]);
let models = ModelFactory(apiRootName, collectionId);
console.log("Seeding Enterprise Attack Collection to API root: ", apiRootName, " collection: ", collectionId);
console.log(" ... from: ", file);
let objectResult = await models.object.insertMany(importJson);
//console.log("Import result: ", objectResult);
} catch(err) {
console.log(err);
}
}
let foundSeedData = false;
try {
let seededAdminRole = await Role.findOne({name: "Admin"});
if(seededAdminRole._id) {
foundSeedData = true
}
} catch(err) {
console.log("No seeded admin role data found, seeding all data.");
}
// Execute imports here
if(!foundSeedData)
{
try {
console.log("Seeding API Root... ");
await basicJsonInsert('seed-apiroot1.json', ApiRoot);
let insertedApiRoot = await ApiRoot.findOne({name: "apiroot1"}).select('_id name');
let insertedApiRootId = { apiRoot : insertedApiRoot._id };
let insertedApiRootName = insertedApiRoot.name;
console.log("Seeding Collection... ");
await basicJsonInsert('seed-collection.json', Collection, insertedApiRootId);
console.log("Seeding Roles... ");
await basicJsonInsert('seed-roles.json', Role);
console.log("Seeding Admin Account... ");
let insertedAdminRole = await Role.findOne({name: "Admin"});
let insertedAdminRoleId = { role: insertedAdminRole._id };
await basicJsonInsert('seed-admin-account.json', Account, insertedAdminRoleId);
await importEnterpriseAttack(insertedApiRootName, '9ee8a9b3-da1b-45d1-9cf6-8141f7039f82');
}
catch(err) {
console.log("Error: ", err);
}
}
}
module.exports = seedTaxiiData;