ChainProvider.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace AlibabaCloud\Credentials\Providers;
  3. use AlibabaCloud\Credentials\Credentials;
  4. use AlibabaCloud\Credentials\Utils\Helper;
  5. use Closure;
  6. use InvalidArgumentException;
  7. use RuntimeException;
  8. /**
  9. * @deprecated
  10. * Class ChainProvider
  11. *
  12. * @package AlibabaCloud\Credentials\Providers
  13. */
  14. class ChainProvider
  15. {
  16. /**
  17. * @var array
  18. */
  19. private static $customChains;
  20. /**
  21. * @param callable ...$providers
  22. */
  23. public static function set(...$providers)
  24. {
  25. if (empty($providers)) {
  26. throw new InvalidArgumentException('No providers in chain');
  27. }
  28. foreach ($providers as $provider) {
  29. if (!$provider instanceof Closure) {
  30. throw new InvalidArgumentException('Providers must all be Closures');
  31. }
  32. }
  33. self::$customChains = $providers;
  34. }
  35. /**
  36. * @return bool
  37. */
  38. public static function hasCustomChain()
  39. {
  40. return (bool)self::$customChains;
  41. }
  42. public static function flush()
  43. {
  44. self::$customChains = [];
  45. }
  46. /**
  47. * @param string $name
  48. */
  49. public static function customProvider($name)
  50. {
  51. foreach (self::$customChains as $provider) {
  52. $provider();
  53. if (Credentials::has($name)) {
  54. break;
  55. }
  56. }
  57. }
  58. /**
  59. * @param string $name
  60. */
  61. public static function defaultProvider($name)
  62. {
  63. $providers = [
  64. self::env(),
  65. self::ini(),
  66. self::instance(),
  67. ];
  68. foreach ($providers as $provider) {
  69. $provider();
  70. if (Credentials::has($name)) {
  71. break;
  72. }
  73. }
  74. }
  75. /**
  76. * @return Closure
  77. */
  78. public static function env()
  79. {
  80. return static function () {
  81. $accessKeyId = Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID');
  82. $accessKeySecret = Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
  83. if ($accessKeyId && $accessKeySecret) {
  84. Credentials::set(
  85. self::getDefaultName(),
  86. [
  87. 'type' => 'access_key',
  88. 'access_key_id' => $accessKeyId,
  89. 'access_key_secret' => $accessKeySecret,
  90. ]
  91. );
  92. }
  93. };
  94. }
  95. /**
  96. * @return string
  97. */
  98. public static function getDefaultName()
  99. {
  100. $name = Helper::envNotEmpty('ALIBABA_CLOUD_PROFILE');
  101. if ($name) {
  102. return $name;
  103. }
  104. return 'default';
  105. }
  106. /**
  107. * @return Closure
  108. */
  109. public static function ini()
  110. {
  111. return static function () {
  112. $filename = Helper::envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_FILE');
  113. if (!$filename) {
  114. $filename = self::getDefaultFile();
  115. }
  116. if (!Helper::inOpenBasedir($filename)) {
  117. return;
  118. }
  119. if ($filename !== self::getDefaultFile() && (!\is_readable($filename) || !\is_file($filename))) {
  120. throw new RuntimeException(
  121. 'Credentials file is not readable: ' . $filename
  122. );
  123. }
  124. $file_array = \parse_ini_file($filename, true);
  125. if (\is_array($file_array) && !empty($file_array)) {
  126. foreach (\array_change_key_case($file_array) as $name => $configures) {
  127. Credentials::set($name, $configures);
  128. }
  129. }
  130. };
  131. }
  132. /**
  133. * Get the default credential file.
  134. *
  135. * @return string
  136. */
  137. public static function getDefaultFile()
  138. {
  139. return Helper::getHomeDirectory() .
  140. DIRECTORY_SEPARATOR .
  141. '.alibabacloud' .
  142. DIRECTORY_SEPARATOR .
  143. 'credentials';
  144. }
  145. /**
  146. * @return Closure
  147. */
  148. public static function instance()
  149. {
  150. return static function () {
  151. $instance = Helper::envNotEmpty('ALIBABA_CLOUD_ECS_METADATA');
  152. if ($instance) {
  153. Credentials::set(
  154. self::getDefaultName(),
  155. [
  156. 'type' => 'ecs_ram_role',
  157. 'role_name' => $instance,
  158. ]
  159. );
  160. }
  161. };
  162. }
  163. }