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 文件。