Skip to content

Commit 029598b

Browse files
author
Hadrien Jouet
committed
basic user handling, auth and permissions
1 parent 25c403c commit 029598b

File tree

7 files changed

+551
-36
lines changed

7 files changed

+551
-36
lines changed

README.md

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ It makes running a Node deployment server as painless as possible.
88

99
## How does it work?
1010

11-
After starting Ishiki, an [API](#api) will be made available. With this API, you can deploy applications and manage
11+
After starting Ishiki, an [API](#api) will become available. With this API, you can deploy applications and manage
1212
them. If your application requires a specific version of Node, it will be set up automatically for you. Each application
1313
will run on its own IP:port internally, while being proxied through the domains specified on your app on whatever public
1414
port you want your sites to run on (e.g. 80).
@@ -39,6 +39,19 @@ Usage:
3939
ishiki
4040
```
4141

42+
### First time
43+
44+
When starting Ishiki for the first time, a default admin user will be created for you and a random password will be generated.
45+
Ishiki should output something along the lines of:
46+
47+
```
48+
Initial admin account created:
49+
> username: ishiki
50+
> password: 12345667890abcdef
51+
```
52+
53+
Make sure you take good note of the password (you can change it later).
54+
4255
## Configuration
4356

4457
By default, Ishiki will run on the following settings:
@@ -59,6 +72,11 @@ By default, Ishiki will run on the following settings:
5972
"database": "ishiki"
6073
},
6174
"logs-size": 100000,
75+
"auth": {
76+
"active": true,
77+
"admin": "ishiki",
78+
"token_expiry": 1800
79+
},
6280
"haibu": {
6381
"env": "development",
6482
"advanced-replies": true,
@@ -74,7 +92,7 @@ By default, Ishiki will run on the following settings:
7492
}
7593
```
7694

77-
Copy `config.sample.js` to `config.js` and modify if you want your own settings.
95+
Copy `config.sample.js` to `config.js` and modify it if you want your own settings.
7896

7997
* `host` is the host Ishiki and its API will run on
8098
* `port` is the port Ishiki and its API will run on
@@ -83,6 +101,7 @@ Copy `config.sample.js` to `config.js` and modify if you want your own settings.
83101
* `port-range` is the range of ports the apps will listen on internally before being proxied
84102
* `mongodb` is the configuration for the MongoDB database
85103
* `logs-size` is the cap on the `log` MongoDB collection where all the user/app logs go
104+
* `auth` is for authentication. Set `active` to `false` to disable authentication, `admin` is the default admin username, `token_expiry` is the time in seconds a token can remain valid without activity (`false` for no expiry)
86105
* `haibu` is whatever settings are available to the haibu module
87106

88107
__Running Ishiki over HTTPS__
@@ -105,16 +124,102 @@ the `-k` (or `--insecure`) flag to ignore the verification.
105124
<a name="api"/>
106125
## API
107126

108-
Ishiki provides its own API
127+
Ishiki provides its own API.
128+
129+
With authentication turned on (default), all calls (except for `/users/login`) will need to explicitly specify a `token` in the URL, such as:
130+
```bash
131+
<http|https>://<ishiki-ip>:<ishiki-port>/<end-point>?token=<my-token>
132+
```
133+
134+
The authentication token can be created with the help of [`/users/login`](#login)
135+
136+
#### _Permissions_
137+
With the exception of logging in, permissions are as follow:
138+
* [__users__](#users): admins can perform any action for any user, non-admins can only update their own password
139+
* [__drones__](#drones): admins can performs any action for any user, non-admins can only perform actions relating to their own drones (where `/:userid` is present)
140+
* [__proxies__](#proxies): only admins may use this
141+
142+
<a name="users"/>
143+
### Users
144+
145+
#### `/users` (`GET`)
146+
Returns a list of all users
147+
148+
#### Call example
149+
```bash
150+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/users?token=<my-token>
151+
```
152+
153+
#### Response
154+
```json
155+
[ { "_id" : "51b12470b4a898d990000001",
156+
"admin" : true,
157+
"last_access" : "2013-06-08T22:47:22.828Z",
158+
"password" : "$2a$10$TtuNxZzX3bHdQSURpLLv4OHZ1QjbW2Fy6yRs3Cv1p6w414OnoOnTi",
159+
"token" : "d22b9961e33700436c76acfab2051ba73276b7fb5aa9e57bb1343fc9e5b1524f",
160+
"username" : "ishiki"
161+
} ]
162+
```
163+
164+
---
165+
166+
#### `/users` (`POST`)
167+
Creates a new user, if `password` is not provided, one will be generated. Set `admin` to `true` to give the new user admin rights.
168+
169+
##### Call example
170+
```bash
171+
curl -X POST -H 'Content-Type: application/json' -d '{"username": "myuser"}' <http|https>://<ishiki-ip>:<ishiki-port>/users?token=<my-token>
172+
```
173+
174+
##### Response
175+
```json
176+
{ "_id" : "51b390b90808e68d93000067",
177+
"admin" : false,
178+
"password" : "52360f1b10488ae7",
179+
"username" : "myuser"
180+
}
181+
```
182+
183+
---
184+
185+
<a name="login"/>
186+
#### `/users/login` (`POST`)
187+
Returns an authentication token to be used for all other calls
188+
189+
##### Call example
190+
```bash
191+
curl -X POST -H 'Content-Type: application/json' -d '{"username": "myuser", "password": "mypassword"}' <http|https>://<ishiki-ip>:<ishiki-port>/users/login
192+
```
193+
194+
##### Response
195+
```json
196+
{ "token" : "f2623f7d089e58069caf123bda4eba614b30b67e20f90074bf7dfd6241e2e0e1" }
197+
```
198+
199+
---
200+
201+
#### `/users/:userid` (`POST`)
202+
Updates a user, non-admin users can only update their own password, admins can update any details of any users with the exception of the `username`
203+
204+
##### Call example
205+
```bash
206+
curl -X POST -H 'Content-Type: application/json' -d '{"password": "mynewpassword"}' <http|https>://<ishiki-ip>:<ishiki-port>/users/myuser?token=<my-token>
207+
```
208+
209+
#### Response
210+
```json
211+
{ "message" : "Updated password" }
212+
```
109213

214+
<a name="drones"/>
110215
### Drones
111216

112217
#### `/drones` (`GET`)
113218
Returns a list of all drones
114219

115220
##### Call example
116221
```bash
117-
curl -X GET <ishiki-ip>:<ishiki-port>/drones
222+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/drones?token=<my-token>
118223
```
119224

120225
##### Response
@@ -152,7 +257,7 @@ Returns all drones for a given user
152257

153258
##### Call example
154259
```bash
155-
curl -X GET <ishiki-ip>:<ishiki-port>/drones/user1
260+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1?token=<my-token>
156261
```
157262

158263
##### Response
@@ -165,7 +270,7 @@ Returns drone info for given user/app
165270

166271
##### Call example
167272
```bash
168-
curl -X GET <ishiki-ip>:<ishiki-port>/drones/user1/site1
273+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1?token=<my-token>
169274
```
170275

171276
##### Response
@@ -178,7 +283,7 @@ Returns all running drones
178283

179284
##### Call example
180285
```bash
181-
curl -X GET <ishiki-ip>:<ishiki-port>/drones/running
286+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/drones/running?token=<my-token>
182287
```
183288

184289
##### Response
@@ -191,7 +296,7 @@ Deploys an app from a tarball for given user/app, with Curl from your app's dire
191296

192297
##### Call example
193298
```bash
194-
tar -cz . | curl -XPOST -m 360 -sSNT- <ishiki-ip>:<ishiki-port>/drones/user1/site1/deploy
299+
tar -cz . | curl -XPOST -m 360 -sSNT- <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1/deploy?token=<my-token>
195300
```
196301

197302
##### Response
@@ -208,7 +313,7 @@ Starts a previously stopped drone for given user/app
208313

209314
##### Call example
210315
```bash
211-
curl -X POST <ishiki-ip>:<ishiki-port>/drones/user1/site1/start
316+
curl -X POST <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1/start?token=<my-token>
212317
```
213318

214319
##### Response
@@ -244,7 +349,7 @@ Stops a running drone for given user/app
244349

245350
##### Call example
246351
```bash
247-
curl -X POST <ishiki-ip>:<ishiki-port>/drones/user1/site1/stop
352+
curl -X POST <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1/stop?token=<my-token>
248353
```
249354

250355
##### Response
@@ -257,7 +362,7 @@ Restarts a running drone for given user/app
257362

258363
##### Call example
259364
```bash
260-
curl -X POST <ishiki-ip>:<ishiki-port>/drones/user1/site1/restart
365+
curl -X POST <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1/restart?token=<my-token>
261366
```
262367

263368
##### Response
@@ -275,7 +380,7 @@ Returns or streams the logs for a given app with optional filtering
275380

276381
##### Call example - basic
277382
```bash
278-
curl -X GET -H 'Content-Type: application/json' -d '{"limit": 2}' <ishiki-ip>:<ishiki-port>/drones/user1/site1/logs
383+
curl -X GET -H 'Content-Type: application/json' -d '{"limit": 2}' <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1/logs?token=<my-token>
279384
```
280385

281386
##### Response (JSON)
@@ -299,7 +404,7 @@ curl -X GET -H 'Content-Type: application/json' -d '{"limit": 2}' <ishiki-ip>:<i
299404

300405
##### Call example - streaming
301406
```bash
302-
curl -X GET -H 'Content-Type: application/json' -d '{"stream": true}' <ishiki-ip>:<ishiki-port>/drones/user1/site1/logs
407+
curl -X GET -H 'Content-Type: application/json' -d '{"stream": true}' <http|https>://<ishiki-ip>:<ishiki-port>/drones/user1/site1/logs?token=<my-token>
303408
```
304409

305410
##### Response (plain text)
@@ -309,14 +414,15 @@ curl -X GET -H 'Content-Type: application/json' -d '{"stream": true}' <ishiki-ip
309414
...
310415
```
311416

417+
<a name="proxies"/>
312418
### Proxy
313419

314420
#### `/proxies` (`GET`)
315421
Returns a list of all proxies and associated routes
316422

317423
##### Call example
318424
```bash
319-
`curl -X GET <ishiki-ip>:<ishiki-port>/proxies`
425+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/proxies?token=<my-token>
320426
```
321427

322428
##### Response
@@ -348,7 +454,7 @@ Returns a list of all routes for proxy on given port
348454

349455
##### Call example
350456
```bash
351-
curl -X GET <ishiki-ip>:<ishiki-port>/proxies/80
457+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/proxies/80?token=<my-token>
352458
```
353459

354460
##### Response example
@@ -376,7 +482,7 @@ Starts a proxy on given port
376482

377483
##### Call example
378484
```bash
379-
curl -X POST <ishiki-ip>:<ishiki-port>/proxies/1234
485+
curl -X POST <http|https>://<ishiki-ip>:<ishiki-port>/proxies/1234?token=<my-token>
380486
```
381487

382488
##### Response
@@ -392,7 +498,7 @@ and target `host` and `port` provided in `POST`. Routes created like this will b
392498

393499
##### Call example
394500
```bash
395-
curl -X POST -H 'Content-Type: application/json' -d '{"port": "12500","host": "internal.ip","domain": "my.domain"}' <ishiki-ip>:<ishiki-port>/proxies/80/set
501+
curl -X POST -H 'Content-Type: application/json' -d '{"port": "12500","host": "internal.ip","domain": "my.domain"}' <http|https>://<ishiki-ip>:<ishiki-port>/proxies/80/set?token=<my-token>
396502
```
397503

398504
##### Response
@@ -407,7 +513,7 @@ Stops and removes proxy and associated routes on given port
407513

408514
##### Call example
409515
```bash
410-
curl -X POST <ishiki-ip>:<ishiki-port>/proxies/1234/delete_proxy
516+
curl -X POST <http|https>://<ishiki-ip>:<ishiki-port>/proxies/1234/delete_proxy?token=<my-token>
411517
```
412518

413519
##### Response
@@ -432,7 +538,7 @@ In this case `POST` can have any of the following values for matching:
432538

433539
##### Call example
434540
```bash
435-
curl -X POST -H 'Content-Type: application/json' -d '{"domain":"my.domain"}' <ishiki-ip>:<ishiki-port>/proxies/1234/delete_route
541+
curl -X POST -H 'Content-Type: application/json' -d '{"domain":"my.domain"}' <http|https>://<ishiki-ip>:<ishiki-port>/proxies/1234/delete_route?token=<my-token>
436542
```
437543

438544
##### Response
@@ -447,7 +553,7 @@ Returns all routes for given user for proxy on given port
447553

448554
##### Call example
449555
```bash
450-
curl -X GET <ishiki-ip>:<ishiki-port>/proxies/80/user1
556+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/proxies/80/user1?token=<my-token>
451557
```
452558

453559
##### Response
@@ -475,7 +581,7 @@ Returns all routes for given user/app for proxy on given port
475581

476582
##### Call example
477583
```bash
478-
curl -X GET <ishiki-ip>:<ishiki-port>/proxies/80/user1/site1
584+
curl -X GET <http|https>://<ishiki-ip>:<ishiki-port>/proxies/80/user1/site1?token=<my-token>
479585
```
480586

481587
##### Response
@@ -497,7 +603,7 @@ Deletes route for given user/app for proxy on given port
497603

498604
##### Call example
499605
```bash
500-
curl -X POST <ishiki-ip>:<ishiki-port>/proxies/80/user1/site1/delete
606+
curl -X POST <http|https>://<ishiki-ip>:<ishiki-port>/proxies/80/user1/site1/delete?token=<my-token>
501607
```
502608

503609
##### Response
@@ -522,11 +628,12 @@ Ishiki will use one of the ports within the proxy port range defined in your con
522628

523629
* [union (0.3.6)](https://github.com/flatiron/union/tree/v0.3.6)
524630
* [flatiron (0.3.3)](https://github.com/flatiron/flatiron/tree/v0.3.3)
525-
* [haibu (0.9.7)](https://github.com/nodejitsu/haibu)
631+
* [haibu (0.10.1)](https://github.com/nodejitsu/haibu/tree/v0.10.1)
526632
* [semver (1.1.2)](https://github.com/isaacs/node-semver/tree/v1.1.2)
527-
* [tar (0.1.14)](https://github.com/isaacs/node-tar/tree/v0.1.14)
528-
* [http-proxy (0.8.7)](https://github.com/nodejitsu/node-http-proxy/tree/v0.8.7)
529-
* [mongodb (1.2.x)](https://github.com/mongodb/node-mongodb-native/tree/V1.2.10)
633+
* [tar (0.1.17)](https://github.com/isaacs/node-tar/tree/v0.1.17)
634+
* [http-proxy (0.10.2)](https://github.com/nodejitsu/node-http-proxy/tree/v0.10.2)
635+
* [mongodb (1.2.x)](https://github.com/mongodb/node-mongodb-native/tree/V1.2.14)
636+
* [bcrypt (0.7.x)](https://github.com/ncb000gt/node.bcrypt.js/tree/0.7.5)
530637

531638
## Requirements
532639

config.sample.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
"database": "ishiki"
1919
},
2020
"logs-size": 100000,
21+
"auth": {
22+
"active": true,
23+
"admin": "ishiki",
24+
"token_expiry": 1800
25+
},
2126
"haibu": {
2227
"env": "development",
2328
"advanced-replies": true,

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ app.config.defaults({
5151
database: 'ishiki'
5252
},
5353
'logs-size': 100000,
54+
auth: {
55+
active: true,
56+
admin: 'ishiki',
57+
token_expiry: 1800
58+
},
5459
haibu: {
5560
env: 'development',
5661
'advanced-replies': true,
@@ -117,7 +122,6 @@ drone.packagesDir = app.config.get('haibu:directories:packages');
117122
if (app.config.get('haibu'))
118123
haibu.config.defaults(app.config.get('haibu'));
119124

120-
121125
//set up proxy
122126
var http_proxy = require('./lib/proxy').Proxy,
123127
proxy = new http_proxy(app, haibu);
@@ -131,6 +135,15 @@ proxy.autoload();
131135
//define routes
132136
require('./lib/ishiki')(app, haibu, path, fs, drone, proxy);
133137

138+
if (app.config.get('auth:active')) {
139+
//authentication
140+
var auth = require('./lib/auth').Auth,
141+
user_auth = new auth(app, haibu);
142+
143+
//check permissions on each request
144+
app.http.before.push(user_auth.check.bind(user_auth));
145+
}
146+
134147
//start ishiki
135148
app.start(app.config.get('port'), app.config.get('host'), function() {
136149
console.log('Haibu Ishiki started on ' + app.config.get('host') + ':' + app.config.get('port'));

0 commit comments

Comments
 (0)