Skip to content

Commit 03eee3d

Browse files
committed
prompting example setup
1 parent bda992b commit 03eee3d

File tree

10 files changed

+436
-117
lines changed

10 files changed

+436
-117
lines changed

etc/test-config.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ var restReaderPassword = 'x';
3030
var restWriterUser = 'rest-writer';
3131
var restWriterPassword = 'x';
3232

33-
var manageUser = 'manage-admin';
34-
var managePassword = 'x';
35-
3633
var testServerName = 'unittest-nodeapi';
3734

3835
// For SSL without client cert, use rejectUnauthorized: false
@@ -61,18 +58,11 @@ module.exports = {
6158
password: restWriterPassword,
6259
authType: restAuthType
6360
},
64-
bootstrapConnection: {
61+
manageAdminConnection: {
6562
host: testHost,
6663
port: managePort,
6764
user: restAdminUser,
6865
password: restAdminPassword,
6966
authType: manageAuthType
70-
},
71-
manageAdminConnection: {
72-
host: testHost,
73-
port: managePort,
74-
user: manageUser,
75-
password: managePassword,
76-
authType: manageAuthType
7767
}
7868
};

etc/test-setup-prompt.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var read = require("read");
17+
var mlutil = require('../lib/mlutil.js');
18+
19+
function adminUserPrompt() {
20+
var self = this;
21+
if (self.user === null) {
22+
read({
23+
prompt: 'admin user (default=admin): '
24+
},
25+
mlutil.callbackOn(self, adminUserCallback)
26+
);
27+
} else {
28+
self.passwordPrompt();
29+
}
30+
}
31+
function adminUserCallback(error, result) {
32+
if (error) {
33+
console.log(error);
34+
process.exit(1);
35+
}
36+
this.user = (result === '') ? 'admin' : result;
37+
this.passwordPrompt();
38+
}
39+
function adminPasswordPrompt() {
40+
var self = this;
41+
if (self.password === null) {
42+
read({
43+
prompt: (self.user === 'admin') ?
44+
'admin password (default=admin): ' : 'admin password: ',
45+
silent: true,
46+
replace: '*',
47+
edit: false
48+
},
49+
mlutil.callbackOn(self, adminPasswordCallback)
50+
);
51+
} else {
52+
self.finish();
53+
}
54+
}
55+
function adminPasswordCallback(error, result) {
56+
if (error) {
57+
console.log(error);
58+
process.exit(1);
59+
}
60+
if (result === '') {
61+
if (this.user === 'admin') {
62+
this.password = 'admin';
63+
} else {
64+
console.log('no admin password specified, so cannot setup');
65+
process.exit(1);
66+
}
67+
} else {
68+
this.password = result;
69+
}
70+
this.finish();
71+
}
72+
function adminFinish() {
73+
this.done(this.user, this.password);
74+
}
75+
76+
function AdminPrompter(done) {
77+
this.done = done;
78+
this.user = null;
79+
this.password = null;
80+
}
81+
AdminPrompter.prototype.userPrompt = adminUserPrompt;
82+
AdminPrompter.prototype.userCallback = adminUserCallback;
83+
AdminPrompter.prototype.passwordPrompt = adminPasswordPrompt;
84+
AdminPrompter.prototype.passwordCallback = adminPasswordCallback;
85+
AdminPrompter.prototype.finish = adminFinish;
86+
87+
function promptForAdmin(done) {
88+
var prompter = new AdminPrompter(done);
89+
90+
var argvLen = process.argv.length;
91+
if (argvLen >= 4) {
92+
var argvMax = argvLen - 1;
93+
for (var argvI=2; argvI < argvMax; argvI++) {
94+
var argvVal = process.argv[argvI];
95+
if (argvVal === '-u') {
96+
argvVal = process.argv[argvI + 1];
97+
var argvSep = argvVal.indexOf(':');
98+
if (argvSep < 0) {
99+
prompter.user = argvVal;
100+
break;
101+
}
102+
if (argvSep > 0) {
103+
prompter.user = argvVal.substring(0, argvSep);
104+
}
105+
if (argvSep < (argvVal.length - 1)) {
106+
prompter.password = argvVal.substring(argvSep + 1);
107+
}
108+
break;
109+
} else if (argvVal === '-h') {
110+
console.log('usage: '+process.argv[1]+' [-u adminUser:adminPassword]');
111+
console.log('without -u, prompts for admin user and/or admin password');
112+
process.exit();
113+
}
114+
};
115+
}
116+
117+
prompter.userPrompt();
118+
}
119+
120+
module.exports = promptForAdmin;

etc/test-setup-users.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var valcheck = require('core-util-is');
17+
18+
var testlib = require('./test-lib.js');
19+
var testconfig = require('./test-config.js');
20+
21+
function setupUsers(manager, done) {
22+
console.log('checking for REST users');
23+
manager.get({
24+
endpoint: '/manage/v2/users'
25+
}).
26+
result(function(response) {
27+
var userName = null;
28+
29+
var requiredUsers = {};
30+
userName = testconfig.restAdminConnection.user;
31+
requiredUsers[userName] = {
32+
role: 'rest-admin',
33+
name: userName,
34+
description: 'rest-admin user',
35+
password: testconfig.restAdminConnection.password
36+
};
37+
userName = testconfig.restReaderConnection.user;
38+
requiredUsers[userName] = {
39+
role: 'rest-reader',
40+
name: userName,
41+
description: 'rest-reader user',
42+
password: testconfig.restReaderConnection.password
43+
};
44+
userName = testconfig.restWriterConnection.user;
45+
requiredUsers[userName] = {
46+
role: 'rest-writer',
47+
name: userName,
48+
description: 'rest-writer user',
49+
password: testconfig.restWriterConnection.password
50+
};
51+
52+
response.data['user-default-list']['list-items']['list-item'].
53+
forEach(function(user) {
54+
userName = user.nameref;
55+
if (!valcheck.isUndefined(requiredUsers[userName])) {
56+
requiredUsers[userName] = undefined;
57+
}
58+
});
59+
60+
var missingUsers = Object.keys(requiredUsers).map(function(key) {
61+
var user = requiredUsers[key];
62+
if (!valcheck.isUndefined(user)) {
63+
return user;
64+
}
65+
}).
66+
filter(function(user) {
67+
return !valcheck.isUndefined(user);
68+
});
69+
70+
addUser(manager, missingUsers, 0, done);
71+
});
72+
}
73+
74+
function addUser(manager, users, next, done) {
75+
if (next >= users.length) {
76+
if (next > 0) {
77+
console.log('finished adding REST users');
78+
} else {
79+
console.log('REST users already exist');
80+
}
81+
82+
if (!valcheck.isUndefined(done)) {
83+
done(manager);
84+
}
85+
86+
return;
87+
} else if (next === 0) {
88+
console.log('adding REST users');
89+
}
90+
91+
var userdef = users[next];
92+
manager.post({
93+
endpoint: '/manage/v2/users',
94+
body: {
95+
name: userdef.name,
96+
password: userdef.password,
97+
description: userdef.description,
98+
role: [
99+
userdef.role
100+
]
101+
}
102+
}).
103+
result(function(response) {
104+
if (response.statusCode < 400) {
105+
addUser(manager, users, next + 1, done);
106+
} else {
107+
console.log('REST user setup failed with HTTP status: '+response.statusCode);
108+
console.log(response.data);
109+
process.exit();
110+
}
111+
});
112+
}
113+
114+
module.exports = setupUsers;

0 commit comments

Comments
 (0)