diff --git a/BUILD.md b/BUILD.md index 0da54b3c86..90abd845c0 100644 --- a/BUILD.md +++ b/BUILD.md @@ -161,8 +161,9 @@ npm run start:api-server ``` After starting the API server, you will see in the logs that plugins were loaded -and that the API is reachable on the port you specified (4000 by default) and -the Web UI (Cockpit) is reachable through port on the port your config +and that the API is reachable on the port you specified (4000 by default). The Web UI (Cockpit) +is disabled by default but can be enabled by changing the property value 'cockpitEnabled' +to true and it is reachable through port on the port your config specified (3000 by default). > You may need to enable manually the CORS patterns in the configuration file. diff --git a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts index ebd99a0727..813a1dbfc3 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts @@ -86,7 +86,7 @@ export class ApiServer { private readonly log: Logger; private pluginRegistry: PluginRegistry | undefined; private readonly httpServerApi: Server | SecureServer; - private readonly httpServerCockpit: Server | SecureServer; + private readonly httpServerCockpit?: Server | SecureServer; private readonly wsApi: SocketIoServer; private readonly grpcServer: GrpcServer; private readonly expressApi: Application; @@ -129,15 +129,17 @@ export class ApiServer { this.httpServerApi = createServer(); } - if (this.options.httpServerCockpit) { - this.httpServerCockpit = this.options.httpServerCockpit; - } else if (this.options.config.cockpitTlsEnabled) { - this.httpServerCockpit = createSecureServer({ - key: this.options.config.cockpitTlsKeyPem, - cert: this.options.config.cockpitTlsCertPem, - }); - } else { - this.httpServerCockpit = createServer(); + if (this.options.config.cockpitEnabled) { + if (this.options.httpServerCockpit) { + this.httpServerCockpit = this.options.httpServerCockpit; + } else if (this.options.config.cockpitTlsEnabled) { + this.httpServerCockpit = createSecureServer({ + key: this.options.config.cockpitTlsKeyPem, + cert: this.options.config.cockpitTlsCertPem, + }); + } else { + this.httpServerCockpit = createServer(); + } } this.grpcServer = this.options.grpcServer || new GrpcServer({}); @@ -194,7 +196,7 @@ export class ApiServer { } async start(): Promise<{ - addressInfoCockpit: AddressInfo; + addressInfoCockpit?: AddressInfo; addressInfoApi: AddressInfo; addressInfoGrpc: AddressInfo; }> { @@ -205,7 +207,10 @@ export class ApiServer { try { const { cockpitTlsEnabled, apiTlsEnabled } = this.options.config; - const addressInfoCockpit = await this.startCockpitFileServer(); + let addressInfoCockpit: AddressInfo | undefined; + if (this.options.config.cockpitEnabled) { + addressInfoCockpit = await this.startCockpitFileServer(); + } const addressInfoApi = await this.startApiServer(); const addressInfoGrpc = await this.startGrpcServer(); @@ -223,9 +228,9 @@ export class ApiServer { this.log.info(`Cactus API reachable ${httpUrl}`); } - { + if (this.options.config.cockpitEnabled) { const { cockpitHost: host } = this.options.config; - const { port } = addressInfoCockpit; + const { port } = addressInfoCockpit as AddressInfo; const protocol = cockpitTlsEnabled ? "https:" : "http:"; const httpUrl = `${protocol}//${host}:${port}`; this.log.info(`Cactus Cockpit reachable ${httpUrl}`); @@ -269,7 +274,7 @@ export class ApiServer { return this.httpServerApi; } - public getHttpServerCockpit(): Server | SecureServer { + public getHttpServerCockpit(): Server | SecureServer | undefined { return this.httpServerCockpit; } @@ -493,19 +498,19 @@ export class ApiServer { const cockpitPort: number = this.options.config.cockpitPort; const cockpitHost: string = this.options.config.cockpitHost; - if (!this.httpServerCockpit.listening) { + if (!this.httpServerCockpit?.listening) { await new Promise((resolve, reject) => { - this.httpServerCockpit.once("error", reject); - this.httpServerCockpit.once("listening", resolve); - this.httpServerCockpit.listen(cockpitPort, cockpitHost); + this.httpServerCockpit?.once("error", reject); + this.httpServerCockpit?.once("listening", resolve); + this.httpServerCockpit?.listen(cockpitPort, cockpitHost); }); } - this.httpServerCockpit.on("request", app); + this.httpServerCockpit?.on("request", app); // the address() method returns a string for unix domain sockets and null // if the server is not listening but we don't car about any of those cases // so the casting here should be safe. Famous last words... I know. - const addressInfo = this.httpServerCockpit.address() as AddressInfo; + const addressInfo = this.httpServerCockpit?.address() as AddressInfo; this.log.info(`Cactus Cockpit net.AddressInfo`, addressInfo); return addressInfo; diff --git a/packages/cactus-cmd-api-server/src/main/typescript/config/config-service.ts b/packages/cactus-cmd-api-server/src/main/typescript/config/config-service.ts index e4938d2467..50b78c4a76 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/config/config-service.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/config/config-service.ts @@ -34,6 +34,7 @@ export interface ICactusApiServerOptions { consortiumId: string; logLevel: LogLevelDesc; tlsDefaultMaxVersion: SecureVersion; + cockpitEnabled: boolean; cockpitHost: string; cockpitPort: number; cockpitCorsDomainCsv: string; @@ -201,6 +202,13 @@ export class ConfigService { env: "TLS_DEFAULT_MAX_VERSION", arg: "tls-default-max-version", }, + cockpitEnabled: { + doc: "Enable Cockpit server.", + format: Boolean, + env: "COCKPIT_ENABLED", + arg: "cockpit-enabled", + default: false, + }, cockpitHost: { doc: "The host to bind the Cockpit webserver to. Secure default is: 127.0.0.1. Use 0.0.0.0 to bind for any host.", @@ -579,6 +587,7 @@ export class ConfigService { apiTlsClientCaPem: "-", // API mTLS is off so this will not crash the server grpcPort, grpcMtlsEnabled, + cockpitEnabled: (schema.cockpitEnabled as SchemaObj).default, cockpitHost, cockpitPort, cockpitWwwRoot: (schema.cockpitWwwRoot as SchemaObj).default, diff --git a/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-via-web-service.test.ts b/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-via-web-service.test.ts index bc2c29251c..39f4a22ac6 100644 --- a/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-via-web-service.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-via-web-service.test.ts @@ -64,7 +64,7 @@ describe(testCase, () => { quorumGenesisOptions: IQuorumGenesisOptions, firstHighNetWorthAccount: string, apiServerStartOut: { - addressInfoCockpit: AddressInfo; + addressInfoCockpit?: AddressInfo; addressInfoApi: AddressInfo; addressInfoGrpc: AddressInfo; };