-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathserver.js
More file actions
215 lines (179 loc) · 6.71 KB
/
Copy pathserver.js
File metadata and controls
215 lines (179 loc) · 6.71 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
require('colors');
const crypto = require('crypto');
const process = require('node:process');
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('morgan');
const passport = require('passport');
const session = require('express-session');
require('./eventlog');
require('./lib/setupProxy');
const exit = require('./lib/exit');
const config = require('./lib/config');
const certificates = require('./lib/certificates');
const connectorServiceSecretsBridge = require('./lib/connectorServiceSecretsBridge');
const endpoints = require('./endpoints');
const secureStorage = require('./lib/secureStorage');
const { loadProvisioningTicket } = require('./lib/provisioningTicket');
const { configureConnection } = require('./lib/configureConnection');
function end () {
console.log('Got SIGTERM, exiting now.');
if (ws_client) {
process.exiting = true;
return ws_client.once('close', function () {
exit(0);
}).close();
}
exit(0);
}
process.on('uncaughtException', function(err) {
console.error(err.stack);
}).once('SIGTERM', end)
.once('SIGINT', end);
let ws_client;
let maxHeaderSize = Number(config.get('MAX_HEADER_SIZE'));
maxHeaderSize = maxHeaderSize > 0 ? maxHeaderSize : 16834;
console.log('');
console.log('');
console.log('');
console.log('======================== STARTING AD-LDAP CONNECTOR ========================');
console.log('Maximum header size = ' + maxHeaderSize);
(async () => {
try {
await connectorServiceSecretsBridge.processBridgeFile();
await config.initialize();
} catch (err) {
console.log(err.message);
return exit(2);
}
const requiredConfigKeys = [
'PROVISIONING_TICKET',
'LDAP_URL',
'LDAP_BASE'
];
const throwImproperInstallError = (reason) => {
if (process.platform === 'win32') {
console.error(`${reason}. Please re-run the installer (.msi) to set these values.`.red);
} else {
console.error(`${reason}. Please run the installation script under setup/index.js to set these values.`.red);
}
process.exit(1);
};
for (const key of requiredConfigKeys) {
if (!config.get(key)) {
throwImproperInstallError(`Missing required config value: ${key}`);
}
}
if (!config.get('ANONYMOUS_SEARCH_ENABLED')) {
if (!config.get('LDAP_BIND_USER') || !await secureStorage.get(secureStorage.keys.LDAP_BIND_PASSWORD)) {
throwImproperInstallError('Anonymous LDAP search is not enabled, and LDAP bind user or password is not set');
}
}
try {
let provisioningTicket = config.get('PROVISIONING_TICKET');
const ticketInfo = await loadProvisioningTicket(provisioningTicket);
// Update config
config.set('AD_HUB', ticketInfo.adHub);
config.set('PROVISIONING_TICKET', provisioningTicket);
config.set('WSFED_ISSUER', ticketInfo.connectionDomain);
config.set('CONNECTION', ticketInfo.connectionName);
config.set('CLIENT_CERT_AUTH', ticketInfo.certAuth);
config.set('KERBEROS_AUTH', ticketInfo.kerberos);
config.set('REALM', ticketInfo.realm.name);
config.set('SITE_NAME', config.get('SITE_NAME') || ticketInfo.connectionName);
config.set(ticketInfo.realm.name, ticketInfo.realm.postTokenUrl);
// Generate self-signed certificates if needed
console.log('Generating self-signed certificates...');
await certificates.initialize({
connectionDomain: ticketInfo.connectionDomain,
connectionName: ticketInfo.connectionName,
});
// Configure connection using the provisioning ticket
console.log('Configuring connection ' + ticketInfo.connectionName + '.');
const { serverUrl, certThumbprint, tenantSigningKey } = await configureConnection({
provisioningTicket,
connectionName: ticketInfo.connectionName,
});
config.set('SERVER_URL', serverUrl);
config.set('LAST_SENT_THUMBPRINT', certThumbprint);
config.set('TENANT_SIGNING_KEY', tenantSigningKey);
// Save config to file
await config.save();
await require('./lib/ldap').initialize();
} catch (e) {
console.error(e.message);
return exit(1);
}
require('./lib/clock_skew_detector');
ws_client = require('./ws_validator');
var latency_test = require('./latency_test');
latency_test.run_many(10);
if (!config.get('KERBEROS_AUTH') && !config.get('CLIENT_CERT_AUTH')) {
return;
}
require('./lib/setupPassport');
var app = express();
// configure the webserver
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(logger('combined'));
if(config.get('KERBEROS_DEBUG_USER')) {
app.use((req, res, next) => {
req.headers['x-forwarded-user'] = config.get('KERBEROS_DEBUG_USER');
next();
});
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
let sessionSecret = await secureStorage.get(secureStorage.keys.CONNECTOR_SESSION_SECRET);
if (!sessionSecret) {
sessionSecret = crypto.randomBytes(32).toString('hex');
await secureStorage.store(secureStorage.keys.CONNECTOR_SESSION_SECRET, sessionSecret);
}
app.use(session({
secret: sessionSecret,
saveUninitialized: false,
resave: false,
}));
app.use(passport.initialize());
await endpoints.install(app);
await config.save();
var options = {
port: config.get('PORT'),
test_user: config.get('KERBEROS_DEBUG_USER'),
maxHeaderSize,
};
// client certificate-based authentication
if (config.get('CLIENT_CERT_AUTH')) {
console.log('Using client certificate-based authentication');
// SSL settings
options.ca = config.get('CA_CERT');
options.pfx = Buffer.from(config.get('SSL_PFX'), 'base64');
options.passphrase = await secureStorage.get(secureStorage.keys.CUSTOM_SSL_PFX_PASSWORD);
options.requestCert = true;
if (!config.get('KERBEROS_AUTH')) {
var https = require('https'); // use https server
https.createServer(options, app).listen(options.port);
}
}
// kerberos authentication
if (config.get('KERBEROS_AUTH')) {
console.log('Using kerberos authentication');
if (process.platform === 'win32') {
var KerberosServer = require('kerberos-server');
var kerberosServer = new KerberosServer(app, options);
kerberosServer.listen(options.port)
.on('error', function (err) {
console.error(err.message);
return process.exit(1);
});
} else if (config.get('WITH_KERBEROS_PROXY_FRONTEND') || config.get('KERBEROS_DEBUG_USER')) {
var http = require('http');
http.createServer({ maxHeaderSize }, app).listen(options.port);
} else {
return console.log('Detected KERBEROS_AUTH in config, but this platform doesn\'t support it.');
}
}
console.log('listening on port: ' + config.get('PORT'));
})();