定制 secretary/core 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

secretary/core

Composer 安装命令:

composer require secretary/core

包简介

Secrets Manager for PHP

README 文档

README

Latest Stable Version Total Downloads

Secrets are an important aspect of most applications you can build. How you store them, and keep them "secret" is a challenge. Luckily, there are tools you can use to keep them all safe.

Secretary is a tool to integrate your PHP application with these tools.

Table of Contents

  1. Installation
  2. Api Documentation
    1. Secretary\Manager
      1. Initializing
      2. getSecret
      3. putSecret
      4. deleteSecret
      5. getAdapter
    2. Secretary\Secret
      1. getKey
      2. getValue

Installation

$ composer req secretary/core
Choose the version you need
Version (X.Y.Z) PHP Symfony Comment
3.* >= 8.1.0 7.0 Current version
2.* >= 8.1.0 5.4, 6.0 Previous version
1.* >= 7.4.0 5.3 Previous version

By itself, the core is useless. You will also need to add at least one adapter:

Storage Engine Badges
AWS Secrets Manager Latest Stable Version Total Downloads
HashiCorp Vault Latest Stable Version Total Downloads
JSON File Latest Stable Version Total Downloads

There are also miscellaneous packages that add on to Secretary

Package Purpose Badges
PSR-6 Cache Adapter Allows for caching secrets using a PSR-6 Cache Interface Latest Stable Version Total Downloads
PSR-16 Cache Adapter Allows for caching secrets using a PSR-16 Cache Interface Latest Stable Version Total Downloads
Secretary Bundle Allows for integrating with the Symfony Framework Latest Stable Version Total Downloads

Api Documentation

There's two classes you interface with in Secretary:

Secretary\Manager

Secretary\Manager->__construct(AdapterInterface $adapter)

Pass in your desired adapter.

<?php
use Secretary\Manager;
use Secretary\Adapter\AWS\SecretsManager\LocalJSONFileAdapter;

$manager = new Manager(
    new LocalJSONFileAdapter([
        'region'      => 'us-east-1',
        'credentials' => [
            'accessKeyId'     => 'myAccessKeyId',
            'secretAccessKey' => 'mySecretAccessKey'
        ]
    ])
);

Optionally, you may wrap your adapter, with one of the two cache adapters.

<?php
use Secretary\Manager;
use Secretary\Adapter\AWS\SecretsManager\LocalJSONFileAdapter;

use Secretary\Adapter\Cache\PSR6Cache\ChainAdapter;
use Cache\Adapter\Apc\ApcCachePool;

$manager = new Manager(
    new ChainAdapter(
        new LocalJSONFileAdapter([
            'region'      => 'us-east-1',
            'credentials' => [
                'accessKeyId'     => 'myAccessKeyId',
                'secretAccessKey' => 'mySecretAccessKey'
            ]
        ]),
        new ApcCachePool()
    )
);

For mor information on the arguments and options for the adapters, view their respective documentation.

Secretary\Manager->getSecret(string $key, ?array $options): Secret

Fetches a secret from the configured adapter. $key is the name of the secret (or path) you are trying to get.

Certain adapters will take custom options as well, like VersionId and VersionStage for the AWS SecretsManager Adapter

This will throw a Secretary\SecretNotFoundException if the secret cannot be found

$secret = $manager->getSecret('databases/redis/dsn');
/*
Secret {
    "path" = "databases/redis/dsn",
    "value" = "redis://localhost:6379"
}
*/

Some adapters also support storing a key/value map as a secret's value.

$secret = $manager->getSecret('databases/redis');
/*
Secret {
    "path" = "databases/redis",
    "value" = [
        "dsn" => "redis://localhost:6379",
        "password" => "my_super_strong_password" 
    ]
}
*/

Secretary\Manager->putSecret(string $key, string|array $value, ?array $options): void

Puts a secret with the given $value, into the storage engine, under the given $key.

If the current adapter doesn't support arrays, and you pass one it, it will throw a Secretary\ValueNotSupportedException.

Again, some adapters allow passing in custom options to send along with the request.

$manager->putSecret('database/redis', 'postgres://localhost:5432');

And for adapters that support a key/value map as a value:

$manager->putSecret('database/redis', ['dsn' => 'redis://localhost:6379', 'password' => 'my_super_strong_password']);

Secretary\Manager->deleteSecret(string $key, ?array $options): void

Deletes a secret from the storage engine using the given $key.

Again, some adapters allow passing in custom options to send along with the request.

$manager->deleteSecret('database/redis');

Secretary\Manager->getAdapter(): AdapterInterface

Will return the adapter that was passed to this manager during construction.

Secretary\Secret

This class implements ArrayAccess, so if your secret supports passing a key/value map, you can grab straight from the map:

Secrets are immutable, so attempting to change a value will throw an Exception.

$secret = $manager->getSecret('database/redis');

$dsn = $secret['dsn'];

Secretary\Secret->getKey(): string

Returns the key for the secret

$secret = $manager->getSecret('dabase/redis');

$secret->getKey() === 'database/redis'; // true

Secretary\Secret->getValue(): string | array

Returns the value for the secret. If the secret is a key/value map, its an array

$secret = $manager->getSecret('dabase/redis/dsn');

$secret->getValue() === 'redis://localhost:6379'; // true

// Or

$secret = $manager->getSecret('dabase/redis');

print_r($secret->getValue()); 
/*
[
    "dsn" => "redis://localhost:6379",
    "password" => "my_super_strong_password" 
]
*/

secretary/core 适用场景与选型建议

secretary/core 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 100.1k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「secrets」 「vault」 「keyvault」 「secretary」 「secretsmanager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 secretary/core 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 secretary/core 我们能提供哪些服务?
定制开发 / 二次开发

基于 secretary/core 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 100.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-29