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
- return [ 'is_deleted' ] ;
12
+ return [ 'is_deleted' , 'owner.is_deleted' , 'certificate.is_deleted' ] ;
11
13
}
12
14
13
15
const internalStream = {
@@ -18,6 +20,12 @@ const internalStream = {
18
20
* @returns {Promise }
19
21
*/
20
22
create : ( access , data ) => {
23
+ const 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,16 +35,44 @@ const internalStream = {
27
35
data . meta = { } ;
28
36
}
29
37
38
+ // streams aren't routed by domain name so don't store domain names in the DB
39
+ let data_no_domains = structuredClone ( data ) ;
40
+ delete data_no_domains . domain_names ;
41
+
30
42
return streamModel
31
43
. query ( )
32
- . insertAndFetch ( data )
44
+ . insertAndFetch ( data_no_domains )
33
45
. then ( utils . omitRow ( omissions ( ) ) ) ;
34
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
+ } )
35
71
. then ( ( row ) => {
36
72
// Configure nginx
37
73
return internalNginx . configure ( streamModel , 'stream' , row )
38
74
. then ( ( ) => {
39
- return internalStream . get ( access , { id : row . id , expand : [ 'owner' ] } ) ;
75
+ return row ;
40
76
} ) ;
41
77
} )
42
78
. then ( ( row ) => {
@@ -60,6 +96,12 @@ const internalStream = {
60
96
* @return {Promise }
61
97
*/
62
98
update : ( access , data ) => {
99
+ const create_certificate = data . certificate_id === 'new' ;
100
+
101
+ if ( create_certificate ) {
102
+ delete data . certificate_id ;
103
+ }
104
+
63
105
return access . can ( 'streams:update' , data . id )
64
106
. then ( ( /*access_data*/ ) => {
65
107
// TODO: at this point the existing streams should have been checked
@@ -71,16 +113,32 @@ const internalStream = {
71
113
throw new error . InternalValidationError ( 'Stream could not be updated, IDs do not match: ' + row . id + ' !== ' + data . id ) ;
72
114
}
73
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
+
74
138
return streamModel
75
139
. query ( )
76
140
. patchAndFetchById ( row . id , data )
77
141
. then ( utils . omitRow ( omissions ( ) ) )
78
- . then ( ( saved_row ) => {
79
- return internalNginx . configure ( streamModel , 'stream' , saved_row )
80
- . then ( ( ) => {
81
- return internalStream . get ( access , { id : row . id , expand : [ 'owner' ] } ) ;
82
- } ) ;
83
- } )
84
142
. then ( ( saved_row ) => {
85
143
// Add to audit log
86
144
return internalAuditLog . add ( access , {
@@ -93,6 +151,17 @@ const internalStream = {
93
151
return saved_row ;
94
152
} ) ;
95
153
} ) ;
154
+ } )
155
+ . then ( ( ) => {
156
+ return internalStream . get ( access , { id : data . id , expand : [ 'owner' , 'certificate' ] } )
157
+ . then ( ( row ) => {
158
+ return internalNginx . configure ( streamModel , 'stream' , row )
159
+ . then ( ( new_meta ) => {
160
+ row . meta = new_meta ;
161
+ row = internalHost . cleanRowCertificateMeta ( row ) ;
162
+ return _ . omit ( row , omissions ( ) ) ;
163
+ } ) ;
164
+ } ) ;
96
165
} ) ;
97
166
} ,
98
167
@@ -115,7 +184,7 @@ const internalStream = {
115
184
. query ( )
116
185
. where ( 'is_deleted' , 0 )
117
186
. andWhere ( 'id' , data . id )
118
- . allowGraph ( '[owner]' )
187
+ . allowGraph ( '[owner,certificate ]' )
119
188
. first ( ) ;
120
189
121
190
if ( access_data . permission_visibility !== 'all' ) {
@@ -132,6 +201,7 @@ const internalStream = {
132
201
if ( ! row || ! row . id ) {
133
202
throw new error . ItemNotFoundError ( data . id ) ;
134
203
}
204
+ row = internalHost . cleanRowCertificateMeta ( row ) ;
135
205
// Custom omissions
136
206
if ( typeof data . omit !== 'undefined' && data . omit !== null ) {
137
207
row = _ . omit ( row , data . omit ) ;
@@ -197,14 +267,14 @@ const internalStream = {
197
267
. then ( ( ) => {
198
268
return internalStream . get ( access , {
199
269
id : data . id ,
200
- expand : [ 'owner' ]
270
+ expand : [ 'certificate' , ' owner']
201
271
} ) ;
202
272
} )
203
273
. then ( ( row ) => {
204
274
if ( ! row || ! row . id ) {
205
275
throw new error . ItemNotFoundError ( data . id ) ;
206
276
} else if ( row . enabled ) {
207
- throw new error . ValidationError ( 'Host is already enabled' ) ;
277
+ throw new error . ValidationError ( 'Stream is already enabled' ) ;
208
278
}
209
279
210
280
row . enabled = 1 ;
@@ -250,7 +320,7 @@ const internalStream = {
250
320
if ( ! row || ! row . id ) {
251
321
throw new error . ItemNotFoundError ( data . id ) ;
252
322
} else if ( ! row . enabled ) {
253
- throw new error . ValidationError ( 'Host is already disabled' ) ;
323
+ throw new error . ValidationError ( 'Stream is already disabled' ) ;
254
324
}
255
325
256
326
row . enabled = 0 ;
@@ -298,7 +368,7 @@ const internalStream = {
298
368
. query ( )
299
369
. where ( 'is_deleted' , 0 )
300
370
. groupBy ( 'id' )
301
- . allowGraph ( '[owner]' )
371
+ . allowGraph ( '[owner,certificate ]' )
302
372
. orderByRaw ( 'CAST(incoming_port AS INTEGER) ASC' ) ;
303
373
304
374
if ( access_data . permission_visibility !== 'all' ) {
@@ -317,6 +387,13 @@ const internalStream = {
317
387
}
318
388
319
389
return query . then ( utils . omitRows ( omissions ( ) ) ) ;
390
+ } )
391
+ . then ( ( rows ) => {
392
+ if ( typeof expand !== 'undefined' && expand !== null && expand . indexOf ( 'certificate' ) !== - 1 ) {
393
+ return internalHost . cleanAllRowsCertificateMeta ( rows ) ;
394
+ }
395
+
396
+ return rows ;
320
397
} ) ;
321
398
} ,
322
399
0 commit comments