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