Skip to content

Commit

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

Closes: #1297
Signed-off-by: Elena Izaguirre <e.izaguirre.equiza@accenture.com>
  • Loading branch information
elenaizaguirre committed Sep 1, 2021
1 parent 2161e0d commit efcfb03
Show file tree
Hide file tree
Showing 7 changed files with 843 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/cactus-plugin-consortium-manual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"axios": "0.21.1",
"body-parser": "1.19.0",
"express": "4.17.1",
"express-openapi-validator": "4.12.12",
"jose": "1.28.1",
"json-stable-stringify": "1.0.1",
"prom-client": "13.2.0",
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": {
"GetNodeJwsResponse": {
Expand All @@ -39,7 +19,7 @@
"properties": {
"jws": {
"description": "The JSON Web Signature of the Cactus node.",
"$ref": "../../../../cactus-core-api/src/main/json/openapi.json#/components/schemas/JWSGeneral",
"$ref": "https://raw.githubusercontent.com/hyperledger/cactus/v0.8.0/packages/cactus-core-api/src/main/json/openapi.json#/components/schemas/JWSGeneral",
"nullable": false
}
}
Expand All @@ -52,7 +32,7 @@
"properties": {
"jws": {
"description": "The JSON Web Signature of the Cactus consortium.",
"$ref": "../../../../cactus-core-api/src/main/json/openapi.json#/components/schemas/JWSGeneral",
"$ref": "https://raw.githubusercontent.com/hyperledger/cactus/v0.8.0/packages/cactus-core-api/src/main/json/openapi.json#/components/schemas/JWSGeneral",
"nullable": false,
"format": "The general format which is a JSON object, not a string."
}
Expand All @@ -64,11 +44,13 @@
},
"GetNodeJwsRequest": {
"type": "object",
"additionalProperties": false,
"properties": {
}
},
"GetConsortiumJwsRequest": {
"type": "object",
"additionalProperties": false,
"properties": {
}
}
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 @@ -2,12 +2,16 @@ import { Server } from "http";
import { Server as SecureServer } from "https";
import { Optional } from "typescript-optional";
import { promisify } from "util";
import express, { Express } from "express";
import express, { Express, NextFunction, Request, Response } from "express";
import bodyParser from "body-parser";
import { JWS, JWK } from "jose";
import jsonStableStringify from "json-stable-stringify";
import { v4 as uuidv4 } from "uuid";

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

import {
ConsortiumDatabase,
IPluginWebService,
Expand Down Expand Up @@ -98,6 +102,10 @@ export class PluginConsortiumManual
this.prometheusExporter.setNodeCount(this.getNodeCount());
}

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

public getInstanceId(): string {
return this.instanceId;
}
Expand Down Expand Up @@ -171,6 +179,39 @@ export class PluginConsortiumManual
}

const webServices = await this.getOrCreateWebServices();
const apiSpec = this.getOpenApiSpecs();
const openApiMiddleware = OpenApiValidator.middleware({
apiSpec,
validateApiSpec: false,
$refParser: {
mode: "dereference",
},
});
app.use(openApiMiddleware);
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();
}
},
);
webServices.forEach((ws) => ws.registerExpress(webApp));
return webServices;
}
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-test-plugin-consortium-manual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@hyperledger/cactus-plugin-consortium-manual": "0.8.0",
"@hyperledger/cactus-plugin-keychain-memory": "0.8.0",
"axios": "0.21.1",
"express-openapi-validator": "4.12.12",
"jose": "1.28.1"
}
}
Loading

0 comments on commit efcfb03

Please sign in to comment.