88 EnvironmentName ,
99} from './types' ;
1010import logger from './logger' ;
11+ import { validateTlsCertificates , validateMasterExpressConfig } from './shared/appUtils' ;
1112
1213export { Config , EnclavedConfig , MasterExpressConfig , TlsMode , AppMode , EnvironmentName } ;
1314
@@ -37,6 +38,8 @@ function determineAppMode(): AppMode {
3738 throw new Error ( `Invalid APP_MODE: ${ mode } . Must be either "enclaved" or "master-express"` ) ;
3839}
3940
41+ export { determineAppMode } ;
42+
4043// ============================================================================
4144// ENCLAVED MODE CONFIGURATION
4245// ============================================================================
@@ -130,20 +133,30 @@ function configureEnclavedMode(): EnclavedConfig {
130133 if ( ! config . tlsKey && config . keyPath ) {
131134 try {
132135 config = { ...config , tlsKey : fs . readFileSync ( config . keyPath , 'utf-8' ) } ;
136+ logger . info ( `Successfully loaded TLS private key from file: ${ config . keyPath } ` ) ;
133137 } catch ( e ) {
134138 const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
135139 throw new Error ( `Failed to read TLS key from keyPath: ${ err . message } ` ) ;
136140 }
141+ } else if ( config . tlsKey ) {
142+ logger . debug ( 'Using TLS private key from environment variable' ) ;
137143 }
144+
138145 if ( ! config . tlsCert && config . crtPath ) {
139146 try {
140147 config = { ...config , tlsCert : fs . readFileSync ( config . crtPath , 'utf-8' ) } ;
148+ logger . info ( `Successfully loaded TLS certificate from file: ${ config . crtPath } ` ) ;
141149 } catch ( e ) {
142150 const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
143151 throw new Error ( `Failed to read TLS certificate from crtPath: ${ err . message } ` ) ;
144152 }
153+ } else if ( config . tlsCert ) {
154+ logger . debug ( 'Using TLS certificate from environment variable' ) ;
145155 }
146156
157+ // Validate that certificates are properly loaded when TLS is enabled
158+ validateTlsCertificates ( config ) ;
159+
147160 return config ;
148161}
149162
@@ -280,18 +293,25 @@ export function configureMasterExpressMode(): MasterExpressConfig {
280293 if ( ! config . tlsKey && config . keyPath ) {
281294 try {
282295 config = { ...config , tlsKey : fs . readFileSync ( config . keyPath , 'utf-8' ) } ;
296+ logger . info ( `Successfully loaded TLS private key from file: ${ config . keyPath } ` ) ;
283297 } catch ( e ) {
284298 const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
285299 throw new Error ( `Failed to read TLS key from keyPath: ${ err . message } ` ) ;
286300 }
301+ } else if ( config . tlsKey ) {
302+ logger . debug ( 'Using TLS private key from environment variable' ) ;
287303 }
304+
288305 if ( ! config . tlsCert && config . crtPath ) {
289306 try {
290307 config = { ...config , tlsCert : fs . readFileSync ( config . crtPath , 'utf-8' ) } ;
308+ logger . info ( `Successfully loaded TLS certificate from file: ${ config . crtPath } ` ) ;
291309 } catch ( e ) {
292310 const err = e instanceof Error ? e : new Error ( String ( e ) ) ;
293311 throw new Error ( `Failed to read TLS certificate from crtPath: ${ err . message } ` ) ;
294312 }
313+ } else if ( config . tlsCert ) {
314+ logger . debug ( 'Using TLS certificate from environment variable' ) ;
295315 }
296316
297317 // Handle cert loading
@@ -302,6 +322,12 @@ export function configureMasterExpressMode(): MasterExpressConfig {
302322 ...config ,
303323 enclavedExpressCert : fs . readFileSync ( config . enclavedExpressCert , 'utf-8' ) ,
304324 } ;
325+ logger . info (
326+ `Successfully loaded Enclaved Express certificate from file: ${ config . enclavedExpressCert . substring (
327+ 0 ,
328+ 50 ,
329+ ) } ...`,
330+ ) ;
305331 } else {
306332 throw new Error ( `Certificate file not found: ${ config . enclavedExpressCert } ` ) ;
307333 }
@@ -311,6 +337,12 @@ export function configureMasterExpressMode(): MasterExpressConfig {
311337 }
312338 }
313339
340+ // Validate that certificates are properly loaded when TLS is enabled
341+ validateTlsCertificates ( config ) ;
342+
343+ // Validate Master Express configuration
344+ validateMasterExpressConfig ( config ) ;
345+
314346 return config ;
315347}
316348
0 commit comments