-
Notifications
You must be signed in to change notification settings - Fork 26
/
server.ts
125 lines (102 loc) · 3.06 KB
/
server.ts
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
#!/usr/bin/env node
import * as fs from 'node:fs/promises';
import path from 'node:path';
import { LdapServerMockConfiguration } from './lib/configuration';
import { LdapServerMock } from './lib/server';
import { LdapUser } from './lib/user';
type Arguments = { readonly confFilePath: string; readonly databaseFilePath: string };
/**
* Exits program and print help.
*
* @param error The error message to print before printing help
* @param code The exit code
*/
function exit(error?: string, code = 1): void {
if (error) {
console.log('error: %s', error);
}
printHelp();
process.exitCode = 1;
}
/**
* Parses command line arguments.
*
* @return args The list of parsed arguments
*/
function getArguments(): Arguments {
let confFilePath: string = '';
let databaseFilePath: string = '';
for (let i = 2; i < process.argv.length; i++) {
const argChunks: RegExpMatchArray | null = process.argv[i].match(/--(conf|database)=(.*)/);
if (!argChunks) {
break;
}
switch (argChunks[1]) {
case 'conf':
confFilePath = path.resolve(argChunks[2]);
break;
case 'database':
databaseFilePath = path.resolve(argChunks[2]);
break;
default:
throw new Error(`unexpected option ${argChunks[1]}`);
break;
}
}
if (!confFilePath) {
throw new Error('missing --conf option');
}
if (!databaseFilePath) {
throw new Error('missing --database option');
}
return { confFilePath, databaseFilePath };
}
async function main(): Promise<void> {
let args: Arguments | undefined;
try {
args = getArguments();
} catch (error) {
exit((error as Error).message);
return;
}
let configurationFilePath: string = args.confFilePath;
let databaseFilePath: string = args.databaseFilePath;
process.on('SIGTERM', async () => {
if (server) {
await server.stop();
}
});
// Load configuration files
let serverConfiguration: LdapServerMockConfiguration;
let users: LdapUser[] = [];
try {
serverConfiguration = require(configurationFilePath);
users = require(databaseFilePath);
} catch (error) {
exit((error as Error).message);
return;
}
// Load certificate's files
let certificatePublicKey: Buffer | undefined;
let certificatePrivateKey: Buffer | undefined;
if (serverConfiguration.certPath && serverConfiguration.certKeyPath) {
try {
certificatePublicKey = await fs.readFile(path.resolve(serverConfiguration.certPath));
certificatePrivateKey = await fs.readFile(path.resolve(serverConfiguration.certKeyPath));
} catch (error) {
exit((error as Error).message);
return;
}
}
const server: LdapServerMock = new LdapServerMock(users, serverConfiguration, certificatePublicKey, certificatePrivateKey);
await server.start();
}
function printHelp(): void {
console.log(`
usage: npx ldap-server-mock options
options:
--conf="file" relative or absolute path to the server configuration file
--database="file" relative or absolute path to the file containing LDAP users in JSON format
`);
}
main();