diff --git a/Aliyun/Log/Client.php b/Aliyun/Log/Client.php index 2a832b6..0d5a162 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_CredentialsProvider 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_CredentialsProvider $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_CredentialsProvider $credentialsProvider = null + ) { + $this->setEndpoint($endpoint); // set $this->logHost + if (!is_null($credentialsProvider)) { + $this->credentialsProvider = $credentialsProvider; + } else { + if (empty($accessKeyId) || empty($accessKey)) { + throw new Aliyun_Log_Exception("InvalidAccessKey", "accessKeyId or accessKeySecret is empty", ""); + } + $this->credentialsProvider = new Aliyun_Log_Models_StaticCredentialsProvider( + $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) { + $credentials = null; + try { + $credentials = $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($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, $this->accessKey,$this->stsToken, $params, $headers ); - $headers ['Authorization'] = "LOG $this->accessKeyId:$signature"; + $signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $credentials->getAccessKeySecret(), $credentials->getSecurityToken(), $params, $headers ); + $headers ['Authorization'] = "LOG ".$credentials->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/CredentialsProvider.php b/Aliyun/Log/Models/CredentialsProvider.php new file mode 100644 index 0000000..beda65f --- /dev/null +++ b/Aliyun/Log/Models/CredentialsProvider.php @@ -0,0 +1,83 @@ +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_CredentialsProvider +{ + /** + * @return Aliyun_Log_Models_Credentials + * @throws Exception + */ + public function getCredentials(): Aliyun_Log_Models_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 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" } 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());