From 82539add265ee4e27476ea7c0b9531f320d1b0b9 Mon Sep 17 00:00:00 2001 From: Chris Breneman Date: Tue, 24 Feb 2015 16:11:14 -0500 Subject: [PATCH] Change how package provider plugins are configured --- conf/full.yaml | 5 +++++ lib/config.js | 21 +++++++++++++++++++++ lib/packages.js | 43 +++++++++++++++---------------------------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/conf/full.yaml b/conf/full.yaml index d6e19e417..88ef49259 100644 --- a/conf/full.yaml +++ b/conf/full.yaml @@ -59,6 +59,11 @@ packages: # # you can override storage directory for a group of packages this way: # storage: 'local_storage' + # Delegate handling package access authorization and settings to an external + # plugin for packages with this prefix + #'external-*': + # plugin: my_plugin + '*': # allow all users to read packages (including non-authenticated users) # diff --git a/lib/config.js b/lib/config.js index 231238056..4977177fc 100644 --- a/lib/config.js +++ b/lib/config.js @@ -84,6 +84,11 @@ function Config(config) { function check_userlist(i, hash, action) { if (hash[action] == null) hash[action] = [] + check_stringlist(i, hash, action) + } + + function check_stringlist(i, hash, action) { + if (!hash[action]) return // if it's a string, split it to array if (typeof(hash[action]) === 'string') { @@ -97,6 +102,20 @@ function Config(config) { hash[action] = flatten(hash[action]) } + // if a field in a string or array, converts into a hash with keys + // of the string and values of empty object + function check_objectset(i, hash, action) { + if (!hash[action]) return + if (Array.isArray(hash[action]) || typeof(hash[action]) === 'string') { + check_stringlist(i, hash, action) + var new_object = {} + hash[action].forEach(function(string) { + new_object[string] = {} + }) + hash[action] = new_object + } + } + for (var i in self.packages) { assert( typeof(self.packages[i]) === 'object' && @@ -108,6 +127,8 @@ function Config(config) { check_userlist(i, self.packages[i], 'proxy_access') check_userlist(i, self.packages[i], 'proxy_publish') + check_objectset(i, self.packages[i], 'plugin') + // deprecated check_userlist(i, self.packages[i], 'access') check_userlist(i, self.packages[i], 'proxy') diff --git a/lib/packages.js b/lib/packages.js index 2c04bc9fb..ed9539ebf 100644 --- a/lib/packages.js +++ b/lib/packages.js @@ -15,17 +15,28 @@ function PackageProvider(config) { logger: self.logger } - self.plugins = load_plugins(config.package_provider, plugin_params, 'package_provider', ['allow_access']) + // load all plugins referenced by any package + for (var i in config.packages) { + if(config.packages[i].plugin) { + config.packages[i].loaded_plugins = load_plugins(config.packages[i].plugin, plugin_params, 'package_provider', ['allow_access']) + } + } - self.plugins.push(new ConfigPackageProvider({}, plugin_params)) - self.plugins.push(new DenyPackageProvider({}, plugin_params)) + self.default_plugin = new ConfigPackageProvider({}, plugin_params) return self } function check_plugin_result(function_name, package, arg, cb) { + var self = this + var plugins = self.config.get_package_setting(package, 'loaded_plugins') + if (!plugins || !plugins.length) { + self.default_plugin[function_name](package, arg, cb) + return + } + var current_result - async.eachSeries(this.plugins, function(plugin, next) { + async.eachSeries(plugins, function(plugin, next) { if(current_result === undefined && typeof plugin[function_name] === 'function') { plugin[function_name](package, arg, function(error, result) { if(error) { @@ -100,27 +111,3 @@ ConfigPackageProvider.prototype.get_package_setting = function(package, setting, }) } - -// package provider to deny all access, used as default after everything else falls through -function DenyPackageProvider() { - var self = Object.create(DenyPackageProvider.prototype) - return self -} - -DenyPackageProvider.prototype.allow_access = function(package, user, cb) { - setImmediate(function() { - cb(null, false) - }) -} - -DenyPackageProvider.prototype.allow_publish = function(package, user, cb) { - setImmediate(function() { - cb(null, false) - }) -} - -DenyPackageProvider.prototype.proxy_access = function(package, uplink, cb) { - setImmediate(function() { - cb(null, false) - }) -}