Skip to content

Commit c8a6fde

Browse files
committed
Merge branch 'develop', prepare 3.2.0
2 parents c8c8c49 + 3f7195a commit c8a6fde

File tree

5 files changed

+795
-6
lines changed

5 files changed

+795
-6
lines changed

docs/Basics.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Config file has the following structure:
100100
},
101101
// rate-limit config
102102
// see "advanced topics" for more info
103-
"rate-limit": {
103+
"rateLimit": {
104104
// rate-limit time period
105105
"period": "1s",
106106
// request rate over given time period
@@ -110,7 +110,11 @@ Config file has the following structure:
110110
},
111111
// template to be used for project deployment
112112
// undefined by default, detected by server based on file structure
113-
"template": "my-template"
113+
"template": "my-template",
114+
// basic auth, [optional]
115+
// this field allows you to have basic auth to access your deployed service
116+
// format is in user:pwhash
117+
"basicAuth": "user:$apr1$$9Cv/OMGj$$ZomWQzuQbL.3TRCS81A1g/"
114118
}
115119
```
116120

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "exoframe",
3-
"version": "3.1.1",
3+
"version": "3.2.0-dev",
44
"description": "Exoframe is a self-hosted tool that allows simple one-command deployments using Docker",
55
"main": "index.js",
66
"repository": "git@github.com:exoframejs/exoframe.git",
@@ -15,6 +15,7 @@
1515
"package": "pkg --targets node8.11.3-linux-x64,node8.11.3-win-x64,node8.11.3-macos-x64 -o exoframe index.js"
1616
},
1717
"dependencies": {
18+
"apache-md5": "^1.1.2",
1819
"boxen": "^2.0.0",
1920
"chalk": "^2.4.1",
2021
"cli-table": "^0.3.1",

src/commands/config.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs');
33
const path = require('path');
44
const chalk = require('chalk');
55
const inquirer = require('inquirer');
6+
const md5 = require('apache-md5');
67

78
const validate = input => input && input.length > 0;
89
const filter = input => (input ? input.trim() : '');
@@ -45,6 +46,7 @@ exports.handler = async () => {
4546
average: 1,
4647
burst: 5,
4748
},
49+
basicAuth: ''
4850
};
4951
try {
5052
fs.statSync(configPath);
@@ -161,6 +163,45 @@ exports.handler = async () => {
161163
default: defaultConfig.template,
162164
filter,
163165
});
166+
prompts.push({
167+
type: 'confirm',
168+
name: 'basicAuth',
169+
message: 'Add a basic auth user? [optional]:',
170+
});
171+
172+
// prompts for recursive questions
173+
const recursivePrompts = []
174+
recursivePrompts.push({
175+
type: 'input',
176+
name: 'username',
177+
message: 'Username for Basic Auth:',
178+
filter,
179+
validate,
180+
});
181+
recursivePrompts.push({
182+
type: 'password',
183+
name: 'password',
184+
message: 'Password for Basic auth:',
185+
filter,
186+
validate,
187+
});
188+
recursivePrompts.push({
189+
type: 'confirm',
190+
name: 'askAgain',
191+
message: 'Add another user?',
192+
default: false,
193+
});
194+
195+
const askForUsers = async users => {
196+
const {username, password, askAgain} = await inquirer.prompt(recursivePrompts);
197+
users.push({username, password});
198+
if (askAgain) {
199+
return askForUsers(users);
200+
} else {
201+
return users;
202+
}
203+
};
204+
164205
// get values from user
165206
const {
166207
name,
@@ -175,7 +216,15 @@ exports.handler = async () => {
175216
hostname,
176217
restart,
177218
template,
219+
basicAuth,
178220
} = await inquirer.prompt(prompts);
221+
222+
const users = [];
223+
224+
if (basicAuth) {
225+
await askForUsers(users);
226+
}
227+
179228
// init config object
180229
const config = {name, restart};
181230
if (domain && domain.length) {
@@ -211,6 +260,13 @@ exports.handler = async () => {
211260
if (template && template.length) {
212261
config.template = template;
213262
}
263+
if (users.length !== 0) {
264+
config.basicAuth = users.reduce((acc, curr, index) => {
265+
const delimeter = users.length - 1 === index ? '' : ',';
266+
const pair = `${curr.username}:${md5(curr.password)}`;
267+
return `${acc}${pair}${delimeter}`;
268+
}, '');
269+
}
214270

215271
// write config
216272
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');

test/config.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const path = require('path');
88
const yaml = require('js-yaml');
99
const sinon = require('sinon');
1010
const inquirer = require('inquirer');
11+
const md5 = require('apache-md5');
1112

1213
// our packages
1314
const {handler: config} = require('../src/commands/config');
@@ -25,9 +26,26 @@ const configData = {
2526
ratelimitPeriod: 10,
2627
ratelimitAverage: 20,
2728
ratelimitBurst: 30,
29+
basicAuth: true,
2830
};
31+
const users = [{
32+
username: 'user1',
33+
password: 'pass',
34+
askAgain: true
35+
}, {
36+
username: 'user2',
37+
password: 'pass',
38+
askAgain: false
39+
}];
2940
const configPath = path.join(process.cwd(), 'exoframe.json');
3041

42+
const verifyBasicAuth = (input, actual) => {
43+
actual.split(',').forEach((element, index) => {
44+
const hash = element.split(':')[1]
45+
expect(hash).toEqual(md5(input[index].password, hash))
46+
});
47+
};
48+
3149
beforeAll(() => {
3250
try {
3351
fs.statSync(configPath);
@@ -40,7 +58,10 @@ beforeAll(() => {
4058
// test config generation
4159
test('Should generate config file', done => {
4260
// stup inquirer answers
43-
sinon.stub(inquirer, 'prompt').callsFake(() => Promise.resolve(configData));
61+
sinon.stub(inquirer, 'prompt')
62+
.onFirstCall().callsFake(() => Promise.resolve(configData))
63+
.onSecondCall().callsFake(() => Promise.resolve(users[0]))
64+
.onThirdCall().callsFake(() => Promise.resolve(users[1]));
4465
// spy on console
4566
const consoleSpy = sinon.spy(console, 'log');
4667
// execute login
@@ -64,6 +85,7 @@ test('Should generate config file', done => {
6485
average: configData.ratelimitAverage,
6586
burst: configData.ratelimitBurst,
6687
});
88+
verifyBasicAuth(users, cfg.basicAuth);
6789
// restore inquirer
6890
inquirer.prompt.restore();
6991
// restore console

0 commit comments

Comments
 (0)