RamRoleArnCredentialTest.php 3.7 KB

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