2013-10-22 02:29:57 -05:00
|
|
|
var assert = require('assert')
|
|
|
|
, crypto = require('crypto')
|
2014-06-26 11:21:23 -05:00
|
|
|
, Path = require('path')
|
2013-10-22 02:29:57 -05:00
|
|
|
, minimatch = require('minimatch')
|
2014-09-10 12:55:26 -05:00
|
|
|
, Error = require('http-errors')
|
2014-09-06 15:50:34 -05:00
|
|
|
, LocalList = require('./local-list')
|
2013-10-22 02:29:57 -05:00
|
|
|
, utils = require('./utils')
|
2013-06-07 20:16:28 -05:00
|
|
|
|
|
|
|
// [[a, [b, c]], d] -> [a, b, c, d]
|
|
|
|
function flatten(array) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var result = []
|
2013-06-07 20:16:28 -05:00
|
|
|
for (var i=0; i<array.length; i++) {
|
|
|
|
if (Array.isArray(array[i])) {
|
2013-10-26 07:18:36 -05:00
|
|
|
result.push.apply(result, flatten(array[i]))
|
2013-06-07 20:16:28 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
result.push(array[i])
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
return result
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Config(config) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (!(this instanceof Config)) return new Config(config)
|
2013-06-07 20:16:28 -05:00
|
|
|
for (var i in config) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (this[i] == null) this[i] = config[i]
|
2013-09-25 04:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// some weird shell scripts are valid yaml files parsed as string
|
2013-10-26 07:18:36 -05:00
|
|
|
assert.equal(typeof(config), 'object', 'CONFIG: this doesn\'t look like a valid config file')
|
|
|
|
|
|
|
|
assert(this.storage, 'CONFIG: storage path not defined')
|
2014-09-06 15:57:40 -05:00
|
|
|
this.localList = LocalList(
|
|
|
|
Path.join(
|
|
|
|
Path.resolve(Path.dirname(this.self_path), this.storage),
|
|
|
|
'.sinopia-db.json'
|
|
|
|
)
|
|
|
|
)
|
2013-10-26 07:18:36 -05:00
|
|
|
|
|
|
|
var users = {all:true, anonymous:true, 'undefined':true, owner:true, none:true}
|
2013-06-07 20:16:28 -05:00
|
|
|
|
|
|
|
var check_user_or_uplink = function(arg) {
|
2014-06-17 19:52:07 -05:00
|
|
|
assert(arg !== 'all' && arg !== 'owner' && arg !== 'anonymous' && arg !== 'undefined' && arg !== 'none', 'CONFIG: reserved user/uplink name: ' + arg)
|
2013-10-26 07:18:36 -05:00
|
|
|
assert(!arg.match(/\s/), 'CONFIG: invalid user name: ' + arg)
|
|
|
|
assert(users[arg] == null, 'CONFIG: duplicate user/uplink name: ' + arg)
|
|
|
|
users[arg] = true
|
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
;['users', 'uplinks', 'packages'].forEach(function(x) {
|
|
|
|
if (this[x] == null) this[x] = {}
|
|
|
|
assert(utils.is_object(this[x]), 'CONFIG: bad "'+x+'" value (object expected)')
|
|
|
|
})
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
for (var i in this.users) check_user_or_uplink(i)
|
|
|
|
for (var i in this.uplinks) check_user_or_uplink(i)
|
2013-06-07 20:16:28 -05:00
|
|
|
|
|
|
|
for (var i in this.users) {
|
2013-10-26 07:18:36 -05:00
|
|
|
assert(this.users[i].password, 'CONFIG: no password for user: ' + i)
|
2013-06-07 20:16:28 -05:00
|
|
|
assert(
|
2014-01-13 11:48:51 -05:00
|
|
|
typeof(this.users[i].password) === 'string' &&
|
2013-06-07 20:16:28 -05:00
|
|
|
this.users[i].password.match(/^[a-f0-9]{40}$/)
|
2013-10-26 07:18:36 -05:00
|
|
|
, 'CONFIG: wrong password format for user: ' + i + ', sha1 expected')
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
|
2013-06-07 20:16:28 -05:00
|
|
|
for (var i in this.uplinks) {
|
2013-10-26 07:18:36 -05:00
|
|
|
assert(this.uplinks[i].url, 'CONFIG: no url for uplink: ' + i)
|
2013-06-07 20:16:28 -05:00
|
|
|
assert(
|
|
|
|
typeof(this.uplinks[i].url) === 'string'
|
2013-10-26 07:18:36 -05:00
|
|
|
, 'CONFIG: wrong url format for uplink: ' + i)
|
|
|
|
this.uplinks[i].url = this.uplinks[i].url.replace(/\/$/, '')
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
|
2013-10-01 13:02:23 -05:00
|
|
|
function check_userlist(i, hash, action) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (hash[action] == null) hash[action] = []
|
2013-06-13 09:21:14 -05:00
|
|
|
|
2013-10-01 13:02:23 -05:00
|
|
|
// if it's a string, split it to array
|
|
|
|
if (typeof(hash[action]) === 'string') {
|
2013-10-26 07:18:36 -05:00
|
|
|
hash[action] = hash[action].split(/\s+/)
|
2013-10-01 13:02:23 -05:00
|
|
|
}
|
2013-06-13 09:21:14 -05:00
|
|
|
|
2013-10-01 13:02:23 -05:00
|
|
|
assert(
|
|
|
|
typeof(hash[action]) === 'object' &&
|
|
|
|
Array.isArray(hash[action])
|
2013-10-26 07:18:36 -05:00
|
|
|
, 'CONFIG: bad "'+i+'" package '+action+' description (array or string expected)')
|
|
|
|
hash[action] = flatten(hash[action])
|
2013-10-01 13:02:23 -05:00
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2013-10-01 13:02:23 -05:00
|
|
|
for (var i in this.packages) {
|
2013-06-07 20:16:28 -05:00
|
|
|
assert(
|
|
|
|
typeof(this.packages[i]) === 'object' &&
|
|
|
|
!Array.isArray(this.packages[i])
|
2013-10-26 07:18:36 -05:00
|
|
|
, 'CONFIG: bad "'+i+'" package description (object expected)')
|
2013-09-24 01:27:27 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
check_userlist(i, this.packages[i], 'allow_access')
|
|
|
|
check_userlist(i, this.packages[i], 'allow_publish')
|
|
|
|
check_userlist(i, this.packages[i], 'proxy_access')
|
|
|
|
check_userlist(i, this.packages[i], 'proxy_publish')
|
2013-09-24 01:27:27 -05:00
|
|
|
|
|
|
|
// deprecated
|
2013-10-26 07:18:36 -05:00
|
|
|
check_userlist(i, this.packages[i], 'access')
|
|
|
|
check_userlist(i, this.packages[i], 'proxy')
|
|
|
|
check_userlist(i, this.packages[i], 'publish')
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-11-24 12:08:20 -05:00
|
|
|
// loading these from ENV if aren't in config
|
|
|
|
;['http_proxy', 'https_proxy', 'no_proxy'].forEach((function(v) {
|
|
|
|
if (!(v in this)) {
|
|
|
|
this[v] = process.env[v] || process.env[v.toUpperCase()]
|
|
|
|
}
|
|
|
|
}).bind(this))
|
|
|
|
|
2013-12-08 22:59:31 -05:00
|
|
|
// unique identifier of this server (or a cluster), used to avoid loops
|
|
|
|
if (!this.server_id) {
|
|
|
|
this.server_id = crypto.pseudoRandomBytes(6).toString('hex')
|
|
|
|
}
|
|
|
|
|
2014-03-31 19:13:59 -05:00
|
|
|
if (this.ignore_latest_tag == null) this.ignore_latest_tag = false
|
2014-03-28 21:31:34 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
return this
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function allow_action(package, who, action) {
|
2014-01-13 11:48:51 -05:00
|
|
|
return (this.get_package_setting(package, action) || []).reduce(function(prev, curr) {
|
2014-09-01 18:09:08 -05:00
|
|
|
if (typeof(who) === 'string' && curr === who) return true
|
2014-09-14 10:06:27 -05:00
|
|
|
if (Array.isArray(who.groups) && who.groups.indexOf(curr) !== -1) return true
|
2014-01-13 11:48:51 -05:00
|
|
|
return prev
|
|
|
|
}, false)
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Config.prototype.allow_access = function(package, user) {
|
2014-09-01 18:09:08 -05:00
|
|
|
return allow_action.call(this, package, user.groups, 'allow_access') || allow_action.call(this, package, user, 'access')
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Config.prototype.allow_publish = function(package, user) {
|
2014-09-01 18:09:08 -05:00
|
|
|
return allow_action.call(this, package, user.groups, 'allow_publish') || allow_action.call(this, package, user, 'publish')
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-09-23 23:27:47 -05:00
|
|
|
Config.prototype.proxy_access = function(package, uplink) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return allow_action.call(this, package, uplink, 'proxy_access') || allow_action.call(this, package, uplink, 'proxy')
|
2013-09-23 23:27:47 -05:00
|
|
|
}
|
|
|
|
|
2013-09-27 02:48:01 -05:00
|
|
|
Config.prototype.proxy_publish = function(package, uplink) {
|
2014-09-01 18:09:08 -05:00
|
|
|
throw new Error('deprecated')
|
|
|
|
//return allow_action.call(this, package, uplink, 'proxy_publish')
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2014-01-13 11:48:51 -05:00
|
|
|
Config.prototype.get_package_setting = function(package, setting) {
|
|
|
|
for (var i in this.packages) {
|
|
|
|
if (minimatch.makeRe(i).exec(package)) {
|
|
|
|
return this.packages[i][setting]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
module.exports = Config
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2014-03-07 22:49:59 -05:00
|
|
|
var parse_interval_table = {
|
2014-03-07 23:37:16 -05:00
|
|
|
'': 1000,
|
2014-03-07 22:49:59 -05:00
|
|
|
ms: 1,
|
|
|
|
s: 1000,
|
|
|
|
m: 60*1000,
|
|
|
|
h: 60*60*1000,
|
|
|
|
d: 86400000,
|
2014-03-07 23:37:16 -05:00
|
|
|
w: 7*86400000,
|
2014-03-07 22:49:59 -05:00
|
|
|
M: 30*86400000,
|
2014-03-07 23:37:16 -05:00
|
|
|
y: 365*86400000,
|
2014-03-07 22:49:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.parse_interval = function(interval) {
|
2014-03-07 23:37:16 -05:00
|
|
|
if (typeof(interval) === 'number') return interval * 1000
|
|
|
|
|
|
|
|
var result = 0
|
|
|
|
var last_suffix = Infinity
|
|
|
|
interval.split(/\s+/).forEach(function(x) {
|
2014-03-07 23:38:37 -05:00
|
|
|
if (!x) return
|
2014-03-07 23:37:16 -05:00
|
|
|
var m = x.match(/^((0|[1-9][0-9]*)(\.[0-9]+)?)(ms|s|m|h|d|w|M|y|)$/)
|
|
|
|
if (!m
|
|
|
|
|| parse_interval_table[m[4]] >= last_suffix
|
|
|
|
|| (m[4] === '' && last_suffix !== Infinity)) {
|
|
|
|
throw new Error('invalid interval: ' + interval)
|
|
|
|
}
|
|
|
|
last_suffix = parse_interval_table[m[4]]
|
|
|
|
result += Number(m[1]) * parse_interval_table[m[4]]
|
|
|
|
})
|
|
|
|
return result
|
2014-03-07 22:49:59 -05:00
|
|
|
}
|
|
|
|
|