Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd-api-server): add runtime type validation to HTTP verbs pulled from OAS #2755

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { installOpenapiValidationMiddleware } from "@hyperledger/cactus-core";

import {
Bools,
isExpressHttpVerbMethodName,
Logger,
LoggerProvider,
newRex,
Expand Down Expand Up @@ -563,6 +564,7 @@ export class ApiServer {
* @param app
*/
async getOrCreateWebServices(app: express.Express): Promise<void> {
const fnTag = `${this.className}#getOrCreateWebServices()}`;
const { log } = this;
const { logLevel } = this.options.config;
const pluginRegistry = await this.getOrInitPluginRegistry();
Expand Down Expand Up @@ -595,10 +597,11 @@ export class ApiServer {
const { "/api/v1/api-server/healthcheck": oasPath } = OAS.paths;
const { http } = oasPath.get["x-hyperledger-cactus"];
const { path: httpPath, verbLowerCase: httpVerb } = http;
(app as express.Express)[httpVerb as keyof express.Express](
httpPath,
healthcheckHandler,
);
if (!isExpressHttpVerbMethodName(httpVerb)) {
const eMsg = `${fnTag} Invalid HTTP verb "${httpVerb}" in cmd-api-server OpenAPI specification for HTTP path: "${httpPath}"`;
throw new RuntimeError(eMsg);
}
app[httpVerb](httpPath, healthcheckHandler);

this.wsApi.on("connection", (socket: SocketIoSocket) => {
const { id } = socket;
Expand Down Expand Up @@ -629,14 +632,19 @@ export class ApiServer {
const {
"/api/v1/api-server/get-prometheus-exporter-metrics": oasPathPrometheus,
} = OAS.paths;

const { http: httpPrometheus } =
oasPathPrometheus.get["x-hyperledger-cactus"];

const { path: httpPathPrometheus, verbLowerCase: httpVerbPrometheus } =
httpPrometheus;
(app as express.Express)[httpVerbPrometheus as keyof express.Express](
httpPathPrometheus,
prometheusExporterHandler,
);

if (!isExpressHttpVerbMethodName(httpVerbPrometheus)) {
const eMsg = `${fnTag} Invalid HTTP verb "${httpVerbPrometheus}" in cmd-api-server OpenAPI specification for HTTP path: "${httpPathPrometheus}"`;
throw new RuntimeError(eMsg);
}

app[httpVerbPrometheus](httpPathPrometheus, prometheusExporterHandler);
}

async startGrpcServer(): Promise<AddressInfo> {
Expand Down