forked from jhs/probe_couchdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
couch.js
331 lines (269 loc) · 10.9 KB
/
couch.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Probe all details about a CouchDB.
//
// Copyright 2011 Iris Couch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
require('defaultable')(module,
{ 'max_users': 1000
, 'url' : null
, 'do_dbs' : true
, 'do_users' : true
, 'do_pingquery': true
, 'only_dbs' : null
}, function(module, exports, DEFS, require) {
var lib = require('./lib')
, util = require('util')
, Emitter = require('./emitter').Emitter
, Database = require('./db').Database
;
module.exports = { "CouchDB": CouchDB
};
util.inherits(CouchDB, Emitter);
function CouchDB (url) {
var self = this;
Emitter.call(self, 'server');
self.url = url || DEFS.url || null;
self.do_dbs = DEFS.do_dbs;
self.only_dbs = DEFS.only_dbs || null;
self.max_users = DEFS.max_users;
self.on('start', function ping_root() {
self.log.debug("Pinging: " + self.url);
self.request({uri:self.url}, function(er, resp, body) {
if(er)
return self.x_emit('error', er);
else if(resp.statusCode !== 200 || body.couchdb !== "Welcome")
return self.x_emit('error', new Error("Bad welcome from " + self.url + ": " + JSON.stringify(body)));
else
self.x_emit('couchdb', body);
})
})
self.on('couchdb', function probe_databases(hello) {
if(!self.do_dbs) {
self.log.debug('Skipping db probe');
return self.x_emit('end_dbs');
}
var all_dbs = lib.join(self.url, '/_all_dbs');
self.log.debug("Scanning databases: " + all_dbs);
self.request({uri:all_dbs}, function(er, resp, body) {
if(er)
return self.x_emit('error', er);
if(resp.statusCode !== 200 || !Array.isArray(body))
return self.x_emit('error', new Error("Bad _all_dbs from " + all_dbs + ": " + JSON.stringify(body)));
self.log.debug(self.url + ' has ' + body.length + ' databases');
var db_names = body;
if(Array.isArray(self.only_dbs))
db_names = db_names.filter(function(name) { return ~ self.only_dbs.indexOf(name) });
else if(lib.isRegExp(self.only_dbs))
db_names = db_names.filter(function(name) { return self.only_dbs.test(name) });
self.x_emit('dbs', db_names);
})
})
self.on('dbs', function emit_db_probes(dbs) {
self.log.debug('Creating probes for ' + dbs.length + ' dbs');
if(dbs.length === 0) {
// Simply mark the dbs as done.
self.x_emit('end_dbs');
return;
}
// Track pending dbs to determine when all are done.
var pending_dbs = {};
// Avoid the warning for "too many" event listeners.
process.on('unused', function() {}); // This avoids an undefined reference exception.
process.setMaxListeners(dbs.length + 10);
dbs.forEach(function(db_name) {
var db = new Database;
db.couch = self
db.name = db_name;
pending_dbs[db.name] = db;
db.on('end', mark_db_done);
db.on('error', function(er) {
mark_db_done();
self.x_emit('error', er)
})
function mark_db_done() {
delete pending_dbs[db.name];
if(Object.keys(pending_dbs).length === 0)
self.x_emit('end_dbs');
}
process.on('exit', function() {
var names = Object.keys(pending_dbs);
if(names.length > 0) {
self.log.debug('Still have %d pending dbs', names.length)
}
})
self.x_emit('db', db);
db.start();
})
})
self.on('couchdb', function probe_session(hello) {
var session_url = lib.join(self.url, '/_session');
self.log.debug("Checking login session: " + session_url);
self.request({uri:session_url}, function(er, resp, session) {
if(er)
return self.x_emit('error', er);
if(resp.statusCode !== 200 || (!session) || session.ok !== true)
return self.x_emit('error', new Error("Bad _session from " + session_url + ": " + JSON.stringify(session)));
self.log.debug("Received session: " + JSON.stringify(session));
// Normalize the user_ctx for the user's convenience.
var normal_session = JSON.parse(JSON.stringify(session));
normal_session.userCtx = normal_session.userCtx || {};
normal_session.userCtx.name = normal_session.userCtx.name || null;
normal_session.userCtx.roles = normal_session.userCtx.roles || [];
if(!~ normal_session.userCtx.roles.indexOf('_admin'))
self.log.debug("Results will be incomplete without _admin access");
self.x_emit('session', normal_session, session);
})
})
self.on('couchdb', function(hello) {
var config_url = lib.join(self.url, '/_config');
self.log.debug("Checking config: " + config_url);
self.request({uri:config_url}, function(er, resp, config) {
if(er)
return self.x_emit('error', er);
if(resp.statusCode !== 200 || (typeof config !== 'object')) {
self.log.debug("Bad config response: " + JSON.stringify(config));
config = null;
}
self.x_emit('config', config);
})
})
// Probe the user accounts.
self.on('config', function(config) {
var all_users = {};
// Of course, the anonymous user is always known to exist.
all_users[null] = self.anonymous_user();
if(!DEFS.do_users) {
self.log.debug('Skipping user probe: disabled by config');
return self.x_emit('users', all_users);
}
// Once the config is known, the list of users can be established.
var auth_db = config && config.couch_httpd_auth && config.couch_httpd_auth.authentication_db;
if(!auth_db) {
auth_db = '_users';
self.log.debug('authentication_db not found in config; trying ' + JSON.stringify(auth_db));
}
var auth_db_url = lib.join(self.url, encodeURIComponent(auth_db).replace(/^_design%2[fF]/, '_design/'));
self.log.debug("Checking auth_db: " + auth_db_url);
self.request({uri:auth_db_url}, function(er, resp, body) {
if(er)
return self.x_emit('error', er);
if(resp.statusCode !== 200 || typeof config !== 'object') {
self.log.debug("Can not access authentication_db: " + auth_db_url);
self.x_emit('users', all_users);
} else if(body.doc_count > self.max_users) {
// TODO
return self.x_emit('error', new Error('User count maximum ('+self.max_users+') is insufficent for this server: '+body.doc_count+' users'));
}
// Looks good. Get all the users.
var users_query = lib.join(auth_db_url, '/_all_docs'
+ '?include_docs=true'
+ '&startkey=' + encodeURIComponent(JSON.stringify("org.couchdb.user:"))
// CouchDB 1.1.0 has a bug preventing a "raw" scan from ':' to ';', so just
// assume that the only documents are well-formed and filter on the client side.
//+ '&endkey=' + encodeURIComponent(JSON.stringify("org.couchdb.user;"))
);
self.log.debug("Fetching all users: " + users_query);
self.request({uri:users_query}, function(er, resp, body) {
if(er)
return self.x_emit('error', er);
if(resp.statusCode !== 200 || !Array.isArray(body.rows))
return self.x_emit('error', new Error("Failed to fetch user listing from " + users_query + ": " + JSON.stringify(body)));
body.rows.forEach(function(row) {
if(!! row.id.match(/^org\.couchdb\.user:/))
all_users[row.id] = row.doc;
})
self.log.debug("Found " + Object.keys(all_users).length + " users (including anonymous): " + auth_db_url);
self.x_emit('users', all_users);
})
})
})
self.known('session', function(session) {
if(!DEFS.do_pingquery) {
self.log.debug('Skipping QS ping: disabled by config');
return self.x_emit('end_pings');
}
if(!~ session.userCtx.roles.indexOf('_admin')) {
self.log.debug('Skipping QS ping: not an _admin session');
return self.x_emit('end_pings');
}
self.known('config', function(config) {
if(!config || !config.httpd_global_handlers || !config.query_servers) {
self.log.debug('Skipping QS ping: bad config');
return self.x_emit('end_pings');
}
var ping_path = null;
Object.keys(config.httpd_global_handlers).forEach(function(path) {
var has_plugin_re = /^\s*{\s*pingquery_couchdb\s*,\s*handle_pingquery_req\s*}\s*$/;
if(config.httpd_global_handlers[path].match(has_plugin_re))
ping_path = path;
})
if(!ping_path) {
return self.log.debug('Skipping QS ping: no pingquery plugin');
return self.x_emit('end_pings');
}
var languages = Object.keys(config.query_servers);
var langs_todo = languages.length;
function did(er, language) {
langs_todo -= 1;
if(langs_todo == 0)
self.x_emit('end_pings');
if(er)
self.x_emit('error', er);
}
var supported = { javascript : "function() { return (typeof log).replace(/^func/, 'ac') }"
, coffeescript: "() -> (typeof log).replace /^func/, 'ac'"
}
languages.forEach(function(language) {
var ping = { 'in':supported[language], 'out':"action" };
if(!ping.in) {
self.log.debug('Skipping ping unsupported QS language: ' + language);
return did(null, language);
}
var req = { method:'POST'
, 'uri':lib.join(self.url, ping_path, language)
, 'body':JSON.stringify(ping)
}
self.log.debug('Pinging QS language: '+language);
self.request(req, function(er, resp, body) {
if(!er)
self.x_emit('pingquery', language, body);
did(er, language);
})
})
})
})
self.known('couchdb', function(welcome) {
self.known('users', function(users) {
self.known('session', function(session) {
self.known('config', function(config) {
self.known('end_dbs', function() {
self.known('end_pings', function() {
self.x_emit('end');
})
})
})
})
})
})
} // CouchDB
CouchDB.prototype.start = function() {
var self = this;
if(!self.url)
throw new Error("url required");
self.x_emit('start');
}
CouchDB.prototype.anonymous_user = function() {
var self = this;
return { name:null, roles: [] };
}
}) // defaultable