1010import json
1111import os
1212import logging
13+ import re
1314
1415from aws_lambda_builders .builder import LambdaBuilder
1516from aws_lambda_builders .exceptions import WorkflowNotFoundError , WorkflowUnknownError , WorkflowFailedError
16-
17+ from aws_lambda_builders import RPC_PROTOCOL_VERSION as lambda_builders_protocol_version
1718
1819log_level = int (os .environ .get ("LAMBDA_BUILDERS_LOG_LEVEL" , logging .INFO ))
1920
2425
2526LOG = logging .getLogger (__name__ )
2627
28+ VERSION_REGEX = re .compile ("^([0-9])+.([0-9]+)$" )
29+
2730
2831def _success_response (request_id , artifacts_dir ):
2932 return json .dumps ({
@@ -46,6 +49,31 @@ def _error_response(request_id, http_status_code, message):
4649 })
4750
4851
52+ def _parse_version (version_string ):
53+
54+ if VERSION_REGEX .match (version_string ):
55+ return float (version_string )
56+ else :
57+ ex = "Protocol Version does not match : {}" .format (VERSION_REGEX .pattern )
58+ LOG .debug (ex )
59+ raise ValueError (ex )
60+
61+
62+ def version_compatibility_check (version ):
63+ # The following check is between current protocol version vs version of the protocol
64+ # with which aws-lambda-builders is called.
65+ # Example:
66+ # 0.2 < 0.2 comparison will fail, don't throw a value Error saying incompatible version.
67+ # 0.2 < 0.3 comparison will pass, throwing a ValueError
68+ # 0.2 < 0.1 comparison will fail, don't throw a value Error saying incompatible version
69+
70+ if _parse_version (lambda_builders_protocol_version ) < version :
71+ ex = "Incompatible Protocol Version : {}, " \
72+ "Current Protocol Version: {}" .format (version , lambda_builders_protocol_version )
73+ LOG .error (ex )
74+ raise ValueError (ex )
75+
76+
4977def _write_response (response , exit_code ):
5078 sys .stdout .write (response )
5179 sys .stdout .flush () # Make sure it is written
@@ -77,11 +105,20 @@ def main(): # pylint: disable=too-many-statements
77105 response = _error_response (request_id , - 32601 , "Method unavailable" )
78106 return _write_response (response , 1 )
79107
108+ try :
109+ protocol_version = _parse_version (params .get ("__protocol_version" ))
110+ version_compatibility_check (protocol_version )
111+
112+ except ValueError :
113+ response = _error_response (request_id , 505 , "Unsupported Protocol Version" )
114+ return _write_response (response , 1 )
115+
80116 capabilities = params ["capability" ]
81117 supported_workflows = params .get ("supported_workflows" )
82118
83119 exit_code = 0
84120 response = None
121+
85122 try :
86123 builder = LambdaBuilder (language = capabilities ["language" ],
87124 dependency_manager = capabilities ["dependency_manager" ],
@@ -93,6 +130,7 @@ def main(): # pylint: disable=too-many-statements
93130 params ["artifacts_dir" ],
94131 params ["scratch_dir" ],
95132 params ["manifest_path" ],
133+ executable_search_paths = params .get ('executable_search_paths' , None ),
96134 runtime = params ["runtime" ],
97135 optimizations = params ["optimizations" ],
98136 options = params ["options" ])
0 commit comments