项目作者: dekelev

项目描述 :
A Feathers service for Kong API Gateway admin API
高级语言: JavaScript
项目地址: git://github.com/dekelev/feathers-kong.git
创建时间: 2019-03-30T19:18:56Z
项目社区:https://github.com/dekelev/feathers-kong

开源协议:MIT License

下载


feathers-kong

Build Status
Coverage Status
JavaScript Style Guide
Dependency Status
npm

A Feathers service for Kong API Gateway admin API

Installation

  1. npm install feathers-kong --save

Documentation

Please refer to the Kong admin API docs for options that can be passed. Feathers service methods map to the following Kong methods:

  • Feathers find -> Kong list all or when not using Consumer service, list resources of consumer by setting params.query.consumer with Kong consumer ID
  • Feathers get -> Kong get resource by id by setting id with resource ID & when not using Consumer service, set params.query.consumer with Kong consumer ID. otherwise it will return consumer by resource ID when supported
  • Feathers create -> Kong create resource. set params.query.consumer to create resource on consumer
  • Feathers patch -> Kong update resource by id
  • Feathers update -> Kong update resource by id
  • Feathers remove -> Kong delete resource by id

If a method is not supported by Kong for a given resource it is not support here as well.

Available Services

The following services are supported and map to the appropriate Kong resource:

  • Consumer
  • Acl
  • Jwt
  • KeyAuth

This is pretty important! Since this connects to your Kong admin API, you want to make sure that you don’t expose these endpoints via your app unless the user has the appropriate permissions. You can prevent any external access by doing this:

  1. const { Forbidden } = require('@feathersjs/errors');
  2. app.service('/kong/consumers').before({
  3. all: [
  4. context => {
  5. if (context.params.provider) {
  6. throw new Forbidden('You are not allowed to access this');
  7. }
  8. }
  9. ]
  10. });

Complete Example

Here’s an example of a Feathers server that uses feathers-authentication for local auth. It includes a users service that uses feathers-mongoose. Note that it does NOT implement any authorization.

  1. const feathers = require('@feathersjs/feathers');
  2. const express = require('@feathersjs/express');
  3. const socketio = require('@feathersjs/socketio');
  4. const { Consumer } = require('feather-kong');
  5. // Initialize the application
  6. const app = feathers()
  7. .configure(express.rest())
  8. .configure(socketio())
  9. // Needed for parsing bodies (login)
  10. .use(express.json())
  11. .use(express.urlencoded({ extended: true }))
  12. // A simple Message service that we can used for testing
  13. .use('/kong/consumers', new Consumer({ url: 'http://localhost:8001' }))
  14. .use('/', feathers.static(__dirname + '/public'))
  15. .use(express.errorHandler({ html: false }));
  16. function validateConsumer() {
  17. return function(hook) {
  18. console.log('Validating consumer code goes here');
  19. };
  20. }
  21. const consumerService = app.service('kong/consumers');
  22. consumerService.before({
  23. create: [validateConsumer()]
  24. });
  25. const consumer = {
  26. username: 'john',
  27. custom_id: 'u1'
  28. };
  29. consumerService.create(consumer).then(result => {
  30. console.log('Consumer created', result);
  31. }).catch(error => {
  32. console.log('Error creating consumer', error);
  33. });
  34. app.listen(3030);
  35. console.log('Feathers authentication app started on 127.0.0.1:3030');

License

Copyright (c) 2019

Licensed under the MIT license.