Skip to content

Commit 00bdafa

Browse files
committed
chore: fix all linting issues
Ticket: WP-4352
1 parent bb3ecd0 commit 00bdafa

File tree

10 files changed

+111
-66
lines changed

10 files changed

+111
-66
lines changed

.eslintrc.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
module.exports = {
22
parser: '@typescript-eslint/parser',
3-
extends: [
4-
'plugin:@typescript-eslint/recommended',
5-
'plugin:prettier/recommended',
6-
],
3+
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
74
parserOptions: {
85
ecmaVersion: 2018,
96
sourceType: 'module',
107
},
118
rules: {
129
'@typescript-eslint/explicit-function-return-type': 'off',
13-
'@typescript-eslint/no-explicit-any': 'warn',
10+
'@typescript-eslint/no-explicit-any': 'off',
1411
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
1512
},
16-
};
13+
ignorePatterns: ['dist/**/*', 'node_modules/**/*'],
14+
};

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ module.exports = {
1111
coverageDirectory: 'coverage',
1212
coverageReporters: ['text', 'lcov'],
1313
verbose: true,
14-
};
14+
};

src/__tests__/config.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ describe('Configuration', () => {
66
beforeEach(() => {
77
jest.resetModules();
88
process.env = { ...originalEnv };
9+
// Explicitly clear MTLS-related environment variables
10+
delete process.env.MTLS_ENABLED;
11+
delete process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS;
912
});
1013

1114
afterAll(() => {
@@ -37,6 +40,12 @@ describe('Configuration', () => {
3740
expect(cfg.tlsMode).toBe(TlsMode.MTLS);
3841
});
3942

43+
it('should throw error when both TLS disabled and mTLS enabled', () => {
44+
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'true';
45+
process.env.MTLS_ENABLED = 'true';
46+
expect(() => config()).toThrow('Cannot have both TLS disabled and mTLS enabled');
47+
});
48+
4049
it('should read mTLS settings from environment variables', () => {
4150
process.env.MTLS_ENABLED = 'true';
4251
process.env.MTLS_REQUEST_CERT = 'true';
@@ -48,4 +57,4 @@ describe('Configuration', () => {
4857
expect(cfg.mtlsRejectUnauthorized).toBe(true);
4958
expect(cfg.mtlsAllowedClientFingerprints).toEqual(['ABC123', 'DEF456']);
5059
});
51-
});
60+
});

src/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ describe('Basic test setup', () => {
66
it('should handle basic math', () => {
77
expect(1 + 1).toBe(2);
88
});
9-
});
9+
});

src/__tests__/routes.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ describe('Routes', () => {
3030
it('should return 404 for non-existent routes', async () => {
3131
const response = await request(app).get('/non-existent-route');
3232
expect(response.status).toBe(404);
33-
expect(response.body).toHaveProperty('error', 'Route not found or not supported in enclaved mode');
33+
expect(response.body).toHaveProperty(
34+
'error',
35+
'Route not found or not supported in enclaved mode',
36+
);
3437
});
3538
});
36-
});
39+
});

src/config.ts

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11
/**
22
* @prettier
33
*/
4+
import fs from 'fs';
5+
import { Config, TlsMode } from './types';
46

5-
export enum TlsMode {
6-
DISABLED = 'disabled', // No TLS (plain HTTP)
7-
ENABLED = 'enabled', // TLS with server cert only
8-
MTLS = 'mtls' // TLS with both server and client certs
9-
}
10-
11-
export interface Config {
12-
port: number;
13-
bind: string;
14-
ipc?: string;
15-
debugNamespace?: string[];
16-
// TLS settings
17-
keyPath?: string;
18-
crtPath?: string;
19-
tlsKey?: string;
20-
tlsCert?: string;
21-
tlsMode: TlsMode;
22-
// mTLS settings
23-
mtlsRequestCert?: boolean;
24-
mtlsRejectUnauthorized?: boolean;
25-
mtlsAllowedClientFingerprints?: string[];
26-
// Other settings
27-
logFile?: string;
28-
timeout: number;
29-
keepAliveTimeout?: number;
30-
headersTimeout?: number;
31-
}
7+
export { Config, TlsMode };
328

33-
const defaultConfig: Config = {
9+
export const defaultConfig: Config = {
3410
port: 3080,
3511
bind: 'localhost',
3612
timeout: 305 * 1000,
3713
logFile: '',
38-
tlsMode: TlsMode.ENABLED, // Default to TLS enabled
14+
tlsMode: TlsMode.ENABLED, // Default to TLS enabled
3915
mtlsRequestCert: false,
4016
mtlsRejectUnauthorized: false,
4117
};
@@ -46,23 +22,38 @@ function readEnvVar(name: string): string | undefined {
4622
}
4723
}
4824

