3
3
import re
4
4
from six import string_types
5
5
6
- from samtranslator .model .intrinsics import ref
7
- from samtranslator .model .intrinsics import make_conditional , fnSub
6
+ from samtranslator .model .intrinsics import ref , is_intrinsic_no_value
7
+ from samtranslator .model .intrinsics import make_conditional , fnSub , is_intrinsic_if
8
8
from samtranslator .model .exceptions import InvalidDocumentException , InvalidTemplateException
9
9
10
10
@@ -804,7 +804,6 @@ def add_resource_policy(self, resource_policy, path, api_id, stage):
804
804
ip_range_blacklist = resource_policy .get ("IpRangeBlacklist" )
805
805
source_vpc_whitelist = resource_policy .get ("SourceVpcWhitelist" )
806
806
source_vpc_blacklist = resource_policy .get ("SourceVpcBlacklist" )
807
- custom_statements = resource_policy .get ("CustomStatements" )
808
807
809
808
if aws_account_whitelist is not None :
810
809
resource_list = self ._get_method_path_uri_list (path , api_id , stage )
@@ -824,16 +823,16 @@ def add_resource_policy(self, resource_policy, path, api_id, stage):
824
823
825
824
if source_vpc_whitelist is not None :
826
825
resource_list = self ._get_method_path_uri_list (path , api_id , stage )
827
- for endpoint in source_vpc_whitelist :
828
- self ._add_vpc_resource_policy_for_method (endpoint , "StringNotEquals" , resource_list )
826
+ self ._add_vpc_resource_policy_for_method (source_vpc_whitelist , "StringNotEquals" , resource_list )
829
827
830
828
if source_vpc_blacklist is not None :
831
829
resource_list = self ._get_method_path_uri_list (path , api_id , stage )
832
- for endpoint in source_vpc_blacklist :
833
- self ._add_vpc_resource_policy_for_method (endpoint , "StringEquals" , resource_list )
830
+ self ._add_vpc_resource_policy_for_method (source_vpc_blacklist , "StringEquals" , resource_list )
834
831
835
- if custom_statements is not None :
836
- self ._add_custom_statement (custom_statements )
832
+ self ._doc [self ._X_APIGW_POLICY ] = self .resource_policy
833
+
834
+ def add_custom_statements (self , custom_statements ):
835
+ self ._add_custom_statement (custom_statements )
837
836
838
837
self ._doc [self ._X_APIGW_POLICY ] = self .resource_policy
839
838
@@ -932,24 +931,33 @@ def _add_ip_resource_policy_for_method(self, ip_list, conditional, resource_list
932
931
statement .extend ([deny_statement ])
933
932
self .resource_policy ["Statement" ] = statement
934
933
935
- def _add_vpc_resource_policy_for_method (self , vpc , conditional , resource_list ):
934
+ def _add_vpc_resource_policy_for_method (self , endpoint_list , conditional , resource_list ):
936
935
"""
937
936
This method generates a policy statement to grant/deny specific VPC/VPCE access to the API method and
938
937
appends it to the swagger under `x-amazon-apigateway-policy`
939
938
:raises ValueError: If the conditional passed in does not match the allowed values.
940
939
"""
941
- if not vpc :
940
+ if not endpoint_list :
942
941
return
943
942
944
943
if conditional not in ["StringNotEquals" , "StringEquals" ]:
945
944
raise ValueError ("Conditional must be one of {}" .format (["StringNotEquals" , "StringEquals" ]))
946
945
947
946
vpce_regex = r"^vpce-"
948
- if not re .match (vpce_regex , vpc ):
949
- endpoint = "aws:SourceVpc"
950
- else :
951
- endpoint = "aws:SourceVpce"
952
-
947
+ vpc_regex = r"^vpc-"
948
+ vpc_list = []
949
+ vpce_list = []
950
+ for endpoint in endpoint_list :
951
+ if re .match (vpce_regex , endpoint ):
952
+ vpce_list .append (endpoint )
953
+ if re .match (vpc_regex , endpoint ):
954
+ vpc_list .append (endpoint )
955
+
956
+ condition = {}
957
+ if vpc_list :
958
+ condition ["aws:SourceVpc" ] = vpc_list
959
+ if vpce_list :
960
+ condition ["aws:SourceVpce" ] = vpce_list
953
961
self .resource_policy ["Version" ] = "2012-10-17"
954
962
allow_statement = {}
955
963
allow_statement ["Effect" ] = "Allow"
@@ -962,7 +970,7 @@ def _add_vpc_resource_policy_for_method(self, vpc, conditional, resource_list):
962
970
deny_statement ["Action" ] = "execute-api:Invoke"
963
971
deny_statement ["Resource" ] = resource_list
964
972
deny_statement ["Principal" ] = "*"
965
- deny_statement ["Condition" ] = {conditional : { endpoint : vpc } }
973
+ deny_statement ["Condition" ] = {conditional : condition }
966
974
967
975
if self .resource_policy .get ("Statement" ) is None :
968
976
self .resource_policy ["Statement" ] = [allow_statement , deny_statement ]
@@ -980,16 +988,17 @@ def _add_custom_statement(self, custom_statements):
980
988
if custom_statements is None :
981
989
return
982
990
983
- if not isinstance (custom_statements , list ):
984
- custom_statements = [custom_statements ]
985
-
986
991
self .resource_policy ["Version" ] = "2012-10-17"
987
992
if self .resource_policy .get ("Statement" ) is None :
988
993
self .resource_policy ["Statement" ] = custom_statements
989
994
else :
995
+ if not isinstance (custom_statements , list ):
996
+ custom_statements = [custom_statements ]
997
+
990
998
statement = self .resource_policy ["Statement" ]
991
999
if not isinstance (statement , list ):
992
1000
statement = [statement ]
1001
+
993
1002
for s in custom_statements :
994
1003
if s not in statement :
995
1004
statement .append (s )
0 commit comments