11import express from 'express' ;
22import https from 'https' ;
33import http from 'http' ;
4- import superagent from 'superagent' ;
5- import { BitGo , BitGoOptions } from 'bitgo' ;
6- import { BitGoBase } from '@bitgo/sdk-core' ;
7- import { version } from 'bitgo/package.json' ;
84import { SSL_OP_NO_TLSv1 , SSL_OP_NO_TLSv1_1 } from 'constants' ;
95
106import { MasterExpressConfig , config , isMasterExpressConfig , TlsMode } from './config' ;
11- import { BitGoRequest } from './types/request' ;
127import {
138 setupLogging ,
149 setupCommonMiddleware ,
1510 createErrorHandler ,
1611 createHttpServer ,
1712 configureServerTimeouts ,
1813 prepareIpc ,
19- setupHealthCheckRoutes ,
2014 createMtlsMiddleware ,
2115} from './shared/appUtils' ;
22- import bodyParser from 'body-parser' ;
23- import { promiseWrapper } from './routes' ;
24- import pjson from '../package.json' ;
25- import { handleGenerateWalletOnPrem } from './masterBitgoExpress/generateWallet' ;
2616import logger from './logger' ;
27-
28- const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${ pjson . version } BitGoJS/${ version } ` ;
17+ import { setupRoutes } from './routes/master' ;
2918
3019/**
3120 * Create a startup function which will be run upon server initialization
@@ -53,57 +42,6 @@ function isTLS(config: MasterExpressConfig): boolean {
5342 return Boolean ( ( keyPath && crtPath ) || ( tlsKey && tlsCert ) ) ;
5443}
5544
56- const expressJSONParser = bodyParser . json ( { limit : '20mb' } ) ;
57-
58- /**
59- * Perform body parsing here only on routes we want
60- */
61- function parseBody ( req : express . Request , res : express . Response , next : express . NextFunction ) {
62- // Set the default Content-Type, in case the client doesn't set it. If
63- // Content-Type isn't specified, Express silently refuses to parse the
64- // request body.
65- req . headers [ 'content-type' ] = req . headers [ 'content-type' ] || 'application/json' ;
66- return expressJSONParser ( req , res , next ) ;
67- }
68-
69- /**
70- * Create the bitgo object in the request
71- * @param config
72- */
73- function prepareBitGo ( config : MasterExpressConfig ) {
74- const { env, customRootUri } = config ;
75-
76- return function prepBitGo (
77- req : express . Request ,
78- res : express . Response ,
79- next : express . NextFunction ,
80- ) {
81- // Get access token
82- let accessToken ;
83- if ( req . headers . authorization ) {
84- const authSplit = req . headers . authorization . split ( ' ' ) ;
85- if ( authSplit . length === 2 && authSplit [ 0 ] . toLowerCase ( ) === 'bearer' ) {
86- accessToken = authSplit [ 1 ] ;
87- }
88- }
89- const userAgent = req . headers [ 'user-agent' ]
90- ? BITGOEXPRESS_USER_AGENT + ' ' + req . headers [ 'user-agent' ]
91- : BITGOEXPRESS_USER_AGENT ;
92-
93- const bitgoConstructorParams : BitGoOptions = {
94- env,
95- customRootURI : customRootUri ,
96- accessToken,
97- userAgent,
98- } ;
99-
100- ( req as BitGoRequest ) . bitgo = new BitGo ( bitgoConstructorParams ) as unknown as BitGoBase ;
101- ( req as BitGoRequest ) . config = config ;
102-
103- next ( ) ;
104- } ;
105- }
106-
10745async function createHttpsServer (
10846 app : express . Application ,
10947 config : MasterExpressConfig ,
@@ -145,70 +83,6 @@ export function createBaseUri(config: MasterExpressConfig): string {
14583 return `http${ ssl ? 's' : '' } ://${ bind } ${ ! isStandardPort ? ':' + port : '' } ` ;
14684}
14785
148- /**
149- * Setup master express specific routes
150- */
151- function setupMasterExpressRoutes ( app : express . Application , cfg : MasterExpressConfig ) : void {
152- // Setup common health check routes
153- setupHealthCheckRoutes ( app , 'master express' ) ;
154-
155- // Add enclaved express ping route
156- app . post ( '/ping/enclavedExpress' , async ( req , res ) => {
157- try {
158- logger . debug ( 'Pinging enclaved express' ) ;
159-
160- let response ;
161- if ( cfg . tlsMode === TlsMode . MTLS ) {
162- // Use Master Express's own certificate as client cert when connecting to Enclaved Express
163- const httpsAgent = new https . Agent ( {
164- rejectUnauthorized : ! cfg . allowSelfSigned ,
165- ca : cfg . enclavedExpressCert ,
166- // Provide client certificate for mTLS
167- key : cfg . tlsKey ,
168- cert : cfg . tlsCert ,
169- } ) ;
170-
171- response = await superagent
172- . post ( `${ cfg . enclavedExpressUrl } /ping` )
173- . ca ( cfg . enclavedExpressCert )
174- . agent ( httpsAgent )
175- . send ( ) ;
176- } else {
177- // When TLS is disabled, use plain HTTP without any TLS configuration
178- response = await superagent . post ( `${ cfg . enclavedExpressUrl } /ping` ) . send ( ) ;
179- }
180-
181- res . json ( {
182- status : 'Successfully pinged enclaved express' ,
183- enclavedResponse : response . body ,
184- } ) ;
185- } catch ( error ) {
186- logger . error ( 'Failed to ping enclaved express:' , { error } ) ;
187- res . status ( 500 ) . json ( {
188- error : 'Failed to ping enclaved express' ,
189- details : error instanceof Error ? error . message : String ( error ) ,
190- } ) ;
191- }
192- } ) ;
193-
194- // TODO: Add api-ts to these new API routes
195- app . post (
196- '/api/:coin/wallet/generate' ,
197- parseBody ,
198- prepareBitGo ( cfg ) ,
199- promiseWrapper ( handleGenerateWalletOnPrem ) ,
200- ) ;
201-
202- // Add a catch-all for unsupported routes
203- app . use ( '*' , ( _req , res ) => {
204- res . status ( 404 ) . json ( {
205- error : 'Route not found or not supported in master express mode' ,
206- } ) ;
207- } ) ;
208-
209- logger . debug ( 'Master express routes configured' ) ;
210- }
211-
21286/**
21387 * Create and configure the express application for master express mode
21488 */
@@ -228,7 +102,8 @@ export function app(cfg: MasterExpressConfig): express.Application {
228102 }
229103
230104 // Setup master express routes
231- setupMasterExpressRoutes ( app , cfg ) ;
105+ setupRoutes ( app , cfg ) ;
106+ setupRoutes ( app , cfg ) ;
232107
233108 // Add error handler
234109 app . use ( createErrorHandler ( ) ) ;
0 commit comments