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

Commit

Permalink
Fixed issue #4 + various changes in dynamic node handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pantsel committed Nov 16, 2016
1 parent 264a2d5 commit e7efb0c
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 136 deletions.
6 changes: 0 additions & 6 deletions backend/api/controllers/KongConsumerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ var KongConsumerController = _.merge(_.cloneDeep(require('../base/KongControlle
.end(function(response){
if(response.error) return res.kongError(response)

ConsumerCredentialsService
.add(req.params.id,req.params.credential,response.body,function(err){})

return res.json(response.body)
})
},
Expand All @@ -79,9 +76,6 @@ var KongConsumerController = _.merge(_.cloneDeep(require('../base/KongControlle
.end(function(response){
if(response.error) return res.kongError(response)

ConsumerCredentialsService
.remove(req.params.id,req.params.credential,req.params.credential_id,function(err){})

return res.json(response.body)
})
},
Expand Down
1 change: 0 additions & 1 deletion backend/api/models/Consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = _.merge(_.cloneDeep(require('../base/Model')), {
autoPK: false,

beforeCreate: function (values, cb) {

if(!values.node_id) values.node_id = sails.config.kong_admin_url
cb();
}
Expand Down
5 changes: 5 additions & 0 deletions backend/api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ module.exports = _.merge(_.cloneDeep(require('../base/Model')), {
defaultsTo: false
},

node_id : {
type : 'string',
defaultsTo: ''
},

// Passport configurations
passports: {
collection: 'Passport',
Expand Down
38 changes: 14 additions & 24 deletions backend/api/policies/activeNodeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,20 @@ var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');
module.exports = function activeNodeData(request, response, next) {
sails.log.verbose(__filename + ':' + __line + ' [Policy.activeNodeData() called]');

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)
}

var c = actionUtil.parseCriteria(request)

if(c.hasOwnProperty('or')){
c['and'] = [{node_id : node.id}]
}else{
c['where'] = {node_id : node.id}
}

request.query.where = JSON.stringify(c)

return next()
})

sails.config.kong_admin_url = request.headers['kong-admin-url'] || sails.config.kong_admin_url

var c = actionUtil.parseCriteria(request)

if(c.hasOwnProperty('or')){
c['and'] = [{node_id : sails.config.kong_admin_url}]
}else{
c['where'] = {node_id : sails.config.kong_admin_url}
}

request.query.where = JSON.stringify(c)

return next()


};
19 changes: 4 additions & 15 deletions backend/api/policies/dynamicNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,9 @@ var _ = require('lodash');
module.exports = function dynamicNode(request, response, next) {
sails.log.verbose(__filename + ':' + __line + ' [Policy.dynamicNode() called]');

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)
}
request.node_id = node.id
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
request.node_id = sails.config.kong_admin_url
return next()

};
27 changes: 0 additions & 27 deletions backend/api/services/ConsumerCredentialsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,13 @@ var async = require('async')

var ConsumerCredentialsService = {

add : function(consumer_id,type,value,cb) {
sails.models.consumer
.findOne(consumer_id)
.exec(function(err,consumer){
var credential = _.merge({type:type},{id:value.id})
consumer.credentials.push(credential)
consumer.save()
return cb(null)
})
},

remove : function(consumer_id,type,credential_id,cb) {
sails.models.consumer
.findOne(consumer_id)
.exec(function(err,consumer){
consumer.credentials.forEach(function(cred,index){
if(cred.type === type && cred.id === credential_id) {
consumer.credentials.splice(index, 1);
consumer.save()
return cb(null)
}
})
})
},

listCredentials : function(consumer_id,cb) {

var credentials = ['jwt','key-auth','basic-auth','hmac-auth','oauth2']
var promises = []

credentials.forEach(function(credential){

console.log(sails.config.kong_admin_url + '/consumers/' + consumer_id + "/" + credential)

promises.push(function(cb) {
unirest.get(sails.config.kong_admin_url + '/consumers/' + consumer_id + "/" + credential)
.end(function(response){
Expand Down
2 changes: 1 addition & 1 deletion backend/config/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ module.exports.cors = {
* response to preflight requests. *
* *
***************************************************************************/
headers: 'content-type, access-control-allow-origin, authorization'
headers: 'content-type, access-control-allow-origin, authorization, kong-admin-url'
};
4 changes: 2 additions & 2 deletions backend/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ module.exports.routes = {

// Remote Storage routes
'GET /remote/adapters' : 'RemoteStorageController.loadAdapters',
'POST /remote/consumers' : 'RemoteStorageController.loadConsumers',
'POST /remote/consumers' : 'RemoteStorageController.loadConsumers',
'GET /remote/connection/test' : 'RemoteStorageController.testConnection',


// Konga API
'GET /api/consumers/:id/credentials/:credential' : 'KongaApiController.getConsumerCredentials',
//'GET /api/consumers/:id/credentials/:credential' : 'KongaApiController.getConsumerCredentials',
'POST /api/consumers' : 'KongaApiController.createConsumer',
'POST /api/apis' : 'KongaApiController.registerApi'

Expand Down
11 changes: 2 additions & 9 deletions backend/test/fixtures/KongNode.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
[
{
"id": 1,
"name" : "node1",
"name" : "default",
"kong_admin_ip": "127.0.0.1",
"kong_admin_port": "8001",
"active": true
},
{
"id": 2,
"name" : "node2",
"kong_admin_ip": "192.168.1.116",
"kong_admin_port": "8183",
"active": true
"active": false
}
]
6 changes: 3 additions & 3 deletions backend/test/functional/common/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ describe('Generic controller test', function controllerTest() {
controller: 'KongNodeController',
url: '/kongnode/',
identifier: 1,
count: 2,
count: 1,
data: {
identifier: {
name : "node1",
name : "default",
kong_admin_ip: "127.0.0.1",
kong_admin_port: "8001",
active: true,
active: false,
},
newRecord: {
name : "node2",
Expand Down
9 changes: 2 additions & 7 deletions frontend/src/app/apis/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
parent : 'frontend',
url: '/apis',
data : {
activeNode : true,
pageName : "APIs",
displayName : "apis",
prefix : '<i class="material-icons">cloud_queue</i>'
Expand All @@ -31,13 +32,7 @@
function resolve(ApiService) {
return ApiService.all()
}
],
_activeNode: [
'NodesService',
function resolve(NodesService) {
return NodesService.isActiveNodeSet()
}
],
]
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
// Add interceptors for $httpProvider and $sailsSocketProvider
$httpProvider.interceptors.push('AuthInterceptor');
$httpProvider.interceptors.push('ErrorInterceptor');
$httpProvider.interceptors.push('TemplateCacheInterceptor');
$httpProvider.interceptors.push('TemplateCacheInterceptor');
$httpProvider.interceptors.push('KongaInterceptor');

// Iterate $httpProvider interceptors and add those to $sailsSocketProvider
angular.forEach($httpProvider.interceptors, function iterator(interceptor) {
Expand Down Expand Up @@ -141,11 +142,11 @@
angular.module('frontend')
.run([
'$rootScope', '$state', '$injector',
'editableOptions','editableThemes','$templateCache',
'editableOptions','editableThemes','$templateCache','NodesService',
'AuthService','cfpLoadingBar',
function run(
$rootScope, $state, $injector,
editableOptions,editableThemes,$templateCache,
editableOptions,editableThemes,$templateCache,NodesService,
AuthService,cfpLoadingBar
) {

Expand All @@ -171,6 +172,12 @@
$state.go('auth.login');
}


if (!NodesService.authorize(toState.data.activeNode)) {
event.preventDefault();
$state.go('settings');
}

if (toState.redirectTo) {
event.preventDefault();
$state.go(toState.redirectTo, params, {location: 'replace'})
Expand Down
8 changes: 1 addition & 7 deletions frontend/src/app/consumers/consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
parent : 'frontend',
url: '/consumers',
data : {
activeNode : true,
pageName : "Consumers",
displayName : "consumers",
prefix : '<i class="material-icons">perm_identity</i>'
Expand All @@ -28,13 +29,6 @@
templateUrl: '/frontend/consumers/index.html',
controller: 'ConsumersController',
resolve : {
_activeNode: [
'NodesService',
function resolve(NodesService) {

return NodesService.isActiveNodeSet()
}
],
_items: [
'ListConfig',
'ConsumerModel',
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/core/auth/login/login-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
$scope, $state,
AuthService, FocusOnService
) {
// Already authenticated so redirect back to books list
// Already authenticated so to dashboard
if (AuthService.isAuthenticated()) {
$state.go('admin');
$state.go('dashboard');
}

// Scope function to perform actual login request to server
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/core/interceptors/AuthInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
*/
config.data.token = token;
config.headers.authorization = 'Bearer ' + token;


}

return config;
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/app/core/interceptors/KongaIntercenptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Auth interceptor for HTTP and Socket request. This interceptor will add required
* JWT (Json Web Token) token to each requests. That token is validated in server side
* application.
*
* @see http://angular-tips.com/blog/2014/05/json-web-tokens-introduction/
* @see http://angular-tips.com/blog/2014/05/json-web-tokens-examples/
*/
(function() {
'use strict';

angular.module('frontend.core.interceptors')
.factory('KongaInterceptor', [
'$q', '$injector', '$localStorage',
function(
$q, $injector, $localStorage
) {
return {
/**
* Interceptor method for $http requests. Main purpose of this method is to add JWT token
* to every request that application does.
*
* @param {*} config HTTP request configuration
*
* @returns {*}
*/
request: function requestCallback(config) {

var konga_node_id = '';

// Yeah we have some user data on local storage
if ($localStorage.credentials) {
konga_node_id = $localStorage.credentials.user.node_id
}

config.headers['kong-admin-url'] = konga_node_id;

return config;
}
};
}
])
;
}());
Loading

0 comments on commit e7efb0c

Please sign in to comment.