-
Notifications
You must be signed in to change notification settings - Fork 8
/
capi.js
116 lines (92 loc) · 2.45 KB
/
capi.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2014, Joyent, Inc.
*/
/*
* UFDS CAPI main file.
*
* CAPI is the backwards compatible HTTP proxy for the UFDS LDAP server.
* It's used by SmartLogin for SSH'ing smart machines.
*/
var path = require('path');
var nopt = require('nopt');
var Logger = require('bunyan');
var restify = require('restify');
var CAPI = require('./capi/server.js');
///--- Globals
var opts = {
'certificate': String,
'config': String,
'debug': Boolean,
'file': String,
'key': String,
'port': Number,
'ufds': String,
'help': Boolean
};
var shortOpts = {
'c': ['--certificate'],
'd': ['--debug'],
'f': ['--file'],
'k': ['--key'],
'p': ['--port'],
'h': ['--help'],
'u': ['--ufds']
};
// --- Helpers
function usage(code, message) {
var _opts = '';
Object.keys(shortOpts).forEach(function (k) {
var longOpt = shortOpts[k][0].replace('--', '');
var type = opts[longOpt].name || 'string';
if (type && type === 'boolean') {
type = '';
}
type = type.toLowerCase();
_opts += ' [--' + longOpt + ' ' + type + ']';
});
_opts += ' dn attribute value(s)';
var msg = (message ? message + '\n' : '') +
'usage: ' + path.basename(process.argv[1]) + _opts;
process.stderr.write(msg + '\n');
process.exit(code);
}
function processConfig() {
var _config;
var parsed = nopt(opts, shortOpts, process.argv, 2);
var file = parsed.file || __dirname + '/etc/config.json';
if (parsed.help) {
usage(0);
}
try {
_config = CAPI.processConfigFile(file);
} catch (e) {
console.error('Unable to parse configuration file: ' + e.message);
process.exit(1);
}
if (parsed.debug) {
_config.logLevel = 'debug';
}
_config.port = (process.env.PORT) ? process.env.PORT : _config.port;
return _config;
}
///--- Mainline
(function main() {
var cfg = processConfig();
cfg.log = new Logger({
name: 'capi',
level: cfg.logLevel,
stream: process.stdout,
serializers: restify.bunyan.serializers
});
var capi = CAPI.createServer(cfg);
capi.connect(function (err) {
if (err) {
process.exit(1);
}
});
}());