Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
352 lines (308 loc) · 11.2 KB
/
Copy pathserver.js
File metadata and controls
352 lines (308 loc) · 11.2 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const fs = require('fs');
const express = require('express');
const session = require('express-session');
const axios = require('axios').default;
console.log('Alarm & Alert Service server starting ... ');
const TEST = false;
let config;
let globConf;
if (!TEST) {
config = JSON.parse(fs.readFileSync('/etc/aaasf/config.json', 'utf8'));
globConf = JSON.parse(fs.readFileSync('/etc/aaasf/globus-config.json', 'utf8'));
} else {
config = JSON.parse(fs.readFileSync('./kube/secrets/config.json', 'utf8'));
globConf = JSON.parse(fs.readFileSync('./kube/secrets/globus-config.json', 'utf8'));
}
console.log(config);
// App
const app = express();
app.use(express.static('./static'));
app.set('view engine', 'pug');
app.set('views', 'views');
app.use(express.json()); // to support JSON-encoded bodies
app.use(session({
secret: 'mamicu mu njegovu',
resave: false,
saveUninitialized: true,
cookie: { secure: false, maxAge: 3600000 },
}));
// set only by the local dev modes (dev/*.js): which data the page shows
app.locals.devBanner = process.env.AAAS_DEV_BANNER;
// lets templates hand data to page scripts via <script type="application/json">
app.locals.json = (data) => JSON.stringify(data).replace(/</g, '\\u003c');
app.use((req, res, next) => {
res.locals.loggedIn = req.session.loggedIn === true;
res.locals.currentUser = req.session.user;
res.locals.path = req.path;
next();
});
const usr = require('./routes/user');
const alarms = require('./routes/alarms');
const heartbeats = require('./routes/heartbeats');
const { loadDocs } = require('./routes/docs');
usr.init(config);
alarms.init(config);
heartbeats.init(config);
app.use('/user', usr.router);
app.use('/alarm', alarms.router);
app.use('/heartbeat', heartbeats.router);
// Kibana dashboards embedded on the Alarms and Heartbeats pages
const KIBANA_DASHBOARDS = 'https://atlas-kibana.mwt2.org:5601/s/aaas/app/dashboards?auth_provider_hint=anonymous1#/view/';
const KIBANA_TIME = '_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)%2Ctime%3A(from%3Anow-7d%2Cto%3Anow))';
const ALARMS_DASHBOARD = 'f3055010-2af1-11ed-afcf-d91dad577662';
const HEARTBEATS_DASHBOARD = 'cf438520-2afb-11ed-afcf-d91dad577662';
// a string in rison, the notation of Kibana's URL state
const risonString = (s) => `'${s.replace(/!/g, '!!').replace(/'/g, "!'")}'`;
// `kql`, if given, becomes the dashboard's query (Kibana app state `_a`)
function kibanaDashboard(title, description, id, kql) {
const appState = kql ? `&_a=${encodeURIComponent(`(query:(language:kuery,query:${risonString(kql)}))`)}` : '';
return {
title,
description,
embedUrl: `${KIBANA_DASHBOARDS}${id}?embed=true&${KIBANA_TIME}${appState}&show-time-filter=true&hide-filter-bar=true`,
kibanaUrl: `${KIBANA_DASHBOARDS}${id}?${KIBANA_TIME}${appState}`,
};
}
// KQL matching the alarms of the subscribed events, grouped per category/subcategory;
// returns the query and the number of distinct events in it
function subscriptionsQuery(subscriptions) {
const quote = (v) => `"${String(v).replace(/[\\"]/g, '\\$&')}"`;
const groups = new Map();
(subscriptions || []).forEach((s) => {
if (!s || !s.category || !s.subcategory || !s.event) return;
const key = JSON.stringify([s.category, s.subcategory]);
if (!groups.has(key)) groups.set(key, { category: s.category, subcategory: s.subcategory, events: new Set() });
groups.get(key).events.add(s.event);
});
const clauses = [...groups.values()].map((g) => `(category:${quote(g.category)} and subcategory:${quote(g.subcategory)}`
+ ` and event:(${[...g.events].map(quote).join(' or ')}))`);
return { kql: clauses.join(' or '), events: [...groups.values()].reduce((n, g) => n + g.events.size, 0) };
}
// the "Ongoing issues" panel on the subscriptions page looks this far back
const ONGOING_HOURS = 24;
// only same-site paths are allowed as post-login destinations
function safeReturnPath(p) {
if (typeof p !== 'string' || !p.startsWith('/') || p.startsWith('//') || p.startsWith('/\\')) return null;
return p;
}
function topologyForPage(topology) {
return (topology || []).map(({
category, subcategory, event, description,
}) => ({
category, subcategory, event, description,
}));
}
function renderError(res, status, title, message) {
res.status(status);
res.render('error', { code: status, title, message });
}
// GLOBUS STUFF
const bup = Buffer.from(`${globConf.CLIENT_ID}:${globConf.CLIENT_SECRET}`).toString('base64');
const auth = `Basic ${bup}`;
const requiresLogin = async (req, res, next) => {
// to be used as middleware
if (req.session.loggedIn !== true) {
console.log('NOT logged in!');
res.redirect(`/?next=${encodeURIComponent(req.originalUrl)}`);
} else {
next();
}
};
app.get('/login', async (req, res) => {
console.log('Logging in');
req.session.returnTo = safeReturnPath(req.query.next);
const params = new URLSearchParams({
scope: 'openid email profile urn:globus:auth:scope:auth.globus.org:view_identities',
state: 'garbageString',
response_type: 'code',
redirect_uri: globConf.REDIRECT_LINK,
client_id: globConf.CLIENT_ID,
client_secret: globConf.CLIENT_SECRET,
});
const red = `${globConf.AUTHORIZE_URI}?${params.toString()}`;
console.log('redirecting to:', red);
res.redirect(red);
});
app.get('/logout', (req, res) => {
if (!req.session.loggedIn) {
res.redirect('/');
return;
}
axios.get(globConf.LOGOUT_URI, {
params: {
client_id: globConf.CLIENT_ID,
},
headers: {
Authorization: `Bearer ${req.session.token}`,
},
})
.then((response) => {
console.log(`logout status: ${response.status} globus logout success.`);
})
.catch((error) => {
console.log('logout failure...', error.message);
})
.then(() => {
req.session.destroy();
res.redirect('/');
});
});
app.get('/authcallback', async (req, res) => {
const { code } = req.query;
if (!code) {
console.log('AUTH CALLBACK without code:', req.query);
res.redirect('/?error=login');
return;
}
const params = new URLSearchParams({
grant_type: 'authorization_code',
code,
redirect_uri: globConf.REDIRECT_LINK,
});
try {
const res1 = await axios.post(
`${globConf.TOKEN_URI}?${params.toString()}`,
null,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: auth,
},
},
);
console.log(`statusCode1: ${res1.status}`);
const res2 = await axios.post(
'https://auth.globus.org/v2/oauth2/userinfo',
null,
{
headers: {
Authorization: `Bearer ${res1.data.access_token}`,
},
},
);
console.log(`statusCode2: ${res2.status}`);
const u = {
id: res2.data.sub,
name: res2.data.name,
email: res2.data.email,
username: res2.data.preferred_username,
affiliation: res2.data.organization,
};
// first-time users must exist in ES before the pages below load them
await usr.addIfNeeded(u);
req.session.user = u;
req.session.loggedIn = true;
} catch (err) {
console.log('login failed:', err.message);
res.redirect('/?error=login');
return;
}
const returnTo = req.session.returnTo || '/subscriptions';
delete req.session.returnTo;
res.redirect(returnTo);
});
app.get('/subscriptions', requiresLogin, async (req, res) => {
console.log(`showing all subscriptions to user: ${req.session.user.id}`);
const [userInfo, alarmTopology, heartbeatTopology, alarmLevels] = await Promise.all([
usr.loadUser(req.session.user.id),
alarms.loadAlarmTopology(),
heartbeats.loadHeartbeatTopology(),
alarms.loadAlarmLevels(),
]);
if (!userInfo) {
renderError(res, 500, 'Couldn’t load your subscriptions', 'Please try again in a moment.');
return;
}
const ongoing = await alarms.loadOngoingIssues(userInfo.subscriptions, ONGOING_HOURS);
const now = Date.now();
res.render('subscriptions', {
pageData: {
userId: req.session.user.id,
subscriptions: userInfo.subscriptions || [],
alarms: topologyForPage(alarmTopology),
heartbeats: topologyForPage(heartbeatTopology),
levels: alarmLevels,
ongoing,
ongoingHours: ONGOING_HOURS,
acknowledgements: userInfo.acknowledgements.filter((a) => a.until > now),
},
});
});
// the user's subscribed events by default; everything with ?show=all, without
// subscriptions, or when subscribed to every event (a filter would change nothing)
app.get('/viewer', requiresLogin, async (req, res) => {
const [userInfo, alarmTopology, heartbeatTopology] = await Promise.all([
usr.loadUser(req.session.user.id),
alarms.loadAlarmTopology(),
heartbeats.loadHeartbeatTopology(),
]);
const subscriptions = (userInfo && userInfo.subscriptions) || [];
const { kql, events } = subscriptionsQuery(subscriptions);
const keyOf = (o) => JSON.stringify([o.category, o.subcategory, o.event]);
const subscribed = new Set(subscriptions.map(keyOf));
const catalog = (alarmTopology || []).concat(heartbeatTopology || []);
const everything = catalog.length > 0 && catalog.every((c) => subscribed.has(keyOf(c)));
const filtered = Boolean(kql) && !everything;
const showAll = req.query.show === 'all' || !filtered;
let description = `Alarms for the ${events} event${events === 1 ? '' : 's'} you’re subscribed to.`;
if (!kql) description = 'You haven’t subscribed to any events yet, so this shows all alarms.';
else if (everything) description = 'You’re subscribed to every event, so this shows all alarms.';
else if (showAll) description = 'All alarms, across every category.';
res.render('dashboard', {
dashboard: kibanaDashboard('Alarms', description, ALARMS_DASHBOARD, showAll ? null : kql),
scope: filtered ? { showAll } : null,
noSubscriptions: !kql,
});
});
app.get('/heartbeats_viewer', requiresLogin, (req, res) => {
res.render('dashboard', {
dashboard: kibanaDashboard('Heartbeats', 'Heartbeats received from monitored services.', HEARTBEATS_DASHBOARD),
});
});
app.get('/docs', (req, res) => {
res.render('docs', loadDocs());
});
app.get('/profile', requiresLogin, async (req, res) => {
console.log('profile called!');
const userInfo = await usr.loadUser(req.session.user.id);
if (!userInfo) {
renderError(res, 500, 'Couldn’t load your profile', 'Please try again in a moment.');
return;
}
userInfo.userId = req.session.user.id;
userInfo.config = config.PREFERENCES;
res.render('profile', userInfo);
});
app.get('/healthz', (_req, res) => {
// console.log('Checking health and if some private pod/service needs deletion.');
try {
res.status(200).send('OK');
} catch (err) {
console.log('not OK.', err);
}
});
app.get('/', (req, res) => {
if (req.session.loggedIn === true) {
res.redirect('/subscriptions');
return;
}
res.render('login', {
next: safeReturnPath(req.query.next),
error: req.query.error !== undefined,
});
});
app.use((req, res) => {
console.error('Unexisting page requested:', req.path, 'params:', req.params);
renderError(res, 404, 'Page not found', 'The page you’re looking for doesn’t exist or has moved.');
});
app.listen(80, () => {
console.log('Listening on port 80.');
});
async function main() {
try {
console.log('main.');
} catch (err) {
console.error('Error: ', err);
}
}
main();