1
- " use strict" ;
2
- var fs = require ( "fs" ) ;
3
- var https = require ( " https" ) ;
1
+ ' use strict' ;
2
+ var fs = require ( 'fs' ) ;
3
+ var https = require ( ' https' ) ;
4
4
5
5
class ServerlessPrivateAWSRegions {
6
6
constructor ( serverless , options ) {
7
7
this . serverless = serverless ;
8
8
this . sdk = this . serverless . providers . aws . sdk ;
9
- this . options = options ;
9
+ this . options = options || { } ;
10
10
11
11
this . commands = {
12
12
region_setup : {
13
- usage : " Sets up Serverless Framework to work in private AWS regions" ,
14
- lifecycleEvents : [ " setup" ]
13
+ usage : ' Sets up Serverless Framework to work in private AWS regions' ,
14
+ lifecycleEvents : [ ' setup' ]
15
15
}
16
16
} ;
17
17
18
18
this . hooks = {
19
- " region_setup:setup" : this . setup . bind ( this ) ,
20
- " before:aws:common:validate:validate" : this . prepRegion . bind ( this ) ,
21
- " before:deploy:deploy" : this . prepRegion . bind ( this ) ,
22
- " before:remove:remove" : this . prepRegion . bind ( this ) ,
23
- " before:deploy:function:initialize" : this . prepRegion . bind ( this ) ,
24
- " after:aws:package:finalize:mergeCustomProviderResources" : this . updatePrincipals . bind (
19
+ ' region_setup:setup' : this . setup . bind ( this ) ,
20
+ ' before:aws:common:validate:validate' : this . prepRegion . bind ( this ) ,
21
+ ' before:deploy:deploy' : this . prepRegion . bind ( this ) ,
22
+ ' before:remove:remove' : this . prepRegion . bind ( this ) ,
23
+ ' before:deploy:function:initialize' : this . prepRegion . bind ( this ) ,
24
+ ' after:aws:package:finalize:mergeCustomProviderResources' : this . updatePrincipals . bind (
25
25
this
26
26
)
27
27
} ;
@@ -70,12 +70,12 @@ class ServerlessPrivateAWSRegions {
70
70
if ( bundle ) {
71
71
} else {
72
72
throw new this . serverless . classes . Error (
73
- " Make sure to define the AWS_CA_BUNDLE environment variable"
73
+ ' Make sure to define the AWS_CA_BUNDLE environment variable'
74
74
) ;
75
75
}
76
76
const certs = [ fs . readFileSync ( bundle ) ] ;
77
77
this . sdk . config . region = this . serverless . service . provider . region ;
78
- this . sdk . config . signatureVersion = "v4" ;
78
+ this . sdk . config . signatureVersion = 'v4' ;
79
79
var endpoint = this . getCustomEndpoint ( ) ;
80
80
if ( endpoint ) {
81
81
this . sdk . config . endpoint = endpoint ;
@@ -96,36 +96,40 @@ class ServerlessPrivateAWSRegions {
96
96
const template_resources = this . serverless . service . provider
97
97
. compiledCloudFormationTemplate . Resources ;
98
98
Object . keys ( template_resources ) . forEach ( resource => {
99
- if ( template_resources [ resource ] . Type == " AWS::Lambda::Permission" ) {
99
+ if ( template_resources [ resource ] . Type == ' AWS::Lambda::Permission' ) {
100
100
// now check principal
101
101
var principal = template_resources [ resource ] . Properties . Principal ;
102
- if ( typeof principal == " string" ) {
103
- service = principal . split ( "." ) [ 0 ] ;
102
+ if ( typeof principal == ' string' ) {
103
+ service = principal . split ( '.' ) [ 0 ] ;
104
104
105
- var new_principal = custom_principals . find ( x => x . service === service ) ;
105
+ var new_principal = custom_principals . find (
106
+ x => x . service === service
107
+ ) ;
106
108
107
109
if ( new_principal ) {
108
- new_principal = new_principal . principal
110
+ new_principal = new_principal . principal ;
109
111
this . pluginLog (
110
112
`Changing Principal from ${ principal } to ${ new_principal } `
111
113
) ;
112
114
principal = new_principal ;
113
115
}
114
- } else if ( " Fn::Join" in principal ) {
116
+ } else if ( ' Fn::Join' in principal ) {
115
117
// using the join intrinsic function to piece together the principal
116
- var join_principal = principal [ "Fn::Join" ] [ 1 ] [ 0 ] ;
117
- var service = join_principal . replace ( / \. + $ / , "" ) ;
118
- var new_principal = custom_principals . find ( x => x . service === service ) ;
118
+ var join_principal = principal [ 'Fn::Join' ] [ 1 ] [ 0 ] ;
119
+ var service = join_principal . replace ( / \. + $ / , '' ) ;
120
+ var new_principal = custom_principals . find (
121
+ x => x . service === service
122
+ ) ;
119
123
120
124
if ( new_principal ) {
121
- new_principal = new_principal . principal
125
+ new_principal = new_principal . principal ;
122
126
this . pluginLog (
123
127
`Changing Principal from ${ principal } to ${ new_principal } `
124
128
) ;
125
129
principal = new_principal ;
126
130
}
127
131
} else {
128
- console . log ( " something else" ) ;
132
+ console . log ( ' something else' ) ;
129
133
console . log ( typeof principal ) ;
130
134
}
131
135
template_resources [ resource ] . Properties . Principal = principal ;
@@ -137,40 +141,40 @@ class ServerlessPrivateAWSRegions {
137
141
if ( ! s3_custom ) {
138
142
return ;
139
143
}
140
- if ( ! s3_custom [ " pattern" ] || ! s3_custom [ " return" ] ) {
144
+ if ( ! s3_custom [ ' pattern' ] || ! s3_custom [ ' return' ] ) {
141
145
throw new this . serverless . classes . Error (
142
- " For custom regions, define both a `pattern` and `return` value for the S3Endpoint"
146
+ ' For custom regions, define both a `pattern` and `return` value for the S3Endpoint'
143
147
) ;
144
148
}
145
149
var linesToAdd = [ ] ;
146
- if ( s3_custom [ " comment" ] ) {
147
- linesToAdd . push ( `// ${ s3_custom [ " comment" ] } ` ) ;
150
+ if ( s3_custom [ ' comment' ] ) {
151
+ linesToAdd . push ( `// ${ s3_custom [ ' comment' ] } ` ) ;
148
152
}
149
153
const custom_endpoint_line = `if (strRegion.match(/${
150
- s3_custom [ " pattern" ]
151
- } /)) return \`${ s3_custom [ " return" ] } \`;`. replace ( / \\ / g, "" ) ;
154
+ s3_custom [ ' pattern' ]
155
+ } /)) return \`${ s3_custom [ ' return' ] } \`;`. replace ( / \\ / g, '' ) ;
152
156
153
157
linesToAdd . push ( custom_endpoint_line ) ;
154
158
const filePath = `${ this . serverless . config . serverlessPath } /plugins/aws/utils/getS3EndpointForRegion.js` ;
155
159
this . addLinesToFile (
156
160
filePath ,
157
- " const strRegion = region.toLowerCase();" ,
161
+ ' const strRegion = region.toLowerCase();' ,
158
162
linesToAdd ,
159
163
2
160
164
) ;
161
165
}
162
166
163
167
addLinesToFile ( filePath , findLine , appendedLines , prepending_spaces = 0 ) {
164
- this . pluginLog ( `Adding \n${ appendedLines . join ( "\n" ) } \n\nto ${ filePath } ` ) ;
168
+ this . pluginLog ( `Adding \n${ appendedLines . join ( '\n' ) } \n\nto ${ filePath } ` ) ;
165
169
this . restoreOrig ( filePath ) ;
166
170
this . backupOrig ( filePath ) ;
167
171
var file_text = fs
168
172
. readFileSync ( filePath )
169
173
. toString ( )
170
- . split ( "\n" ) ;
174
+ . split ( '\n' ) ;
171
175
const trimmed = file_text . map ( s => s . trim ( ) ) ;
172
176
var appendedLines = appendedLines . map ( s => {
173
- return `${ " " . repeat ( prepending_spaces ) } ${ s } ` ;
177
+ return `${ ' ' . repeat ( prepending_spaces ) } ${ s } ` ;
174
178
} ) ;
175
179
const line_no = trimmed . indexOf ( findLine ) ;
176
180
if ( line_no < 0 ) {
@@ -179,7 +183,7 @@ class ServerlessPrivateAWSRegions {
179
183
) ;
180
184
} else {
181
185
file_text . splice ( line_no + 1 , 0 , ...appendedLines ) ;
182
- fs . writeFileSync ( filePath , file_text . join ( "\n" ) , err => {
186
+ fs . writeFileSync ( filePath , file_text . join ( '\n' ) , err => {
183
187
if ( err ) throw err ;
184
188
this . pluginLog ( `Updated ${ filePath } ` ) ;
185
189
} ) ;
@@ -203,7 +207,7 @@ class ServerlessPrivateAWSRegions {
203
207
}
204
208
205
209
setup ( ) {
206
- this . pluginLog ( " Running setup for private region" ) ;
210
+ this . pluginLog ( ' Running setup for private region' ) ;
207
211
this . alterS3EndpointFunction ( ) ;
208
212
this . configureAwsSdk ( ) ;
209
213
}
0 commit comments