mittwald/vault-php

HashiCorp Vault 的 PHP 客户端库

2.1.0 2024-08-26 11:39 UTC

This package is auto-updated.

Last update: 2024-08-26 11:40:28 UTC


README

Tests

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

欢迎提交 Pull Requests 以添加改进或缺失的功能。

安装

Composer

composer require mittwald/vault-php

实现的功能

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

基本用法

// 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 中指示的密钥名称不存在时抛出。