From 24cc537839643392b055a4632c3f9fa57cb3fc16 Mon Sep 17 00:00:00 2001 From: "shuizhao.gh" Date: Tue, 28 May 2024 11:32:31 +0800 Subject: [PATCH 1/5] feat: support credentials provider --- Aliyun/Log/Client.php | 67 +++++++++----- Aliyun/Log/Models/CredentailsProvider.php | 107 ++++++++++++++++++++++ 2 files changed, 151 insertions(+), 23 deletions(-) create mode 100644 Aliyun/Log/Models/CredentailsProvider.php diff --git a/Aliyun/Log/Client.php b/Aliyun/Log/Client.php index 2a832b6..404cc2a 100644 --- a/Aliyun/Log/Client.php +++ b/Aliyun/Log/Client.php @@ -24,19 +24,9 @@ class Aliyun_Log_Client { /** - * @var string aliyun accessKey + *@var Aliyun_Log_Models_CredentailsProvider credentialsProvider */ - protected $accessKey; - - /** - * @var string aliyun accessKeyId - */ - protected $accessKeyId; - - /** - *@var string aliyun sts token - */ - protected $stsToken; + protected $credentialsProvider; /** * @var string LOG endpoint @@ -64,7 +54,9 @@ class Aliyun_Log_Client { protected $source; /** - * Aliyun_Log_Client constructor + * Aliyun_Log_Client constructor. + * + * Either $accessKeyId/$accessKeySecret or $credentialsProvider must be provided. * * @param string $endpoint * LOG host name, for example, http://cn-hangzhou.sls.aliyuncs.com @@ -72,12 +64,30 @@ class Aliyun_Log_Client { * aliyun accessKeyId * @param string $accessKey * aliyun accessKey + * @param string $token + * aliyun token + * @param Aliyun_Log_Models_CredentailsProvider $credentialsProvider */ - public function __construct($endpoint, $accessKeyId, $accessKey,$token = "") { - $this->setEndpoint ( $endpoint ); // set $this->logHost - $this->accessKeyId = $accessKeyId; - $this->accessKey = $accessKey; - $this->stsToken = $token; + public function __construct( + string $endpoint, + string $accessKeyId = "", + string $accessKey = "", + string $token = "", + Aliyun_Log_Models_CredentailsProvider $credentialsProvider = null + ) { + $this->setEndpoint($endpoint); // set $this->logHost + if (!is_null($credentialsProvider)) { + $this->credentialsProvider = $credentialsProvider; + } else { + if (empty($accessKeyId) || empty($accessKeySecret)) { + throw new Aliyun_Log_Exception("InvalidAccessKey", "accessKeyId or accessKeySecret is empty", ""); + } + $this->credentialsProvider = new Aliyun_Log_Models_StaticCredentailsProvider( + $accessKeyId, + $accessKey, + $token + ); + } $this->source = Aliyun_Log_Util::getLocalIp(); } private function setEndpoint($endpoint) { @@ -186,6 +196,17 @@ private function sendRequest($method, $url, $body, $headers) { * @throws Aliyun_Log_Exception */ private function send($method, $project, $body, $resource, $params, $headers) { + $credentails = null; + try { + $credentails = $this->credentialsProvider->getCredentials(); + } catch (Exception $ex) { + throw new Aliyun_Log_Exception( + 'InvalidCredentials', + 'Fail to get credentials:' . $ex->getMessage(), + '' + ); + } + if ($body) { $headers ['Content-Length'] = strlen ( $body ); if(isset($headers ["x-log-bodyrawsize"])==false) @@ -199,13 +220,13 @@ private function send($method, $project, $body, $resource, $params, $headers) { $headers ['x-log-apiversion'] = API_VERSION; $headers ['x-log-signaturemethod'] = 'hmac-sha1'; - if(strlen($this->stsToken) >0) - $headers ['x-acs-security-token'] = $this -> stsToken; + if(strlen($credentails->getSecurityToken()) >0) + $headers ['x-acs-security-token'] = $credentails->getSecurityToken(); if(is_null($project))$headers ['Host'] = $this->logHost; else $headers ['Host'] = "$project.$this->logHost"; $headers ['Date'] = $this->GetGMT (); - $signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $this->accessKey,$this->stsToken, $params, $headers ); - $headers ['Authorization'] = "LOG $this->accessKeyId:$signature"; + $signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $credentails->getAccessKeySecret(), $credentails->getSecurityToken(), $params, $headers ); + $headers ['Authorization'] = "LOG ".$credentails->getAccessKeyId().":$signature"; $url = $resource; if ($params) @@ -738,7 +759,7 @@ public function executeProjectSqlJson(Aliyun_Log_Models_ProjectSqlRequest $reque * Get logs from Log service. * Unsuccessful opertaion will cause an Aliyun_Log_Exception. * - * @param Aliyun_Log_Models_GetProjectLogsRequest $request the GetLogs request parameters class. + * @param Aliyun_Log_Models_ProjectSqlRequest $request the GetLogs request parameters class. * @throws Aliyun_Log_Exception * @return Aliyun_Log_Models_GetLogsResponse */ diff --git a/Aliyun/Log/Models/CredentailsProvider.php b/Aliyun/Log/Models/CredentailsProvider.php new file mode 100644 index 0000000..3b73e34 --- /dev/null +++ b/Aliyun/Log/Models/CredentailsProvider.php @@ -0,0 +1,107 @@ +accessKeyId = $accessKeyId; + $this->accessKeySecret = $accessKeySecret; + $this->securityToken = $securityToken; + } + + /** + * @return string accessKeyId + */ + public function getAccessKeyId() + { + return $this->accessKeyId; + } + /** + * @param string $accessKeyId + */ + public function setAccessKeyId(string $accessKeyId) + { + $this->accessKeyId = $accessKeyId; + } + /** + * @return string accessKeySecret + */ + public function getAccessKeySecret() + { + return $this->accessKeySecret; + } + /** + * @param string $accessKeySecret + */ + public function setAccessKeySecret(string $accessKeySecret) + { + $this->accessKeySecret = $accessKeySecret; + } + /** + * @return string securityToken + */ + public function getSecurityToken() + { + return $this->securityToken; + } + /** + * @param string $securityToken + */ + public function setSecurityToken(string $securityToken) + { + $this->securityToken = $securityToken; + } +} + +interface Aliyun_Log_Models_CredentailsProvider +{ + /** + * @return Aliyun_Log_Models_Credentails + * @throws Exception + */ + public function getCredentials(): Aliyun_Log_Models_Credentails; +} + +class Aliyun_Log_Models_StaticCredentailsProvider implements Aliyun_Log_Models_CredentailsProvider +{ + /** + * @var Aliyun_Log_Models_Credentails + */ + private $credentials; + + /** + * @param string $accessKeyId + * @param string $accessKeySecret + * @param string $securityToken + */ + public function __construct(string $accessKeyId, string $accessKeySecret, string $securityToken = '') + { + $this->credentials = new Aliyun_Log_Models_Credentails($accessKeyId, $accessKeySecret, $securityToken); + } + /** + * @return Aliyun_Log_Models_Credentails + */ + public function getCredentials(): Aliyun_Log_Models_Credentails + { + return $this->credentials; + } +} From 6b7a45c13b28d50889a1de04706c4849b63ed26e Mon Sep 17 00:00:00 2001 From: "shuizhao.gh" Date: Tue, 28 May 2024 11:41:40 +0800 Subject: [PATCH 2/5] rename --- Aliyun/Log/Client.php | 22 ++++++------ Aliyun/Log/Models/CredentailsProvider.php | 32 +++-------------- .../Log/Models/StaticCredentialsProvider.php | 34 +++++++++++++++++++ 3 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 Aliyun/Log/Models/StaticCredentialsProvider.php diff --git a/Aliyun/Log/Client.php b/Aliyun/Log/Client.php index 404cc2a..0d5a162 100644 --- a/Aliyun/Log/Client.php +++ b/Aliyun/Log/Client.php @@ -24,7 +24,7 @@ class Aliyun_Log_Client { /** - *@var Aliyun_Log_Models_CredentailsProvider credentialsProvider + *@var Aliyun_Log_Models_CredentialsProvider credentialsProvider */ protected $credentialsProvider; @@ -66,23 +66,23 @@ class Aliyun_Log_Client { * aliyun accessKey * @param string $token * aliyun token - * @param Aliyun_Log_Models_CredentailsProvider $credentialsProvider + * @param Aliyun_Log_Models_CredentialsProvider $credentialsProvider */ public function __construct( string $endpoint, string $accessKeyId = "", string $accessKey = "", string $token = "", - Aliyun_Log_Models_CredentailsProvider $credentialsProvider = null + Aliyun_Log_Models_CredentialsProvider $credentialsProvider = null ) { $this->setEndpoint($endpoint); // set $this->logHost if (!is_null($credentialsProvider)) { $this->credentialsProvider = $credentialsProvider; } else { - if (empty($accessKeyId) || empty($accessKeySecret)) { + if (empty($accessKeyId) || empty($accessKey)) { throw new Aliyun_Log_Exception("InvalidAccessKey", "accessKeyId or accessKeySecret is empty", ""); } - $this->credentialsProvider = new Aliyun_Log_Models_StaticCredentailsProvider( + $this->credentialsProvider = new Aliyun_Log_Models_StaticCredentialsProvider( $accessKeyId, $accessKey, $token @@ -196,9 +196,9 @@ private function sendRequest($method, $url, $body, $headers) { * @throws Aliyun_Log_Exception */ private function send($method, $project, $body, $resource, $params, $headers) { - $credentails = null; + $credentials = null; try { - $credentails = $this->credentialsProvider->getCredentials(); + $credentials = $this->credentialsProvider->getCredentials(); } catch (Exception $ex) { throw new Aliyun_Log_Exception( 'InvalidCredentials', @@ -220,13 +220,13 @@ private function send($method, $project, $body, $resource, $params, $headers) { $headers ['x-log-apiversion'] = API_VERSION; $headers ['x-log-signaturemethod'] = 'hmac-sha1'; - if(strlen($credentails->getSecurityToken()) >0) - $headers ['x-acs-security-token'] = $credentails->getSecurityToken(); + if(strlen($credentials->getSecurityToken()) >0) + $headers ['x-acs-security-token'] = $credentials->getSecurityToken(); if(is_null($project))$headers ['Host'] = $this->logHost; else $headers ['Host'] = "$project.$this->logHost"; $headers ['Date'] = $this->GetGMT (); - $signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $credentails->getAccessKeySecret(), $credentails->getSecurityToken(), $params, $headers ); - $headers ['Authorization'] = "LOG ".$credentails->getAccessKeyId().":$signature"; + $signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $credentials->getAccessKeySecret(), $credentials->getSecurityToken(), $params, $headers ); + $headers ['Authorization'] = "LOG ".$credentials->getAccessKeyId().":$signature"; $url = $resource; if ($params) diff --git a/Aliyun/Log/Models/CredentailsProvider.php b/Aliyun/Log/Models/CredentailsProvider.php index 3b73e34..beda65f 100644 --- a/Aliyun/Log/Models/CredentailsProvider.php +++ b/Aliyun/Log/Models/CredentailsProvider.php @@ -6,7 +6,7 @@ */ -class Aliyun_Log_Models_Credentails +class Aliyun_Log_Models_Credentials { /** * @var string @@ -72,36 +72,12 @@ public function setSecurityToken(string $securityToken) } } -interface Aliyun_Log_Models_CredentailsProvider +interface Aliyun_Log_Models_CredentialsProvider { /** - * @return Aliyun_Log_Models_Credentails + * @return Aliyun_Log_Models_Credentials * @throws Exception */ - public function getCredentials(): Aliyun_Log_Models_Credentails; + public function getCredentials(): Aliyun_Log_Models_Credentials; } -class Aliyun_Log_Models_StaticCredentailsProvider implements Aliyun_Log_Models_CredentailsProvider -{ - /** - * @var Aliyun_Log_Models_Credentails - */ - private $credentials; - - /** - * @param string $accessKeyId - * @param string $accessKeySecret - * @param string $securityToken - */ - public function __construct(string $accessKeyId, string $accessKeySecret, string $securityToken = '') - { - $this->credentials = new Aliyun_Log_Models_Credentails($accessKeyId, $accessKeySecret, $securityToken); - } - /** - * @return Aliyun_Log_Models_Credentails - */ - public function getCredentials(): Aliyun_Log_Models_Credentails - { - return $this->credentials; - } -} diff --git a/Aliyun/Log/Models/StaticCredentialsProvider.php b/Aliyun/Log/Models/StaticCredentialsProvider.php new file mode 100644 index 0000000..f176e5d --- /dev/null +++ b/Aliyun/Log/Models/StaticCredentialsProvider.php @@ -0,0 +1,34 @@ +credentials = new Aliyun_Log_Models_Credentials($accessKeyId, $accessKeySecret, $securityToken); + } + /** + * @return Aliyun_Log_Models_Credentials + */ + public function getCredentials(): Aliyun_Log_Models_Credentials + { + return $this->credentials; + } +} \ No newline at end of file From 63b368a74057df2c87cbfac3fdeed8480b9aa482 Mon Sep 17 00:00:00 2001 From: "shuizhao.gh" Date: Tue, 28 May 2024 11:56:36 +0800 Subject: [PATCH 3/5] rename file --- .../Models/{CredentailsProvider.php => CredentialsProvider.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Aliyun/Log/Models/{CredentailsProvider.php => CredentialsProvider.php} (100%) diff --git a/Aliyun/Log/Models/CredentailsProvider.php b/Aliyun/Log/Models/CredentialsProvider.php similarity index 100% rename from Aliyun/Log/Models/CredentailsProvider.php rename to Aliyun/Log/Models/CredentialsProvider.php From a84e103f9a9532d28ff48dc27cab507c80784df3 Mon Sep 17 00:00:00 2001 From: "shuizhao.gh" Date: Tue, 28 May 2024 11:58:39 +0800 Subject: [PATCH 4/5] chore: add sample --- sample/credentialsProviderSample.php | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 sample/credentialsProviderSample.php diff --git a/sample/credentialsProviderSample.php b/sample/credentialsProviderSample.php new file mode 100644 index 0000000..2583daa --- /dev/null +++ b/sample/credentialsProviderSample.php @@ -0,0 +1,49 @@ + 'TestContent', + 'kv_json' => '{"a": "b", "c": 19021}' + ); + $logItem = new Aliyun_Log_Models_LogItem(); + $logItem->setTime(time()); + $logItem->setContents($contents); + $logitems = array($logItem); + $request = new Aliyun_Log_Models_PutLogsRequest( + $project, + $logstore, + $topic, + null, + $logitems + ); + + try { + $response = $client->putLogs($request); + } catch (Aliyun_Log_Exception $ex) { + var_dump($ex); + } catch (Exception $ex) { + var_dump($ex); + } +} + +putLogs($client, $project, $logstore); +$res = $client->getLogs($req); +var_dump($res->getLogs()); From 1268e03f7c0ad1a1205aa952da8166050e178c23 Mon Sep 17 00:00:00 2001 From: "shuizhao.gh" Date: Tue, 28 May 2024 13:55:57 +0800 Subject: [PATCH 5/5] chore: update reademe and version --- README.md | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 61f1ac5..9051a4d 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ## API VERSION -0.6.2 +0.6.4 ## SDK RELEASE TIME -2018-02-18 +2024-05-28 ## Introduction diff --git a/composer.json b/composer.json index bc5f72e..4ecca9a 100644 --- a/composer.json +++ b/composer.json @@ -32,5 +32,5 @@ ] }, "name": "alibabacloud/aliyun-log-php-sdk", - "version": "0.6.3" + "version": "0.6.4" }