forked from screwdriver-cd/screwdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
240 lines (207 loc) · 9.59 KB
/
server.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
'use strict';
const Hapi = require('@hapi/hapi');
const logger = require('screwdriver-logger');
const registerPlugins = require('./registerPlugins');
process.on('unhandledRejection', (reason, p) => {
console.error('Unhandled Rejection at: Promise', p, 'reason:', reason); /* eslint-disable-line no-console */
});
/**
* If we're throwing errors, let's have them say a little more than just 500
* @method prettyPrintErrors
* @param {Hapi.Request} request Hapi Request object
* @param {Hapi.h} h Hapi Response Toolkit
*/
function prettyPrintErrors(request, h) {
const { response } = request;
const { release } = request.server.app;
if (release && release.cookieName && request.state && !request.state[release.cookieName]) {
h.state(release.cookieName, release.cookieValue);
}
if (!response.isBoom) {
return h.continue;
}
const err = response;
const errName = err.output.payload.error;
const errMessage = err.message;
const { statusCode } = err.output.payload;
const stack = err.stack || errMessage;
if (statusCode === 500) {
request.log(['server', 'error'], stack);
}
const res = {
statusCode,
error: errName,
message: errMessage
};
if (err.data) {
res.data = err.data;
}
return h.response(res).code(statusCode);
}
/**
* Configures & starts up a HapiJS server
* @method
* @param {Object} config
* @param {Object} config.httpd
* @param {Integer} config.httpd.port Port number to listen to
* @param {String} config.httpd.host Host to listen on
* @param {String} config.httpd.uri Public routable address
* @param {Object} config.httpd.tls TLS Configuration
* @param {Object} config.webhooks Webhooks settings
* @param {String} config.webhooks.restrictPR Restrict PR setting
* @param {Boolean} config.webhooks.chainPR Chain PR flag
* @param {Object} config.ecosystem List of hosts in the ecosystem
* @param {Object} config.ecosystem.ui URL for the User Interface
* @param {Factory} config.pipelineFactory Pipeline Factory instance
* @param {Factory} config.jobFactory Job Factory instance
* @param {Factory} config.userFactory User Factory instance
* @param {Factory} config.bannerFactory Banner Factory instance
* @param {Factory} config.buildFactory Build Factory instance
* @param {Factory} config.buildClusterFactory Build Cluster Factory instance
* @param {Factory} config.stepFactory Step Factory instance
* @param {Factory} config.secretFactory Secret Factory instance
* @param {Factory} config.tokenFactory Token Factory instance
* @param {Factory} config.eventFactory Event Factory instance
* @param {Factory} config.collectionFactory Collection Factory instance
* @param {Factory} config.stageFactory Stage Factory instance
* @param {Factory} config.stageBuildFactory Stage Build Factory instance
* @param {Factory} config.triggerFactory Trigger Factory instance
* @param {Object} config.builds Config to include for builds plugin
* @param {Object} config.builds.ecosystem List of hosts in the ecosystem
* @param {Object} config.builds.authConfig Configuration for auth
* @param {Object} config.builds.externalJoin Flag to allow external join
* @param {Object} config.unzipArtifactsEnabled Flag to allow unzip artifacts
* @param {Function} callback Callback to invoke when server has started.
* @return {http.Server} A listener: NodeJS http.Server object
*/
module.exports = async config => {
try {
// Hapi Cross-origin resource sharing configuration
// See http://hapijs.com/api for available options
let corsOrigins = [config.ecosystem.ui];
if (Array.isArray(config.ecosystem.allowCors)) {
corsOrigins = corsOrigins.concat(config.ecosystem.allowCors);
}
const cors = {
origin: corsOrigins,
additionalExposedHeaders: ['x-more-data'],
credentials: true
};
// Create a server with a host and port
const server = new Hapi.Server({
port: config.httpd.port,
host: config.httpd.host,
uri: config.httpd.uri,
routes: {
cors,
log: { collect: true }
},
state: {
strictHeader: false
},
router: {
stripTrailingSlash: true
}
});
// Set the factories within server.app
// Instantiating the server with the factories will apply a shallow copy
server.app = {
commandFactory: config.commandFactory,
commandTagFactory: config.commandTagFactory,
templateFactory: config.templateFactory,
templateTagFactory: config.templateTagFactory,
pipelineTemplateFactory: config.pipelineTemplateFactory,
pipelineTemplateVersionFactory: config.pipelineTemplateVersionFactory,
jobTemplateTagFactory: config.jobTemplateTagFactory,
pipelineTemplateTagFactory: config.pipelineTemplateTagFactory,
stageFactory: config.stageFactory,
stageBuildFactory: config.stageBuildFactory,
triggerFactory: config.triggerFactory,
pipelineFactory: config.pipelineFactory,
jobFactory: config.jobFactory,
userFactory: config.userFactory,
buildFactory: config.buildFactory,
stepFactory: config.stepFactory,
bannerFactory: config.bannerFactory,
secretFactory: config.secretFactory,
tokenFactory: config.tokenFactory,
eventFactory: config.eventFactory,
collectionFactory: config.collectionFactory,
buildClusterFactory: config.buildClusterFactory,
ecosystem: config.ecosystem,
release: config.release,
queueWebhook: config.queueWebhook,
unzipArtifacts: config.unzipArtifactsEnabled
};
const bellConfigs = await config.auth.scm.getBellConfiguration();
config.auth.bell = bellConfigs;
if (config.release && config.release.cookieName) {
server.state(config.release.cookieName, {
path: '/',
ttl: config.release.cookieTimeout * 60 * 1000, // (2 mins)
isSecure: config.auth.https,
isHttpOnly: !config.auth.https
});
}
// Write prettier errors
server.ext('onPreResponse', prettyPrintErrors);
// Audit log
if (config.log && config.log.audit.enabled) {
server.ext('onCredentials', (request, h) => {
const { username, scope, pipelineId } = request.auth.credentials;
if (scope) {
const validScope = config.log.audit.scope.filter(s => scope.includes(s));
if (Array.isArray(validScope) && validScope.length > 0) {
let context;
if (validScope.includes('admin')) {
context = `Admin ${username}`;
} else if (validScope.includes('user')) {
context = `User ${username}`;
} else if (validScope.includes('build') || validScope.includes('temporal')) {
context = `Build ${username}`;
} else if (validScope.includes('pipeline')) {
context = `Pipeline ${pipelineId}`;
} else {
context = `Guest ${username}`;
}
logger.info(`[Login] ${context} ${request.method} ${request.path}`);
}
}
return h.continue;
});
}
// Register events for notifications plugin
server.event(['build_status', 'job_status']);
// Register plugins
await registerPlugins(server, config);
// Initialize common data in buildFactory and jobFactory
server.app.buildFactory.apiUri = server.info.uri;
server.app.buildFactory.tokenGen = (buildId, metadata, scmContext, expiresIn, scope = ['temporal']) =>
server.plugins.auth.generateToken(
server.plugins.auth.generateProfile({ username: buildId, scmContext, scope, metadata }),
expiresIn
);
server.app.buildFactory.executor.tokenGen = server.app.buildFactory.tokenGen;
server.app.jobFactory.apiUri = server.info.uri;
server.app.jobFactory.tokenGen = (username, metadata, scmContext, scope = ['user']) =>
server.plugins.auth.generateToken(
server.plugins.auth.generateProfile({ username, scmContext, scope, metadata })
);
server.app.jobFactory.executor.userTokenGen = server.app.jobFactory.tokenGen;
if (server.plugins.shutdown) {
server.plugins.shutdown.handler({
taskname: 'executor-queue-cleanup',
task: async () => {
await server.app.jobFactory.cleanUp();
logger.info('completed clean up tasks');
}
});
}
// Start the server
await server.start();
return server;
} catch (err) {
logger.error('Failed to start server', err);
throw err;
}
};