Skip to content

Commit

Permalink
fix(cmd-api-server): cockpit off by default #1239
Browse files Browse the repository at this point in the history
Signed-off-by: AzaharaC <a.castano.benito@accenture.com>
  • Loading branch information
AzaharaC authored and petermetz committed Nov 17, 2021
1 parent 3d63b14 commit 10344b5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
5 changes: 3 additions & 2 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 26 additions & 21 deletions packages/cactus-cmd-api-server/src/main/typescript/api-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({});
Expand Down Expand Up @@ -194,7 +196,7 @@ export class ApiServer {
}

async start(): Promise<{
addressInfoCockpit: AddressInfo;
addressInfoCockpit?: AddressInfo;
addressInfoApi: AddressInfo;
addressInfoGrpc: AddressInfo;
}> {
Expand All @@ -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();

Expand All @@ -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}`);
Expand Down Expand Up @@ -269,7 +274,7 @@ export class ApiServer {
return this.httpServerApi;
}

public getHttpServerCockpit(): Server | SecureServer {
public getHttpServerCockpit(): Server | SecureServer | undefined {
return this.httpServerCockpit;
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ICactusApiServerOptions {
consortiumId: string;
logLevel: LogLevelDesc;
tlsDefaultMaxVersion: SecureVersion;
cockpitEnabled: boolean;
cockpitHost: string;
cockpitPort: number;
cockpitCorsDomainCsv: string;
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe(testCase, () => {
quorumGenesisOptions: IQuorumGenesisOptions,
firstHighNetWorthAccount: string,
apiServerStartOut: {
addressInfoCockpit: AddressInfo;
addressInfoCockpit?: AddressInfo;
addressInfoApi: AddressInfo;
addressInfoGrpc: AddressInfo;
};
Expand Down

0 comments on commit 10344b5

Please sign in to comment.