【发布时间】:2021-02-15 08:47:47
【问题描述】:
我在我的 API 中使用 GuzzleHTTP。我正在使用 Lumen 开发用于与 HashiCorp Vault API 通信的 API。为此,我安装了 GuzzleHTTP 来进行 API 调用。
例如,我正在尝试根据特定角色为数据库用户生成凭据。在命令行 cURL 请求向我响应新用户的凭据。虽然使用 Guzzle 并没有给我任何与新创建用户相关的信息。
我是 Guzzle 的新手,所以我很乐意提供任何建议。
cURL 请求如下所示:
curl -X GET \
--header "X-Vault-Token: $VAULT_TOKEN" \
http://192.168.1.3:8200/v1/database/creds/my-role | json_pp
响应给了我我想要的凭据:
{
"request_id" : "3eacc89b57b-2e2e-af6f-849f-868bdbfd2dc5e1",
"wrap_info" : null,
"renewable" : true,
"data" : {
"username" : "randomgeneratedusername",
"password" : "randomgeneratedpassword"
},
"auth" : null,
"lease_duration" : 3600,
"warnings" : null,
"lease_id" : "database/creds/my-role/randomsecretname"
}
当我的 Guzzle 代码如下所示时:
class MySQLVaultAPI extends VaultAPI
{
public function createUser() {
$response = $this->getClient()->request('GET', $this->url.'/database/creds/my-role',
['headers' => $this->headers]);
}
}
还有:
class VaultAPI
{
protected string $url;
protected Client $http;
protected array $headers;
public function __construct(Client $client)
{
$this->url = 'http://192.168.1.3:8200/v1';
$this->http = $client;
$this->headers = [
'X-Vault-Token' => 'secrettoken',
'Accept' => 'application/json',
];
}
}
以及来自 GuzzleHTTP 的响应(我在 $response 对象上使用了 symfony 转储程序):
^ GuzzleHttp\Psr7\Response {#43 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:4 [▼
"Cache-Control" => array:1 [▶]
"Content-Type" => array:1 [▶]
"Date" => array:1 [▶]
"Content-Length" => array:1 [▶]
]
-headerNames: array:4 [▼
"cache-control" => "Cache-Control"
"content-type" => "Content-Type"
"date" => "Date"
"content-length" => "Content-Length"
]
-protocol: "1.1"
-stream: GuzzleHttp\Psr7\Stream {#21 ▼
-stream: stream resource @137 ▶}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
【问题讨论】: