forked from auth0/ad-ldap-connector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtroubleshoot.js
314 lines (286 loc) · 9.3 KB
/
troubleshoot.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
require('colors');
require('./lib/initConf');
require('./lib/setupProxy');
var _ = require('lodash');
var fs = require('fs');
var url = require('url');
var path = require('path');
var ldap = require('./lib/ldap');
var async = require('async');
var nconf = require('nconf');
var request = require('request');
var winston = require('winston');
var thumbprint = require('thumbprint');
var WebSocket = require('ws');
var isWindows = (process.platform == 'win32');
var cas = require('./lib/add_certs');
var logger = new winston.Logger({
transports: [
new winston.transports.Console({
timestamp: function() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min + ":" + sec;
},
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.trying = function(message, arg) {
if (!arg) arg = '';
logger.info((isWindows ? '* ' : '\u272D ').yellow + message, arg);
};
logger.success = function(message, arg) {
if (!arg) arg = '';
logger.info((isWindows ? '\u221A ' : '\u2714 ').green + message, arg);
};
logger.failed = function(message, arg) {
if (!arg) arg = '';
logger.error((isWindows ? '\u00D7 ' : '\u2716 ').red + message, arg);
};
process.on('uncaughtException', function (err) {
console.log(err);
});
console.log('\n Troubleshooting AD LDAP connector\n');
async.series([
function(callback){
cas.inject(callback);
},
function(callback){
var HTTP_PROXY = process.env.HTTP_PROXY || process.env.http_proxy;
if (HTTP_PROXY) {
logger.info('Proxy configured: %s', HTTP_PROXY);
} else {
logger.info('No proxy server configured.');
}
callback();
},
function(callback){
logger.trying('Testing connectivity to Auth0...');
var connectivity_url = 'https://login.auth0.com/test';
if (nconf.get('PROVISIONING_TICKET')) {
connectivity_url = 'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
}
logger.info(' > Test endpoint: ' + connectivity_url.green);
request.get({
uri: connectivity_url,
json: true
}, function (err, res, body) {
if (err || res.statusCode !== 200) {
logger.failed('Error connecting to Auth0.');
if (err)
logger.error(' > Error: %s', JSON.stringify(err));
logger.error(' > Status: %s', res.statusCode);
if (body)
logger.error(' > Body: %s', body.replace(/\n$/, ''));
} else {
logger.success('Connection to test endpoint %s.', 'succeeded'.green);
}
callback();
});
},
function(callback){
logger.trying('Testing hub connectivity (WS).');
var hubUrl = nconf.get('AD_HUB');
if (!hubUrl) {
hubUrl = "https://login.auth0.com/lo/hub";
logger.warn('Could not load AD_HUB from config. Setting to default.');
}
var socket_server_address = hubUrl.replace(/^http/i, 'ws');
var ws = new WebSocket(socket_server_address);
ws.on('open', function () {
logger.success('Connection to hub %s.', 'succeeded'.green);
ws.close();
callback();
}).on('message', function (msg) {
logger.success('Message received: %s.', msg);
ws.close();
callback();
}).on('error', function (err) {
logger.failed('Connection to hub %s.', 'failed'.red);
logger.error(' > Body: %s', err.replace(/\n$/, ''));
ws.close();
callback();
});
},
function(callback){
logger.trying('Testing clock skew...');
var clock_url = 'https://login.auth0.com/test';
if (nconf.get('PROVISIONING_TICKET')) {
clock_url = 'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
}
request.get({
uri: clock_url,
json: true
}, function (err, resp, body) {
if (err || !body || !body.clock) {
logger.failed('Error calling the test endpoint.');
return callback();
}
var auth0_time = body.clock;
var local_time = new Date().getTime();
var diff = Math.abs(auth0_time - local_time);
if (diff > 5000) {
logger.failed('Clock skew detected:');
logger.error(' > Local time: ' + new Date(local_time).toISOString().replace(/T/, ' ').replace(/\..+/, ''));
logger.error(' > Auth0 time: ' + new Date(auth0_time).toISOString().replace(/T/, ' ').replace(/\..+/, '').red);
}
else {
logger.success('Everything %s. No clock skew detected.', 'OK'.green);
}
callback();
});
},
function(callback){
logger.trying('Testing certificates...');
var certPath = path.join(__dirname, 'certs', 'cert.pem');
fs.exists(certPath, function (exists) {
var local_thumbprint;
var server_thumbprint;
if (!exists) {
logger.warn(' > Local certificate ' + 'certs/cert.pem'.yellow + ' does not exist. Cannot read thumbprint.');
}
else {
var certContents = fs.readFileSync(path.join(__dirname, 'certs', 'cert.pem')).toString();
var cert = /-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/g.exec(certContents);
if (cert.length > 0) {
cert = cert[1].replace(/[\n|\r\n]/g, '');
}
local_thumbprint = thumbprint.calculate(cert);
logger.info(' > Local thumbprint: ' + local_thumbprint);
}
if (!nconf.get("PROVISIONING_TICKET")) {
logger.warn(' > ' + 'PROVISIONING_TICKET'.yellow + ' not set. Cannot compare with connection thumbprint (This is optional).');
return callback();
}
var info_url = nconf.get('PROVISIONING_TICKET') + '/info';
request.get({
uri: info_url,
json: true
}, function (err, res, body) {
if (res && res.statusCode !== 200 || err) {
logger.error(' > Error loading certificate from Auth0: %s', res && res.statusCode || err);
logger.warn(' > Cannot compare with connection thumbprint.');
} else {
var thumbprints = body.thumbprints
if (!thumbprints || thumbprints.length === 0) {
logger.error(' > No thumbprints available in the connection information. Cannot compare certificates.');
}
else {
server_thumbprint = body.thumbprints[0];
logger.info(' > Server thumbprint: ' + server_thumbprint);
}
}
if (local_thumbprint && server_thumbprint) {
if (local_thumbprint === server_thumbprint) {
logger.success('Local and server certificates match.');
}
else {
logger.failed('Local and server certificates ' + 'don\'t match'.red + '.');
}
}
callback();
});
});
},
function(callback) {
logger.trying('Running NLTEST...');
if (!isWindows) {
logger.warn(' > NLTEST can only run on Windows.');
return callback();
}
try {
var output = '';
var spawn = require('child_process').spawn;
var nltest = spawn('nltest', ['/dsgetdc:']);
nltest.on('error', function (err) {
logger.failed('Running NLTEST %s.', 'failed'.red);
if (err && err.message)
logger.error(' > Error: %s', err.message.replace(/\r\n|\r|\n/, '').red);
return callback();
});
nltest.stdout.on('data', function (data) { output += data; });
nltest.stderr.on('data', function (data) { output += data; });
nltest.on('close', function (code) {
if (output) {
var lines = output.replace(/^\s+|\s+$/g,'').replace(/\r\n\s+/g,'\r\n').split(/\r\n/g);
for (var i=0; i<lines.length; i++) {
if (code === 0)
logger.info(' > ' + lines[i]);
else
logger.error(' > ' + lines[i]);
}
}
return callback();
});
} catch (err) {
logger.failed('Running NLTEST %s.', 'failed'.red);
if (err && err.message)
logger.error(' > Error: %s', err.message.replace(/\r\n|\r|\n/, '').red);
return callback();
}
},
function(callback) {
logger.trying('Testing LDAP connectivity.');
if (!nconf.get("LDAP_BASE")) {
logger.warn(' > ' + 'LDAP_BASE'.yellow + 'not set. Cannot test connectivity.');
return callback();
}
logger.info(' > LDAP BASE: %s', nconf.get("LDAP_BASE"))
var opts = {
scope: 'sub',
sizeLimit: 5,
filter: nconf.get('LDAP_SEARCH_ALL_QUERY')
};
try {
ldap.client.search(nconf.get("LDAP_BASE"), opts, function(err, res) {
if (err) {
logger.failed('Connection to LDAP %s.', 'failed'.red);
if (err && err.message)
logger.error(' > Error: %s', err.message.replace(/\r\n|\r|\n/, '').red);
return callback();
}
var entries = [];
res.on('searchEntry', function(entry) {
logger.info(' > Found user: %s', entry.object.sAMAccountName || entry.object.mail || entry.object.name);
entries.push(entry);
});
res.on('error', function(err) {
if (err.message === 'Size Limit Exceeded' && entries.length > 0) {
logger.success('Connection to LDAP %s.', 'succeeded'.green);
return callback();
}
logger.failed('Connection to LDAP %s.', 'failed'.red);
if (err && err.message)
logger.error(' > Error: %s', err.message.replace(/\r\n|\r|\n/, '').trim().red);
return callback();
});
res.on('end', function() {
console.log('end');
if (!entries || entries.length === 0) {
logger.error(' > Error: %s', 'Unable to find users. Verify the permissions for the current user.'.red);
}
return callback();
});
});
} catch (e) {
logger.failed('Connection to LDAP %s.', 'failed'.red);
if (e && e.message)
logger.error(' > Error: %s', e.message.replace(/\r\n|\r|\n/, '').red);
return callback();
}
}
],
function(err){
logger.info('Done!\n');
process.exit(0);
});