-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
137 lines (117 loc) · 4.21 KB
/
generate.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
const { Client } = require('ldapts');
const faker = require('faker');
const args = require('args');
args
.option('url', 'The URL of the target LDAP server', 'ldaps://localhost:636')
.option('basePath', 'The LDAP base path used in the directory', 'dc=example, dc=org')
.option('bindUser', 'The DN of the bind user with write permissions', 'cn=admin,dc=example,dc=org')
.option('bindPassword', 'The password of the bind user', 'admin')
.option('numberOfSchools', 'The number of schools to create', 5)
.option('numberOfUsers', 'The number of users to create (per school)', 100)
.option('numberOfClasses', 'the number of classes to create (per school)', 10)
const options = args.parse(process.argv);
currentId = 0;
const getId = () => {
currentId += 1;
return currentId;
}
(async () => {
const client = new Client({
url: options.url,
timeout: 0,
connectTimeout: 0,
tlsOptions: {
minVersion: 'TLSv1.2',
rejectUnauthorized: false
},
strictDN: true,
});
const createDirectory = async (name, base) => {
await client.add(`ou=${name}, ${base ? base + ',' : ''} ${options.basePath}`, {
objectClass: ['top', 'organizationalUnit'],
ou: name,
});
}
const createSchool = async (name, domain) => {
const entry = {
objectClass: ['top', 'organizationalUnit'],
ou: domain,
description: name,
}
await client.add(`ou=${domain}, ${options.basePath}`, entry);
}
const createGroup = async (name, members=[], base='ou=groups') => {
const entry = {
objectClass: ['top', 'posixGroup'],
cn: name,
gidNumber: `${getId()}`,
}
if (members.length > 0) {
entry.memberUid = members;
}
await client.add(`cn=${name}, ${base ? base + ',' : ''} ${options.basePath}`, entry);
}
const createUser = async (person, base) => {
const username = `${person.firstName.toLowerCase()}.${person.lastName.toLowerCase()}`;
const entry = {
cn: username,
givenName: person.firstName,
sn: person.lastName,
uidNumber: `${person.id}`,
gidNumber: `${person.id}`,
homeDirectory: `/home/${username}/`,
uid: username,
mail: `${username}@example.org`,
objectClass: ['person', 'inetOrgPerson', 'posixAccount'],
};
const dn = `uid=${username}, ou=users, ${base ? base + ',' : ''} ${options.basePath}`;
await client.add(dn, entry);
return dn;
}
try {
await client.bind(options.bindUser, options.bindPassword);
for (let schoolId = 0; schoolId < options.numberOfSchools; schoolId += 1) {
const domain = `school${schoolId}.de`;
const base = `ou=${domain}`;
await createSchool(`LDAP Test School #${schoolId}`, domain);
await Promise.all(['users', 'roles', 'groups'].map((dir) => createDirectory(dir, base)));
const students = [];
const teachers = [];
const admins = [];
const ignored = [];
for (let i = 0; i < options.numberOfUsers; i += 1) {
const dn = await createUser({
id: getId(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
}, base);
const r = Math.random();
if (r < 0.1 && admins.length < 10) { admins.push(dn); continue; }
if (r < 0.1 && ignored.length < 10) { ignored.push(dn); continue; }
if (r < 0.2) { teachers.push(dn); continue; }
students.push(dn);
}
await Promise.all([
createGroup('admins', admins, `ou=roles,${base}`),
createGroup('teachers', teachers, `ou=roles,${base}`),
createGroup('students', students, `ou=roles,${base}`),
createGroup('ignored', ignored, `ou=roles,${base}`),
]);
const classUsers = teachers.concat(teachers).concat(ignored);
const randomUser = () => classUsers[Math.floor(Math.random() * classUsers.length)];
for (let i = 0; i < options.numberOfClasses; i += 1) {
const name = faker.company.catchPhrase();
const members = new Set();
const numberOfUsers = Math.floor(Math.random() * 20) + 5;
for (let j = 0; j < numberOfUsers; j += 1) {
members.add(randomUser());
}
await createGroup(name, Array.from(members), `ou=groups,${base}`);
}
}
} catch (err) {
console.log(err);
} finally {
await client.unbind();
}
})()