Skip to content

Commit

Permalink
fix: openapi-validation for fabric plugin
Browse files Browse the repository at this point in the history
add missing validation for fabric plugin rest endpoints

Closes: hyperledger-cacti#1295
Signed-off-by: Elena Izaguirre <e.izaguirre.equiza@accenture.com>
  • Loading branch information
elenaizaguirre committed Sep 2, 2021
1 parent 2161e0d commit fff8db9
Show file tree
Hide file tree
Showing 7 changed files with 1,339 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"bl": "5.0.0",
"bn.js": "4.12.0",
"express": "4.17.1",
"express-openapi-validator": "4.12.12",
"fabric-ca-client": "2.3.0-snapshot.49",
"fabric-common": "2.3.0-snapshot.49",
"fabric-network": "2.3.0-snapshot.49",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"servers": [
{
"url": "https://www.cactus.stream/{basePath}",
"description": "Public test instance",
"variables": {
"basePath": {
"default": ""
}
}
},
{
"url": "http://localhost:4000/{basePath}",
"description": "Local test instance",
"variables": {
"basePath": {
"default": ""
}
}
}
],
"components": {
"schemas": {
"VaultTransitKey" : {
Expand Down Expand Up @@ -372,6 +352,7 @@
"methodName",
"params"
],
"additionalProperties": false,
"properties": {
"endorsingPeers": {
"description": "An array of MSP IDs to set as the list of endorsing peers for the transaction.",
Expand Down Expand Up @@ -577,6 +558,7 @@
"targetPeerAddresses",
"tlsRootCertFiles"
],
"additionalProperties": false,
"properties": {
"policyDslSource": {
"type": "string",
Expand Down Expand Up @@ -716,6 +698,7 @@
"orderer",
"ordererTLSHostnameOverride"
],
"additionalProperties": false,
"properties": {
"ccLang": {
"$ref": "#/components/schemas/ChainCodeProgrammingLanguage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Configuration } from "./configuration";
// @ts-ignore
import globalAxios, { AxiosPromise, AxiosInstance } from 'axios';

export const BASE_PATH = "https://www.cactus.stream".replace(/\/+$/, "");
export const BASE_PATH = "http://localhost".replace(/\/+$/, "");

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Server } from "http";
import { Server as SecureServer } from "https";

import { Certificate } from "@fidm/x509";
import { Express } from "express";
import "multer";
import { Express, NextFunction, Request, Response } from "express";

import temp from "temp";
import {
NodeSSH,
Expand All @@ -24,6 +24,10 @@ import {
Wallet,
} from "fabric-network";

import OAS from "../json/openapi.json";
import * as OpenApiValidator from "express-openapi-validator";
import { OpenAPIV3 } from "express-openapi-validator/dist/framework/types";

import { Optional } from "typescript-optional";

import {
Expand Down Expand Up @@ -216,6 +220,10 @@ export class PluginLedgerConnectorFabric
return res;
}

getOpenApiSpecs(): OpenAPIV3.Document {
return (OAS as unknown) as OpenAPIV3.Document;
}

public getInstanceId(): string {
return this.instanceId;
}
Expand Down Expand Up @@ -775,6 +783,36 @@ export class PluginLedgerConnectorFabric

async registerWebServices(app: Express): Promise<IWebServiceEndpoint[]> {
const webServices = await this.getOrCreateWebServices();
app.use(
OpenApiValidator.middleware({
apiSpec: this.getOpenApiSpecs(),
validateApiSpec: false,
}),
);
app.use(
(
err: {
status?: number;
errors: [
{
path: string;
message: string;
errorCode: string;
},
];
},
req: Request,
res: Response,
next: NextFunction,
) => {
if (err) {
res.status(err.status || 500);
res.send(err.errors);
} else {
next();
}
},
);
await Promise.all(webServices.map((ws) => ws.registerExpress(app)));
return webServices;
}
Expand Down
Loading

0 comments on commit fff8db9

Please sign in to comment.