定制 henzeb/laravel-encrypted-data-rotator 二次开发

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

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

henzeb/laravel-encrypted-data-rotator

Composer 安装命令:

composer require henzeb/laravel-encrypted-data-rotator

包简介

Rotates your encrypted data.

README 文档

README

Build Status Latest Version on Packagist Total Downloads License

Laravel 11 comes with app key rotation. Sadly, at this point, laravel won't re-encrypt the data unless a user changes it.

This package allows you to rotate Models by default, but also allows you to rotate your own custom encrypted data.

Installation

Just install with the following command.

composer require henzeb/laravel-encrypted-data-rotator

Usage

Configuring

You do not need to modify anything when you are just going to rotate the Models. Everything is done automatically for you. Most is configurable by environment variables.

Publishing the configuration

The configuration file is published with the following command:

php artisan vendor:publish --tag=rotator

Rotate the key

Laravel does not come with a handy key rotate command, you need to manually save the key in the APP_PREVIOUS_KEYS variable, which you might forget, or simply make mistakes with.

php artisan key:rotate

You can also select a different environment.

php artisan key:rotate --env your-env

On production, you are being asked if you want to rotate the keys. you can force this.

php artisan key:rotate --force

Rotate your data

When you have rotated the key, the encrypted data hasn't changed yet. Laravel will use the previous keys if the current one cannot decrypt the data. Using the following the data will also be rotated.

php artisan key:rotate-data

You do not have to do anything on the Models. The Models and their encrypted attributes are automatically detected.

Note: just like the key rotation command, key:rotate-data accepts env to specify an environment and force to start rotation without confirmation.

Rotating mutators

If you have mutators in your model that handles encrypted data, you want them to be rotated as well. For those situations you can use the Attribute EncryptsData. The key:rotate-data command will then detect the mutator and rotate its data.

use Henzeb\Rotator\Attributes\EncryptsData;

#[EncryptsData]
public function myAttribute(): Attribute
{
    return Attribute::set(
        fn($value) => encrypt($value)
    )->get(fn($value) => decrypt($value));
}

Rotating custom attribute casts

It is possible to rotate custom casts. Your CastsAttributes implementation should either have encrypted in its class name, or implement the Henzeb\Rotator\Contracts\CastsEncryptedAttributes interface.

use Henzeb\Rotator\Contracts\CastsEncryptedAttributes;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;


class YourCustomAttribute implements CastsAttributes, CastsEncryptedAttributes
{
    // your implementation
}

Note: This also works with CastsInboundAttributes implementations

rotating custom encrypted data

To rotate encrypted data that is not accessible by models, you can implement the rotateEncryptedData method on the \Henzeb\Rotator\Contracts\RotatesEncryptedData interface. When the default configuration is applied, these implementations are also automatically detected.

customize rotation on Models

The RotatesEncryptedData interface can also be applied on Models. These are per-record basis.

Events

For each processed Model, a ModelEncryptionRotated event is emitted. It contains the Model for easy access.

Note: For custom implementations, there are no events. You have to implement them yourself.

Queue

Everything is done utilizing the queue. By default it uses the default queue and default connection. You can change that by setting the ROTATOR_QUEUE and ROTATOR_QUEUE_CONNECTION, or the corresponding keys in the configuration file.

To avoid out of memory, the amount of jobs in the given queue is limited by the ROTATOR_JOB_LIMIT. Whenever the queue size is too big, the RotateModelsWithEncryptedData job will be placed back on the queue waiting for the jobs it had dispatched to finish. This is by default 100 jobs.

To avoid heavy load on your database, you can modify the ROTATOR_CHUNK_SIZE. By default, 50 records are pulled from your database and dispatched to the queue.

Cleaning up previous keys

if you rotate regularly, or when you are confident the keys are no longer needed, you can cleanup the keys simply by running the following command:

php artisan key:cleanup-previous-keys

Note: just like the other commands, key:cleanup-previous-keys accepts env to specify an environment and force to clean up without confirmation.

Testing this package

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email henzeberkheij@gmail.com instead of using the issue tracker.

Credits

License

The GNU AGPLv. Please see License File for more information.

henzeb/laravel-encrypted-data-rotator 适用场景与选型建议

henzeb/laravel-encrypted-data-rotator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 henzeb/laravel-encrypted-data-rotator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: AGPL-3.0-only
  • 更新时间: 2024-08-04