123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials;
- use PHPUnit\Framework\TestCase;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ServerException;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeAccessPointsRequest;
- /**
- * Class RamRoleArnCredentialTest
- *
- * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials
- */
- class RamRoleArnCredentialTest extends TestCase
- {
- /**
- * @var string
- */
- private $clientName = 'RamRoleArnCredentialTest';
- /**
- * @throws ClientException
- */
- public function setUp()
- {
- $regionId = 'cn-hangzhou';
- $accessKeyId = \getenv('ACCESS_KEY_ID');
- $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
- $roleArn = 'acs:ram::1325847523475998:role/ecsramroletest';
- $roleSessionName = 'role_session_name';
- $policy = '{
- "Version": "1",
- "Statement": [
- {
- "Effect": "Allow",
- "Action": "ecs:Describe*",
- "Resource": "acs:ecs:cn-hangzhou:*:*"
- },
- {
- "Effect": "Allow",
- "Action": [
- "oss:ListObjects",
- "oss:GetObject"
- ],
- "Resource": [
- "acs:oss:*:*:mybucket",
- "acs:oss:*:*:mybucket/*"
- ],
- "Condition":{
- "IpAddress": {
- "acs:SourceIp": ["42.120.88.10", "42.120.66.0/24"]
- }
- }
- }
- ]
- }';
- AlibabaCloud::ramRoleArnClient(
- $accessKeyId,
- $accessKeySecret,
- $roleArn,
- $roleSessionName,
- $policy
- )->regionId($regionId)->name($this->clientName);
- }
- /**
- * @throws ClientException
- */
- public function tearDown()
- {
- AlibabaCloud::del($this->clientName);
- }
- /**
- * @throws ClientException
- */
- public function testEcs()
- {
- try {
- $result = (new DescribeAccessPointsRequest())
- ->client($this->clientName)
- ->connectTimeout(25)
- ->timeout(30)
- ->request();
- static::assertTrue(isset($result['AccessPointSet']));
- } catch (ServerException $e) {
- self::assertEquals(
- 'You are not authorized to do this action. You should be authorized by RAM.',
- $e->getErrorMessage()
- );
- }
- }
- /**
- * @throws ClientException
- */
- public function testPolicyAsArray()
- {
- $regionId = 'cn-hangzhou';
- $accessKeyId = \getenv('ACCESS_KEY_ID');
- $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
- $roleArn = 'acs:ram::1325847523475998:role/ecsramroletest';
- $roleSessionName = 'role_session_name';
- $policy = [
- 'Version' => '1',
- 'Statement' => [
- ],
- ];
- AlibabaCloud::ramRoleArnClient(
- $accessKeyId,
- $accessKeySecret,
- $roleArn,
- $roleSessionName,
- $policy
- )->regionId($regionId)->name($this->clientName);
- try {
- $result = (new DescribeAccessPointsRequest())
- ->client($this->clientName)
- ->connectTimeout(25)
- ->timeout(30)
- ->request();
- static::assertTrue(isset($result['AccessPointSet']));
- } catch (ServerException $e) {
- self::assertEquals(
- 'You are not authorized to do this action. You should be authorized by RAM.',
- $e->getErrorMessage()
- );
- }
- }
- }
|