Skip to content

Commit b201293

Browse files
committed
Verify _HANDLER points to a lambda handler implementation
1 parent 33dbd2e commit b201293

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/main/php/xp/lambda/AwsRunner.class.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php namespace xp\lambda;
22

33
use Throwable;
4-
use com\amazon\aws\lambda\{Context, Environment};
4+
use com\amazon\aws\lambda\{Context, Environment, Handler};
55
use io\IOException;
6-
use lang\{XPClass, XPException, Environment as System};
6+
use lang\{XPClass, XPException, IllegalArgumentException, Environment as System};
77
use peer\http\{HttpConnection, RequestData};
88
use text\json\{Json, StreamInput};
99
use util\cmd\Console;
@@ -12,6 +12,7 @@
1212
* Custom AWS Lambda runtimes
1313
*
1414
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
15+
* @test com.amazon.aws.lambda.unittest.AwsRunnerTest
1516
*/
1617
class AwsRunner {
1718

@@ -22,9 +23,16 @@ class AwsRunner {
2223
* @param [:string] $environment
2324
* @param io.streams.StringWriter $writer
2425
* @return com.amazon.aws.lambda.Handler
26+
* @throws lang.ClassLoadingException
27+
* @throws lang.IllegalArgumentException
2528
*/
26-
private static function handler($environment, $writer) {
27-
return XPClass::forName($environment['_HANDLER'])->newInstance(new Environment(
29+
public static function handler($environment, $writer) {
30+
$impl= XPClass::forName($environment['_HANDLER'] ?? '');
31+
if (!$impl->isSubclassOf(Handler::class)) {
32+
throw new IllegalArgumentException('Class '.$impl->getName().' is not a lambda handler');
33+
}
34+
35+
return $impl->newInstance(new Environment(
2836
$environment['LAMBDA_TASK_ROOT'] ?? '.',
2937
$writer,
3038
$environment
@@ -40,7 +48,7 @@ private static function handler($environment, $writer) {
4048
* @param string $path
4149
* @return peer.http.HttpConnection
4250
*/
43-
private static function endpoint($environment, $path) {
51+
public static function endpoint($environment, $path) {
4452
$c= new HttpConnection("http://{$environment['AWS_LAMBDA_RUNTIME_API']}/2018-06-01/runtime/{$path}");
4553

4654
// Use a 15 minute timeout, this is the maximum lambda runtime, see

0 commit comments

Comments
 (0)