forked from uber-archive/canduit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canduit.js
175 lines (148 loc) · 4.57 KB
/
canduit.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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
var request = require('request');
module.exports = createCanduit;
function createCanduit (opts, cb) {
if (!cb) {
cb = opts;
opts = { };
}
return new Canduit(opts, cb);
}
function Canduit (opts, cb) {
this.client = opts.client || 'canduit';
this.logger = opts.logger || {
log: function silent () { }
};
this.api = opts.api;
this.user = opts.user;
this.cert = opts.cert;
this.token = opts.token;
this.configFile = opts.configFile;
if (!this.configFile) {
if (process.env.HOME) {
this.configFile = path.join(process.env.HOME, '.arcrc');
} else {
var noHomeEnvError = new Error(
'Unable to find $HOME/.arcrc: ' +
'$HOME env variable is not defined.'
);
return cb(noHomeEnvError, null);
}
}
var self = this;
if (!this.api) {
this.parseConfigFile(function (err) {
if (err) return cb(err, null);
self.authenticate(cb);
});
} else {
self.authenticate(cb);
}
}
Canduit.conduitError = function conduitError (data) {
var err = new Error(data.error_info);
err.code = data.error_code;
return err;
};
Canduit.serverError = function serverError (response) {
var err = new Error(response.body &&
response.body.toString());
err.code = response.statusCode;
return err;
};
Canduit.prototype.parseConfigFile = function parseConfigFile (cb) {
var self = this;
fs.readFile(this.configFile, function (err, data) {
if (err) return cb(err, null);
try {
var arcrc = JSON.parse(data);
var host = Object.keys(arcrc.hosts)[0];
self.user = arcrc.hosts[host].user;
self.cert = arcrc.hosts[host].cert;
self.token = arcrc.hosts[host].token;
self.api = host;
cb(null);
} catch (e) {
cb(e, null);
}
});
};
Canduit.prototype.createRequest = function createRequest (route, params, cb) {
if (this.session) {
params.__conduit__ = this.session;
} else if (this.token) {
params.__conduit__ = { token: this.token };
}
var req = request.post(this.api + route, {
json: true,
form: {
output: 'json',
params: JSON.stringify(params)
}
}, cb);
this.logger.log('POST to %s with %s',
this.api + route, (req.body || '').toString());
return req;
};
Canduit.prototype.exec = function exec (route, params, cb) {
var logger = this.logger;
var req = this.createRequest(route, params || {}, processResponse);
function processResponse(error, response, data) {
if (error) return cb(error, null);
if (response.statusCode >= 400) {
return cb(Canduit.serverError(response), null);
}
if (data.result) {
data = data.result;
}
if (data.error_info) {
return cb(Canduit.conduitError(data), null);
}
logger.log('%s responded with %s',
req.href, JSON.stringify(data));
cb(null, data);
}
return req;
};
Canduit.prototype.authenticate = function authenticate (cb) {
if (!this.cert || this.token) return cb(null, this);
var authToken = Date.now() / 1000;
var authSignature = crypto
.createHash('sha1')
.update(authToken + this.cert)
.digest('hex');
var self = this;
this.exec('conduit.connect', {
user: self.user,
host: self.host,
client: self.client,
authToken: authToken,
authSignature: authSignature
}, function (err, result) {
if (err) return cb(err, null);
self.session = result;
return cb(null, self);
});
return this;
};