@@ -86,7 +86,7 @@ const internalAccessList = {
86
86
// re-fetch with expansions
87
87
return internalAccessList . get ( access , {
88
88
id : data . id ,
89
- expand : [ 'owner' , 'items' , 'clients' , 'clientcas' , 'proxy_hosts.access_list.[clientcas.certificate ,clients,items]' ]
89
+ expand : [ 'owner' , 'items' , 'clients' , 'clientcas.certificate ' , 'proxy_hosts.access_list.[clientcas,clients,items]' ]
90
90
} , true /* <- skip masking */ ) ;
91
91
} )
92
92
. then ( ( row ) => {
@@ -261,10 +261,11 @@ const internalAccessList = {
261
261
// re-fetch with expansions
262
262
return internalAccessList . get ( access , {
263
263
id : data . id ,
264
- expand : [ 'owner' , 'items' , 'clients' , 'clientcas' , 'proxy_hosts.[certificate,access_list.[clientcas.certificate,clients,items]]' ]
264
+ expand : [ 'owner' , 'items' , 'clients' , 'clientcas.certificate ' , 'proxy_hosts.[certificate,access_list.[clientcas.certificate,clients,items]]' ]
265
265
} , true /* <- skip masking */ ) ;
266
266
} )
267
267
. then ( ( row ) => {
268
+ console . log ( row ) ;
268
269
return internalAccessList . build ( row )
269
270
. then ( ( ) => {
270
271
if ( row . proxy_host_count ) {
@@ -299,7 +300,7 @@ const internalAccessList = {
299
300
. joinRaw ( 'LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0' )
300
301
. where ( 'access_list.is_deleted' , 0 )
301
302
. andWhere ( 'access_list.id' , data . id )
302
- . withGraphFetched ( '[owner,items,clients,clientcas,proxy_hosts.[certificate,access_list.[clientcas.certificate,clients,items]]]' )
303
+ . allowGraph ( '[owner,items,clients,clientcas.certificate ,proxy_hosts.[certificate,access_list.[clientcas.certificate,clients,items]]]' )
303
304
. first ( ) ;
304
305
305
306
if ( access_data . permission_visibility !== 'all' ) {
@@ -420,7 +421,7 @@ const internalAccessList = {
420
421
. joinRaw ( 'LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0' )
421
422
. where ( 'access_list.is_deleted' , 0 )
422
423
. groupBy ( 'access_list.id' )
423
- . withGraphFetched ( '[owner,items,clients,clientcas.certificate]' )
424
+ . allowGraph ( '[owner,items,clients,clientcas.certificate]' )
424
425
. orderBy ( 'access_list.name' , 'ASC' ) ;
425
426
426
427
if ( access_data . permission_visibility !== 'all' ) {
@@ -508,17 +509,27 @@ const internalAccessList = {
508
509
return '/data/access/' + list . id ;
509
510
} ,
510
511
512
+ /**
513
+ * @param {Object } list
514
+ * @param {Integer } list.id
515
+ * @returns {String }
516
+ */
517
+ getClientCAFilename : ( list ) => {
518
+ return '/data/clientca/' + list . id ;
519
+ } ,
520
+
511
521
/**
512
522
* @param {Object } list
513
523
* @param {Integer } list.id
514
524
* @param {String } list.name
515
525
* @param {Array } list.items
526
+ * @param {Array } list.clientcas
516
527
* @returns {Promise }
517
528
*/
518
529
build : ( list ) => {
519
- logger . info ( 'Building Access file #' + list . id + ' for: ' + list . name ) ;
520
530
521
- return new Promise ( ( resolve , reject ) => {
531
+ const htPasswdBuild = new Promise ( ( resolve , reject ) => {
532
+ logger . info ( 'Building Access file #' + list . id + ' for: ' + list . name ) ;
522
533
let htpasswd_file = internalAccessList . getFilename ( list ) ;
523
534
524
535
// 1. remove any existing access file
@@ -566,6 +577,39 @@ const internalAccessList = {
566
577
} ) ;
567
578
}
568
579
} ) ;
580
+
581
+ const caCertificateBuild = new Promise ( ( resolve , reject ) => {
582
+ // TODO: we need to ensure this rebuild is run if any certificates change
583
+ logger . info ( 'Building Client CA file #' + list . id + ' for: ' + list . name ) ;
584
+ let clientca_file = internalAccessList . getClientCAFilename ( list ) ;
585
+
586
+ const certificate_bodies = list . clientcas
587
+ . filter ( ( clientca ) => {
588
+ return clientca . certificate . meta === undefined ;
589
+ } )
590
+ . map ( ( clientca ) => {
591
+ return clientca . certificate . meta . certificate ;
592
+ } ) ;
593
+
594
+ // Unlink the original file (nginx retains file handle till reload)
595
+ try {
596
+ fs . unlinkSync ( clientca_file ) ;
597
+ } catch ( err ) {
598
+ // do nothing
599
+ }
600
+
601
+ // Write the new file in one shot
602
+ try {
603
+ fs . writeFileSync ( clientca_file , certificate_bodies . join ( '\n' ) , { encoding : 'utf8' } ) ;
604
+ logger . success ( 'Built Client CA file #' + list . id + ' for: ' + list . name ) ;
605
+ resolve ( clientca_file ) ;
606
+ } catch ( err ) {
607
+ reject ( err ) ;
608
+ }
609
+ } ) ;
610
+
611
+ // Execute both promises concurrently
612
+ return Promise . all ( [ htPasswdBuild , caCertificateBuild ] ) ;
569
613
}
570
614
} ;
571
615
0 commit comments