新增
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace AlibabaCloud\Client\Credentials\Ini;
|
||||
|
||||
use AlibabaCloud\Client\SDK;
|
||||
use AlibabaCloud\Client\Clients\Client;
|
||||
use AlibabaCloud\Client\Clients\AccessKeyClient;
|
||||
use AlibabaCloud\Client\Clients\RamRoleArnClient;
|
||||
use AlibabaCloud\Client\Clients\RsaKeyPairClient;
|
||||
use AlibabaCloud\Client\Clients\EcsRamRoleClient;
|
||||
use AlibabaCloud\Client\Exception\ClientException;
|
||||
use AlibabaCloud\Client\Clients\BearerTokenClient;
|
||||
|
||||
/**
|
||||
* Trait CreateTrait
|
||||
*
|
||||
* @package AlibabaCloud\Client\Credentials\Ini
|
||||
*
|
||||
* @mixin IniCredential
|
||||
*/
|
||||
trait CreateTrait
|
||||
{
|
||||
/**
|
||||
* @param string $clientName
|
||||
* @param array $credential
|
||||
*
|
||||
* @return Client|bool
|
||||
* @throws ClientException
|
||||
*/
|
||||
protected function createClient($clientName, array $credential)
|
||||
{
|
||||
if (!isset($credential['enable']) || !$credential['enable']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($credential['type'])) {
|
||||
$this->missingRequired('type', $clientName);
|
||||
}
|
||||
|
||||
return $this->createClientByType($clientName, $credential)->name($clientName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $clientName
|
||||
* @param array $credential
|
||||
*
|
||||
* @return AccessKeyClient|BearerTokenClient|EcsRamRoleClient|RamRoleArnClient|RsaKeyPairClient
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function createClientByType($clientName, array $credential)
|
||||
{
|
||||
switch (\strtolower($credential['type'])) {
|
||||
case 'access_key':
|
||||
return $this->accessKeyClient($clientName, $credential);
|
||||
case 'ecs_ram_role':
|
||||
return $this->ecsRamRoleClient($clientName, $credential);
|
||||
case 'ram_role_arn':
|
||||
return $this->ramRoleArnClient($clientName, $credential);
|
||||
case 'bearer_token':
|
||||
return $this->bearerTokenClient($clientName, $credential);
|
||||
case 'rsa_key_pair':
|
||||
return $this->rsaKeyPairClient($clientName, $credential);
|
||||
default:
|
||||
throw new ClientException(
|
||||
"Invalid type '{$credential['type']}' for '$clientName' in {$this->filename}",
|
||||
SDK::INVALID_CREDENTIAL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $credential
|
||||
* @param string $clientName
|
||||
*
|
||||
* @return AccessKeyClient
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function accessKeyClient($clientName, array $credential)
|
||||
{
|
||||
if (!isset($credential['access_key_id'])) {
|
||||
$this->missingRequired('access_key_id', $clientName);
|
||||
}
|
||||
|
||||
if (!isset($credential['access_key_secret'])) {
|
||||
$this->missingRequired('access_key_secret', $clientName);
|
||||
}
|
||||
|
||||
return new AccessKeyClient(
|
||||
$credential['access_key_id'],
|
||||
$credential['access_key_secret']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $clientName
|
||||
* @param array $credential
|
||||
*
|
||||
* @return EcsRamRoleClient
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function ecsRamRoleClient($clientName, array $credential)
|
||||
{
|
||||
if (!isset($credential['role_name'])) {
|
||||
$this->missingRequired('role_name', $clientName);
|
||||
}
|
||||
|
||||
return new EcsRamRoleClient($credential['role_name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $clientName
|
||||
* @param array $credential
|
||||
*
|
||||
* @return RamRoleArnClient
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function ramRoleArnClient($clientName, array $credential)
|
||||
{
|
||||
if (!isset($credential['access_key_id'])) {
|
||||
$this->missingRequired('access_key_id', $clientName);
|
||||
}
|
||||
|
||||
if (!isset($credential['access_key_secret'])) {
|
||||
$this->missingRequired('access_key_secret', $clientName);
|
||||
}
|
||||
|
||||
if (!isset($credential['role_arn'])) {
|
||||
$this->missingRequired('role_arn', $clientName);
|
||||
}
|
||||
|
||||
if (!isset($credential['role_session_name'])) {
|
||||
$this->missingRequired('role_session_name', $clientName);
|
||||
}
|
||||
|
||||
return new RamRoleArnClient(
|
||||
$credential['access_key_id'],
|
||||
$credential['access_key_secret'],
|
||||
$credential['role_arn'],
|
||||
$credential['role_session_name']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $clientName
|
||||
* @param array $credential
|
||||
*
|
||||
* @return BearerTokenClient
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function bearerTokenClient($clientName, array $credential)
|
||||
{
|
||||
if (!isset($credential['bearer_token'])) {
|
||||
$this->missingRequired('bearer_token', $clientName);
|
||||
}
|
||||
|
||||
return new BearerTokenClient($credential['bearer_token']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $credential
|
||||
* @param string $clientName
|
||||
*
|
||||
* @return RsaKeyPairClient
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function rsaKeyPairClient($clientName, array $credential)
|
||||
{
|
||||
if (!isset($credential['public_key_id'])) {
|
||||
$this->missingRequired('public_key_id', $clientName);
|
||||
}
|
||||
|
||||
if (!isset($credential['private_key_file'])) {
|
||||
$this->missingRequired('private_key_file', $clientName);
|
||||
}
|
||||
|
||||
return new RsaKeyPairClient(
|
||||
$credential['public_key_id'],
|
||||
$credential['private_key_file']
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace AlibabaCloud\Client\Credentials\Ini;
|
||||
|
||||
use AlibabaCloud\Client\SDK;
|
||||
use AlibabaCloud\Client\Clients\Client;
|
||||
use AlibabaCloud\Client\Exception\ClientException;
|
||||
|
||||
/**
|
||||
* Class IniCredential
|
||||
*
|
||||
* @package AlibabaCloud\Client\Credentials\Ini
|
||||
*/
|
||||
class IniCredential
|
||||
{
|
||||
use CreateTrait;
|
||||
use OptionsTrait;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $hasLoaded;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $filename;
|
||||
|
||||
/**
|
||||
* IniCredential constructor.
|
||||
*
|
||||
* @param string $filename
|
||||
*/
|
||||
public function __construct($filename = '')
|
||||
{
|
||||
$this->filename = $filename ?: $this->getDefaultFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default credential file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultFile()
|
||||
{
|
||||
return self::getHomeDirectory() . DIRECTORY_SEPARATOR . '.alibabacloud' . DIRECTORY_SEPARATOR . 'credentials';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the environment's HOME directory.
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
private static function getHomeDirectory()
|
||||
{
|
||||
if (getenv('HOME')) {
|
||||
return getenv('HOME');
|
||||
}
|
||||
|
||||
return (getenv('HOMEDRIVE') && getenv('HOMEPATH'))
|
||||
? getenv('HOMEDRIVE') . getenv('HOMEPATH')
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear credential cache.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function forgetLoadedCredentialsFile()
|
||||
{
|
||||
self::$hasLoaded = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the credential file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFilename()
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isNotEmpty(array $array, $key)
|
||||
{
|
||||
return isset($array[$key]) && !empty($array[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $clientName
|
||||
*
|
||||
* @throws ClientException
|
||||
*/
|
||||
public function missingRequired($key, $clientName)
|
||||
{
|
||||
throw new ClientException(
|
||||
"Missing required '$key' option for '$clientName' in " . $this->getFilename(),
|
||||
SDK::INVALID_CREDENTIAL
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed
|
||||
* @throws ClientException
|
||||
*/
|
||||
public function load()
|
||||
{
|
||||
// If it has been loaded, assign the client directly.
|
||||
if (isset(self::$hasLoaded[$this->filename])) {
|
||||
/**
|
||||
* @var $client Client
|
||||
*/
|
||||
foreach (self::$hasLoaded[$this->filename] as $projectName => $client) {
|
||||
$client->name($projectName);
|
||||
}
|
||||
|
||||
return self::$hasLoaded[$this->filename];
|
||||
}
|
||||
|
||||
return $this->loadFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceptions will be thrown if the file is unreadable and not the default file.
|
||||
*
|
||||
* @return array|mixed
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function loadFile()
|
||||
{
|
||||
if (!\AlibabaCloud\Client\inOpenBasedir($this->filename)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!\is_readable($this->filename) || !\is_file($this->filename)) {
|
||||
if ($this->filename === $this->getDefaultFile()) {
|
||||
// @codeCoverageIgnoreStart
|
||||
return [];
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
throw new ClientException(
|
||||
'Credential file is not readable: ' . $this->getFilename(),
|
||||
SDK::INVALID_CREDENTIAL
|
||||
);
|
||||
}
|
||||
|
||||
return $this->parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the ini file into an array.
|
||||
*
|
||||
* @return array|mixed
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function parseFile()
|
||||
{
|
||||
try {
|
||||
$file = \parse_ini_file($this->filename, true);
|
||||
if (\is_array($file) && $file !== []) {
|
||||
return $this->initClients($file);
|
||||
}
|
||||
throw new ClientException(
|
||||
'Format error: ' . $this->getFilename(),
|
||||
SDK::INVALID_CREDENTIAL
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
throw new ClientException(
|
||||
$e->getMessage(),
|
||||
SDK::INVALID_CREDENTIAL,
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize clients.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array|mixed
|
||||
* @throws ClientException
|
||||
*/
|
||||
private function initClients($array)
|
||||
{
|
||||
foreach (\array_change_key_case($array) as $clientName => $configures) {
|
||||
$configures = \array_change_key_case($configures);
|
||||
$clientInstance = $this->createClient($clientName, $configures);
|
||||
if ($clientInstance instanceof Client) {
|
||||
self::$hasLoaded[$this->filename][$clientName] = $clientInstance;
|
||||
self::setClientAttributes($configures, $clientInstance);
|
||||
self::setCert($configures, $clientInstance);
|
||||
self::setProxy($configures, $clientInstance);
|
||||
}
|
||||
}
|
||||
|
||||
return isset(self::$hasLoaded[$this->filename])
|
||||
? self::$hasLoaded[$this->filename]
|
||||
: [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace AlibabaCloud\Client\Credentials\Ini;
|
||||
|
||||
use AlibabaCloud\Client\Clients\Client;
|
||||
use AlibabaCloud\Client\Exception\ClientException;
|
||||
|
||||
/**
|
||||
* Trait OptionsTrait
|
||||
*
|
||||
* @package AlibabaCloud\Client\Credentials\Ini
|
||||
*
|
||||
* @mixin IniCredential
|
||||
*/
|
||||
trait OptionsTrait
|
||||
{
|
||||
/**
|
||||
* @param array $configures
|
||||
* @param Client $client
|
||||
*
|
||||
* @throws ClientException
|
||||
*/
|
||||
private static function setClientAttributes($configures, Client $client)
|
||||
{
|
||||
if (self::isNotEmpty($configures, 'region_id')) {
|
||||
$client->regionId($configures['region_id']);
|
||||
}
|
||||
|
||||
if (isset($configures['debug'])) {
|
||||
$client->options(
|
||||
[
|
||||
'debug' => (bool)$configures['debug'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if (self::isNotEmpty($configures, 'timeout')) {
|
||||
$client->options(
|
||||
[
|
||||
'timeout' => $configures['timeout'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if (self::isNotEmpty($configures, 'connect_timeout')) {
|
||||
$client->options(
|
||||
[
|
||||
'connect_timeout' => $configures['connect_timeout'],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $configures
|
||||
* @param Client $client
|
||||
*/
|
||||
private static function setProxy($configures, Client $client)
|
||||
{
|
||||
if (self::isNotEmpty($configures, 'proxy')) {
|
||||
$client->options(
|
||||
[
|
||||
'proxy' => $configures['proxy'],
|
||||
]
|
||||
);
|
||||
}
|
||||
$proxy = [];
|
||||
if (self::isNotEmpty($configures, 'proxy_http')) {
|
||||
$proxy['http'] = $configures['proxy_http'];
|
||||
}
|
||||
if (self::isNotEmpty($configures, 'proxy_https')) {
|
||||
$proxy['https'] = $configures['proxy_https'];
|
||||
}
|
||||
if (self::isNotEmpty($configures, 'proxy_no')) {
|
||||
$proxy['no'] = \explode(',', $configures['proxy_no']);
|
||||
}
|
||||
if ($proxy !== []) {
|
||||
$client->options(
|
||||
[
|
||||
'proxy' => $proxy,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $configures
|
||||
* @param Client $client
|
||||
*/
|
||||
private static function setCert($configures, Client $client)
|
||||
{
|
||||
if (self::isNotEmpty($configures, 'cert_file') && !self::isNotEmpty($configures, 'cert_password')) {
|
||||
$client->options(
|
||||
[
|
||||
'cert' => $configures['cert_file'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if (self::isNotEmpty($configures, 'cert_file') && self::isNotEmpty($configures, 'cert_password')) {
|
||||
$client->options(
|
||||
[
|
||||
'cert' => [
|
||||
$configures['cert_file'],
|
||||
$configures['cert_password'],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user