battis / lazy-secrets
一个对google/cloud-secret-manager的轻量级封装,用于减少样板代码
v1.1
2024-03-05 19:23 UTC
Requires
- google/cloud-secret-manager: ^1.9
- psr/simple-cache: ^3.0
README
一个对google/cloud-secret-manager的轻量级封装,用于减少样板代码
安装
composer require battis/lazy-secrets
使用
use Battis\LazySecrets\Secrets; $data = Secrets::get("MY_APP_SECRET");
背景
虽然Google Cloud Secret Manager 是存储(和访问)应用程序密钥的好方法,但它也带来了一大堆我不希望犯错的样板代码。所以,与其写...
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; $client = new SecretManagerServiceClient(); $project = $_ENV["GOOGLE_CLOUD_PROJECT"]; $key = "MY_APP_SECRET"; $version = "latest"; $secret = $client->accessSecretVersion( "projects/$project/secrets/$key/versions/$version" ); $data = $secret->getPayload()->getData(); // and even (if you're packing a lot into one secret) $obj = json_decode($data); // ...and then using the $data or $obj
...我更愿意写
use Battis\LazySecrets\Secrets; $data = Secrets::get("MY_APP_SECRET"); // or Secrets::init($project, true); $obj = Secrets::get("MY_APP_SECRET");
或者,还有一个PSR-16 Simple Cache实现(方便与依赖注入一起使用)
use Battis\LazySecrets\Cache; // assume that the `GOOGLE_CLOUD_PROJECT` environment variable is set $secrets = new Cache(); $obj = $secrets->get("MY_APP_SECRET");
或者
/** src/Example/DependencyConsumer */ namespace Example; use Psr\SimpleCache\CacheInterface; class DependencyConsumer { public function __constructor(CacheInterface $cache) { // ... } }
/** src/app.php */ $container = new DI\Container([ Psr\SimpleCache\CacheInterface::class => DI\create( \Battis\LazySecrets\Cache::class ), ]); $consumer = $container->get(DependencyConsumer::class);