Skip to content

Commit 5dccc24

Browse files
author
Peter Giacomo Lombardo
authored
AWS Lambda: Safeties in Lambda Handler parsing (#253)
1 parent f383173 commit 5dccc24

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

instana/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def get_lambda_handler_or_default():
6161

6262
if handler:
6363
parts = handler.split(".")
64-
handler_function = parts.pop()
65-
handler_module = ".".join(parts)
64+
handler_function = parts.pop().strip()
65+
handler_module = ".".join(parts).strip()
6666
except Exception:
6767
pass
6868

tests/platforms/test_lambda.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ def test_get_handler(self):
128128
self.assertEqual("tests", handler_module)
129129
self.assertEqual("lambda_handler", handler_function)
130130

131+
def test_get_handler_with_multi_subpackages(self):
132+
os.environ["LAMBDA_HANDLER"] = "tests.one.two.three.lambda_handler"
133+
handler_module, handler_function = get_lambda_handler_or_default()
134+
135+
self.assertEqual("tests.one.two.three", handler_module)
136+
self.assertEqual("lambda_handler", handler_function)
137+
138+
def test_get_handler_with_space_in_it(self):
139+
os.environ["LAMBDA_HANDLER"] = " tests.another_module.lambda_handler"
140+
handler_module, handler_function = get_lambda_handler_or_default()
141+
142+
self.assertEqual("tests.another_module", handler_module)
143+
self.assertEqual("lambda_handler", handler_function)
144+
145+
os.environ["LAMBDA_HANDLER"] = "tests.another_module.lambda_handler "
146+
handler_module, handler_function = get_lambda_handler_or_default()
147+
148+
self.assertEqual("tests.another_module", handler_module)
149+
self.assertEqual("lambda_handler", handler_function)
150+
131151
def test_agent_extra_http_headers(self):
132152
os.environ['INSTANA_EXTRA_HTTP_HEADERS'] = "X-Test-Header;X-Another-Header;X-And-Another-Header"
133153
self.create_agent_and_setup_tracer()
@@ -590,4 +610,4 @@ def test_agent_default_log_level(self):
590610
def test_agent_custom_log_level(self):
591611
os.environ['INSTANA_LOG_LEVEL'] = "eRror"
592612
self.create_agent_and_setup_tracer()
593-
assert self.agent.options.log_level == logging.ERROR
613+
assert self.agent.options.log_level == logging.ERROR

0 commit comments

Comments
 (0)