Skip to content

Commit 410338b

Browse files
committed
feat: support sts vpc endpoint
1 parent cd0f651 commit 410338b

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

src/Credential/RefreshResult.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use AlibabaCloud\Credentials\Providers\Credentials;
66

7-
use function PHPUnit\Framework\isNull;
8-
97
class RefreshResult
108
{
119

src/Providers/OIDCRoleArnCredentialsProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,16 @@ private function filterPolicy(array $params)
167167

168168
private function filterSTSEndpoint(array $params)
169169
{
170+
$prefix = 'sts';
171+
if (Helper::envNotEmpty('ALIBABA_CLOUD_VPC_ENDPOINT_ENABLED') || (isset($params['enableVpc']) && $params['enableVpc'] === true)) {
172+
$prefix = 'sts-vpc';
173+
}
170174
if (Helper::envNotEmpty('ALIBABA_CLOUD_STS_REGION')) {
171-
$this->stsEndpoint = 'sts' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
175+
$this->stsEndpoint = $prefix . '.' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
172176
}
173177

174178
if (isset($params['stsRegionId'])) {
175-
$this->stsEndpoint = 'sts' . $params['stsRegionId'] . '.aliyuncs.com';
179+
$this->stsEndpoint = $prefix . '.' . $params['stsRegionId'] . '.aliyuncs.com';
176180
}
177181

178182
if (isset($params['stsEndpoint'])) {
@@ -249,7 +253,7 @@ public function refreshCredentials()
249253
'securityToken' => $credentials['SecurityToken'],
250254
'expiration' => \strtotime($credentials['Expiration']),
251255
'providerName' => $this->getProviderName(),
252-
]), $this->getStaleTime(strtotime($credentials['Expiration'])) );
256+
]), $this->getStaleTime(strtotime($credentials['Expiration'])));
253257
}
254258

255259
public function key()

src/Providers/RamRoleArnCredentialsProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,16 @@ private function filterExternalId(array $params)
149149

150150
private function filterSTSEndpoint(array $params)
151151
{
152+
$prefix = 'sts';
153+
if (Helper::envNotEmpty('ALIBABA_CLOUD_VPC_ENDPOINT_ENABLED') || (isset($params['enableVpc']) && $params['enableVpc'] === true)) {
154+
$prefix = 'sts-vpc';
155+
}
152156
if (Helper::envNotEmpty('ALIBABA_CLOUD_STS_REGION')) {
153-
$this->stsEndpoint = 'sts.' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
157+
$this->stsEndpoint = $prefix . '.' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
154158
}
155159

156160
if (isset($params['stsRegionId'])) {
157-
$this->stsEndpoint = 'sts.' . $params['stsRegionId'] . '.aliyuncs.com';
161+
$this->stsEndpoint = $prefix . '.' . $params['stsRegionId'] . '.aliyuncs.com';
158162
}
159163

160164
if (isset($params['stsEndpoint'])) {

tests/Unit/Providers/OIDCRoleArnCredentialsProviderTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function testConstruct()
4646
'durationSeconds' => 3600,
4747
'policy' => 'policy',
4848
'stsRegionId' => 'cn-beijing',
49+
'enableVpc' => true,
4950
'stsEndpoint' => 'sts.cn-zhangjiakou.aliyuncs.com'
5051
];
5152
$config = [
@@ -57,6 +58,7 @@ public function testConstruct()
5758
putenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE=/b/c");
5859
putenv("ALIBABA_CLOUD_ROLE_SESSION_NAME=sessionName");
5960
putenv("ALIBABA_CLOUD_STS_REGION=cn-hangzhou");
61+
putenv("ALIBABA_CLOUD_VPC_ENDPOINT_ENABLED=true");
6062

6163
$provider = new OIDCRoleArnCredentialsProvider($params, $config);
6264
self::assertEquals('oidc_role_arn', $provider->getProviderName());
@@ -77,7 +79,7 @@ public function testConstruct()
7779
$policy = $this->getPrivateField($provider, 'policy');
7880
$oidcTokenFilePath = $this->getPrivateField($provider, 'oidcTokenFilePath');
7981
$durationSeconds = $this->getPrivateField($provider, 'durationSeconds');
80-
self::assertEquals('stscn-hangzhou.aliyuncs.com', $stsEndpoint);
82+
self::assertEquals('sts-vpc.cn-hangzhou.aliyuncs.com', $stsEndpoint);
8183
self::assertNull($policy);
8284
self::assertEquals('/b/c', $oidcTokenFilePath);
8385
self::assertEquals(3600, $durationSeconds);
@@ -87,6 +89,7 @@ public function testConstruct()
8789
putenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE=");
8890
putenv("ALIBABA_CLOUD_ROLE_SESSION_NAME=");
8991
putenv("ALIBABA_CLOUD_STS_REGION=");
92+
putenv("ALIBABA_CLOUD_VPC_ENDPOINT_ENABLED=");
9093
}
9194

9295
public function testConstructErrorRoleArn()

tests/Unit/Providers/RamRoleArnCredentialsProviderTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testConstruct()
4747
'policy' => 'policy',
4848
'externalId' => 'externalId',
4949
'stsRegionId' => 'cn-beijing',
50+
'enableVpc' => true,
5051
'stsEndpoint' => 'sts.cn-zhangjiakou.aliyuncs.com'
5152
];
5253
$config = [
@@ -56,6 +57,7 @@ public function testConstruct()
5657
putenv("ALIBABA_CLOUD_ROLE_ARN=roleArn");
5758
putenv("ALIBABA_CLOUD_ROLE_SESSION_NAME=sessionName");
5859
putenv("ALIBABA_CLOUD_STS_REGION=cn-hangzhou");
60+
putenv("ALIBABA_CLOUD_VPC_ENDPOINT_ENABLED=true");
5961

6062
$provider = new RamRoleArnCredentialsProvider($params, $config);
6163

@@ -86,12 +88,13 @@ public function testConstruct()
8688
self::assertEquals('ram_role_arn#credential#foo#roleArn#roleArn#roleSessionName#sessionName', $provider->key());
8789
$stsEndpoint = $this->getPrivateField($provider, 'stsEndpoint');
8890
$externalId = $this->getPrivateField($provider, 'externalId');
89-
self::assertEquals('sts.cn-hangzhou.aliyuncs.com', $stsEndpoint);
91+
self::assertEquals('sts-vpc.cn-hangzhou.aliyuncs.com', $stsEndpoint);
9092
self::assertNull($externalId);
9193

9294
putenv("ALIBABA_CLOUD_ROLE_ARN=");
9395
putenv("ALIBABA_CLOUD_ROLE_SESSION_NAME=");
9496
putenv("ALIBABA_CLOUD_STS_REGION=");
97+
putenv("ALIBABA_CLOUD_VPC_ENDPOINT_ENABLED=");
9598
}
9699

97100
public function testConstructErrorCredentials()

0 commit comments

Comments
 (0)