Repository navigation
Expand file tree
/
Copy pathdev.js
More file actions
174 lines (166 loc) · 5.64 KB
/
Copy pathdev.js
File metadata and controls
174 lines (166 loc) · 5.64 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Non-interactive development setup for fog-node.
*
* Mirrors `tools/setup/index.js` but takes no prompts: it writes the
* git-ignored `config/local.js` and `config/models.js` with sane localhost
* defaults (overridable via env vars) and seeds the Administrator account.
*
* Intended to pair with the bundled `docker-compose.yml` Mongo so a fresh
* clone can be lifted with:
*
* docker compose up -d # or: podman compose up -d
* npm run setup:dev
* node app.js # development mode; npm start is production
*
* Env overrides:
* FOG_DEV_DB_HOST (default 127.0.0.1)
* FOG_DEV_DB_PORT (default 27017)
* FOG_DEV_DB_NAME (default fog)
* FOG_DEV_DB_USER (default <none>)
* FOG_DEV_DB_PASS (default <none>)
* FOG_DEV_ADMIN_EMAIL (default admin@example.com)
* FOG_DEV_ADMIN_PASSWORD (default fogadmin1 -- must be >= 8 chars)
*
* This is for LOCAL DEVELOPMENT ONLY. For a real install use
* `node tools/setup/index.js`.
*/
const fs = require('fs');
const path = require('path');
const config = require('../lib/config');
const secure = require('./lib/secure');
const pki = require('./pki');
const escrow = require('../../api/lib/agent-ca-escrow');
const schema = require('./lib/schema');
const cfgPath = path.join(config.appPath, 'config');
const localCfg = path.join(cfgPath, 'local.js');
const modelsCfg = path.join(cfgPath, 'models.js');
const env = process.env;
const db = {
host: env.FOG_DEV_DB_HOST || '127.0.0.1',
port: env.FOG_DEV_DB_PORT || '27017',
name: env.FOG_DEV_DB_NAME || 'fog',
user: env.FOG_DEV_DB_USER || '',
pass: env.FOG_DEV_DB_PASS || ''
};
const admin = {
email: env.FOG_DEV_ADMIN_EMAIL || 'admin@example.com',
password: env.FOG_DEV_ADMIN_PASSWORD || 'fogadmin1'
};
function mongoUrl() {
let auth = '';
if (db.user.length) {
auth = encodeURIComponent(db.user);
if (db.pass.length) auth += ':' + encodeURIComponent(db.pass);
auth += '@';
}
return `mongodb://${auth}${db.host}:${db.port}/${encodeURIComponent(db.name)}`;
}
function writeIfMissing(file, label, contents) {
if (fs.existsSync(file)) {
console.log(`${label} already exists -- leaving it untouched (delete it to regenerate).`);
return;
}
fs.writeFileSync(file, contents);
console.log(`Wrote ${label}.`);
}
if (admin.password.length < 8) {
console.error('FOG_DEV_ADMIN_PASSWORD must be at least 8 characters.');
process.exit(1);
}
writeIfMissing(localCfg, 'config/local.js', `module.exports = {
auth: {
bcrypt: {
rounds: 10
},
jwt: {
secret: '${secure.generateSecret()}',
options: {
expiresIn: '1d'
},
session: false,
cookie: {
maxAge: 24 * 1000 * 60 * 60,
signed: true
}
}
},
// The fog-agent HTTPS port (api/hooks/agent-tls). The certificate names are
// the default list: localhost, this computer and its addresses.
custom: {
agentTlsPort: 8443
},
datastores: {
fogdb: {
adapter: 'sails-mongo',
url: '${mongoUrl()}'
}
},
schema: 1
};
`);
writeIfMissing(modelsCfg, 'config/models.js', `module.exports.models = {
schema: true,
// 'safe' = never auto-migrate. Mongo is schemaless so no migration is needed,
// and 'alter' can drop/recreate collections (data loss) -- especially with more
// than one app instance pointed at the same database.
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true },
updatedAt: { type: 'number', autoUpdatedAt: true },
id: { type: 'string', columnName: '_id' }
},
datastore: 'fogdb',
cascadeOnDestroy: true,
dataEncryptionKeys: {
default: '${secure.generateSecret()}'
}
};
`);
// The fog-agent CAs. local.js exists now, so a custom.pkiDir in it is honored.
// Files that already exist are left as they are.
const pkiDir = pki.pkiDirFor(config.appPath);
// The agent CA backup passphrase (tools/setup/pki.js setupPki). A new backup
// gets a generated one, printed once. A restore reads the first line of the
// file after --agent-ca-passphrase-file, or opens nothing without the flag.
const passphraseAt = process.argv.indexOf('--agent-ca-passphrase-file');
const passphraseFile = passphraseAt === -1 ? null : process.argv[passphraseAt + 1];
const getPassphrase = async ({purpose}) => {
if (purpose === 'restore') return passphraseFile ? pki.readPassphraseFile(passphraseFile) : null;
const words = escrow.generatePassphrase();
pki.printPassphrase(words);
return words;
};
console.log('Seeding Administrator account...');
schema.generate(admin.password, admin.email, async (err) => {
if (err) {
console.error(`Failed to seed database: ${err}`);
process.exit(1);
}
try {
// A recorded root that pki/ no longer has stops setup unless --new-agent-root.
await pki.setupPki({
pkiDir,
newRoot: process.argv.includes('--new-agent-root'),
settings: sails.models.setting,
log: console.log,
command: 'setup:dev',
escrows: sails.models.agentcaescrow,
getPassphrase
});
} catch (pkiErr) {
// The guard errors carry the full message: print it as is.
console.error(pki.isRootError(pkiErr) ? pkiErr.message : `Failed to make the agent CA files: ${pkiErr.message}`);
process.exit(1);
}
console.log(`Agent CA files are in ${pkiDir}`);
console.log('');
console.log('Development setup complete.');
console.log(' URL: http://localhost:1337');
console.log(' Username: Administrator');
console.log(` Email: ${admin.email}`);
console.log(` Password: ${admin.password}`);
console.log('');
// Development mode, not npm start: it rebuilds assets when a file changes.
console.log('Start the server with `node app.js`.');
process.exit(0);
});