1
- const _ = require ( 'lodash' ) ;
2
- const error = require ( '../lib/error' ) ;
3
- const utils = require ( '../lib/utils' ) ;
4
- const streamModel = require ( '../models/stream' ) ;
5
- const internalNginx = require ( './nginx' ) ;
6
- const internalAuditLog = require ( './audit-log' ) ;
1
+ const _ = require ( 'lodash' ) ;
2
+ const error = require ( '../lib/error' ) ;
3
+ const utils = require ( '../lib/utils' ) ;
4
+ const streamModel = require ( '../models/stream' ) ;
5
+ const internalNginx = require ( './nginx' ) ;
6
+ const internalAuditLog = require ( './audit-log' ) ;
7
+ const internalCertificate = require ( './certificate' ) ;
8
+ const internalHost = require ( './host' ) ;
7
9
8
10
function omissions ( ) {
9
11
return [ 'is_deleted' ] ;
@@ -17,6 +19,12 @@ const internalStream = {
17
19
* @returns {Promise }
18
20
*/
19
21
create : ( access , data ) => {
22
+ let create_certificate = data . certificate_id === 'new' ;
23
+
24
+ if ( create_certificate ) {
25
+ delete data . certificate_id ;
26
+ }
27
+
20
28
return access . can ( 'streams:create' , data )
21
29
. then ( ( /*access_data*/ ) => {
22
30
// TODO: At this point the existing ports should have been checked
@@ -26,11 +34,40 @@ const internalStream = {
26
34
data . meta = { } ;
27
35
}
28
36
37
+ let data_no_domains = structuredClone ( data ) ;
38
+
39
+ // streams aren't routed by ___domain name so don't store ___domain names in the DB
40
+ delete data_no_domains . domain_names ;
41
+
29
42
return streamModel
30
43
. query ( )
31
- . insertAndFetch ( data )
44
+ . insertAndFetch ( data_no_domains )
32
45
. then ( utils . omitRow ( omissions ( ) ) ) ;
33
46
} )
47
+ . then ( ( row ) => {
48
+ if ( create_certificate ) {
49
+ return internalCertificate . createQuickCertificate ( access , data )
50
+ . then ( ( cert ) => {
51
+ // update host with cert id
52
+ return internalStream . update ( access , {
53
+ id : row . id ,
54
+ certificate_id : cert . id
55
+ } ) ;
56
+ } )
57
+ . then ( ( ) => {
58
+ return row ;
59
+ } ) ;
60
+ } else {
61
+ return row ;
62
+ }
63
+ } )
64
+ . then ( ( row ) => {
65
+ // re-fetch with cert
66
+ return internalStream . get ( access , {
67
+ id : row . id ,
68
+ expand : [ 'certificate' , 'owner' ]
69
+ } ) ;
70
+ } )
34
71
. then ( ( row ) => {
35
72
// Configure nginx
36
73
return internalNginx . configure ( streamModel , 'stream' , row )
@@ -59,6 +96,12 @@ const internalStream = {
59
96
* @return {Promise }
60
97
*/
61
98
update : ( access , data ) => {
99
+ let create_certificate = data . certificate_id === 'new' ;
100
+
101
+ if ( create_certificate ) {
102
+ delete data . certificate_id ;
103
+ }
104
+
62
105
return access . can ( 'streams:update' , data . id )
63
106
. then ( ( /*access_data*/ ) => {
64
107
// TODO: at this point the existing streams should have been checked
@@ -70,6 +113,28 @@ const internalStream = {
70
113
throw new error . InternalValidationError ( 'Stream could not be updated, IDs do not match: ' + row . id + ' !== ' + data . id ) ;
71
114
}
72
115
116
+ if ( create_certificate ) {
117
+ return internalCertificate . createQuickCertificate ( access , {
118
+ domain_names : data . domain_names || row . domain_names ,
119
+ meta : _ . assign ( { } , row . meta , data . meta )
120
+ } )
121
+ . then ( ( cert ) => {
122
+ // update host with cert id
123
+ data . certificate_id = cert . id ;
124
+ } )
125
+ . then ( ( ) => {
126
+ return row ;
127
+ } ) ;
128
+ } else {
129
+ return row ;
130
+ }
131
+ } )
132
+ . then ( ( row ) => {
133
+ // Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
134
+ data = _ . assign ( { } , {
135
+ domain_names : row . domain_names
136
+ } , data ) ;
137
+
73
138
return streamModel
74
139
. query ( )
75
140
. patchAndFetchById ( row . id , data )
@@ -114,7 +179,7 @@ const internalStream = {
114
179
. query ( )
115
180
. where ( 'is_deleted' , 0 )
116
181
. andWhere ( 'id' , data . id )
117
- . allowGraph ( '[owner]' )
182
+ . allowGraph ( '[owner,certificate ]' )
118
183
. first ( ) ;
119
184
120
185
if ( access_data . permission_visibility !== 'all' ) {
@@ -131,6 +196,7 @@ const internalStream = {
131
196
if ( ! row ) {
132
197
throw new error . ItemNotFoundError ( data . id ) ;
133
198
}
199
+ row = internalHost . cleanRowCertificateMeta ( row ) ;
134
200
// Custom omissions
135
201
if ( typeof data . omit !== 'undefined' && data . omit !== null ) {
136
202
row = _ . omit ( row , data . omit ) ;
@@ -196,14 +262,14 @@ const internalStream = {
196
262
. then ( ( ) => {
197
263
return internalStream . get ( access , {
198
264
id : data . id ,
199
- expand : [ 'owner' ]
265
+ expand : [ 'certificate' , ' owner']
200
266
} ) ;
201
267
} )
202
268
. then ( ( row ) => {
203
269
if ( ! row ) {
204
270
throw new error . ItemNotFoundError ( data . id ) ;
205
271
} else if ( row . enabled ) {
206
- throw new error . ValidationError ( 'Host is already enabled' ) ;
272
+ throw new error . ValidationError ( 'Stream is already enabled' ) ;
207
273
}
208
274
209
275
row . enabled = 1 ;
@@ -249,7 +315,7 @@ const internalStream = {
249
315
if ( ! row ) {
250
316
throw new error . ItemNotFoundError ( data . id ) ;
251
317
} else if ( ! row . enabled ) {
252
- throw new error . ValidationError ( 'Host is already disabled' ) ;
318
+ throw new error . ValidationError ( 'Stream is already disabled' ) ;
253
319
}
254
320
255
321
row . enabled = 0 ;
@@ -297,7 +363,7 @@ const internalStream = {
297
363
. query ( )
298
364
. where ( 'is_deleted' , 0 )
299
365
. groupBy ( 'id' )
300
- . allowGraph ( '[owner]' )
366
+ . allowGraph ( '[owner,certificate ]' )
301
367
. orderBy ( 'incoming_port' , 'ASC' ) ;
302
368
303
369
if ( access_data . permission_visibility !== 'all' ) {
@@ -316,6 +382,13 @@ const internalStream = {
316
382
}
317
383
318
384
return query . then ( utils . omitRows ( omissions ( ) ) ) ;
385
+ } )
386
+ . then ( ( rows ) => {
387
+ if ( typeof expand !== 'undefined' && expand !== null && expand . indexOf ( 'certificate' ) !== - 1 ) {
388
+ return internalHost . cleanAllRowsCertificateMeta ( rows ) ;
389
+ }
390
+
391
+ return rows ;
319
392
} ) ;
320
393
} ,
321
394
0 commit comments