Skip to content

Commit e9c03fa

Browse files
authored
Merge pull request #27 from BitGo/WP-00000-fix-config-loading
chore: fix loading the certs repetetively
2 parents e9a899e + eb46240 commit e9c03fa

File tree

13 files changed

+27
-28
lines changed

13 files changed

+27
-28
lines changed

src/__tests__/config.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'should';
2-
import { config, isEnclavedConfig, TlsMode } from '../config';
2+
import { initConfig, isEnclavedConfig, TlsMode } from '../initConfig';
33

44
describe('Configuration', () => {
55
const originalEnv = process.env;
@@ -17,14 +17,14 @@ describe('Configuration', () => {
1717
});
1818

1919
it('should throw error when APP_MODE is not set', () => {
20-
(() => config()).should.throw(
20+
(() => initConfig()).should.throw(
2121
'APP_MODE environment variable is required. Set APP_MODE to either "enclaved" or "master-express"',
2222
);
2323
});
2424

2525
it('should throw error when APP_MODE is invalid', () => {
2626
process.env.APP_MODE = 'invalid';
27-
(() => config()).should.throw(
27+
(() => initConfig()).should.throw(
2828
'Invalid APP_MODE: invalid. Must be either "enclaved" or "master-express"',
2929
);
3030
});
@@ -39,7 +39,7 @@ describe('Configuration', () => {
3939
});
4040

4141
it('should use default configuration when no environment variables are set', () => {
42-
const cfg = config();
42+
const cfg = initConfig();
4343
isEnclavedConfig(cfg).should.be.true();
4444
if (isEnclavedConfig(cfg)) {
4545
cfg.port.should.equal(3080);
@@ -54,7 +54,7 @@ describe('Configuration', () => {
5454

5555
it('should read port from environment variable', () => {
5656
process.env.ENCLAVED_EXPRESS_PORT = '4000';
57-
const cfg = config();
57+
const cfg = initConfig();
5858
isEnclavedConfig(cfg).should.be.true();
5959
if (isEnclavedConfig(cfg)) {
6060
cfg.port.should.equal(4000);
@@ -67,7 +67,7 @@ describe('Configuration', () => {
6767
it('should read TLS mode from environment variables', () => {
6868
// Test with TLS disabled
6969
process.env.TLS_MODE = 'disabled';
70-
let cfg = config();
70+
let cfg = initConfig();
7171
isEnclavedConfig(cfg).should.be.true();
7272
if (isEnclavedConfig(cfg)) {
7373
cfg.tlsMode.should.equal(TlsMode.DISABLED);
@@ -76,7 +76,7 @@ describe('Configuration', () => {
7676

7777
// Test with mTLS explicitly enabled
7878
process.env.TLS_MODE = 'mtls';
79-
cfg = config();
79+
cfg = initConfig();
8080
isEnclavedConfig(cfg).should.be.true();
8181
if (isEnclavedConfig(cfg)) {
8282
cfg.tlsMode.should.equal(TlsMode.MTLS);
@@ -87,13 +87,13 @@ describe('Configuration', () => {
8787

8888
// Test with invalid TLS mode
8989
process.env.TLS_MODE = 'invalid';
90-
(() => config()).should.throw(
90+
(() => initConfig()).should.throw(
9191
'Invalid TLS_MODE: invalid. Must be either "disabled" or "mtls"',
9292
);
9393

9494
// Test with no TLS mode (should default to MTLS)
9595
delete process.env.TLS_MODE;
96-
cfg = config();
96+
cfg = initConfig();
9797
isEnclavedConfig(cfg).should.be.true();
9898
if (isEnclavedConfig(cfg)) {
9999
cfg.tlsMode.should.equal(TlsMode.MTLS);
@@ -108,7 +108,7 @@ describe('Configuration', () => {
108108
process.env.MTLS_REJECT_UNAUTHORIZED = 'true';
109109
process.env.MTLS_ALLOWED_CLIENT_FINGERPRINTS = 'ABC123,DEF456';
110110

111-
const cfg = config();
111+
const cfg = initConfig();
112112
isEnclavedConfig(cfg).should.be.true();
113113
if (isEnclavedConfig(cfg)) {
114114
cfg.mtlsRequestCert!.should.be.true();

src/api/enclaved/postIndependentKey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function postIndependentKey(
1212

1313
// setup clients
1414
const bitgo: BitGo = req.bitgo;
15-
const kms = new KmsClient();
15+
const kms = new KmsClient(req.config);
1616

1717
// create public and private key pairs on BitGo SDK
1818
const coin = bitgo.coin(req.params.coin);

src/api/enclaved/signMultisigTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function signMultisigTransaction(
1919
}
2020

2121
const bitgo = req.bitgo;
22-
const kms = new KmsClient();
22+
const kms = new KmsClient(req.config);
2323

2424
// Retrieve the private key from KMS
2525
let prv: string;

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { determineAppMode, AppMode } from './config';
1+
import { determineAppMode, AppMode } from './initConfig';
22
import * as enclavedApp from './enclavedApp';
33
import * as masterExpressApp from './masterExpressApp';
44
import logger from './logger';

src/enclavedApp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import http from 'http';
44
import morgan from 'morgan';
55
import { SSL_OP_NO_TLSv1, SSL_OP_NO_TLSv1_1 } from 'constants';
66

7-
import { EnclavedConfig, config, TlsMode, isEnclavedConfig } from './config';
7+
import { EnclavedConfig, initConfig, TlsMode, isEnclavedConfig } from './initConfig';
88
import { setupRoutes } from './routes/enclaved';
99
import {
1010
setupLogging,
@@ -117,7 +117,7 @@ export function app(cfg: EnclavedConfig): express.Application {
117117
}
118118

119119
export async function init(): Promise<void> {
120-
const cfg = config();
120+
const cfg = initConfig();
121121

122122
// Type-safe validation that we're in enclaved mode
123123
if (!isEnclavedConfig(cfg)) {

src/config.ts renamed to src/initConfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ function masterExpressEnvConfig(): Partial<MasterExpressConfig> {
214214
throw new Error('ENCLAVED_EXPRESS_URL environment variable is required and cannot be empty');
215215
}
216216

217-
if (!enclavedExpressCert) {
218-
throw new Error('ENCLAVED_EXPRESS_CERT environment variable is required and cannot be empty');
217+
if (tlsMode === TlsMode.MTLS && !enclavedExpressCert) {
218+
throw new Error('ENCLAVED_EXPRESS_CERT environment variable is required for MTLS mode.');
219219
}
220220

221221
// Debug mTLS environment variables
@@ -375,7 +375,7 @@ export function configureMasterExpressMode(): MasterExpressConfig {
375375
// MAIN CONFIG FUNCTION
376376
// ============================================================================
377377

378-
export function config(): Config {
378+
export function initConfig(): Config {
379379
const appMode = determineAppMode();
380380

381381
if (appMode === AppMode.ENCLAVED) {

src/kms/kmsClient.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import debug from 'debug';
22
import * as superagent from 'superagent';
3-
import { config, isMasterExpressConfig } from '../config';
3+
import { EnclavedConfig, isMasterExpressConfig } from '../initConfig';
44
import { PostKeyKmsSchema, PostKeyParams, PostKeyResponse } from './types/postKey';
55
import { GetKeyKmsSchema, GetKeyParams, GetKeyResponse } from './types/getKey';
66

@@ -9,8 +9,7 @@ const debugLogger = debug('bitgo:express:kmsClient');
99
export class KmsClient {
1010
private readonly url: string;
1111

12-
constructor() {
13-
const cfg = config();
12+
constructor(cfg: EnclavedConfig) {
1413
if (isMasterExpressConfig(cfg)) {
1514
throw new Error('Configuration is not in enclaved express mode');
1615
}

src/masterBitgoExpress/handleSendMany.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RequestTracer, PrebuildTransactionOptions, Memo, KeyIndices } from '@bi
22
import { createEnclavedExpressClient } from './enclavedExpressClient';
33
import logger from '../logger';
44
import { MasterApiSpecRouteRequest } from './routers/masterApiSpec';
5-
import { isMasterExpressConfig } from '../config';
5+
import { isMasterExpressConfig } from '../initConfig';
66

77
/**
88
* Defines the structure for a single recipient in a send-many transaction.

src/masterBitgoExpress/routers/enclavedExpressHealth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createRouter, type WrappedRouter } from '@api-ts/typed-express-router';
44
import { Response } from '@api-ts/response';
55
import https from 'https';
66
import superagent from 'superagent';
7-
import { MasterExpressConfig, TlsMode } from '../../config';
7+
import { MasterExpressConfig, TlsMode } from '../../initConfig';
88
import logger from '../../logger';
99
import { responseHandler } from '../../shared/middleware';
1010

src/masterBitgoExpress/routers/masterApiSpec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { Response } from '@api-ts/response';
1515
import express from 'express';
1616
import { BitGoRequest } from '../../types/request';
17-
import { MasterExpressConfig } from '../../config';
17+
import { MasterExpressConfig } from '../../initConfig';
1818
import { handleGenerateWalletOnPrem } from '../generateWallet';
1919
import { prepareBitGo, responseHandler } from '../../shared/middleware';
2020
import { handleSendMany } from '../handleSendMany';

0 commit comments

Comments
 (0)