Skip to content

Commit

Permalink
fix(cmd-api-server): cockpit off by default hyperledger-cacti#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 committed Oct 28, 2021
1 parent f7eb39b commit 5e73ecf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
4 changes: 1 addition & 3 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ 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
specified (3000 by default).
and that the API is reachable on the port you specified (4000 by default).

> You may need to enable manually the CORS patterns in the configuration file.
This may be slightly inconvenient, but something we are unable to compromise on
Expand Down
53 changes: 25 additions & 28 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 @@ -204,8 +206,11 @@ export class ApiServer {
tls.DEFAULT_MAX_VERSION = tlsMaxVersion;

try {
const { cockpitTlsEnabled, apiTlsEnabled } = this.options.config;
const addressInfoCockpit = await this.startCockpitFileServer();
const { apiTlsEnabled } = this.options.config;
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,14 +228,6 @@ export class ApiServer {
this.log.info(`Cactus API reachable ${httpUrl}`);
}

{
const { cockpitHost: host } = this.options.config;
const { port } = addressInfoCockpit;
const protocol = cockpitTlsEnabled ? "https:" : "http:";
const httpUrl = `${protocol}//${host}:${port}`;
this.log.info(`Cactus Cockpit reachable ${httpUrl}`);
}

return { addressInfoCockpit, addressInfoApi, addressInfoGrpc };
} catch (ex) {
const errorMessage = `Failed to start ApiServer: ${ex.stack}`;
Expand Down Expand Up @@ -269,7 +266,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 +490,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

0 comments on commit 5e73ecf

Please sign in to comment.