forked from Vulnogram/Vulnogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-vulnogram.js
68 lines (57 loc) · 1.91 KB
/
init-vulnogram.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
const pbkdf2 = require('./lib/pbkdf2.js');
const User = require('./models/user.js');
const mongoose = require('mongoose');
const config = require('./config/conf');
mongoose.Promise = global.Promise;
mongoose.set('strictQuery', false);
mongoose.connect(config.database, {
keepAlive: false,
});
User.findOne({username: process.env.VULNOGRAM_ADMIN_USERNAME}, function(err, adminUser) {
if (err) {
console.log("MongoDB Error: " + err);
return false; // or callback
}
if (adminUser) {
console.log(`Admin user, ${process.env.VULNOGRAM_ADMIN_USERNAME}, already exists. Skipping initialization.`);
mongoose.connection.close();
process.exit(0);
}
pbkdf2.hash(process.env.VULNOGRAM_ADMIN_PASSWORD, function (err, hash) {
if (err) {
console.error(err);
}
let newUser = new User({
name: process.env.VULNOGRAM_ADMIN_NAME,
email: process.env.VULNOGRAM_ADMIN_EMAIL,
username: process.env.VULNOGRAM_ADMIN_USERNAME,
priv: 0,
group: process.env.VULNOGRAM_ADMIN_CNA_EMAIL,
password: hash
}, { _id: false });
if(error = newUser.validateSync()) {
console.log("Error: " + error);
process.exit(1);
}
User.findOneAndUpdate({
username: newUser.username
},
newUser,
{
upsert: true,
setDefaultsOnInsert: true
},
function (err, doc) {
if (err) {
console.error(err);
} else {
if (doc) {
console.log('Success', 'User ' + doc.username + ' is now updated.\n');
} else {
console.log('Success', 'New user is now registered and can log in: ' + newUser.username);
}
}
mongoose.connection.close();
});
});
});