2424class Aliyun_Log_Client {
2525
2626 /**
27- * @var string aliyun accessKey
27+ *@var Aliyun_Log_Models_CredentialsProvider credentialsProvider
2828 */
29- protected $ accessKey ;
30-
31- /**
32- * @var string aliyun accessKeyId
33- */
34- protected $ accessKeyId ;
35-
36- /**
37- *@var string aliyun sts token
38- */
39- protected $ stsToken ;
29+ protected $ credentialsProvider ;
4030
4131 /**
4232 * @var string LOG endpoint
@@ -64,20 +54,40 @@ class Aliyun_Log_Client {
6454 protected $ source ;
6555
6656 /**
67- * Aliyun_Log_Client constructor
57+ * Aliyun_Log_Client constructor.
58+ *
59+ * Either $accessKeyId/$accessKeySecret or $credentialsProvider must be provided.
6860 *
6961 * @param string $endpoint
7062 * LOG host name, for example, http://cn-hangzhou.sls.aliyuncs.com
7163 * @param string $accessKeyId
7264 * aliyun accessKeyId
7365 * @param string $accessKey
7466 * aliyun accessKey
67+ * @param string $token
68+ * aliyun token
69+ * @param Aliyun_Log_Models_CredentialsProvider $credentialsProvider
7570 */
76- public function __construct ($ endpoint , $ accessKeyId , $ accessKey ,$ token = "" ) {
77- $ this ->setEndpoint ( $ endpoint ); // set $this->logHost
78- $ this ->accessKeyId = $ accessKeyId ;
79- $ this ->accessKey = $ accessKey ;
80- $ this ->stsToken = $ token ;
71+ public function __construct (
72+ string $ endpoint ,
73+ string $ accessKeyId = "" ,
74+ string $ accessKey = "" ,
75+ string $ token = "" ,
76+ Aliyun_Log_Models_CredentialsProvider $ credentialsProvider = null
77+ ) {
78+ $ this ->setEndpoint ($ endpoint ); // set $this->logHost
79+ if (!is_null ($ credentialsProvider )) {
80+ $ this ->credentialsProvider = $ credentialsProvider ;
81+ } else {
82+ if (empty ($ accessKeyId ) || empty ($ accessKey )) {
83+ throw new Aliyun_Log_Exception ("InvalidAccessKey " , "accessKeyId or accessKeySecret is empty " , "" );
84+ }
85+ $ this ->credentialsProvider = new Aliyun_Log_Models_StaticCredentialsProvider (
86+ $ accessKeyId ,
87+ $ accessKey ,
88+ $ token
89+ );
90+ }
8191 $ this ->source = Aliyun_Log_Util::getLocalIp ();
8292 }
8393 private function setEndpoint ($ endpoint ) {
@@ -186,6 +196,17 @@ private function sendRequest($method, $url, $body, $headers) {
186196 * @throws Aliyun_Log_Exception
187197 */
188198 private function send ($ method , $ project , $ body , $ resource , $ params , $ headers ) {
199+ $ credentials = null ;
200+ try {
201+ $ credentials = $ this ->credentialsProvider ->getCredentials ();
202+ } catch (Exception $ ex ) {
203+ throw new Aliyun_Log_Exception (
204+ 'InvalidCredentials ' ,
205+ 'Fail to get credentials: ' . $ ex ->getMessage (),
206+ ''
207+ );
208+ }
209+
189210 if ($ body ) {
190211 $ headers ['Content-Length ' ] = strlen ( $ body );
191212 if (isset ($ headers ["x-log-bodyrawsize " ])==false )
@@ -199,13 +220,13 @@ private function send($method, $project, $body, $resource, $params, $headers) {
199220
200221 $ headers ['x-log-apiversion ' ] = API_VERSION ;
201222 $ headers ['x-log-signaturemethod ' ] = 'hmac-sha1 ' ;
202- if (strlen ($ this -> stsToken ) >0 )
203- $ headers ['x-acs-security-token ' ] = $ this -> stsToken ;
223+ if (strlen ($ credentials -> getSecurityToken () ) >0 )
224+ $ headers ['x-acs-security-token ' ] = $ credentials -> getSecurityToken () ;
204225 if (is_null ($ project ))$ headers ['Host ' ] = $ this ->logHost ;
205226 else $ headers ['Host ' ] = "$ project. $ this ->logHost " ;
206227 $ headers ['Date ' ] = $ this ->GetGMT ();
207- $ signature = Aliyun_Log_Util::getRequestAuthorization ( $ method , $ resource , $ this -> accessKey , $ this -> stsToken , $ params , $ headers );
208- $ headers ['Authorization ' ] = "LOG $ this -> accessKeyId : $ signature " ;
228+ $ signature = Aliyun_Log_Util::getRequestAuthorization ( $ method , $ resource , $ credentials -> getAccessKeySecret (), $ credentials -> getSecurityToken () , $ params , $ headers );
229+ $ headers ['Authorization ' ] = "LOG " . $ credentials -> getAccessKeyId (). " : $ signature " ;
209230
210231 $ url = $ resource ;
211232 if ($ params )
@@ -738,7 +759,7 @@ public function executeProjectSqlJson(Aliyun_Log_Models_ProjectSqlRequest $reque
738759 * Get logs from Log service.
739760 * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
740761 *
741- * @param Aliyun_Log_Models_GetProjectLogsRequest $request the GetLogs request parameters class.
762+ * @param Aliyun_Log_Models_ProjectSqlRequest $request the GetLogs request parameters class.
742763 * @throws Aliyun_Log_Exception
743764 * @return Aliyun_Log_Models_GetLogsResponse
744765 */
0 commit comments