repair

package
v1.11.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

字段加密修复工具

概述

ecmdb repair 命令用于修复历史数据中的字段加密问题。当您在系统中添加了新的加密字段配置后,此工具可以帮助您将现有的未加密数据按照新的加密规则进行加密处理。

功能特性

  • 🔐 智能加密检测:自动检测哪些字段需要加密,避免重复加密已加密的数据
  • 📊 批量处理:支持大量数据的批量处理,提高处理效率
  • 🔍 默认干跑模式:默认以干跑模式运行,安全预览操作而不实际修改数据
  • 🚀 安全执行:需要明确指定 --execute 参数才会实际修改数据
  • 📈 进度提示:实时显示处理进度和统计信息
  • ⚠️ 错误处理:完善的错误处理机制,确保数据安全

使用方法

基本用法
# 默认干跑模式,安全预览操作(推荐)
go run main.go repair

# 显式指定干跑模式
go run main.go repair --dry-run

# 实际执行修复,会修改数据(谨慎使用)
go run main.go repair --execute
参数说明
参数 必需 默认值 说明
--dry-run ❌ true 干跑模式,不实际修改数据(默认开启)
--execute ❌ false 实际执行修复,会修改数据
使用建议
  1. 首次使用:建议先运行 go run main.go repair 查看预览结果
  2. 确认无误后:使用 go run main.go repair --execute 实际执行修复
  3. 安全第一:默认干跑模式确保不会意外修改数据

工作原理

  1. 获取模型信息:扫描系统中所有的数据模型
  2. 识别加密字段:查找标记为需要加密的字段配置
  3. 批量处理数据:按模型分批处理资源数据
  4. 智能加密判断:
    • 检查字段是否已经加密(通过尝试解密判断)
    • 只对未加密的字段进行加密处理
    • 保持已加密字段不变
  5. 更新数据:将加密后的数据更新到数据库

安全特性

  • 默认干跑模式:默认以干跑模式运行,防止意外修改数据
  • 幂等性:多次运行不会重复加密已加密的数据
  • 数据完整性:加密失败时保持原数据不变
  • 错误恢复:单个资源处理失败不影响其他资源的处理
  • 明确执行:需要明确指定 --execute 参数才会实际修改数据

使用场景

  • 新增敏感字段的加密配置后,需要加密历史数据
  • 修改加密密钥后,需要重新加密所有数据
  • 系统升级后,需要按照新的加密规则处理现有数据

注意事项

⚠️ 重要提醒:

  1. 默认安全:工具默认以干跑模式运行,不会修改数据
  2. 预览优先:建议先运行默认模式查看预览结果
  3. 明确执行:确认无误后使用 --execute 参数实际执行
  4. 备份数据:在执行 --execute 前,请务必备份数据库
  5. 测试环境:建议先在测试环境中验证修复效果
  6. 系统负载:大量数据处理可能对系统性能产生影响,建议在低峰期执行

输出示例

干跑模式(默认)
$ go run main.go repair
🔧 开始执行字段加密修复...
🔍 运行在干跑模式,不会实际修改数据
📋 正在获取所有模型...
🔐 正在获取加密字段配置...
📝 模型 host 有 3 个加密字段: [test mutis password]
📝 模型 AuthGateway 有 2 个加密字段: [private_key password]
🎯 共找到 2 个模型需要处理加密字段

🔄 正在处理模型: host
✅ 模型 host 处理完成: 处理 22 条,更新 0 条

🔄 正在处理模型: AuthGateway
✅ 模型 AuthGateway 处理完成: 处理 4 条,更新 0 条

🎉 修复完成! 总计处理 26 条资源,更新 0 条
✅ 字段加密修复完成,耗时: 85ms
实际执行模式
$ go run main.go repair --execute
🔧 开始执行字段加密修复...
📋 正在获取所有模型...
🔐 正在获取加密字段配置...
📝 模型 host 有 3 个加密字段: [test mutis password]
📝 模型 AuthGateway 有 2 个加密字段: [private_key password]
🎯 共找到 2 个模型需要处理加密字段

🔄 正在处理模型: host
✅ 模型 host 处理完成: 处理 22 条,更新 20 条

🔄 正在处理模型: AuthGateway
✅ 模型 AuthGateway 处理完成: 处理 4 条,更新 4 条

🎉 修复完成! 总计处理 26 条资源,更新 24 条
✅ 字段加密修复完成,耗时: 88ms

故障排除

常见问题
  1. 初始化服务失败

    • 检查数据库连接配置
    • 确认所有依赖服务正常运行
  2. 加密失败

    • 检查加密密钥是否正确
    • 确认字段数据类型是否支持加密
  3. 更新资源失败

    • 检查数据库权限
    • 确认资源数据格式正确
日志分析

工具会输出详细的处理日志,包括:

  • 处理进度信息
  • 错误详情
  • 统计信息

根据日志信息可以快速定位和解决问题。

技术实现

  • 批量处理:每批处理 100 条记录,平衡性能和内存使用
  • 智能检测:通过尝试解密判断字段是否已加密
  • 错误隔离:单个记录处理失败不影响整批处理
  • 进度跟踪:每处理 1000 条记录显示一次进度

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "repair",
	Short: "修复数据加密字段",
	Long:  "对历史数据进行字段加密处理,将未加密但需要加密的字段进行加密",
	RunE:  runRepair,
}

Functions

This section is empty.

Types

type FieldEncryptionRepairer

type FieldEncryptionRepairer struct {
	// contains filtered or unexported fields
}

FieldEncryptionRepairer 字段加密修复器

func NewFieldEncryptionRepairer

func NewFieldEncryptionRepairer(
	modelSvc model.Service,
	attrSvc attribute.Service,
	resourceSvc resource.EncryptedSvc,
	dryRun bool,
) *FieldEncryptionRepairer

NewFieldEncryptionRepairer 创建字段加密修复器

func (*FieldEncryptionRepairer) Repair

Repair 执行修复

type FieldProcessor

type FieldProcessor struct {
	// contains filtered or unexported fields
}

FieldProcessor 字段处理器

func NewFieldProcessor

func NewFieldProcessor(resourceSvc resource.EncryptedSvc, dryRun bool) *FieldProcessor

NewFieldProcessor 创建字段处理器

func (*FieldProcessor) ProcessResources

func (p *FieldProcessor) ProcessResources(
	ctx context.Context,

	modelUid string,
	secureFields []string,
) (*StatsRepair, error)

ProcessResources 处理资源

type StatsRepair

type StatsRepair struct {
	Processed int
	Updated   int
}

StatsRepair 修复统计信息

func (*StatsRepair) Add

func (s *StatsRepair) Add(other *StatsRepair)

Add 添加统计信息

func (*StatsRepair) PrintSummary

func (s *StatsRepair) PrintSummary()

PrintSummary 打印统计摘要

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL