keboola / object-encryptor
JSON对象加密库
Requires
- php: >=8.2
- ext-json: *
- ext-zlib: *
- aws/aws-sdk-php: ^3.209
- defuse/php-encryption: ^2.3
- google/cloud-kms: ^1.20
- keboola/azure-key-vault-client: ^3.0
- keboola/common-exceptions: ^1.2
- vkartaviy/retry: ^0.2
Requires (Dev)
- infection/infection: ^0.26
- keboola/coding-standard: ^15.0
- phpstan/phpstan: ^1.8
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- sempro/phpunit-pretty-print: ^1.4
- symfony/dotenv: ^5.4|^6.0
README
对象加密器
该库提供了加密PHP数组、stdclass对象和标量的接口。密文可以包含额外的元数据,这些元数据限制了密文可以被解密的条件。该库使用AWS KMS或Azure Key Vault管理密钥或Google Cloud KMS的defuse/php-encryption加密方法。
要求
该库支持PHP 8.1及以上版本。
用法
库的入口点是ObjectEncryptorFactory
类,该类创建ObjectEncryptor
类的实例,该类具有encryptGeneric
、encryptForComponent
、encryptForProject
、encryptForConfiguration
、encryptForProjectWide
、encryptForBranchType
、encryptForBranchTypeConfiguration
、encryptForProjectWideBranchType
和相应的decryptXXX
方法。实际的加密和解密机制使用加密包装器实现。加密包装器使用密文元数据实现不同的验证方法。
用法
使用工厂类初始化库
$encryptor = ObjectEncryptorFactory::getEncryptor( new EncryptorOptions( 'my-stack', $kmsKeyId, $kmsRegion, $akvUrl ) ); $encryptor->encryptForComponent('secret', 'my-component');
或者,您可以使用getAwsEncryptor
、getAzureEncryptor
和getGcpEncryptor
来获取特定于云的对象加密器。
包装器
根据提供的密钥和参数,以下包装器将可用
GenericKMSWrapper
- 用于KBC::Secure::
密文的AWS KMS包装器,需要kmsKeyId
和kmsRegion
。此外,运行者必须具备AWS凭证(AWS_ACCESS_KEY_ID
、AWS_SECRET_ACCESS_KEY
)。ComponentKMSWrapper
- 用于KBC::ComponentSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
和componentId
。ProjectKMSWrapper
- 用于KBC::ProjectSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
、componentId
和projectId
。ConfigurationKMSWrapper
- 用于KBC::ConfigSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
、componentId
、projectId
和configurationId
。ProjectWideKMSWrapper
- 用于KBC::ProjectWideSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
和projectId
。BranchTypeProjectKMSWrapper
- 用于KBC::BranchTypeSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
、componentId
、projectId
和branchType
。BranchTypeConfigurationKMSWrapper
- 用于KBC::BranchTypeConfigSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
、componentId
、projectId
、configurationId
和branchType
。BranchTypeProjectWideKMSWrapper
- 用于KBC::ProjectWideBranchTypeSecure::
密文的AWS KMS包装器,需要kmsKeyId
、kmsRegion
、stackId
、projectId
和branchType
。GenericAKVWrapper
- 用于KBC::SecureKV::
密文的Azure Key Vault包装器,需要akvUrl
。此外,运行者必须具备AWS凭证(AWS_ACCESS_KEY_ID
、AWS_SECRET_ACCESS_KEY
)。ComponentAKVWrapper
- 用于KBC::ComponentSecureKV::
密文的Azure Key Vault包装器,需要akvUrl
、stackId
和componentId
。ProjectAKVWrapper
- 用于KBC::ProjectSecureKV::
密文的Azure Key Vault包装器,需要akvUrl
、stackId
、componentId
和projectId
。ConfigurationAKVWrapper
- 用于KBC::ConfigSecureKV::
密文的Azure Key Vault包装器,需要akvUrl
、stackId
、componentId
、projectId
和configurationId
。ProjectWideAKVWrapper
- 用于KBC::ProjectWideSecureKV::
密文的Azure Key Vault包装器,需要akvUrl
、stackId
和projectId
。BranchTypeProjectAKVWrapper
- Azure Key Vault 包装类KBC::BranchTypeSecureKV::
加密,需要akvUrl
、stackId
、componentId
、projectId 和branchType
。BranchTypeConfigurationAKVWrapper
- Azure Key Vault 包装类KBC::BranchTypeConfigSecureKV::
加密,需要akvUrl
、stackId
、componentId
、projectId
、configurationId
和branchType
。BranchTypeProjectWideAKVWrapper
- Azure Key Vault 包装类KBC::ProjectWideBranchTypeSecureKV::
加密,需要akvUrl
、stackId
、projectId
和branchType
。GenericGKMSWrapper
- Google KMS 包装类KBC::SecureGKMS::
加密,需要gkmsKeyId
。此外,运行者必须有可用的 AWS 凭据(AWS_ACCESS_KEY_ID
、AWS_SECRET_ACCESS_KEY
)ComponentGKMSWrapper
- Google KMS 包装类KBC::ComponentSecureGKMS::
加密,需要gkmsKeyId
、stackId
和componentId
ProjectGKMSWrapper
- Google KMS 包装类KBC::ProjectSecureGKMS::
加密,需要gkmsKeyId
、stackId
、componentId
和projectId
。ConfigurationGKMSWrapper
- Google KMS 包装类KBC::ConfigSecureGKMS::
加密,需要gkmsKeyId
、stackId
、componentId
、projectId
和configurationId
。ProjectWideGKMSWrapper
- Google KMS 包装类KBC::ProjectWideSecureGKMS::
加密,需要gkmsKeyId
、stackId
和projectId
。BranchTypeProjectGKMSWrapper
- Google KMS 包装类KBC::BranchTypeSecureGKMS::
加密,需要gkmsKeyId
、stackId
、componentId
、projectId 和branchType
。BranchTypeConfigurationGKMSWrapper
- Google KMS 包装类KBC::BranchTypeConfigSecureGKMS::
加密,需要gkmsKeyId
、stackId
、componentId
、projectId
、configurationId
和branchType
。BranchTypeProjectWideGKMSWrapper
- Google KMS 包装类KBC::ProjectWideBranchTypeSecureGKMS::
加密,需要gkmsKeyId
、stackId
、projectId
和branchType
。
加密过程中必须指定包装类(每个 encryptXXX
方法使用一个)。解密时,包装类会根据密文前缀自动选择。这意味着 decryptForConfiguration
方法也能解密由 encryptForComponent
或 encryptForProject
创建的密文。如果包装类不可用(密钥或参数未设置或与密文中的一致),则无法解密值并抛出异常。
开发
先决条件
- 配置了云服务提供商的访问权限
- 安装了 Azure CLI
az
(并运行az login
) - 安装了 AWS CLI
aws
(并运行aws configure --profile YOUR_AWS_PROFILE_NAME
) - 安装了 GCP CLI
gcloud
(并运行gcloud auth login
或gcloud auth application-default login
)
- 安装了 Azure CLI
- 安装了
terraform
(https://www.terraform.io)和jq
(https://stedolan.github.io/jq)以设置本地环境 - 安装了
docker
和docker-compose
以运行和开发应用程序
export NAME_PREFIX= # your name/nickname to make your resource unique & recognizable export AWS_PROFILE= # your AWS profile name e.g. Keboola-Dev-Platform-Services-AWSAdministratorAccess cat <<EOF > ./provisioning/local/terraform.tfvars name_prefix = "${NAME_PREFIX}" EOF terraform -chdir=./provisioning/local init -backend-config="key=object-encryptor/${NAME_PREFIX}.tfstate" terraform -chdir=./provisioning/local apply ./provisioning/local/update-env.sh aws # or azure or gcp docker-compose run --rm tests
许可证
MIT 许可,请参阅 LICENSE 文件。