@@ -57,9 +57,22 @@ const defaultEnclavedConfig: EnclavedConfig = {
5757} ;
5858
5959function determineTlsMode ( ) : TlsMode {
60- const disableTls = readEnvVar ( 'MASTER_BITGO_EXPRESS_DISABLE_TLS' ) === 'true' ;
61- if ( disableTls ) return TlsMode . DISABLED ;
62- return TlsMode . MTLS ;
60+ const tlsMode = readEnvVar ( 'TLS_MODE' ) ?. toLowerCase ( ) ;
61+
62+ if ( ! tlsMode ) {
63+ logger . warn ( 'TLS_MODE not set, defaulting to MTLS. Set TLS_MODE=disabled to disable TLS.' ) ;
64+ return TlsMode . MTLS ;
65+ }
66+
67+ if ( tlsMode === 'disabled' ) {
68+ return TlsMode . DISABLED ;
69+ }
70+
71+ if ( tlsMode === 'mtls' ) {
72+ return TlsMode . MTLS ;
73+ }
74+
75+ throw new Error ( `Invalid TLS_MODE: ${ tlsMode } . Must be either "disabled" or "mtls"` ) ;
6376}
6477
6578function enclavedEnvConfig ( ) : Partial < EnclavedConfig > {
@@ -129,33 +142,36 @@ function configureEnclavedMode(): EnclavedConfig {
129142 const env = enclavedEnvConfig ( ) ;
130143 let config = mergeEnclavedConfigs ( env ) ;
131144
132- // Handle file loading for TLS certificates
133- if ( ! config . tlsKey && config . keyPath ) {
134- try {
135- config = { ...config , tlsKey : fs . readFileSync ( config . keyPath , 'utf-8' ) } ;
136- logger . info ( `Successfully loaded TLS private key from file: ${ config . keyPath } ` ) ;
137- } catch ( e ) {
138- const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
139- throw new Error ( `Failed to read TLS key from keyPath: ${ err . message } ` ) ;
145+ // Only load certificates if TLS is enabled
146+ if ( config . tlsMode !== TlsMode . DISABLED ) {
147+ // Handle file loading for TLS certificates
148+ if ( ! config . tlsKey && config . keyPath ) {
149+ try {
150+ config = { ...config , tlsKey : fs . readFileSync ( config . keyPath , 'utf-8' ) } ;
151+ logger . info ( `Successfully loaded TLS private key from file: ${ config . keyPath } ` ) ;
152+ } catch ( e ) {
153+ const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
154+ throw new Error ( `Failed to read TLS key from keyPath: ${ err . message } ` ) ;
155+ }
156+ } else if ( config . tlsKey ) {
157+ logger . debug ( 'Using TLS private key from environment variable' ) ;
140158 }
141- } else if ( config . tlsKey ) {
142- logger . debug ( 'Using TLS private key from environment variable' ) ;
143- }
144159
145- if ( ! config . tlsCert && config . crtPath ) {
146- try {
147- config = { ...config , tlsCert : fs . readFileSync ( config . crtPath , 'utf-8' ) } ;
148- logger . info ( `Successfully loaded TLS certificate from file: ${ config . crtPath } ` ) ;
149- } catch ( e ) {
150- const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
151- throw new Error ( `Failed to read TLS certificate from crtPath: ${ err . message } ` ) ;
160+ if ( ! config . tlsCert && config . crtPath ) {
161+ try {
162+ config = { ...config , tlsCert : fs . readFileSync ( config . crtPath , 'utf-8' ) } ;
163+ logger . info ( `Successfully loaded TLS certificate from file: ${ config . crtPath } ` ) ;
164+ } catch ( e ) {
165+ const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
166+ throw new Error ( `Failed to read TLS certificate from crtPath: ${ err . message } ` ) ;
167+ }
168+ } else if ( config . tlsCert ) {
169+ logger . debug ( 'Using TLS certificate from environment variable' ) ;
152170 }
153- } else if ( config . tlsCert ) {
154- logger . debug ( 'Using TLS certificate from environment variable' ) ;
155- }
156171
157- // Validate that certificates are properly loaded when TLS is enabled
158- validateTlsCertificates ( config ) ;
172+ // Validate that certificates are properly loaded when TLS is enabled
173+ validateTlsCertificates ( config ) ;
174+ }
159175
160176 return config ;
161177}
@@ -289,32 +305,38 @@ export function configureMasterExpressMode(): MasterExpressConfig {
289305 }
290306 config = { ...config , ...updates } ;
291307
292- // Handle file loading for TLS certificates
293- if ( ! config . tlsKey && config . keyPath ) {
294- try {
295- config = { ...config , tlsKey : fs . readFileSync ( config . keyPath , 'utf-8' ) } ;
296- logger . info ( `Successfully loaded TLS private key from file: ${ config . keyPath } ` ) ;
297- } catch ( e ) {
298- const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
299- throw new Error ( `Failed to read TLS key from keyPath: ${ err . message } ` ) ;
308+ // Only load certificates if TLS is enabled
309+ if ( config . tlsMode !== TlsMode . DISABLED ) {
310+ // Handle file loading for TLS certificates
311+ if ( ! config . tlsKey && config . keyPath ) {
312+ try {
313+ config = { ...config , tlsKey : fs . readFileSync ( config . keyPath , 'utf-8' ) } ;
314+ logger . info ( `Successfully loaded TLS private key from file: ${ config . keyPath } ` ) ;
315+ } catch ( e ) {
316+ const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
317+ throw new Error ( `Failed to read TLS key from keyPath: ${ err . message } ` ) ;
318+ }
319+ } else if ( config . tlsKey ) {
320+ logger . debug ( 'Using TLS private key from environment variable' ) ;
300321 }
301- } else if ( config . tlsKey ) {
302- logger . debug ( 'Using TLS private key from environment variable' ) ;
303- }
304322
305- if ( ! config . tlsCert && config . crtPath ) {
306- try {
307- config = { ...config , tlsCert : fs . readFileSync ( config . crtPath , 'utf-8' ) } ;
308- logger . info ( `Successfully loaded TLS certificate from file: ${ config . crtPath } ` ) ;
309- } catch ( e ) {
310- const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
311- throw new Error ( `Failed to read TLS certificate from crtPath: ${ err . message } ` ) ;
323+ if ( ! config . tlsCert && config . crtPath ) {
324+ try {
325+ config = { ...config , tlsCert : fs . readFileSync ( config . crtPath , 'utf-8' ) } ;
326+ logger . info ( `Successfully loaded TLS certificate from file: ${ config . crtPath } ` ) ;
327+ } catch ( e ) {
328+ const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
329+ throw new Error ( `Failed to read TLS certificate from crtPath: ${ err . message } ` ) ;
330+ }
331+ } else if ( config . tlsCert ) {
332+ logger . debug ( 'Using TLS certificate from environment variable' ) ;
312333 }
313- } else if ( config . tlsCert ) {
314- logger . debug ( 'Using TLS certificate from environment variable' ) ;
334+
335+ // Validate that certificates are properly loaded when TLS is enabled
336+ validateTlsCertificates ( config ) ;
315337 }
316338
317- // Handle cert loading
339+ // Handle cert loading for Enclaved Express (always required for Master Express)
318340 if ( config . enclavedExpressCert ) {
319341 try {
320342 if ( fs . existsSync ( config . enclavedExpressCert ) ) {
@@ -337,9 +359,6 @@ export function configureMasterExpressMode(): MasterExpressConfig {
337359 }
338360 }
339361
340- // Validate that certificates are properly loaded when TLS is enabled
341- validateTlsCertificates ( config ) ;
342-
343362 // Validate Master Express configuration
344363 validateMasterExpressConfig ( config ) ;
345364
0 commit comments