承接 paragonie/ciphersweet-provider-aws-kms 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

paragonie/ciphersweet-provider-aws-kms

Composer 安装命令:

composer require paragonie/ciphersweet-provider-aws-kms

包简介

CipherSweet key provider backed by AWS KMS

README 文档

README

Static Analysis Latest Stable Version Latest Unstable Version License Downloads

This repository exists to provide a distinct Composer package useful for integrating CipherSweet with AWS KMS.

Installing

composer require paragonie/ciphersweet-provider-aws-kms

Usage

KmsKeyProvider

The basic KmsKeyProvider class is intended to work with a single Encrypted Data Key (EDK). If you're looking to provide multi-tenancy (e.g., one data key per user), look instead at MultiTenantKmsKeyProvider.

First, you'll need a KmsClient object, a desired CipherSweet backend, and the Key ID or ARN for the KMS key you want to use.

<?php
use Aws\Kms\KmsClient;
use ParagonIE\Certainty\RemoteFetch;
use ParagonIE\CipherSweet\Backend\BoringCrypto;
use ParagonIE\CipherSweet\KeyProvider\KmsKeyProvider;

// Recommended: always use the latest CACert bundle
$remoteFetch = new RemoteFetch('/path/to/cacert-dir');
$latestBundle = $remoteFetch->getLatestBundle()->getFilePath();
$keyID = ''; /* get this from KMS */

$kmsClient = new KmsClient([
    'profile' => 'default',
    'region' => 'us-east-1',
    'http' => ['verify' => $latestBundle]
]);

// Recommended: Use encryption context for your apps
$encryptionContext = [
    'app' => 'foo.example.com'
];

Once you have these value defined, you will first want to generate a new data key and persist the Encrypted Data Key to be reused, like so:

$newKey = KmsKeyProvider::generate(
    $kmsClient,
    new BoringCrypto(), // Your backend goes here
    $keyID,
    $encryptionContext
);
// Save this somewhere so you can reuse it:
$edk = $newKey->getEncryptedDataKey();

From now on, you can simply load your backend as follows:

// Moving forward, you can simply instantiate your key provider like so:
$provider = new KmsKeyProvider(
    $kmsClient,
    new BoringCrypto(), // Your backend goes here
    $keyID,
    $encryptionContext,
    $edk
);

See also: caching

MultiTenantKmsKeyProvider

The purpose of the provided MultiTenantKmsKeyProvider class is to facilitate workloads where multiple users have their data encrypted with different EDKs. This can safely be used with the same KMS Key or with different KMS Keys. Whatever makes the most sense for your application.

The basic idea behind our design is that some metadata about tenants is stored in a column (which has a value populated for each row):

/** @var \ParagonIE\CipherSweet\KeyProvider\MultiTenantKmsKeyProvider $multiPro */
$multiPro->setTenantColumnForTable('table_name', 'tenant_id_column_name');

Somewhere else in your application, you will need a mapping of tenant IDs to EDKs. This MAY be a separate SQL table. We have provided some convenience utilities to make integration easier, but you're free to decide your own mapping and persistence strategy.

To that end, our multi-tenant key provider allows you to provide a class that implements TenantEDKInterface to fetch EDKs and other metadata, as well as create tenants. You are free to implement this however you wish. See, for example, our EasyDB test class.

To create a new tenant (and a new EDK), simply pass the new tenant's ID, the KMS Key ID or ARN, and Encryption Context to use for encrypting this key.

// Calling createTenant() will persist it to memory
$specificProvider = $multiPro->createTenant($tenantID, $kmsKeyID, $encryptionContext);

With this little bit of additional glue code on your end, you're all set.

<?php
use ParagonIE\CipherSweet\CipherSweet;
use ParagonIE\CipherSweet\EncryptedMultiRows;
use ParagonIE\CipherSweet\KeyProvider\MultiTenantKmsKeyProvider;

/**
 * @var \Aws\Kms\KmsClient $kmsClient
 * @var \ParagonIE\CipherSweet\KeyProvider\TenantEDKInterface $edkLookup
 */

$multiPro = (new MultiTenantKmsKeyProvider())
    ->setEDKLookup($edkLookup)
    ->setKmsClient($kmsClient);

$multiPro->setTenantColumnForTable('table_1_name', 'tenant_id');

$multiPro->createTenant('example_1', 'kms_key_id_goes_here', ['region' => 'us-east-2']);
$multiPro->createTenant('example_2', 'kms_key_id_goes_here', ['region' => 'us-west-1']);

$engine = new CipherSweet($multiPro, $multiPro->getBackend());
$encryptManyRows = (new EncryptedMultiRows($engine))->setAutoBindContext(true);

And then you can just use CipherSweet as usual.

Using a Static Key for Blind Indexing with Multi-Tenancy

You can toggle this on with a single call to multi-tenant key provider.

$multiPro->setStaticBlindIndexTenant("dedicated-searching-tenant-id-goes-here");

To toggle this feature back off, pass null instead:

$multiPro->setStaticBlindIndexTenant(null);

Caching

Network round-trips to AWS KMS can be a performance bottleneck for your application, especially if you're running it outside of AWS.

Applications MAY provide a PSR-16 compatible cache to persist plaintext data keys across requests.

/**
 * @var \ParagonIE\CipherSweet\KeyProvider\MultiTenantKmsKeyProvider $multiPro
 * @var \ParagonIE\CipherSweet\KeyProvider\KmsKeyProvider $provider
 * @var \Psr\SimpleCache\CacheInterface $yourCache
 */

// This will pass $yourCache to all KmsKeyProviders managed by this multi-tenant provider:
$multiPro->setDataKeyCache($yourCache);

// For only one single-tenant provider:
$provider->setDataKeyCache($yourCache);

paragonie/ciphersweet-provider-aws-kms 适用场景与选型建议

paragonie/ciphersweet-provider-aws-kms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 66 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 paragonie/ciphersweet-provider-aws-kms 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 66
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: ISC
  • 更新时间: 2024-05-13