@@ -54,6 +54,12 @@ if (process.env.NOOBAA_LOG_LEVEL) {
54
54
dbg_conf . endpoint . map ( module => dbg . set_module_level ( dbg_conf . level , module ) ) ;
55
55
}
56
56
57
+ const SERVICES_TYPES_ENUM = Object . freeze ( {
58
+ S3 : 'S3' ,
59
+ STS : 'STS' ,
60
+ IAM : 'IAM'
61
+ } ) ;
62
+
57
63
const new_umask = process . env . NOOBAA_ENDPOINT_UMASK || 0o000 ;
58
64
const old_umask = process . umask ( new_umask ) ;
59
65
let fork_count ;
@@ -114,10 +120,6 @@ async function main(options = {}) {
114
120
const metrics_port = options . metrics_port || config . EP_METRICS_SERVER_PORT ;
115
121
if ( fork_utils . start_workers ( metrics_port , fork_count ) ) return ;
116
122
117
- const http_port = options . http_port || config . ENDPOINT_PORT ;
118
- const https_port = options . https_port || config . ENDPOINT_SSL_PORT ;
119
- const https_port_sts = options . https_port_sts || Number ( process . env . ENDPOINT_SSL_PORT_STS ) || 7443 ;
120
- const https_port_iam = options . https_port_iam || config . ENDPOINT_SSL_IAM_PORT ;
121
123
const endpoint_group_id = process . env . ENDPOINT_GROUP_ID || 'default-endpoint-group' ;
122
124
123
125
const virtual_hosts = Object . freeze (
@@ -182,59 +184,25 @@ async function main(options = {}) {
182
184
init_request_sdk = create_init_request_sdk ( rpc , internal_rpc_client , object_io ) ;
183
185
}
184
186
185
- const endpoint_request_handler = create_endpoint_handler ( init_request_sdk , virtual_hosts , /*is_sts?*/ false ,
186
- bucket_logger , notification_logger ) ;
187
- const endpoint_request_handler_sts = create_endpoint_handler ( init_request_sdk , virtual_hosts , /*is_sts?*/ true ) ;
187
+ // START S3, STS & IAM SERVERS & CERTS
188
+ const http_port_s3 = options . http_port || config . ENDPOINT_PORT ;
189
+ const https_port_s3 = options . https_port || config . ENDPOINT_SSL_PORT ;
190
+ const https_port_sts = options . https_port_sts || Number ( process . env . ENDPOINT_SSL_PORT_STS ) || 7443 ; // || (process.env.NC_NSFS_NO_DB_ENV === 'true' ? -1 : 7443);
191
+ const https_port_iam = options . https_port_iam || config . ENDPOINT_SSL_IAM_PORT ;
188
192
189
- const ssl_cert_info = await ssl_utils . get_ssl_cert_info ( 'S3' , options . nsfs_config_root ) ;
190
- const https_server = await create_https_server ( ssl_cert_info , true , endpoint_request_handler ) ;
191
- const sts_ssl_cert_info = await ssl_utils . get_ssl_cert_info ( ' STS' ) ;
192
- const https_server_sts = await create_https_server ( sts_ssl_cert_info , true , endpoint_request_handler_sts ) ;
193
+ await start_server_and_cert ( SERVICES_TYPES_ENUM . S3 , init_request_sdk ,
194
+ { ... options , https_port : https_port_s3 , http_port : http_port_s3 , virtual_hosts , bucket_logger , notification_logger } ) ;
195
+ await start_server_and_cert ( SERVICES_TYPES_ENUM . STS , init_request_sdk , { https_port : https_port_sts , virtual_hosts } ) ;
196
+ await start_server_and_cert ( SERVICES_TYPES_ENUM . IAM , init_request_sdk , { https_port : https_port_iam } ) ;
193
197
194
- ssl_cert_info . on ( 'update' , updated_ssl_cert_info => {
195
- dbg . log0 ( "Setting updated S3 ssl certs for endpoint." ) ;
196
- const updated_ssl_options = { ...updated_ssl_cert_info . cert , honorCipherOrder : true } ;
197
- https_server . setSecureContext ( updated_ssl_options ) ;
198
- } ) ;
199
- sts_ssl_cert_info . on ( 'update' , updated_sts_ssl_cert_info => {
200
- dbg . log0 ( "Setting updated STS ssl certs for endpoint." ) ;
201
- const updated_ssl_options = { ...updated_sts_ssl_cert_info . cert , honorCipherOrder : true } ;
202
- https_server_sts . setSecureContext ( updated_ssl_options ) ;
203
- } ) ;
204
- if ( options . nsfs_config_root && ! config . ALLOW_HTTP ) {
205
- dbg . warn ( 'HTTP is not allowed for NC NSFS.' ) ;
206
- } else {
207
- const http_server = http . createServer ( endpoint_request_handler ) ;
208
- if ( http_port > 0 ) {
209
- dbg . log0 ( 'Starting S3 HTTP' , http_port ) ;
210
- await listen_http ( http_port , http_server ) ;
211
- dbg . log0 ( 'Started S3 HTTP successfully' ) ;
212
- }
213
- }
214
- if ( https_port > 0 ) {
215
- dbg . log0 ( 'Starting S3 HTTPS' , https_port ) ;
216
- await listen_http ( https_port , https_server ) ;
217
- dbg . log0 ( 'Started S3 HTTPS successfully' ) ;
218
- }
219
- if ( https_port_sts > 0 ) {
220
- dbg . log0 ( 'Starting STS HTTPS' , https_port_sts ) ;
221
- await listen_http ( https_port_sts , https_server_sts ) ;
222
- dbg . log0 ( 'Started STS HTTPS successfully' ) ;
223
- }
224
- if ( https_port_iam > 0 ) {
225
- dbg . log0 ( 'Starting IAM HTTPS' , https_port_iam ) ;
226
- const endpoint_request_handler_iam = create_endpoint_handler_iam ( init_request_sdk ) ;
227
- // NOTE: The IAM server currently uses the S3 server's certificate. This *will* cause route failures in Openshift.
228
- // TODO: Generate, mount and utilize an appropriate IAM certificate once the service and route are implemented
229
- const https_server_iam = await create_https_server ( ssl_cert_info , true , endpoint_request_handler_iam ) ;
230
- await listen_http ( https_port_iam , https_server_iam ) ;
231
- dbg . log0 ( 'Started IAM HTTPS successfully' ) ;
232
- }
198
+
199
+ // START METRICS SERVER
233
200
if ( metrics_port > 0 && cluster . isPrimary ) {
234
201
dbg . log0 ( 'Starting metrics server' , metrics_port ) ;
235
202
await prom_reporting . start_server ( metrics_port , false ) ;
236
203
dbg . log0 ( 'Started metrics server successfully' ) ;
237
204
}
205
+
238
206
// TODO: currently NC NSFS deployments don't have internal_rpc_client nor db,
239
207
// there for namespace monitor won't be registered
240
208
if ( internal_rpc_client && config . NAMESPACE_MONITOR_ENABLED ) {
@@ -271,54 +239,99 @@ async function main(options = {}) {
271
239
}
272
240
273
241
/**
274
- * @param {EndpointHandler } init_request_sdk
275
- * @param {readonly string[] } virtual_hosts
276
- * @returns {EndpointHandler }
242
+ * start_server_and_cert starts the server by type and options and creates a certificate if required
243
+ * @param {('S3'|'IAM'|'STS') } server_type
244
+ * @param {EndpointHandler } init_request_sdk
245
+ * @param {{ http_port?: number, https_port?: number, virtual_hosts?: readonly string[],
246
+ * bucket_logger?: PersistentLogger, notification_logger?: PersistentLogger,
247
+ * nsfs_config_root?: string}} options
277
248
*/
278
- function create_endpoint_handler ( init_request_sdk , virtual_hosts , sts , logger , notification_logger ) {
279
- const blob_rest_handler = process . env . ENDPOINT_BLOB_ENABLED === 'true' ? blob_rest : unavailable_handler ;
280
- const lambda_rest_handler = config . DB_TYPE === 'mongodb' ? lambda_rest : unavailable_handler ;
281
-
282
- /** @type {EndpointHandler } */
283
- const endpoint_request_handler = ( req , res ) => {
284
- endpoint_utils . set_noobaa_server_header ( res ) ;
285
- endpoint_utils . prepare_rest_request ( req ) ;
286
- req . virtual_hosts = virtual_hosts ;
287
- if ( logger ) req . bucket_logger = logger ;
288
- if ( notification_logger ) req . notification_logger = notification_logger ;
289
- init_request_sdk ( req , res ) ;
290
- if ( req . url . startsWith ( '/2015-03-31/functions' ) ) {
291
- return lambda_rest_handler ( req , res ) ;
292
- } else if ( req . headers [ 'x-ms-version' ] ) {
293
- return blob_rest_handler ( req , res ) ;
294
- } else if ( req . url . startsWith ( '/total_fork_count' ) ) {
295
- return fork_count_handler ( req , res ) ;
296
- } else if ( req . url . startsWith ( '/endpoint_fork_id' ) ) {
297
- return endpoint_fork_id_handler ( req , res ) ;
249
+ async function start_server_and_cert ( server_type , init_request_sdk , options = { } ) {
250
+ const { http_port, https_port, nsfs_config_root } = options ;
251
+ const endpoint_request_handler = create_endpoint_handler ( server_type , init_request_sdk , options ) ;
252
+
253
+ if ( server_type === SERVICES_TYPES_ENUM . S3 ) {
254
+ if ( nsfs_config_root && ! config . ALLOW_HTTP ) {
255
+ dbg . warn ( 'HTTP is not allowed for NC NSFS.' ) ;
298
256
} else {
299
- return s3_rest . handler ( req , res ) ;
257
+ const http_server = http . createServer ( endpoint_request_handler ) ;
258
+ if ( http_port > 0 ) {
259
+ dbg . log0 ( `Starting ${ server_type } HTTP - ${ http_port } ` ) ;
260
+ await listen_http ( http_port , http_server ) ;
261
+ dbg . log0 ( `Started ${ server_type } HTTP successfully` ) ;
262
+ }
300
263
}
301
- } ;
302
- /** @type {EndpointHandler } */
303
- const endpoint_sts_request_handler = ( req , res ) => {
304
- endpoint_utils . set_noobaa_server_header ( res ) ;
305
- endpoint_utils . prepare_rest_request ( req ) ;
306
- init_request_sdk ( req , res ) ;
307
- return sts_rest ( req , res ) ;
308
- } ;
309
-
310
- return sts ? endpoint_sts_request_handler : endpoint_request_handler ;
264
+ }
265
+ if ( https_port > 0 ) {
266
+ const ssl_cert_info = await ssl_utils . get_ssl_cert_info ( server_type , nsfs_config_root ) ;
267
+ const https_server = await create_https_server ( ssl_cert_info , true , endpoint_request_handler ) ;
268
+ ssl_cert_info . on ( 'update' , updated_ssl_cert_info => {
269
+ dbg . log0 ( `Setting updated ${ server_type } ssl certs for endpoint.` ) ;
270
+ const updated_ssl_options = { ...updated_ssl_cert_info . cert , honorCipherOrder : true } ;
271
+ https_server . setSecureContext ( updated_ssl_options ) ;
272
+ } ) ;
273
+ dbg . log0 ( `Starting ${ server_type } HTTPS - ${ https_port } ` ) ;
274
+ await listen_http ( https_port , https_server ) ;
275
+ dbg . log0 ( `Started ${ server_type } HTTPS successfully` ) ;
276
+ }
311
277
}
312
278
313
- function create_endpoint_handler_iam ( init_request_sdk ) {
314
- /** @type {EndpointHandler } */
315
- const endpoint_iam_request_handler = ( req , res ) => {
316
- endpoint_utils . set_noobaa_server_header ( res ) ;
317
- endpoint_utils . prepare_rest_request ( req ) ;
318
- init_request_sdk ( req , res ) ;
319
- return iam_rest ( req , res ) ;
320
- } ;
321
- return endpoint_iam_request_handler ;
279
+ /**
280
+ * @param {('S3'|'IAM'|'STS') } server_type
281
+ * @param {EndpointHandler } init_request_sdk
282
+ * @param {{virtual_hosts?: readonly string[], bucket_logger?: PersistentLogger, notification_logger?: PersistentLogger} } options
283
+ * @returns {EndpointHandler }
284
+ */
285
+ function create_endpoint_handler ( server_type , init_request_sdk , { virtual_hosts, bucket_logger, notification_logger } ) {
286
+ if ( server_type === SERVICES_TYPES_ENUM . S3 ) {
287
+ const blob_rest_handler = process . env . ENDPOINT_BLOB_ENABLED === 'true' ? blob_rest : unavailable_handler ;
288
+ const lambda_rest_handler = config . DB_TYPE === 'mongodb' ? lambda_rest : unavailable_handler ;
289
+
290
+ /** @type {EndpointHandler } */
291
+ const s3_endpoint_request_handler = ( req , res ) => {
292
+ endpoint_utils . set_noobaa_server_header ( res ) ;
293
+ endpoint_utils . prepare_rest_request ( req ) ;
294
+ req . virtual_hosts = virtual_hosts ;
295
+ if ( bucket_logger ) req . bucket_logger = bucket_logger ;
296
+ if ( notification_logger ) req . notification_logger = notification_logger ;
297
+ init_request_sdk ( req , res ) ;
298
+ if ( req . url . startsWith ( '/2015-03-31/functions' ) ) {
299
+ return lambda_rest_handler ( req , res ) ;
300
+ } else if ( req . headers [ 'x-ms-version' ] ) {
301
+ return blob_rest_handler ( req , res ) ;
302
+ } else if ( req . url . startsWith ( '/total_fork_count' ) ) {
303
+ return fork_count_handler ( req , res ) ;
304
+ } else if ( req . url . startsWith ( '/endpoint_fork_id' ) ) {
305
+ return endpoint_fork_id_handler ( req , res ) ;
306
+ } else {
307
+ return s3_rest . handler ( req , res ) ;
308
+ }
309
+ } ;
310
+ return s3_endpoint_request_handler ;
311
+ }
312
+
313
+ if ( server_type === SERVICES_TYPES_ENUM . STS ) {
314
+ /** @type {EndpointHandler } */
315
+ const sts_endpoint_request_handler = ( req , res ) => {
316
+ endpoint_utils . set_noobaa_server_header ( res ) ;
317
+ endpoint_utils . prepare_rest_request ( req ) ;
318
+ // req.virtual_hosts = virtual_hosts;
319
+ init_request_sdk ( req , res ) ;
320
+ return sts_rest ( req , res ) ;
321
+ } ;
322
+ return sts_endpoint_request_handler ;
323
+ }
324
+
325
+ if ( server_type === SERVICES_TYPES_ENUM . IAM ) {
326
+ /** @type {EndpointHandler } */
327
+ const iam_endpoint_request_handler = ( req , res ) => {
328
+ endpoint_utils . set_noobaa_server_header ( res ) ;
329
+ endpoint_utils . prepare_rest_request ( req ) ;
330
+ init_request_sdk ( req , res ) ;
331
+ return iam_rest ( req , res ) ;
332
+ } ;
333
+ return iam_endpoint_request_handler ;
334
+ }
322
335
}
323
336
324
337
function endpoint_fork_id_handler ( req , res ) {
@@ -547,7 +560,6 @@ function setup_http_server(server) {
547
560
548
561
exports . main = main ;
549
562
exports . create_endpoint_handler = create_endpoint_handler ;
550
- exports . create_endpoint_handler_iam = create_endpoint_handler_iam ;
551
563
exports . create_init_request_sdk = create_init_request_sdk ;
552
564
553
565
if ( require . main === module ) main ( ) ;
0 commit comments