nerd4ever/vault-php

Vault 的 PHP 库

1.0.0 2020-11-26 17:14 UTC

This package is not auto-updated.

Last update: 2024-09-17 09:19:16 UTC


README

Tests

Hashicorp Vault 服务的 PHP 客户端库。此客户端遵循 Hashicorp Vault 客户端文档中的请求和响应数据。

请随意通过 Pull Requests 添加改进或缺失的功能。

安装

Composer

composer require mittwald/vault-php

实现的功能

  • 认证
    • 用户/密码
    • 令牌
    • Kubernetes
  • 秘密引擎
    • 传输引擎
      • 加密/解密
      • 更新密钥配置
      • 创建密钥
      • 删除密钥
      • 列出密钥

基本用法

// setting up independent http client 
$httpClient = new Client();

// setting up vault auth provider
$auth = new Token('foo');

// creating the vault request client
$client = new VaultClient(
    $httpClient,
    $auth,
    'http://127.0.0.1:8200'
);

// selecting the desired secret engine
// e.g. Transit Secret Engine
$api = new Transit($client);

// calling specific endpoint
$response = $api->listKeys();

//reading results
var_dump($response->getKeys());
//...
//...
//Profit...

VaultClient

public function __construct(
    HttpClient $httpClient,
    AuthenticationProviderInterface $authProvider,
    string $apiHost
)

HttpClient 使用所有符合 PSR-18 的 HTTP 客户端适配器,例如 "php-http/curl-client": "^1.7"

AuthenticationProviderInterface 来自 /authentication/provider/* 的认证提供者

$apiHost Hashicorp Vault REST 端点 URL

批量请求

使用批量请求还需要遍历响应,并在每个批量项的 MetaData 中调用 hasErrors,以确保其成功处理。

异常

调用库方法将抛出异常,指示何时提供无效数据或发生 HTTP 错误或遇到 Vault 通用端点错误。

VaultException

通用根异常,本库中所有异常都从此异常扩展而来。

VaultHttpException

当 HTTP 处理内部出现错误时,将抛出此异常。

VaultAuthenticationException

当 API 端点认证失败时,将抛出此异常。

VaultResponseException

当遇到 5xx 状态代码错误时,将抛出此异常。

InvalidRouteException

调用无效/不存在/已禁用的 Vault API 端点将抛出此异常。

InvalidDataException

异常指示服务器有效载荷验证失败。

KeyNameNotFoundException

尝试请求一个 API 端点,其中键名(即 url 中指示的)不存在时,将抛出此异常。