Skip to content

Commit 10344b5

Browse files
AzaharaCpetermetz
authored andcommitted
fix(cmd-api-server): cockpit off by default #1239
Signed-off-by: AzaharaC <[email protected]>
1 parent 3d63b14 commit 10344b5

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

BUILD.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ npm run start:api-server
161161
```
162162

163163
After starting the API server, you will see in the logs that plugins were loaded
164-
and that the API is reachable on the port you specified (4000 by default) and
165-
the Web UI (Cockpit) is reachable through port on the port your config
164+
and that the API is reachable on the port you specified (4000 by default). The Web UI (Cockpit)
165+
is disabled by default but can be enabled by changing the property value 'cockpitEnabled'
166+
to true and it is reachable through port on the port your config
166167
specified (3000 by default).
167168

168169
> You may need to enable manually the CORS patterns in the configuration file.

packages/cactus-cmd-api-server/src/main/typescript/api-server.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class ApiServer {
8686
private readonly log: Logger;
8787
private pluginRegistry: PluginRegistry | undefined;
8888
private readonly httpServerApi: Server | SecureServer;
89-
private readonly httpServerCockpit: Server | SecureServer;
89+
private readonly httpServerCockpit?: Server | SecureServer;
9090
private readonly wsApi: SocketIoServer;
9191
private readonly grpcServer: GrpcServer;
9292
private readonly expressApi: Application;
@@ -129,15 +129,17 @@ export class ApiServer {
129129
this.httpServerApi = createServer();
130130
}
131131

132-
if (this.options.httpServerCockpit) {
133-
this.httpServerCockpit = this.options.httpServerCockpit;
134-
} else if (this.options.config.cockpitTlsEnabled) {
135-
this.httpServerCockpit = createSecureServer({
136-
key: this.options.config.cockpitTlsKeyPem,
137-
cert: this.options.config.cockpitTlsCertPem,
138-
});
139-
} else {
140-
this.httpServerCockpit = createServer();
132+
if (this.options.config.cockpitEnabled) {
133+
if (this.options.httpServerCockpit) {
134+
this.httpServerCockpit = this.options.httpServerCockpit;
135+
} else if (this.options.config.cockpitTlsEnabled) {
136+
this.httpServerCockpit = createSecureServer({
137+
key: this.options.config.cockpitTlsKeyPem,
138+
cert: this.options.config.cockpitTlsCertPem,
139+
});
140+
} else {
141+
this.httpServerCockpit = createServer();
142+
}
141143
}
142144

143145
this.grpcServer = this.options.grpcServer || new GrpcServer({});
@@ -194,7 +196,7 @@ export class ApiServer {
194196
}
195197

196198
async start(): Promise<{
197-
addressInfoCockpit: AddressInfo;
199+
addressInfoCockpit?: AddressInfo;
198200
addressInfoApi: AddressInfo;
199201
addressInfoGrpc: AddressInfo;
200202
}> {
@@ -205,7 +207,10 @@ export class ApiServer {
205207

206208
try {
207209
const { cockpitTlsEnabled, apiTlsEnabled } = this.options.config;
208-
const addressInfoCockpit = await this.startCockpitFileServer();
210+
let addressInfoCockpit: AddressInfo | undefined;
211+
if (this.options.config.cockpitEnabled) {
212+
addressInfoCockpit = await this.startCockpitFileServer();
213+
}
209214
const addressInfoApi = await this.startApiServer();
210215
const addressInfoGrpc = await this.startGrpcServer();
211216

@@ -223,9 +228,9 @@ export class ApiServer {
223228
this.log.info(`Cactus API reachable ${httpUrl}`);
224229
}
225230

226-
{
231+
if (this.options.config.cockpitEnabled) {
227232
const { cockpitHost: host } = this.options.config;
228-
const { port } = addressInfoCockpit;
233+
const { port } = addressInfoCockpit as AddressInfo;
229234
const protocol = cockpitTlsEnabled ? "https:" : "http:";
230235
const httpUrl = `${protocol}//${host}:${port}`;
231236
this.log.info(`Cactus Cockpit reachable ${httpUrl}`);
@@ -269,7 +274,7 @@ export class ApiServer {
269274
return this.httpServerApi;
270275
}
271276

272-
public getHttpServerCockpit(): Server | SecureServer {
277+
public getHttpServerCockpit(): Server | SecureServer | undefined {
273278
return this.httpServerCockpit;
274279
}
275280

@@ -493,19 +498,19 @@ export class ApiServer {
493498
const cockpitPort: number = this.options.config.cockpitPort;
494499
const cockpitHost: string = this.options.config.cockpitHost;
495500

496-
if (!this.httpServerCockpit.listening) {
501+
if (!this.httpServerCockpit?.listening) {
497502
await new Promise((resolve, reject) => {
498-
this.httpServerCockpit.once("error", reject);
499-
this.httpServerCockpit.once("listening", resolve);
500-
this.httpServerCockpit.listen(cockpitPort, cockpitHost);
503+
this.httpServerCockpit?.once("error", reject);
504+
this.httpServerCockpit?.once("listening", resolve);
505+
this.httpServerCockpit?.listen(cockpitPort, cockpitHost);
501506
});
502507
}
503-
this.httpServerCockpit.on("request", app);
508+
this.httpServerCockpit?.on("request", app);
504509

505510
// the address() method returns a string for unix domain sockets and null
506511
// if the server is not listening but we don't car about any of those cases
507512
// so the casting here should be safe. Famous last words... I know.
508-
const addressInfo = this.httpServerCockpit.address() as AddressInfo;
513+
const addressInfo = this.httpServerCockpit?.address() as AddressInfo;
509514
this.log.info(`Cactus Cockpit net.AddressInfo`, addressInfo);
510515

511516
return addressInfo;

packages/cactus-cmd-api-server/src/main/typescript/config/config-service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface ICactusApiServerOptions {
3434
consortiumId: string;
3535
logLevel: LogLevelDesc;
3636
tlsDefaultMaxVersion: SecureVersion;
37+
cockpitEnabled: boolean;
3738
cockpitHost: string;
3839
cockpitPort: number;
3940
cockpitCorsDomainCsv: string;
@@ -201,6 +202,13 @@ export class ConfigService {
201202
env: "TLS_DEFAULT_MAX_VERSION",
202203
arg: "tls-default-max-version",
203204
},
205+
cockpitEnabled: {
206+
doc: "Enable Cockpit server.",
207+
format: Boolean,
208+
env: "COCKPIT_ENABLED",
209+
arg: "cockpit-enabled",
210+
default: false,
211+
},
204212
cockpitHost: {
205213
doc:
206214
"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 {
579587
apiTlsClientCaPem: "-", // API mTLS is off so this will not crash the server
580588
grpcPort,
581589
grpcMtlsEnabled,
590+
cockpitEnabled: (schema.cockpitEnabled as SchemaObj).default,
582591
cockpitHost,
583592
cockpitPort,
584593
cockpitWwwRoot: (schema.cockpitWwwRoot as SchemaObj).default,

packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-via-web-service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe(testCase, () => {
6464
quorumGenesisOptions: IQuorumGenesisOptions,
6565
firstHighNetWorthAccount: string,
6666
apiServerStartOut: {
67-
addressInfoCockpit: AddressInfo;
67+
addressInfoCockpit?: AddressInfo;
6868
addressInfoApi: AddressInfo;
6969
addressInfoGrpc: AddressInfo;
7070
};

0 commit comments

Comments
 (0)