Skip to content

Commit

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

Closes: #1286
Signed-off-by: Elena Izaguirre <e.izaguirre.equiza@accenture.com>
  • Loading branch information
elenaizaguirre committed Aug 31, 2021
1 parent f5ffb92 commit b548765
Show file tree
Hide file tree
Showing 6 changed files with 928 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@hyperledger/cactus-core-api": "0.8.0",
"axios": "0.21.1",
"express": "4.17.1",
"express-openapi-validator": "4.12.12",
"prom-client": "13.2.0",
"typescript-optional": "2.0.1",
"web3": "1.3.5",
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": {
"Web3SigningCredential": {
Expand Down Expand Up @@ -333,19 +313,19 @@
},
"transactionHash": {
"type": "string",
"minLength": 64,
"maxLength": 64,
"pattern": "/^0x([A-Fa-f0-9]{64})$/"
"minLength": 66,
"maxLength": 66,
"pattern": "^0x([A-Fa-f0-9]{64})$"
},
"transactionIndex": {
"type": "number",
"nullable": false
},
"blockHash": {
"type": "string",
"minLength": 64,
"maxLength": 64,
"pattern": "/^0x([A-Fa-f0-9]{64})$/"
"minLength": 66,
"maxLength": 66,
"pattern": "^0x([A-Fa-f0-9]{64})$"
},
"blockNumber": {
"type": "number",
Expand Down Expand Up @@ -375,6 +355,7 @@
"web3SigningCredential",
"transactionConfig"
],
"additionalProperties": false,
"properties": {
"web3SigningCredential": {
"$ref": "#/components/schemas/Web3SigningCredential",
Expand Down Expand Up @@ -412,6 +393,7 @@
"web3SigningCredential",
"keychainId"
],
"additionalProperties": false,
"properties": {
"contractName": {
"type": "string",
Expand Down Expand Up @@ -475,6 +457,7 @@
"methodName",
"params"
],
"additionalProperties": false,
"properties": {
"contractName": {
"type": "string",
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
@@ -1,7 +1,7 @@
import { Server } from "http";
import { Server as SecureServer } from "https";

import { Express } from "express";
import { Express, NextFunction, Request, Response } from "express";
import { promisify } from "util";
import { Optional } from "typescript-optional";
import Web3 from "web3";
Expand All @@ -12,6 +12,10 @@ const Contract = new Web3().eth.Contract;
import { ContractSendMethod } from "web3-eth-contract";
import { TransactionReceipt } from "web3-eth";

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

import {
ConsensusAlgorithmFamily,
IPluginLedgerConnector,
Expand Down Expand Up @@ -133,6 +137,10 @@ export class PluginLedgerConnectorQuorum
return res;
}

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

public getInstanceId(): string {
return this.instanceId;
}
Expand All @@ -155,6 +163,36 @@ export class PluginLedgerConnectorQuorum

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 b548765

Please sign in to comment.