RamRoleArnCredentialTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeAccessPointsRequest;
  8. /**
  9. * Class RamRoleArnCredentialTest
  10. *
  11. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials
  12. */
  13. class RamRoleArnCredentialTest extends TestCase
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $clientName = 'RamRoleArnCredentialTest';
  19. /**
  20. * @throws ClientException
  21. */
  22. public function setUp()
  23. {
  24. $regionId = 'cn-hangzhou';
  25. $accessKeyId = \getenv('ACCESS_KEY_ID');
  26. $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
  27. $roleArn = 'acs:ram::1325847523475998:role/ecsramroletest';
  28. $roleSessionName = 'role_session_name';
  29. $policy = '{
  30. "Version": "1",
  31. "Statement": [
  32. {
  33. "Effect": "Allow",
  34. "Action": "ecs:Describe*",
  35. "Resource": "acs:ecs:cn-hangzhou:*:*"
  36. },
  37. {
  38. "Effect": "Allow",
  39. "Action": [
  40. "oss:ListObjects",
  41. "oss:GetObject"
  42. ],
  43. "Resource": [
  44. "acs:oss:*:*:mybucket",
  45. "acs:oss:*:*:mybucket/*"
  46. ],
  47. "Condition":{
  48. "IpAddress": {
  49. "acs:SourceIp": ["42.120.88.10", "42.120.66.0/24"]
  50. }
  51. }
  52. }
  53. ]
  54. }';
  55. AlibabaCloud::ramRoleArnClient(
  56. $accessKeyId,
  57. $accessKeySecret,
  58. $roleArn,
  59. $roleSessionName,
  60. $policy
  61. )->regionId($regionId)->name($this->clientName);
  62. }
  63. /**
  64. * @throws ClientException
  65. */
  66. public function tearDown()
  67. {
  68. AlibabaCloud::del($this->clientName);
  69. }
  70. /**
  71. * @throws ClientException
  72. */
  73. public function testEcs()
  74. {
  75. try {
  76. $result = (new DescribeAccessPointsRequest())
  77. ->client($this->clientName)
  78. ->connectTimeout(25)
  79. ->timeout(30)
  80. ->request();
  81. static::assertTrue(isset($result['AccessPointSet']));
  82. } catch (ServerException $e) {
  83. self::assertEquals(
  84. 'You are not authorized to do this action. You should be authorized by RAM.',
  85. $e->getErrorMessage()
  86. );
  87. }
  88. }
  89. /**
  90. * @throws ClientException
  91. */
  92. public function testPolicyAsArray()
  93. {
  94. $regionId = 'cn-hangzhou';
  95. $accessKeyId = \getenv('ACCESS_KEY_ID');
  96. $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
  97. $roleArn = 'acs:ram::1325847523475998:role/ecsramroletest';
  98. $roleSessionName = 'role_session_name';
  99. $policy = [
  100. 'Version' => '1',
  101. 'Statement' => [
  102. ],
  103. ];
  104. AlibabaCloud::ramRoleArnClient(
  105. $accessKeyId,
  106. $accessKeySecret,
  107. $roleArn,
  108. $roleSessionName,
  109. $policy
  110. )->regionId($regionId)->name($this->clientName);
  111. try {
  112. $result = (new DescribeAccessPointsRequest())
  113. ->client($this->clientName)
  114. ->connectTimeout(25)
  115. ->timeout(30)
  116. ->request();
  117. static::assertTrue(isset($result['AccessPointSet']));
  118. } catch (ServerException $e) {
  119. self::assertEquals(
  120. 'You are not authorized to do this action. You should be authorized by RAM.',
  121. $e->getErrorMessage()
  122. );
  123. }
  124. }
  125. }