Skip to content

Commit 86f8e07

Browse files
authored
Credentials provider (#47)
* feat: support credentials provider
1 parent ed0bedc commit 86f8e07

File tree

6 files changed

+213
-26
lines changed

6 files changed

+213
-26
lines changed

Aliyun/Log/Client.php

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,9 @@
2424
class 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
*/
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) Alibaba Cloud Computing
5+
* All rights reserved
6+
*/
7+
8+
9+
class Aliyun_Log_Models_Credentials
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $accessKeyId;
15+
/**
16+
* @var string
17+
*/
18+
private $accessKeySecret;
19+
/**
20+
* @var string
21+
*/
22+
private $securityToken;
23+
24+
public function __construct(string $accessKeyId, string $accessKeySecret, string $securityToken = '')
25+
{
26+
$this->accessKeyId = $accessKeyId;
27+
$this->accessKeySecret = $accessKeySecret;
28+
$this->securityToken = $securityToken;
29+
}
30+
31+
/**
32+
* @return string accessKeyId
33+
*/
34+
public function getAccessKeyId()
35+
{
36+
return $this->accessKeyId;
37+
}
38+
/**
39+
* @param string $accessKeyId
40+
*/
41+
public function setAccessKeyId(string $accessKeyId)
42+
{
43+
$this->accessKeyId = $accessKeyId;
44+
}
45+
/**
46+
* @return string accessKeySecret
47+
*/
48+
public function getAccessKeySecret()
49+
{
50+
return $this->accessKeySecret;
51+
}
52+
/**
53+
* @param string $accessKeySecret
54+
*/
55+
public function setAccessKeySecret(string $accessKeySecret)
56+
{
57+
$this->accessKeySecret = $accessKeySecret;
58+
}
59+
/**
60+
* @return string securityToken
61+
*/
62+
public function getSecurityToken()
63+
{
64+
return $this->securityToken;
65+
}
66+
/**
67+
* @param string $securityToken
68+
*/
69+
public function setSecurityToken(string $securityToken)
70+
{
71+
$this->securityToken = $securityToken;
72+
}
73+
}
74+
75+
interface Aliyun_Log_Models_CredentialsProvider
76+
{
77+
/**
78+
* @return Aliyun_Log_Models_Credentials
79+
* @throws Exception
80+
*/
81+
public function getCredentials(): Aliyun_Log_Models_Credentials;
82+
}
83+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) Alibaba Cloud Computing
5+
* All rights reserved
6+
*/
7+
8+
require_once realpath(dirname(__FILE__) . '/CredentialsProvider.php');
9+
10+
11+
class Aliyun_Log_Models_StaticCredentialsProvider implements Aliyun_Log_Models_CredentialsProvider
12+
{
13+
/**
14+
* @var Aliyun_Log_Models_Credentials
15+
*/
16+
private $credentials;
17+
18+
/**
19+
* @param string $accessKeyId
20+
* @param string $accessKeySecret
21+
* @param string $securityToken
22+
*/
23+
public function __construct(string $accessKeyId, string $accessKeySecret, string $securityToken = '')
24+
{
25+
$this->credentials = new Aliyun_Log_Models_Credentials($accessKeyId, $accessKeySecret, $securityToken);
26+
}
27+
/**
28+
* @return Aliyun_Log_Models_Credentials
29+
*/
30+
public function getCredentials(): Aliyun_Log_Models_Credentials
31+
{
32+
return $this->credentials;
33+
}
34+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## API VERSION
44

5-
0.6.2
5+
0.6.4
66

77
## SDK RELEASE TIME
88

9-
2018-02-18
9+
2024-05-28
1010

1111
## Introduction
1212

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
]
3333
},
3434
"name": "alibabacloud/aliyun-log-php-sdk",
35-
"version": "0.6.3"
35+
"version": "0.6.4"
3636
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
echo "Hello, World!";
3+
require_once realpath(dirname(__FILE__) . '/./Log_Autoload.php');
4+
5+
$endpoint = 'cn-hangzhou.log.aliyuncs.com';
6+
$accessKeyId = '';
7+
$accessKey = '';
8+
$token = "";
9+
$project = 'test';
10+
$logstore = 'test';
11+
12+
13+
$credentialsProvider = new Aliyun_Log_Models_StaticCredentialsProvider($accessKeyId, $accessKey, $token);
14+
$client = new Aliyun_Log_Client($endpoint, "", "", "", $credentialsProvider);
15+
16+
$req = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, 1698740109, 1698744321, '', '*', null, null, null, null);
17+
18+
function putLogs(Aliyun_Log_Client $client, $project, $logstore)
19+
{
20+
$topic = 'TestTopic';
21+
22+
$contents = array( // key-value pair
23+
'TestKey' => 'TestContent',
24+
'kv_json' => '{"a": "b", "c": 19021}'
25+
);
26+
$logItem = new Aliyun_Log_Models_LogItem();
27+
$logItem->setTime(time());
28+
$logItem->setContents($contents);
29+
$logitems = array($logItem);
30+
$request = new Aliyun_Log_Models_PutLogsRequest(
31+
$project,
32+
$logstore,
33+
$topic,
34+
null,
35+
$logitems
36+
);
37+
38+
try {
39+
$response = $client->putLogs($request);
40+
} catch (Aliyun_Log_Exception $ex) {
41+
var_dump($ex);
42+
} catch (Exception $ex) {
43+
var_dump($ex);
44+
}
45+
}
46+
47+
putLogs($client, $project, $logstore);
48+
$res = $client->getLogs($req);
49+
var_dump($res->getLogs());

0 commit comments

Comments
 (0)