pmg / cred-commands
此包已被弃用且不再维护。未建议替代包。
Symfony 控制台命令,用于与 AWS SSM 参数存储交互
v2.0.0
2022-01-11 14:42 UTC
Requires
- php: ^8.0
- aws/aws-sdk-php: ^3.0
- symfony/console: ^4.4 || ^5.4
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/phpunit-bridge: ^4.4 || ^5.4
README
这个库的诞生是为了帮助管理 AWS ECS 中的秘密,详情请参阅 此处。
在 ECS 开始支持 秘密 之前,PMG 还使用这些命令。
pmg/cred-commands
这是一组与 AWS SSM 参数存储 交互的 symfony 控制台命令。
目标是提供一个简单的方法,在应用启动时将凭据(环境变量)加载到内存中。有关为什么可能需要这样做的一些细节,请参阅 此博客文章。
安装
composer require pmg/cred-commands
使用方法
使用内置应用
#!/usr/bin/env php <?php use Aws\Ssm\SsmClient; use PMG\CredCommands\Application; use PMG\CredCommands\CredentialClient; $ssm = SsmClient::factory([ 'version' => 'latest', 'region' => 'us-east-1', ]); $client = new CredentialClient($ssm); $app = new Application($client, 'App Name', 'App Version'); $app->run();
向现有控制台应用添加命令
#!/usr/bin/env php <?php use Aws\Ssm\SsmClient; use Symfony\Component\Console\Application; use PMG\CredCommands\CredentialClient; use PMG\CredCommands\Command\GetCommand; use PMG\CredCommands\Command\PutCommand; use PMG\CredCommands\Command\RemoveCommand; $app = new Application(); // other command added here or something... $ssm = SsmClient::factory([ 'version' => 'latest', 'region' => 'us-east-1', ]); $client = new CredentialClient($ssm); $app->add(new GetCommand($client)); $app->add(new PutCommand($client)); $app->add(new RemoveCommand($client)); $app->run();
CLI 使用方法
./bin/console creds:{get,put,remove}
自定义凭据名称格式化
默认情况下,所有传递给 CLI 的凭据名称都直接使用,但可以通过 CredentialNameFormatter
实现来更改。
默认提供了一些,都在 PMG\CredCommands\Formatter
命名空间中。
NullFormatter
这是默认设置,直接返回凭据名称。
use PMG\CredCommands\Formatter\NullFormatter; $formatter = new NullFormatter(); $formater->format('someCredential'); // 'someCredential'
TemplateFormatter
构造函数中接受一个 $template
,并将模板中的 {cred}
替换为凭据名称。
use PMG\CredCommands\Formatter\TemplateFormatter; $formatter = new TemplateFormater('prefix_{cred}'); $formater->format('someCredential'); // 'prefix_someCredential'
AppEnvFormatter
构建格式为 /{appName}/{environment}/{cred}
的类似路径的凭据名称。
use PMG\CredCommands\Formatter\AppEnvFormatter; $formatter = new AppEnvFormater('appName', 'prod'); $formater->format('someCredential'); // '/appName/prod/someCredential'
为什么要格式化?
因为前缀参数名称可以用于 限制凭据访问,通过配置使用实际参数名称的 IAM 权限。
例如,一个 IAM 角色可能仅包括对名为 /appName/prod/*
的参数的访问权限。
使用格式化工具
格式化器可以作为CredentialClient
的第二个参数传递。
<?php use Aws\Ssm\SsmClient; use PMG\CredCommands\Application; use PMG\CredCommands\CredentialClient; use PMG\CredCommands\Command\GetCommand; use PMG\CredCommands\Formatter\AppEnvFormatter; $ssm = SsmClient::factory([ // ... ]); $client = new CredentialClient( $ssm, new AppEnvFormatter('example', 'dev') ); // new GetCommand($client); // new Application($client, 'name', 'version'); // etc.
使用自定义KMS密钥进行参数加密
默认情况下,AWS(以及通过扩展此库)使用AWS账户的默认KMS密钥加密参数,当它们的类型设置为SecureString
时,正如在此库中所做的那样。
将第三个参数传递给CredentialClient
以指定KMS密钥ID。这可以是实际密钥ID(一个UUID)或密钥别名(格式为alias/{别名名称}
)。
<?php use Aws\Ssm\SsmClient; use PMG\CredCommands\CredentialClient; use PMG\CredCommands\Formatter\AppEnvFormatter; $ssm = SsmClient::factory([ // ... ]); // with a key ID (example, not a real key ID) $client = new CredentialClient( $ssm, new AppEnvFormatter('example', 'dev'), 'df502ce0-49e1-4579-a682-395274de6eb4', ); // with a key alias (example, not a real key alias) $client = new CredentialClient( $ssm, new AppEnvFormatter('example', 'dev'), 'alias/some-alias-here' );