Skip to content

Commit 47c7eac

Browse files
committed
Make credentials accessible via Environment::credentials()
1 parent be825e7 commit 47c7eac

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/main/php/com/amazon/aws/lambda/Environment.class.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Runtime environment of a lambda
1111
*
1212
* @test com.amazon.aws.lambda.unittest.EnvironmentTest
13+
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
1314
*/
1415
class Environment {
1516
public $root, $variables, $writer, $properties;
@@ -46,6 +47,19 @@ public function variable($name) {
4647
return $this->variables[$name] ?? null;
4748
}
4849

50+
/**
51+
* Returns credentials from this environment
52+
*
53+
* @return com.amazon.aws.lambda.Credentials
54+
*/
55+
public function credentials() {
56+
return new Credentials(
57+
$this->variables['AWS_ACCESS_KEY_ID'],
58+
$this->variables['AWS_SECRET_ACCESS_KEY'],
59+
$this->variables['AWS_SESSION_TOKEN'] ?? null,
60+
);
61+
}
62+
4963
/**
5064
* Writes a trace message
5165
*

src/test/php/com/amazon/aws/lambda/unittest/EnvironmentTest.class.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace com\amazon\aws\lambda\unittest;
22

3-
use com\amazon\aws\lambda\Environment;
3+
use com\amazon\aws\lambda\{Environment, Credentials};
44
use io\streams\{MemoryOutputStream, StringWriter};
55
use io\{File, Files, Path};
66
use lang\ElementNotFoundException;
@@ -45,6 +45,33 @@ public function non_existant_variable() {
4545
Assert::null((new Environment('.', null, []))->variable('TEST'));
4646
}
4747

48+
#[Test]
49+
public function credentials() {
50+
$env= [
51+
'AWS_ACCESS_KEY_ID' => 'KEY',
52+
'AWS_SECRET_ACCESS_KEY' => 'SECRET',
53+
];
54+
55+
Assert::equals(
56+
new Credentials('KEY', 'SECRET'),
57+
(new Environment('.', null, $env))->credentials()
58+
);
59+
}
60+
61+
#[Test]
62+
public function credentials_with_session() {
63+
$env= [
64+
'AWS_ACCESS_KEY_ID' => 'KEY',
65+
'AWS_SECRET_ACCESS_KEY' => 'SECRET',
66+
'AWS_SESSION_TOKEN' => 'SESSION',
67+
];
68+
69+
Assert::equals(
70+
new Credentials('KEY', 'SECRET', 'SESSION'),
71+
(new Environment('.', null, $env))->credentials()
72+
);
73+
}
74+
4875
#[Test]
4976
public function trace() {
5077
$stream= new MemoryOutputStream();

0 commit comments

Comments
 (0)