Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Changed API logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pantsel committed Nov 14, 2016
1 parent f0b6782 commit c68a7b8
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 83 deletions.
4 changes: 2 additions & 2 deletions backend/api/controllers/KongaApiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var KongaApiController = {
* @param res
*/
createConsumer : function(req,res) {
KongaApiService.consumers.create(req,function(err,response){
KongaApiService.consumers.create(req.body,function(err,response){
if(err) return res.kongError(err)

// Sync consumers
Expand All @@ -38,7 +38,7 @@ var KongaApiController = {
* @param res
*/
registerApi : function(req,res) {
KongaApiService.apis.register(req,function(err,response){
KongaApiService.apis.register(req.body,function(err,response){
if(err) return res.kongError(err)
return res.json(response)
})
Expand Down
60 changes: 60 additions & 0 deletions backend/api/hooks/konga-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

var _ = require('lodash');
var KongaApiService = require('../services/KongaApiService')
var ConsumerService = require('../services/ConsumerService')


module.exports = function hook(sails) {
return {

process: function process(next) {


if(!sails.config.expose_api) return next()

var konga_api = {
"name" : "konga",
"request_path" : "/konga",
"strip_request_path" : true,
"preserve_host" : true,
"upstream_url" : sails.config.konga_url + '/api',
"plugins" : [{
"name" : "key-auth",
"config.key_names" : ["apiKey"]
}]
}

var konga_api_consumer = {
"username" : "konga",
"custom_id" : "konga",
"acls" : ["konga"],
"authorizations" : [,{
"name" : "key-auth"
}]
}

KongaApiService.apis.register(konga_api,function(err,api) {
KongaApiService.consumers.create(konga_api_consumer,function(err,consumer){
ConsumerService.sync(function(err,done){
return next()
})
})
})
},

/**
* Method that runs automatically when the hook initializes itself.
*
* @param {Function} next Callback function to call after all is done
*/
initialize: function initialize(next) {
var self = this;

// Wait for sails orm hook to be loaded
sails.after('hook:orm:loaded', function onAfter() {
self.process(next);
});
}
};
};
15 changes: 8 additions & 7 deletions backend/api/models/Consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ module.exports = _.merge(_.cloneDeep(require('../base/Model')), {
type: 'string'
},
node_id : {
type : 'string',
required : true
},
credentials : {
type: 'array',
defaultsTo : []
type : 'string'
}
},
autoPK: false
autoPK: false,

beforeCreate: function (values, cb) {

if(!values.node_id) values.node_id = sails.config.kong_admin_url
cb();
}
});
25 changes: 25 additions & 0 deletions backend/api/policies/exposedApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var _ = require('lodash');

/**
* Policy to set necessary update data to body. Note that this policy will also remove some body items.
*
* @param {Request} request Request object
* @param {Response} response Response object
* @param {Function} next Callback function
*/
module.exports = function exposedApi(request, response, next) {
sails.log.verbose(__filename + ':' + __line + ' [Policy.exposedApi() called]');


if(!sails.config.expose_api) {
var error = new Error();
error.message = 'Forbidden'
error.status = 403;
return next(error)
}

return next()

};
49 changes: 10 additions & 39 deletions backend/api/policies/kongRequestHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,24 @@ module.exports = function kongRequestHost(request, response, next) {
sails.log.verbose(__filename + ':' + __line + ' [Policy.kongRequestHost() called]');

// First of all check if the request is coming from a valid kong proxy host
if(sails.config.kong_proxy_hosts.indexOf(request.headers.host) < 0 ) {
if(sails.config.whitelist_hosts.indexOf(request.host) < 0 ) {
var error = new Error();

error.status = 403;
error.message = 'Forbidden - Invalid request host.';

return next(error);
}

// Set the node to the requested one
var node_id = request.headers['konga-node-id']
if(!node_id) {
//var error = new Error();
//error.message = 'Missing required header "konga-node-id"'
//error.status = 400;
//return next(error)

sails.models.kongnode.findOne({
active:true
}).exec(function afterwards(err, node){
if (err) return next(err)
if(!node) {
var error = new Error();
error.message = 'Unable to find a node to connect'
error.code = 'E_NODE_UNDEFINED'
error.status = 500;
return next(error)
}

sails.config.kong_admin_url = 'http://' + node.kong_admin_ip + ':' + node.kong_admin_port
return next()
})
}else{
sails.models.kongnode.findOne(node_id).exec(function afterwards(err, node){
if (err) return next(err)
if(!node) {
var error = new Error();
error.message = 'Requested node not found'
error.status = 404;
return next(error)
}
sails.config.kong_admin_url = 'http://' + node.kong_admin_ip + ':' + node.kong_admin_port

return next()
})
}
sails.config.kong_admin_url = request.headers['kong-admin-url'] || sails.config.kong_admin_url


if(!sails.config.kong_admin_url) {
var error = new Error();
error.message = 'Kong admin URL is not defined'
error.status = 400;
return next(error)
}

return next()

};
11 changes: 3 additions & 8 deletions backend/api/services/ConsumerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ var ConsumerService = {

var kongConsumers = response.body.data


sails.models.consumer.find({})
.exec(function (err, consumers) {
if (err) return cb(err)

sails.log.debug('ConsumerService:sync => Konga consumers',consumers);

// Find the consumers that only exist in Kong's db
var onlyInKong = kongConsumers.filter(function (current) {
return consumers.filter(function (current_b) {
Expand All @@ -35,17 +38,9 @@ var ConsumerService = {

async.series([
function (callback) {
sails.log.info('Importing Kong\'s consumers in Konga]');

// Add node_id property
onlyInKong.forEach(function (consumer) {
consumer.node_id = req.node_id
})

sails.models.consumer.create(onlyInKong)
.exec(function (err, docs) {
if (err) return callback(err)

// ToDo maybe sync Authorization plugins as well?

return callback()
Expand Down
17 changes: 8 additions & 9 deletions backend/api/services/KongaApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ var async= require('async')
var KongaApiService = {

consumers : {
create: function (req, cb) {
create: function (inc, cb) {

var consumer = {}
unirest.post(sails.config.kong_admin_url + '/consumers')
.send({
username : req.body.username,
custom_id : req.body.custom_id
username : inc.username,
custom_id : inc.custom_id
})
.end(function (response) {
if (response.error) {
if(response.body) {
response.body.path = "consumer"
response.body.obj = {
username : req.body.username,
custom_id : req.body.custom_id
username : inc.username,
custom_id : inc.custom_id
}
}

Expand All @@ -32,7 +32,7 @@ var KongaApiService = {

KongaApiService
.consumers
.addAcls(consumer.id,req.body.acls,function(err,result){
.addAcls(consumer.id,inc.acls,function(err,result){
if(err) {
// Try to delete the created consumer in case of error
KongaApiService
Expand All @@ -44,7 +44,7 @@ var KongaApiService = {

KongaApiService
.consumers
.addAuthorizations(consumer.id,req.body.authorizations,function(err,auths){
.addAuthorizations(consumer.id,inc.authorizations,function(err,auths){
if(err) {
// Try to delete the created consumer in case of error
KongaApiService
Expand Down Expand Up @@ -138,9 +138,8 @@ var KongaApiService = {
},

apis : {
register : function(req,cb) {
register : function(api,cb) {

var api = req.body
var result = {}
var plugins = []
if(api.plugins) {
Expand Down
33 changes: 30 additions & 3 deletions backend/config/local_example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Created by pang on 7/10/2016.
*/
'use strict';

/**
Expand All @@ -24,17 +27,41 @@
* http://links.sailsjs.org/docs/config/local
*/
module.exports = {

/**
* The full URL of the backend server.
* In case of localhost, this attribute must contain
* the machine's actual ip address instead of 'localhost' or '127.0.0.1'
* so that Kong can access it if running on a virtual host.
*/
konga_url : 'http://192.168.1.2:1338',


/**
* The default fallback URL to Kong's admin API.
*/
kong_admin_url : 'http://127.0.0.1:8001',

/**
* Whether or not Konga will expose it's Utilities API.
*/
expose_api : false,

/**
* An array of hosts from which Konga's API will accept requests.
* Only used when 'expose_api' attribute is set to true
*/
whitelist_hosts : ['127.0.0.1'],

connections: {

},
models: {
connection : 'localDiskDb'

},
session: {
secret: '' // Add your own SECRET string here
},
kong_admin_url : '',
kong_proxy_hosts : ['127.0.0.1:8000'],
port: 1338,
environment: 'development',
log: {
Expand Down
2 changes: 1 addition & 1 deletion backend/config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.policies = {


KongaApiController : {
'*' : ['kongRequestHost']
'*' : ['exposedApi','kongRequestHost']
},

AuthController: {
Expand Down
13 changes: 0 additions & 13 deletions frontend/src/app/consumers/consumer-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@
};
}

// Add node id to load
//if(parameters.hasOwnProperty('where')){
// parameters.where.and = [{
// node_id : '2'
// }]
//}else{
// parameters.where = {
// and : [{
// node_id : '2'
// }]
// }
//}

return DataService
.collection(self.endpoint, parameters)
.then(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kongadmin",
"version": "0.2.2",
"version": "0.2.3",
"description": "Kong admin GUI",
"keywords": [
"sails.js",
Expand Down

0 comments on commit c68a7b8

Please sign in to comment.