-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_ldap.js
89 lines (80 loc) · 3.14 KB
/
test_ldap.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
'use strict';
const dns = require('dns');
const ldap = require('ldapjs');
const ldapBindScheme = 'ldap://';
const ldapBindFqdn = 'corp.contoso.com';
const ldapBindPort = '389';
const ldapBindDn = 'CN=bigip2faldapuser,OU=Service Accounts,DC=corp,DC=contoso,DC=com';
const ldapBindPwd = 'COMPLEX_PASSWORD_STRING';
const ldapUserDn = 'CN=John S.,OU=User Accounts,DC=corp,DC=contoso,DC=com';
const ldapUserAttr = 'extensionAttribute2';
const ldapUserSecret = 'BASE64_STRING';
const ldapNameResolver = '198.51.100.10|198.51.100.11';
let ldapModifyRec = (hosts, ldapChange, i) => {
return new Promise((resolve, reject) => {
const ldapBindUrl = ldapBindScheme + hosts[i] + ':' + ldapBindPort;
const ldapClient = ldap.createClient({url: ldapBindUrl, tlsOptions: {'rejectUnauthorized': false}});
ldapClient.on('error', (error) => {
// LDAP bind failed
return reject('LDAP bind error: ' + error);
});
ldapClient.bind(ldapBindDn, ldapBindPwd, (error) => {
if (error) {
// LDAP bind failed
return reject('LDAP bind error: ' + error);
} else {
console.log('LDAP bind success ' + ldapBindUrl);
try {
ldapClient.modify(ldapUserDn, ldapChange, (error) => {
ldapClient.unbind((error) => {
if (error) {
// LDAP unbind failed
console.error('LDAP unbind error: ' + error);
}
});
if (error) {
// LDAP modify failed
return reject('LDAP modify error: ' + error);
} else {
// LDAP modify successful
return resolve();
}
});
} catch (error) {
// LDAP modify failed
return reject('LDAP modify error: ' + error);
}
}
});
});
};
let modifyNext = (records, ldapChange, i) => {
ldapModifyRec(records, ldapChange, i).then(() => {
console.log('LDAP modify success');
}).catch((error) => {
console.error('ldapModifyRec error: ' + error);
if (records.length - 1 === i) {
// LDAP server list reached its end
console.error('None of LDAP servers returned success');
} else {
modifyNext(records, ldapChange, ++i);
}
});
};
try {
dns.setServers(ldapNameResolver.split('|'));
} catch (error) {
console.error('DNS config error: ' + error);
}
dns.resolve(ldapBindFqdn, (error, records) => {
if (error) {
// DNS resolve failed
console.error('DNS resolve error: ' + error);
} else {
console.log('DNS resolve success: ' + records);
const ldapModification = {};
ldapModification[ldapUserAttr] = ldapUserSecret;
const ldapChange = new ldap.Change({operation: 'replace', modification: ldapModification});
modifyNext(records, ldapChange, 0);
}
});