@@ -24,6 +24,7 @@ module.exports = {
24
24
validateHandlerProperty ( funcObject , functionName ) ;
25
25
validateEventsProperty ( funcObject , functionName ) ;
26
26
validateVpcConnectorProperty ( funcObject , functionName ) ;
27
+ validateIamProperty ( funcObject , functionName ) ;
27
28
28
29
const funcTemplate = getFunctionTemplate (
29
30
funcObject ,
@@ -51,6 +52,11 @@ module.exports = {
51
52
_ . get ( this , 'serverless.service.provider.environment' ) ,
52
53
funcObject . environment // eslint-disable-line comma-dangle
53
54
) ;
55
+ funcTemplate . accessControl . gcpIamPolicy . bindings = _ . unionBy (
56
+ _ . get ( funcObject , 'iam.bindings' ) ,
57
+ _ . get ( this , 'serverless.service.provider.iam.bindings' ) ,
58
+ 'role'
59
+ ) ;
54
60
55
61
if ( ! funcTemplate . properties . serviceAccountEmail ) {
56
62
delete funcTemplate . properties . serviceAccountEmail ;
@@ -83,16 +89,30 @@ module.exports = {
83
89
84
90
funcTemplate . properties . httpsTrigger = { } ;
85
91
funcTemplate . properties . httpsTrigger . url = url ;
92
+
93
+ if ( funcObject . allowUnauthenticated ) {
94
+ funcTemplate . accessControl . gcpIamPolicy . bindings = _ . unionBy (
95
+ [ { role : 'roles/cloudfunctions.invoker' , members : [ 'allUsers' ] } ] ,
96
+ funcTemplate . accessControl . gcpIamPolicy . bindings ,
97
+ 'role'
98
+ ) ;
99
+ }
86
100
}
87
101
if ( eventType === 'event' ) {
88
102
const type = funcObject . events [ 0 ] . event . eventType ;
89
103
const path = funcObject . events [ 0 ] . event . path ; //eslint-disable-line
90
104
const resource = funcObject . events [ 0 ] . event . resource ;
105
+ const failurePolicy = funcObject . events [ 0 ] . event . failurePolicy ;
91
106
92
107
funcTemplate . properties . eventTrigger = { } ;
93
108
funcTemplate . properties . eventTrigger . eventType = type ;
94
109
if ( path ) funcTemplate . properties . eventTrigger . path = path ;
95
110
funcTemplate . properties . eventTrigger . resource = resource ;
111
+ if ( failurePolicy ) funcTemplate . properties . eventTrigger . failurePolicy = failurePolicy ;
112
+ }
113
+
114
+ if ( ! funcTemplate . accessControl . gcpIamPolicy . bindings . length ) {
115
+ delete funcTemplate . accessControl ;
96
116
}
97
117
98
118
this . serverless . service . provider . compiledConfigurationTemplate . resources . push ( funcTemplate ) ;
@@ -157,6 +177,29 @@ const validateVpcConnectorProperty = (funcObject, functionName) => {
157
177
}
158
178
} ;
159
179
180
+ const validateIamProperty = ( funcObject , functionName ) => {
181
+ if ( _ . get ( funcObject , 'iam.bindings' ) && funcObject . iam . bindings . length > 0 ) {
182
+ funcObject . iam . bindings . forEach ( ( binding ) => {
183
+ if ( ! binding . role ) {
184
+ const errorMessage = [
185
+ `The function "${ functionName } " has no role specified for an IAM binding.` ,
186
+ ' Each binding requires a role. For details on supported roles, see the documentation' ,
187
+ ' at: https://cloud.google.com/iam/docs/understanding-roles' ,
188
+ ] . join ( '' ) ;
189
+ throw new Error ( errorMessage ) ;
190
+ }
191
+ if ( ! Array . isArray ( binding . members ) || ! binding . members . length ) {
192
+ const errorMessage = [
193
+ `The function "${ functionName } " has no members specified for an IAM binding.` ,
194
+ ' Each binding requires at least one member to be assigned. See the IAM documentation' ,
195
+ ' for details on configuring members: https://cloud.google.com/iam/docs/overview' ,
196
+ ] . join ( '' ) ;
197
+ throw new Error ( errorMessage ) ;
198
+ }
199
+ } ) ;
200
+ }
201
+ } ;
202
+
160
203
const getFunctionTemplate = ( funcObject , projectName , region , sourceArchiveUrl ) => {
161
204
//eslint-disable-line
162
205
return {
@@ -168,8 +211,13 @@ const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl)
168
211
runtime : 'nodejs8' ,
169
212
timeout : '60s' ,
170
213
entryPoint : funcObject . handler ,
171
- function : funcObject . name ,
214
+ function : funcObject . useLegacyNaming ? funcObject . handler : funcObject . name ,
172
215
sourceArchiveUrl,
173
216
} ,
217
+ accessControl : {
218
+ gcpIamPolicy : {
219
+ bindings : [ ] ,
220
+ } ,
221
+ } ,
174
222
} ;
175
223
} ;
0 commit comments