DefaultProfile.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. *
  22. */
  23. define('AUTH_TYPE_RAM_AK', 'RAM_AK');
  24. /**
  25. *
  26. */
  27. define('AUTH_TYPE_RAM_ROLE_ARN', 'RAM_ROLE_ARN');
  28. /**
  29. *
  30. */
  31. define('AUTH_TYPE_ECS_RAM_ROLE', 'ECS_RAM_ROLE');
  32. /**
  33. *
  34. */
  35. define('AUTH_TYPE_BEARER_TOKEN', 'BEARER_TOKEN');
  36. class DefaultProfile implements IClientProfile
  37. {
  38. /**
  39. * @var IClientProfile
  40. */
  41. private static $profile;
  42. /**
  43. * @var array
  44. */
  45. private static $endpoints;
  46. /**
  47. * @var AbstractCredential
  48. */
  49. private static $credential;
  50. /**
  51. * @var string
  52. */
  53. private static $regionId;
  54. /**
  55. * @var string
  56. */
  57. private static $acceptFormat;
  58. /**
  59. * @var string
  60. */
  61. private static $authType;
  62. /**
  63. * @var ISigner
  64. */
  65. private static $isigner;
  66. /**
  67. * @var AbstractCredential
  68. */
  69. private static $iCredential;
  70. /**
  71. * DefaultProfile constructor.
  72. *
  73. * @param $regionId
  74. * @param $credential
  75. * @param string $authType
  76. * @param null $isigner
  77. */
  78. private function __construct($regionId, $credential, $authType = AUTH_TYPE_RAM_AK, $isigner = null)
  79. {
  80. self::$regionId = $regionId;
  81. self::$credential = $credential;
  82. self::$authType = $authType;
  83. self::$isigner = $isigner;
  84. }
  85. /**
  86. * @param $regionId
  87. * @param $accessKeyId
  88. * @param $accessSecret
  89. * @param null $securityToken
  90. *
  91. * @return DefaultProfile|IClientProfile
  92. */
  93. public static function getProfile($regionId, $accessKeyId, $accessSecret, $securityToken = null)
  94. {
  95. $credential = new Credential($accessKeyId, $accessSecret, $securityToken);
  96. self::$profile = new DefaultProfile($regionId, $credential);
  97. return self::$profile;
  98. }
  99. /**
  100. * @param $regionId
  101. * @param $accessKeyId
  102. * @param $accessSecret
  103. * @param $roleArn
  104. * @param $roleSessionName
  105. *
  106. * @return DefaultProfile|IClientProfile
  107. */
  108. public static function getRamRoleArnProfile($regionId, $accessKeyId, $accessSecret, $roleArn, $roleSessionName)
  109. {
  110. $credential = new RamRoleArnCredential($accessKeyId, $accessSecret, $roleArn, $roleSessionName);
  111. self::$profile = new DefaultProfile($regionId, $credential, AUTH_TYPE_RAM_ROLE_ARN);
  112. return self::$profile;
  113. }
  114. /**
  115. * @param $regionId
  116. * @param $roleName
  117. *
  118. * @return DefaultProfile|IClientProfile
  119. */
  120. public static function getEcsRamRoleProfile($regionId, $roleName)
  121. {
  122. $credential = new EcsRamRoleCredential($roleName);
  123. self::$profile = new DefaultProfile($regionId, $credential, AUTH_TYPE_ECS_RAM_ROLE);
  124. return self::$profile;
  125. }
  126. /**
  127. * @param $regionId
  128. * @param $bearerToken
  129. *
  130. * @return DefaultProfile|IClientProfile
  131. */
  132. public static function getBearerTokenProfile($regionId, $bearerToken)
  133. {
  134. $credential = new BearerTokenCredential($bearerToken);
  135. self::$profile = new DefaultProfile($regionId, $credential, AUTH_TYPE_BEARER_TOKEN, new BearTokenSigner());
  136. return self::$profile;
  137. }
  138. /**
  139. * @return ISigner|ShaHmac1Signer|null
  140. */
  141. public function getSigner()
  142. {
  143. if (null == self::$isigner) {
  144. self::$isigner = new ShaHmac1Signer();
  145. }
  146. return self::$isigner;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getRegionId()
  152. {
  153. return self::$regionId;
  154. }
  155. /**
  156. * @return string
  157. */
  158. public function getFormat()
  159. {
  160. return self::$acceptFormat;
  161. }
  162. /**
  163. * @return AbstractCredential
  164. */
  165. public function getCredential()
  166. {
  167. if (null == self::$credential && null != self::$iCredential) {
  168. self::$credential = self::$iCredential;
  169. }
  170. return self::$credential;
  171. }
  172. /**
  173. * @return bool
  174. */
  175. public function isRamRoleArn()
  176. {
  177. return self::$authType == AUTH_TYPE_RAM_ROLE_ARN;
  178. }
  179. /**
  180. * @return bool
  181. */
  182. public function isEcsRamRole()
  183. {
  184. return self::$authType == AUTH_TYPE_ECS_RAM_ROLE;
  185. }
  186. /**
  187. * @return array
  188. */
  189. public static function getEndpoints()
  190. {
  191. if (null == self::$endpoints) {
  192. self::$endpoints = EndpointProvider::getEndpoints();
  193. }
  194. return self::$endpoints;
  195. }
  196. /**
  197. * @param $endpointName
  198. * @param $regionId
  199. * @param $product
  200. * @param $domain
  201. */
  202. public static function addEndpoint($endpointName, $regionId, $product, $domain)
  203. {
  204. if (null == self::$endpoints) {
  205. self::$endpoints = self::getEndpoints();
  206. }
  207. $endpoint = self::findEndpointByName($endpointName);
  208. if (null == $endpoint) {
  209. self::addEndpoint_($endpointName, $regionId, $product, $domain);
  210. } else {
  211. self::updateEndpoint($regionId, $product, $domain, $endpoint);
  212. }
  213. LocationService::addEndPoint($regionId, $product, $domain);
  214. }
  215. /**
  216. * @param $endpointName
  217. *
  218. * @return mixed
  219. */
  220. public static function findEndpointByName($endpointName)
  221. {
  222. foreach (self::$endpoints as $key => $endpoint) {
  223. if ($endpoint->getName() == $endpointName) {
  224. return $endpoint;
  225. }
  226. }
  227. }
  228. /**
  229. * @param $endpointName
  230. * @param $regionId
  231. * @param $product
  232. * @param $domain
  233. */
  234. private static function addEndpoint_($endpointName, $regionId, $product, $domain)
  235. {
  236. $regionIds = array($regionId);
  237. $productsDomains = array(new ProductDomain($product, $domain));
  238. $endpoint = new Endpoint($endpointName, $regionIds, $productsDomains);
  239. self::$endpoints[] = $endpoint;
  240. }
  241. /**
  242. * @param string $regionId
  243. * @param string $product
  244. * @param string $domain
  245. * @param Endpoint $endpoint
  246. */
  247. private static function updateEndpoint($regionId, $product, $domain, $endpoint)
  248. {
  249. $regionIds = $endpoint->getRegionIds();
  250. if (!in_array($regionId, $regionIds)) {
  251. $regionIds[] = $regionId;
  252. $endpoint->setRegionIds($regionIds);
  253. }
  254. $productDomains = $endpoint->getProductDomains();
  255. if (null == self::findProductDomainAndUpdate($productDomains, $product, $domain)) {
  256. $productDomains[] = new ProductDomain($product, $domain);
  257. }
  258. $endpoint->setProductDomains($productDomains);
  259. }
  260. /**
  261. * @param $productDomains
  262. * @param $product
  263. * @param $domain
  264. *
  265. * @return string|null
  266. */
  267. private static function findProductDomainAndUpdate($productDomains, $product, $domain)
  268. {
  269. foreach ($productDomains as $key => $productDomain) {
  270. if ($productDomain->getProductName() == $product) {
  271. $productDomain->setDomainName($domain);
  272. return $productDomain;
  273. }
  274. }
  275. return null;
  276. }
  277. }