let select apikey type in setDBAuth: regular, default, master

This commit is contained in:
Eneko Lakasta 2018-02-08 11:29:17 +01:00
parent d9a34f3384
commit 6ebf51ce45
5 changed files with 32 additions and 19 deletions

View File

@ -101,8 +101,7 @@ AuthApi.prototype.authorize = function(req, res, callback) {
}
if (isAuthorizedByApikey) {
const asMaster = false;
return this.pgConnection.setDBAuth(user, res.locals, asMaster, function (err) {
return this.pgConnection.setDBAuth(user, res.locals, 'regular', function (err) {
req.profiler.done('setDBAuth');
if (err) {
@ -119,8 +118,7 @@ AuthApi.prototype.authorize = function(req, res, callback) {
}
if (isAuthorizedBySigner) {
const asMaster = true;
return this.pgConnection.setDBAuth(user, res.locals, asMaster, function (err) {
return this.pgConnection.setDBAuth(user, res.locals, 'master', function (err) {
req.profiler.done('setDBAuth');
if (err) {
@ -131,10 +129,17 @@ AuthApi.prototype.authorize = function(req, res, callback) {
});
}
// if no signer name was given, let dbparams and
// PostgreSQL do the rest.
// if no signer name was given, use default api key
if (!res.locals.signer) {
return callback(null, true); // authorized so far
return this.pgConnection.setDBAuth(user, res.locals, 'default', function (err) {
req.profiler.done('setDBAuth');
if (err) {
return callback(err);
}
callback(null, true);
});
}
// if signer name was given, return no authorization

View File

@ -20,8 +20,8 @@ module.exports = PgConnection;
//
// @param callback function(err)
//
PgConnection.prototype.setDBAuth = function(username, params, asMaster, callback) {
if (asMaster) {
PgConnection.prototype.setDBAuth = function(username, params, apikeyType, callback) {
if (apikeyType === 'master') {
this.metadataBackend.getMasterApikey(username, (err, apikey) => {
if (err) {
return callback(err);
@ -32,7 +32,7 @@ PgConnection.prototype.setDBAuth = function(username, params, asMaster, callback
return callback();
});
} else {
} else if (apikeyType === 'regular') {
this.metadataBackend.getApikey(username, params.api_key || params.map_key, (err, apikey) => {
if (err) {
return callback(err);
@ -43,6 +43,19 @@ PgConnection.prototype.setDBAuth = function(username, params, asMaster, callback
return callback();
});
} else if (apikeyType === 'default') {
this.metadataBackend.getApikey(username, 'default_public', (err, apikey) => {
if (err) {
return callback(err);
}
params.dbuser = apikey.databaseRole;
params.dbpassword = apikey.databasePassword;
return callback();
});
} else {
return callback(new Error(`Invalid Apikey type: ${apikeyType}, valid ones: master, regular, default`));
}
};
@ -97,8 +110,7 @@ PgConnection.prototype.getConnection = function(username, callback) {
require('debug')('cachechan')("getConn1");
step(
function setAuth() {
const asMaster = true;
self.setDBAuth(username, params, asMaster, this);
self.setDBAuth(username, params, 'master', this);
},
function setConn(err) {
assert.ifError(err);

View File

@ -22,8 +22,7 @@ PgQueryRunner.prototype.run = function(username, query, callback) {
step(
function setAuth() {
const asMaster = true;
self.pgConnection.setDBAuth(username, params, asMaster, this);
self.pgConnection.setDBAuth(username, params, 'master', this);
},
function setConn(err) {
assert.ifError(err);

View File

@ -108,9 +108,7 @@ MapConfigNamedLayersAdapter.prototype.getMapConfig = function (user, requestMapC
var dbAuth = {};
if (_.some(layers, isNamedTypeLayer)) {
// Lazy load dbAuth
const asMaster = true;
this.pgConnection.setDBAuth(user, dbAuth, asMaster, function(err) {
this.pgConnection.setDBAuth(user, dbAuth, 'master', function(err) {
if (err) {
return callback(err);
}

View File

@ -235,8 +235,7 @@ NamedMapMapConfigProvider.prototype.setDBParams = function(cdbuser, params, call
var self = this;
step(
function setAuth() {
const asMaster = true;
self.pgConnection.setDBAuth(cdbuser, params, asMaster, this);
self.pgConnection.setDBAuth(cdbuser, params, 'master', this);
},
function setConn(err) {
assert.ifError(err);