-
Notifications
You must be signed in to change notification settings - Fork 33
/
app.js
299 lines (255 loc) · 8.43 KB
/
app.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* @module
* @file Configurate API endpoints and view pages.
*/
'use strict';
import express from 'express';
import compression from 'compression';
import bodyParser from 'body-parser';
import multer from 'multer';
import path from 'path';
import Fs from 'fs';
import pkg from 'immutable';
import { v4 as uuidv4 } from 'uuid';
import passport from 'passport';
import LdapAuth from 'ldapauth-fork';
import { BasicStrategy } from 'passport-http';
import Job from './lib/job.js';
import Orchestrator from './lib/orchestrator.js';
import RequestState from './lib/request-state.js';
import SpecberusWrapper from './lib/specberus-wrapper.js';
import sendMessage from './lib/mailer.js';
import { importJSON } from './lib/util.js';
await import(`${process.cwd()}/${process.env.CONFIG || 'config.js'}`);
const { Map } = pkg;
const meta = importJSON('./package.json', import.meta.url);
const app = express();
const requests = {};
const port = process.argv[4] || global.DEFAULT_PORT;
const argTempLocation = process.argv[2] || global.DEFAULT_TEMP_LOCATION;
const argHttpLocation = process.argv[3] || global.DEFAULT_HTTP_LOCATION;
const argResultLocation = process.argv[5] || global.DEFAULT_RESULT_LOCATION;
/**
* Add CORS headers to responses if the client is explicitly allowed.
*
* First, this ensures that the testbed page on the test server, listening on a different port, can GET and POST to Echidna.
* Most importantly, this is necessary to attend publication requests from third parties, eg GitHub.
*/
function corsHandler(req, res, next) {
if (req && req.headers && req.headers.origin) {
if (global.ALLOWED_CLIENTS.some(regex => regex.test(req.headers.origin))) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.header('Access-Control-Allow-Headers', 'Content-Type');
}
}
next();
}
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(corsHandler);
app.use(express.static('assets/'));
app.set('json spaces', 2);
app.set('trust proxy', true);
// Index Page
app.get('/', (request, response) => {
response.sendFile(`${process.cwd()}/views/index.html`);
});
// New UI
app.get('/ui', (request, response) => {
response.sendFile(`${process.cwd()}/views/web-interface.html`);
});
// API methods
app.get('/api/version', (req, res) => {
res.send(meta.version);
});
app.get('/api/version-specberus', async (req, res) => {
res.send(await SpecberusWrapper.version());
});
app.get('/api/status', (req, res) => {
const id = req.query ? req.query.id : null;
const file = `${argResultLocation + path.sep + id}.json`;
if (id) {
Fs.access(file, Fs.constants.F_OK, error => {
if (!error) res.status(200).sendFile(file);
else if (requests && requests[id]) res.status(200).json(requests[id]);
else res.status(404).send(`No job found with ID “${id}”.`);
});
} else res.status(400).send('Missing required parameter “ID”.');
});
function dumpJobResult(dest, result) {
Fs.writeFile(dest, `${JSON.stringify(result, null, 2)}\n`, err => {
if (err) console.error(err);
});
}
/**
* @function processRequest
* @description **Handler of user request.** Distinguish if the request contains a tar file or a link to manifest.
* @param {Object} req
* @param {Object} res
* @param {Boolean} isTar whether request contains a tar file
*/
const processRequest = (req, res, isTar) => {
const id = uuidv4();
const decision = req.body ? req.body.decision : null;
const url = !isTar && req.body ? req.body.url : null;
const token = req.body ? req.body.token : null;
const tar = isTar ? req.file : null;
const { user } = req;
const dryRun = Boolean(
req.body && req.body['dry-run'] && /^true$/i.test(req.body['dry-run']),
);
const ccEmail = req.body ? req.body.cc : null;
if (!(url || tar) || !decision) {
res
.status(400) // Bad Request
.send(
'Missing parameters. The valid combination of parameters are "url + token + decision",' +
' "tar + token + decision" or "tar + W3C credentials + decision".',
);
} else if (tar && !user && (url || tar) && !token) {
// If submitting the tar without W3C credentials, and submitting the tar or URL without token
res
.setHeader('WWW-Authenticate', 'Basic realm="W3CACL"')
.status(401) // Unauthorized
.send(
'Unauthorized request, missing "token" or "W3C credentials" in the parameters. The valid combination of parameters are "url + token + decision", "tar + token + decision" or "tar + W3C credentials + decision".',
);
} else {
const tempLocation = `${argTempLocation}/${id}/`;
const httpLocation = `${argHttpLocation}/${id}/Overview.html`;
requests[id] = {};
requests[id].id = id;
if (req.body && req.body.annotation)
requests[id].annotation = req.body.annotation;
if (isTar) requests[id].tar = tar.originalname;
else requests[id].url = url;
requests[id].version = meta.version;
requests[id]['version-specberus'] = SpecberusWrapper.version;
requests[id].decision = decision;
const jobList = [
'retrieve-resources',
'metadata',
'specberus',
'transition-checker',
'publish',
'tr-install',
'update-tr-shortlink',
];
if (token) {
jobList.unshift('ip-checker');
jobList.splice(3, 0, 'token-checker');
} else {
// submitted with credentials
jobList.splice(2, 0, 'user-checker');
}
if (dryRun) jobList.splice(jobList.indexOf('publish'));
// create empty result placeholder in /api/status?id=xxx
requests[id].results = new RequestState(
'',
new Map(
jobList.reduce((object, value) => {
object[value] = new Job();
return object;
}, {}),
),
);
// Orchestrator: jobList executed.
const orchestrator = new Orchestrator(
url,
tar,
token,
user,
tempLocation,
httpLocation,
argResultLocation,
req.ip,
dryRun,
);
Orchestrator.iterate(
state => orchestrator.next(state),
Orchestrator.hasFinished,
state => {
requests[id].results = state;
},
requests[id].results,
)
.then(async state => {
console.log(`[${state.get('status').toUpperCase()}] ${url}`);
dumpJobResult(
`${argResultLocation + path.sep + id}.json`,
requests[id],
);
if (dryRun) console.log('Dry-run: omitting e-mail notification');
else {
sendMessage(
id,
state,
requests[id],
url || tar.originalname,
ccEmail,
decision,
);
}
})
.done();
res.status(202).send(id);
}
};
// Making sure the user and password match, and the user participate in the group delivering the document.
passport.use(
new BasicStrategy((username, password, done) => {
const opts = {
url: global.LDAP_URL,
bindDn: global.LDAP_BIND_DN.replace(/{{username}}/, username),
bindCredentials: password,
searchBase: global.LDAP_SEARCH_BASE,
searchFilter: '(uid={{username}})',
searchAttributes: ['memberOf'],
cache: false,
};
const ldap = new LdapAuth(opts);
ldap.authenticate(username, password, (err, user) => {
if (err) {
console.log('LDAP auth error: %s', err);
}
done(null, user);
});
}),
);
app.post('/api/request', (req, res, next) => {
if (req.is('application/x-www-form-urlencoded')) {
// URL + token method
processRequest(req, res, false);
} else if (req.is('multipart/form-data')) {
// tar method
next();
} else {
res.status(501).send({ status: 'Form Content-Type not supported' });
}
});
app.post('/api/request', multer().single('tar'), (req, res, next) => {
if (req.headers.authorization) {
// basic authentication
next();
} else {
processRequest(req, res, true); // token
}
});
app.post(
'/api/request',
passport.authenticate('basic', { session: false }),
(req, res) => {
processRequest(req, res, true);
},
);
app.listen(process.env.PORT || port).on('error', err => {
if (err) {
console.error(`Error while trying to launch the server: “${err}”.`);
}
});
console.log(
`${meta.name} version ${meta.version} running on ${
process.platform
} and listening on port ${port}. The server time is ${new Date().toLocaleTimeString()}.`,
);