@@ -129,7 +129,8 @@ def add_path(self, path, method=None):
129
129
130
130
path_dict .setdefault (method , {})
131
131
132
- def add_lambda_integration (self , path , method , integration_uri , condition = None ):
132
+ def add_lambda_integration (self , path , method , integration_uri ,
133
+ method_auth_config = None , api_auth_config = None , condition = None ):
133
134
"""
134
135
Adds aws_proxy APIGW integration to the given path+method.
135
136
@@ -156,6 +157,15 @@ def add_lambda_integration(self, path, method, integration_uri, condition=None):
156
157
'uri' : integration_uri
157
158
}
158
159
160
+ method_auth_config = method_auth_config or {}
161
+ api_auth_config = api_auth_config or {}
162
+ if method_auth_config .get ('Authorizer' ) == 'AWS_IAM' \
163
+ or api_auth_config .get ('DefaultAuthorizer' ) == 'AWS_IAM' and not method_auth_config :
164
+ self .paths [path ][method ][self ._X_APIGW_INTEGRATION ]['credentials' ] = self ._generate_integration_credentials (
165
+ method_invoke_role = method_auth_config .get ('InvokeRole' ),
166
+ api_invoke_role = api_auth_config .get ('InvokeRole' )
167
+ )
168
+
159
169
# If 'responses' key is *not* present, add it with an empty dict as value
160
170
path_dict [method ].setdefault ('responses' , {})
161
171
@@ -169,6 +179,13 @@ def make_path_conditional(self, path, condition):
169
179
"""
170
180
self .paths [path ] = make_conditional (condition , self .paths [path ])
171
181
182
+ def _generate_integration_credentials (self , method_invoke_role = None , api_invoke_role = None ):
183
+ return self ._get_invoke_role (method_invoke_role or api_invoke_role )
184
+
185
+ def _get_invoke_role (self , invoke_role ):
186
+ CALLER_CREDENTIALS_ARN = 'arn:aws:iam::*:user/*'
187
+ return invoke_role if invoke_role and invoke_role != 'CALLER_CREDENTIALS' else CALLER_CREDENTIALS_ARN
188
+
172
189
def iter_on_path (self ):
173
190
"""
174
191
Yields all the paths available in the Swagger. As a caller, if you add new paths to Swagger while iterating,
@@ -409,7 +426,6 @@ def add_auth_to_method(self, path, method_name, auth, api):
409
426
def set_method_authorizer (self , path , method_name , authorizer_name , authorizers , default_authorizer ,
410
427
is_default = False ):
411
428
normalized_method_name = self ._normalize_method_name (method_name )
412
-
413
429
# It is possible that the method could have two definitions in a Fn::If block.
414
430
for method_definition in self .get_method_contents (self .get_path (path )[normalized_method_name ]):
415
431
@@ -418,7 +434,10 @@ def set_method_authorizer(self, path, method_name, authorizer_name, authorizers,
418
434
continue
419
435
existing_security = method_definition .get ('security' , [])
420
436
# TEST: [{'sigv4': []}, {'api_key': []}])
421
- authorizer_names = set (authorizers .keys ())
437
+ authorizer_list = ['AWS_IAM' ]
438
+ if authorizers :
439
+ authorizer_list .extend (authorizers .keys ())
440
+ authorizer_names = set (authorizer_list )
422
441
existing_non_authorizer_security = []
423
442
existing_authorizer_security = []
424
443
@@ -473,6 +492,22 @@ def set_method_authorizer(self, path, method_name, authorizer_name, authorizers,
473
492
if security :
474
493
method_definition ['security' ] = security
475
494
495
+ # The first element of the method_definition['security'] should be AWS_IAM
496
+ # because authorizer_list = ['AWS_IAM'] is hardcoded above
497
+ if 'AWS_IAM' in method_definition ['security' ][0 ]:
498
+ aws_iam_security_definition = {
499
+ 'AWS_IAM' : {
500
+ 'x-amazon-apigateway-authtype' : 'awsSigv4' ,
501
+ 'type' : 'apiKey' ,
502
+ 'name' : 'Authorization' ,
503
+ 'in' : 'header'
504
+ }
505
+ }
506
+ if not self .security_definitions :
507
+ self .security_definitions = aws_iam_security_definition
508
+ elif 'AWS_IAM' not in self .security_definitions :
509
+ self .security_definitions .update (aws_iam_security_definition )
510
+
476
511
@property
477
512
def swagger (self ):
478
513
"""
0 commit comments