25+
function determineTlsMode(): TlsMode {
26+
const disableTls = readEnvVar('MASTER_BITGO_EXPRESS_DISABLE_TLS') === 'true';
27+
const mtlsEnabled = readEnvVar('MTLS_ENABLED') === 'true';
28+
29+
if (disableTls && mtlsEnabled) {
30+
throw new Error('Cannot have both TLS disabled and mTLS enabled');
31+
}
32+
33+
if (disableTls) {
34+
return TlsMode.DISABLED;
35+
}
36+
if (mtlsEnabled) {
37+
return TlsMode.MTLS;
38+
}
39+
return TlsMode.ENABLED;
40+
}
41+
4942
export function config(): Config {
5043
const envConfig: Partial<Config> = {
5144
port: Number(readEnvVar('MASTER_BITGO_EXPRESS_PORT')) || defaultConfig.port,
5245
bind: readEnvVar('MASTER_BITGO_EXPRESS_BIND') || defaultConfig.bind,
5346
ipc: readEnvVar('MASTER_BITGO_EXPRESS_IPC'),
54-
debugNamespace: (readEnvVar('MASTER_BITGO_EXPRESS_DEBUG_NAMESPACE') || '').split(',').filter(Boolean),
47+
debugNamespace: (readEnvVar('MASTER_BITGO_EXPRESS_DEBUG_NAMESPACE') || '')
48+
.split(',')
49+
.filter(Boolean),
5550
// Basic TLS settings from MASTER_BITGO_EXPRESS
5651
keyPath: readEnvVar('MASTER_BITGO_EXPRESS_KEYPATH'),
5752
crtPath: readEnvVar('MASTER_BITGO_EXPRESS_CRTPATH'),
5853
tlsKey: readEnvVar('MASTER_BITGO_EXPRESS_TLS_KEY'),
5954
tlsCert: readEnvVar('MASTER_BITGO_EXPRESS_TLS_CERT'),
6055
// Determine TLS mode
61-
tlsMode: readEnvVar('MASTER_BITGO_EXPRESS_DISABLE_TLS') === 'true'
62-
? TlsMode.DISABLED
63-
: readEnvVar('MTLS_ENABLED') === 'true'
64-
? TlsMode.MTLS
65-
: TlsMode.ENABLED,
56+
tlsMode: determineTlsMode(),
6657
// mTLS settings
6758
mtlsRequestCert: readEnvVar('MTLS_REQUEST_CERT') === 'true',
6859
mtlsRejectUnauthorized: readEnvVar('MTLS_REJECT_UNAUTHORIZED') === 'true',
@@ -77,15 +68,15 @@ export function config(): Config {
7768
// Support loading key/cert from file if keyPath/crtPath are set and tlsKey/tlsCert are not
7869
if (!envConfig.tlsKey && envConfig.keyPath) {
7970
try {
80-
envConfig.tlsKey = require('fs').readFileSync(envConfig.keyPath, 'utf-8');
71+
envConfig.tlsKey = fs.readFileSync(envConfig.keyPath, 'utf-8');
8172
} catch (e) {
8273
const err = e instanceof Error ? e : new Error(String(e));
8374
throw new Error(`Failed to read TLS key from keyPath: ${err.message}`);
8475
}
8576
}
8677
if (!envConfig.tlsCert && envConfig.crtPath) {
8778
try {
88-
envConfig.tlsCert = require('fs').readFileSync(envConfig.crtPath, 'utf-8');
79+
envConfig.tlsCert = fs.readFileSync(envConfig.crtPath, 'utf-8');
8980
} catch (e) {
9081
const err = e instanceof Error ? e : new Error(String(e));
9182
throw new Error(`Failed to read TLS certificate from crtPath: ${err.message}`);

src/enclavedApp.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
* @prettier
33
*/
44
import express from 'express';
5-
import * as path from 'path';
5+
import path from 'path';
66
import debug from 'debug';
7-
import * as https from 'https';
8-
import * as http from 'http';
7+
import https from 'https';
8+
import http from 'http';
99
import morgan from 'morgan';
10-
import * as fs from 'fs';
10+
import fs from 'fs';
1111
import timeout from 'connect-timeout';
12-
import * as bodyParser from 'body-parser';
13-
import * as _ from 'lodash';
12+
import bodyParser from 'body-parser';
13+
import _ from 'lodash';
1414
import { SSL_OP_NO_TLSv1 } from 'constants';
15+
import pjson from '../package.json';
1516

1617
import { Config, config, TlsMode } from './config';
1718
import * as routes from './routes';
1819

1920
const debugLogger = debug('enclaved:express');
20-
const pjson = require('../package.json');
2121

2222
/**
2323
* Set up the logging middleware provided by morgan
@@ -80,16 +80,17 @@ function isTLS(config: Config): boolean {
8080
}
8181

8282
async function createHttpsServer(app: express.Application, config: Config): Promise<https.Server> {
83-
const { keyPath, crtPath, tlsKey, tlsCert, tlsMode, mtlsRequestCert, mtlsRejectUnauthorized } = config;
83+
const { keyPath, crtPath, tlsKey, tlsCert, tlsMode, mtlsRequestCert, mtlsRejectUnauthorized } =
84+
config;
8485
let key: string;
8586
let cert: string;
8687
if (tlsKey && tlsCert) {
8788
key = tlsKey;
8889
cert = tlsCert;
8990
console.log('Using TLS key and cert from environment variables');
9091
} else if (keyPath && crtPath) {
91-
const privateKeyPromise = require('fs').promises.readFile(keyPath, 'utf8');
92-
const certificatePromise = require('fs').promises.readFile(crtPath, 'utf8');
92+
const privateKeyPromise = fs.promises.readFile(keyPath, 'utf8');
93+
const certificatePromise = fs.promises.readFile(crtPath, 'utf8');
9394
[key, cert] = await Promise.all([privateKeyPromise, certificatePromise]);
9495
console.log(`Using TLS key and cert from files: ${keyPath}, ${crtPath}`);
9596
} else {
@@ -133,7 +134,10 @@ function createHttpServer(app: express.Application): http.Server {
133134
return http.createServer(app);
134135
}
135136

136-
export async function createServer(config: Config, app: express.Application): Promise<https.Server | http.Server> {
137+
export async function createServer(
138+
config: Config,
139+
app: express.Application,
140+
): Promise<https.Server | http.Server> {
137141
const server = isTLS(config) ? await createHttpsServer(app, config) : createHttpServer(app);
138142
if (config.keepAliveTimeout !== undefined) {
139143
server.keepAliveTimeout = config.keepAliveTimeout;
@@ -155,7 +159,12 @@ export function createBaseUri(config: Config): string {
155159
* Create error handling middleware
156160
*/
157161
function errorHandler() {
158-
return function (err: any, req: express.Request, res: express.Response, _next: express.NextFunction) {
162+
return function (
163+
err: any,
164+
req: express.Request,
165+
res: express.Response,
166+
_next: express.NextFunction,
167+
) {
159168
debugLogger('Error: ' + (err && err.message ? err.message : String(err)));
160169
const statusCode = err && err.status ? err.status : 500;
161170
const result = {
@@ -191,7 +200,11 @@ export function app(cfg: Config): express.Application {
191200
}
192201

193202
// Be more robust about accepting URLs with double slashes
194-
app.use(function replaceUrlSlashes(req: express.Request, res: express.Response, next: express.NextFunction) {
203+
app.use(function replaceUrlSlashes(
204+
req: express.Request,
205+
res: express.Response,
206+
next: express.NextFunction,
207+
) {
195208
req.url = req.url.replace(/\/{2,}/g, '/');
196209
next();
197210
});

src/routes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import express from 'express';
55
import debug from 'debug';
6+
import pjson from '../package.json';
67

78
const debugLogger = debug('enclaved:routes');
89

@@ -21,7 +22,6 @@ function handlePingExpress(_req: express.Request) {
2122
* Handler for version info
2223
*/
2324
function handleVersionInfo(_req: express.Request) {
24-
const pjson = require('../package.json');
2525
return {
2626
version: pjson.version,
2727
name: pjson.name,
@@ -37,7 +37,7 @@ function setupPingRoutes(app: express.Application) {
3737
app.get('/version', promiseWrapper(handleVersionInfo));
3838
}
3939

40-
function setupKeyGenRoutes(app: express.Application) {
40+
function setupKeyGenRoutes() {
4141
// Register additional routes here as needed
4242
debugLogger('KeyGen routes configured');
4343
}
@@ -51,7 +51,7 @@ export function setupRoutes(app: express.Application): void {
5151
setupPingRoutes(app);
5252

5353
// Register keygen routes
54-
setupKeyGenRoutes(app);
54+
setupKeyGenRoutes();
5555

5656
// Add a catch-all for unsupported routes
5757
app.use('*', (_req, res) => {

src/types.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @prettier
3+
*/
4+
export enum TlsMode {
5+
DISABLED = 'disabled', // No TLS (plain HTTP)
6+
ENABLED = 'enabled', // TLS with server cert only
7+
MTLS = 'mtls', // TLS with both server and client certs
8+
}
9+
10+
export interface Config {
11+
port: number;
12+
bind: string;
13+
ipc?: string;
14+
debugNamespace?: string[];
15+
// TLS settings
16+
keyPath?: string;
17+
crtPath?: string;
18+
tlsKey?: string;
19+
tlsCert?: string;
20+
tlsMode: TlsMode;
21+
// mTLS settings
22+
mtlsRequestCert?: boolean;
23+
mtlsRejectUnauthorized?: boolean;
24+
mtlsAllowedClientFingerprints?: string[];
25+
// Other settings
26+
logFile?: string;
27+
timeout: number;
28+
keepAliveTimeout?: number;
29+
headersTimeout?: number;
30+
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
"lib": ["es2018"],
66
"declaration": true,
77
"outDir": "./dist",
8-
"rootDir": "./src",
8+
"rootDir": ".",
99
"strict": true,
1010
"esModuleInterop": true,
1111
"skipLibCheck": true,
1212
"forceConsistentCasingInFileNames": true,
13+
"resolveJsonModule": true,
1314
"types": ["node", "jest"]
1415
},
15-
"include": ["src/**/*"],
16+
"include": ["src/**/*", "package.json"],
1617
"exclude": ["node_modules", "dist", "**/*.test.ts"]
1718
}

0 commit comments

Comments
 (0)