From 0c9a07cbad9e61470b3483028213985b6efb6cf7 Mon Sep 17 00:00:00 2001 From: Thiago da Rosa de Bustamante Date: Mon, 26 Mar 2018 20:55:44 -0300 Subject: [PATCH 1/2] readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c64764d..b291ae9a 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ And its done. You can test it accessing in your browser: `http://localhost:8000/ ## Gateway Configuration Reference -Take a better look into Tree Gateway by checking out the project and working with it guided by our [Docs](https://github.com/Leanty/tree-gateway/wiki). +Check the [Docs](https://github.com/Leanty/tree-gateway/wiki). ## Migrating from previous versions From 854755c107884e3cc05545df2269669576c352dd Mon Sep 17 00:00:00 2001 From: Thiago da Rosa de Bustamante Date: Tue, 27 Mar 2018 00:01:17 -0300 Subject: [PATCH 2/2] Refactory SDK to remove dependency to GatewayConfig --- src/admin/config/cli-tool.ts | 7 +++- src/admin/config/sdk-middleware.ts | 7 +--- src/admin/config/sdk.ts | 55 +++++++++++++----------------- src/utils/config.ts | 33 +++++++++++++++--- test/unit/admin.spec.ts | 7 +++- test/unit/install-apis.spec.ts | 7 +++- test/unit/uninstall-apis.spec.ts | 7 +++- 7 files changed, 76 insertions(+), 47 deletions(-) diff --git a/src/admin/config/cli-tool.ts b/src/admin/config/cli-tool.ts index 47cf55a3..c607c6c0 100644 --- a/src/admin/config/cli-tool.ts +++ b/src/admin/config/cli-tool.ts @@ -9,6 +9,7 @@ import { Configuration } from '../../configuration'; import { Inject } from 'typescript-ioc'; import * as fs from 'fs-extra-promise'; import * as YAML from 'yamljs'; +import { getSwaggerHost, getSwaggerUrl, generateSecurityToken } from '../../utils/config'; export class Cli { @Inject private config: Configuration; @@ -21,7 +22,11 @@ export class Cli { } async processCommand(): Promise { - this.sdk = await SDK.initialize(this.config.gateway); + this.sdk = await SDK.initialize({ + defaultHost: getSwaggerHost(this.config.gateway), + swaggerUrl: getSwaggerUrl(this.config.gateway), + token: generateSecurityToken(this.config.gateway) + }); return this.doCommand(); } diff --git a/src/admin/config/sdk-middleware.ts b/src/admin/config/sdk-middleware.ts index db9dd00c..1b3cbc05 100644 --- a/src/admin/config/sdk-middleware.ts +++ b/src/admin/config/sdk-middleware.ts @@ -4,8 +4,6 @@ import * as _ from 'lodash'; import * as fs from 'fs-extra-promise'; import * as path from 'path'; import * as request from 'request'; -import { GatewayConfig } from '../../config/gateway'; -import { getSwaggerHost } from '../../utils/config'; import { getResponseBody, checkStatus, invoke } from './utils'; export interface Middleware { @@ -91,12 +89,9 @@ export class MiddlewareClient implements Middleware { private authToken: string; private middlewareRequest: any; - constructor(swaggerClient: any, authToken: string, gateway: GatewayConfig) { + constructor(swaggerClient: any, authToken: string) { this.swaggerClient = swaggerClient; this.authToken = authToken; - if (!this.swaggerClient.spec || !this.swaggerClient.spec.host) { - this.swaggerClient.spec.host = getSwaggerHost(gateway); - } if (!this.swaggerClient.spec || !this.swaggerClient.spec.schemes || !this.swaggerClient.spec.schemes.length || !this.swaggerClient.spec.host) { throw new Error('Invalid swagger specification. Can not found the target endpoint to call.'); diff --git a/src/admin/config/sdk.ts b/src/admin/config/sdk.ts index 6f83274d..9b067cdb 100644 --- a/src/admin/config/sdk.ts +++ b/src/admin/config/sdk.ts @@ -5,13 +5,15 @@ import { Config, ConfigClient } from './sdk-config-package'; import { Users, UsersClient } from './sdk-users'; import { Gateway, GatewayClient } from './sdk-gateway'; import { Middleware, MiddlewareClient } from './sdk-middleware'; -import { GatewayConfig } from '../../config/gateway'; -import * as path from 'path'; -import { getSwaggerHost } from '../../utils/config'; -import * as jwt from 'jsonwebtoken'; const swagger = require('swagger-client'); +export interface SDKOptions { + swaggerUrl: string; + token: string; + defaultHost?: string; +} + export class SDK { private apisClient: Apis; private gatewayClient: Gateway; @@ -19,44 +21,33 @@ export class SDK { private usersClient: Users; private configClient: Config; - private constructor(swaggerClient: any, authToken: string, gateway: GatewayConfig) { + private constructor(swaggerClient: any, authToken: string) { this.apisClient = new ApisClient(swaggerClient); this.configClient = new ConfigClient(swaggerClient); this.gatewayClient = new GatewayClient(swaggerClient); - this.middlewareClient = new MiddlewareClient(swaggerClient, authToken, gateway); + this.middlewareClient = new MiddlewareClient(swaggerClient, authToken); this.usersClient = new UsersClient(swaggerClient); } - static async initialize(gateway: GatewayConfig): Promise { - const token: string = SDK.generateSecurityToken(gateway); - const swaggerUrl = SDK.getSwaggerUrl(gateway); - const swaggerClient = await swagger(swaggerUrl, { + static async initialize(options: SDKOptions): Promise { + if (!options || !options.token || + !options.swaggerUrl) { + throw new Error('Invalid parameters. You must informa a valid authentication token and a valid URL to the gateway swagger file.'); + } + + const swaggerClient = await swagger(options.swaggerUrl, { authorizations: { - Bearer: `Bearer ${token}` + Bearer: `Bearer ${options.token}` } }); - return new SDK(swaggerClient, token, gateway); - } - - private static generateSecurityToken(gateway: GatewayConfig) { - const dataToken = { - login: 'treeGateway SDK', - name: 'treeGateway SDK', - roles: ['admin', 'config'] - }; - - const token = jwt.sign(dataToken, gateway.admin.userService.jwtSecret, { - expiresIn: 7200 - }); - return token; - } - - private static getSwaggerUrl(gateway: GatewayConfig) { - if (gateway && gateway.admin && gateway.admin.apiDocs) { - const protocol = (gateway.admin.protocol.https ? 'https' : 'http'); - return `${protocol}://` + path.posix.join(`${getSwaggerHost(gateway)}`, gateway.admin.apiDocs.path, 'json'); + if (!swaggerClient.spec) { + swaggerClient.spec = {}; + } + if (!swaggerClient.spec.host && options.defaultHost) { + swaggerClient.spec.host = options.defaultHost; } - throw new Error('No admin apiDocs configured. Can not access the server rest API'); + + return new SDK(swaggerClient, options.token); } get apis(): Apis { diff --git a/src/utils/config.ts b/src/utils/config.ts index 60cdcfd3..7f728517 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -2,6 +2,8 @@ import { GatewayConfig } from '../config/gateway'; import * as _ from 'lodash'; +import * as path from 'path'; +import * as jwt from 'jsonwebtoken'; export function getSwaggerHost(gateway: GatewayConfig) { let host; @@ -15,12 +17,33 @@ export function getSwaggerHost(gateway: GatewayConfig) { return host; } -export function castArray(obj: any, path: string) { - if (!obj || !path) { +export function getSwaggerUrl(gateway: GatewayConfig) { + if (gateway && gateway.admin && gateway.admin.apiDocs) { + const protocol = (gateway.admin.protocol.https ? 'https' : 'http'); + return `${protocol}://` + path.posix.join(`${getSwaggerHost(gateway)}`, gateway.admin.apiDocs.path, 'json'); + } + throw new Error('No admin apiDocs configured. Can not access the server rest API'); +} + +export function generateSecurityToken(gateway: GatewayConfig) { + const dataToken = { + login: 'treeGateway CLI', + name: 'treeGateway CLI', + roles: ['admin', 'config'] + }; + + const token = jwt.sign(dataToken, gateway.admin.userService.jwtSecret, { + expiresIn: 7200 + }); + return token; +} + +export function castArray(obj: any, objPath: string) { + if (!obj || !objPath) { return; } - if (_.has(obj, path)) { - const value = _.get(obj, path, null); - _.set(obj, path, _.castArray(value)); + if (_.has(obj, objPath)) { + const value = _.get(obj, objPath, null); + _.set(obj, objPath, _.castArray(value)); } } diff --git a/test/unit/admin.spec.ts b/test/unit/admin.spec.ts index fb2d240a..8c6396f1 100644 --- a/test/unit/admin.spec.ts +++ b/test/unit/admin.spec.ts @@ -10,6 +10,7 @@ import {ApiConfig} from '../../src/config/api'; import {Container} from 'typescript-ioc'; import {Configuration} from '../../src/configuration'; import {SDK} from '../../src/admin/config/sdk'; +import { generateSecurityToken, getSwaggerUrl, getSwaggerHost } from '../../src/utils/config'; const expect = chai.expect; // tslint:disable:no-unused-expression @@ -60,7 +61,11 @@ describe('Gateway Admin Tasks', () => { config = Container.get(Configuration); adminRequest = request.defaults({baseUrl: `http://localhost:${config.gateway.admin.protocol.http.listenPort}`}); - sdk = await SDK.initialize(config.gateway); + sdk = await SDK.initialize({ + defaultHost: getSwaggerHost(config.gateway), + swaggerUrl: getSwaggerUrl(config.gateway), + token: generateSecurityToken(config.gateway) + }); await createUsers(); }); diff --git a/test/unit/install-apis.spec.ts b/test/unit/install-apis.spec.ts index 0ec46e3a..c5b49d8c 100644 --- a/test/unit/install-apis.spec.ts +++ b/test/unit/install-apis.spec.ts @@ -13,6 +13,7 @@ import {SDK} from '../../src/admin/config/sdk'; import * as YAML from 'yamljs'; import { ApiConfig } from '../../src/config/api'; import { ConfigPackage } from '../../src/config/config-package'; +import { getSwaggerHost, getSwaggerUrl, generateSecurityToken } from '../../src/utils/config'; chai.use(chaiPromises); @@ -262,7 +263,11 @@ describe('Gateway SDKs', () => { await gateway.start(); await gateway.startAdmin(); gateway.server.set('env', 'test'); - sdk = await SDK.initialize(config.gateway); + sdk = await SDK.initialize({ + defaultHost: getSwaggerHost(config.gateway), + swaggerUrl: getSwaggerUrl(config.gateway), + token: generateSecurityToken(config.gateway) + }); } function timeout(ms: number) { diff --git a/test/unit/uninstall-apis.spec.ts b/test/unit/uninstall-apis.spec.ts index 511fd370..a8712db1 100644 --- a/test/unit/uninstall-apis.spec.ts +++ b/test/unit/uninstall-apis.spec.ts @@ -9,6 +9,7 @@ import {Configuration} from '../../src/configuration'; import {Gateway} from '../../src/gateway'; import {Database} from '../../src/database'; import {SDK} from '../../src/admin/config/sdk'; +import { getSwaggerHost, getSwaggerUrl, generateSecurityToken } from '../../src/utils/config'; // tslint:disable:no-unused-expression // tslint:disable:no-console @@ -24,7 +25,11 @@ describe('Gateway APIs uninstall', () => { database = Container.get(Database); gateway = Container.get(Gateway); - sdk = await SDK.initialize(config.gateway); + sdk = await SDK.initialize({ + defaultHost: getSwaggerHost(config.gateway), + swaggerUrl: getSwaggerUrl(config.gateway), + token: generateSecurityToken(config.gateway) + }); }); it('should be able to uninstall APIs', async () => {