sync: 同步 v1.0.4 开源代码并脱敏
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package batchfields
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
ItemIDLimit = 64
|
||||
DetailReasonLimit = 512
|
||||
FailureReasonByteLimit = 4096
|
||||
)
|
||||
|
||||
var mysqlErrorAfterSQL = regexp.MustCompile(`(?i): Error [0-9]{3,5} \([0-9A-Z]{5}\): [^\r\n]{0,160}`)
|
||||
|
||||
func DetailReason(value string) string {
|
||||
runes := []rune(strings.TrimSpace(value))
|
||||
if len(runes) > DetailReasonLimit {
|
||||
return string(runes[:DetailReasonLimit])
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func ItemID(value string) string {
|
||||
if utf8.RuneCountInString(value) > ItemIDLimit {
|
||||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func FailureReason(value string) string {
|
||||
value = strings.ToValidUTF8(strings.TrimSpace(value), "")
|
||||
upper := strings.ToUpper(value)
|
||||
for _, marker := range []string{": INSERT INTO ", ": UPDATE ", ": SELECT ", ": DELETE FROM "} {
|
||||
if index := strings.Index(upper, marker); index >= 0 {
|
||||
databaseError := mysqlErrorAfterSQL.FindString(value[index+len(marker):])
|
||||
value = value[:index] + " (SQL omitted)" + databaseError
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(value) <= FailureReasonByteLimit {
|
||||
return value
|
||||
}
|
||||
end := FailureReasonByteLimit - 3
|
||||
for end > 0 && !utf8.RuneStart(value[end]) {
|
||||
end--
|
||||
}
|
||||
return value[:end] + "..."
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package batchfields
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestItemIDRejectsOversizedCardIdentifier(t *testing.T) {
|
||||
if got := ItemID(strings.Repeat("9", 65)); got != "" {
|
||||
t.Fatalf("oversized identifier = %q, want empty", got)
|
||||
}
|
||||
if got := ItemID(strings.Repeat("9", 64)); len(got) != 64 {
|
||||
t.Fatalf("64-character identifier = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetailReasonFitsColumn(t *testing.T) {
|
||||
if got := DetailReason(strings.Repeat("错", 513)); utf8.RuneCountInString(got) != 512 {
|
||||
t.Fatalf("detail reason length = %d", utf8.RuneCountInString(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailureReasonOmitsStatementAndFitsColumn(t *testing.T) {
|
||||
value := "insert failed: INSERT INTO batch_detail VALUES (" + strings.Repeat("错", 30000) + ")"
|
||||
got := FailureReason(value)
|
||||
if strings.Contains(got, "VALUES") || len(got) > FailureReasonByteLimit || !utf8.ValidString(got) {
|
||||
t.Fatalf("unsafe failure reason: %q", got)
|
||||
}
|
||||
got = FailureReason(strings.Repeat("错", 30000))
|
||||
if len(got) > FailureReasonByteLimit || !utf8.ValidString(got) {
|
||||
t.Fatalf("invalid truncated UTF-8 reason")
|
||||
}
|
||||
got = FailureReason("insert detail failed: INSERT INTO batch_detail VALUES ('" + strings.Repeat("9", 1000) + "'): Error 1406 (22001): Data too long for column 'item_id' at row 1")
|
||||
if strings.Contains(got, "VALUES") || !strings.Contains(got, "Error 1406 (22001)") {
|
||||
t.Fatalf("SQL should be removed but database error preserved: %q", got)
|
||||
}
|
||||
}
|
||||
@@ -241,24 +241,29 @@ func publishWithConfirm(ctx context.Context, ch *amqp.Channel, queue string, mes
|
||||
|
||||
// Consume 启动消费者,使用手动 Ack,handler 成功才确认消息。
|
||||
func Consume(ctx context.Context, queue, consumer string, handler Handler) error {
|
||||
_, err := ConsumeWithDone(ctx, queue, consumer, handler)
|
||||
return err
|
||||
}
|
||||
|
||||
func ConsumeWithDone(ctx context.Context, queue, consumer string, handler Handler) (<-chan struct{}, error) {
|
||||
ch, err := Channel(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err = declareQueue(ch, queue); err != nil {
|
||||
_ = ch.Close()
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err = ch.Qos(1, 0, false); err != nil {
|
||||
_ = ch.Close()
|
||||
return gerror.Wrap(err, "rabbitmq set qos failed")
|
||||
return nil, gerror.Wrap(err, "rabbitmq set qos failed")
|
||||
}
|
||||
|
||||
// autoAck=false,业务处理成功后手动 Ack,处理失败时 Nack 并重新入队。
|
||||
deliveries, err := ch.Consume(queue, consumer, false, false, false, false, nil)
|
||||
if err != nil {
|
||||
_ = ch.Close()
|
||||
return gerror.Wrap(err, "rabbitmq consume failed")
|
||||
return nil, gerror.Wrap(err, "rabbitmq consume failed")
|
||||
}
|
||||
|
||||
consumerState := &managedConsumer{
|
||||
@@ -269,6 +274,8 @@ func Consume(ctx context.Context, queue, consumer string, handler Handler) error
|
||||
registerConsumer(consumerState)
|
||||
go func() {
|
||||
defer close(consumerState.done)
|
||||
defer ch.Close()
|
||||
defer unregisterConsumer(consumerState)
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
@@ -302,7 +309,7 @@ func Consume(ctx context.Context, queue, consumer string, handler Handler) error
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return consumerState.done, nil
|
||||
}
|
||||
|
||||
// ConsumeJSON 启动 JSON 消费者,统一完成反序列化和 Ack/Nack 处理。
|
||||
@@ -423,3 +430,16 @@ func cloneHeaders(headers amqp.Table) map[string]any {
|
||||
func isClosedError(err error) bool {
|
||||
return err == nil || errors.Is(err, amqp.ErrClosed)
|
||||
}
|
||||
|
||||
func unregisterConsumer(target *managedConsumer) {
|
||||
consumerMu.Lock()
|
||||
defer consumerMu.Unlock()
|
||||
for index, consumer := range consumers {
|
||||
if consumer == target {
|
||||
remaining := make([]*managedConsumer, 0, len(consumers)-1)
|
||||
remaining = append(remaining, consumers[:index]...)
|
||||
consumers = append(remaining, consumers[index+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,3 +149,22 @@ func resetRabbitMQState(t *testing.T) {
|
||||
connMu.Unlock()
|
||||
})
|
||||
}
|
||||
|
||||
func TestUnregisterConsumerPreservesShutdownSnapshot(t *testing.T) {
|
||||
consumerMu.Lock()
|
||||
original := consumers
|
||||
first, second, third := &managedConsumer{}, &managedConsumer{}, &managedConsumer{}
|
||||
consumers = []*managedConsumer{first, second, third}
|
||||
snapshot := consumers
|
||||
consumerMu.Unlock()
|
||||
defer func() { consumerMu.Lock(); consumers = original; consumerMu.Unlock() }()
|
||||
unregisterConsumer(second)
|
||||
consumerMu.Lock()
|
||||
defer consumerMu.Unlock()
|
||||
if len(consumers) != 2 || consumers[0] != first || consumers[1] != third {
|
||||
t.Fatal("consumer not removed")
|
||||
}
|
||||
if snapshot[1] != second || snapshot[2] != third {
|
||||
t.Fatal("shutdown snapshot mutated")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package rolepolicy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const TemplateConfigKey = "agent.role.template.role_id"
|
||||
|
||||
func Template(ctx context.Context) (*entity.SysRole, error) {
|
||||
var configs []*entity.SysConfig
|
||||
if err := dao.SysConfig.Ctx(ctx).Master().Where("tenant_id", consts.DefaultSystemTenantCode).Where("config_key", TemplateConfigKey).Scan(&configs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(configs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if len(configs) != 1 {
|
||||
return nil, gerror.New("代理模板角色系统参数重复")
|
||||
}
|
||||
return ValidateTemplate(ctx, configs[0].ConfigValue)
|
||||
}
|
||||
|
||||
func ValidateTemplate(ctx context.Context, value string) (*entity.SysRole, error) {
|
||||
roleID, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
|
||||
if err != nil || roleID <= 0 || roleID == consts.SuperAdminRoleId {
|
||||
return nil, gerror.New("代理模板参数必须填写非超级管理员的有效角色ID")
|
||||
}
|
||||
var role *entity.SysRole
|
||||
if err = dao.SysRole.Ctx(ctx).Master().Where("role_id", roleID).Where("tenant_id", consts.DefaultSystemTenantCode).Scan(&role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if role == nil || role.Status != "0" || (role.CreatedDept > 0 && role.CreatedDept != role.DeptId) || role.DeptId <= 0 {
|
||||
return nil, gerror.New("代理模板参数对应角色须正常启用、绑定本级机构且非上级继承角色")
|
||||
}
|
||||
if role.DataScope != consts.SysRoleDataScopeDeptAndBelow {
|
||||
return nil, gerror.New("代理模板数据范围必须为本机构及下级")
|
||||
}
|
||||
var dept *entity.SysDept
|
||||
if err := dao.SysDept.Ctx(ctx).Master().Where("tenant_id", role.TenantId).Where("dept_id", role.DeptId).Where("status", "0").Scan(&dept); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dept == nil {
|
||||
return nil, gerror.New("代理模板所属机构不存在或已停用")
|
||||
}
|
||||
invalidScopes, err := dao.SysRoleMenu.Ctx(ctx).Master().Where("tenant_id", role.TenantId).Where("role_id", role.RoleId).WhereIn("data_scope", []string{consts.SysRoleDataScopeAll, consts.SysRoleDataScopeCustom}).Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if invalidScopes > 0 {
|
||||
return nil, gerror.New("代理模板按钮不允许全部或自定义数据范围")
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
func MenuIDs(ctx context.Context, roleID int64) ([]int64, error) {
|
||||
var menus []*entity.SysRoleMenu
|
||||
if err := dao.SysRoleMenu.Ctx(ctx).Master().Where("role_id", roleID).Scan(&menus); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := make([]int64, 0, len(menus))
|
||||
for _, menu := range menus {
|
||||
ids = append(ids, menu.MenuId)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func Intersect(left, right []int64) []int64 {
|
||||
allowed := make(map[int64]bool, len(right))
|
||||
for _, id := range right {
|
||||
allowed[id] = true
|
||||
}
|
||||
result := make([]int64, 0, len(left))
|
||||
for _, id := range left {
|
||||
if allowed[id] {
|
||||
result = append(result, id)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func CurrentSuperAdmin(ctx context.Context, userID int64) (bool, error) {
|
||||
if userID <= 0 {
|
||||
return false, nil
|
||||
}
|
||||
var relations []*entity.SysUserRole
|
||||
if err := dao.SysUserRole.Ctx(ctx).Master().Where("tenant_id", consts.DefaultSystemTenantCode).Where("user_id", userID).Where("role_id", consts.SuperAdminRoleId).Scan(&relations); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(relations) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
var role *entity.SysRole
|
||||
if err := dao.SysRole.Ctx(ctx).Master().Where("tenant_id", consts.DefaultSystemTenantCode).Where("role_id", consts.SuperAdminRoleId).Where("status", "0").Scan(&role); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return role != nil, nil
|
||||
}
|
||||
|
||||
// RestrictMenus validates organization membership and role bindings. Templates are
|
||||
// overwrite sources, not runtime permission ceilings: platform role edits must
|
||||
// remain effective until the next explicit template distribution.
|
||||
func RestrictMenus(ctx context.Context, tenantID string, deptID int64, ids []int64) ([]int64, error) {
|
||||
template, err := Template(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if template == nil {
|
||||
return ids, nil
|
||||
}
|
||||
seen := make(map[int64]bool)
|
||||
for deptID > 0 {
|
||||
if seen[deptID] {
|
||||
return nil, gerror.New("机构上下级存在循环")
|
||||
}
|
||||
seen[deptID] = true
|
||||
var dept *entity.SysDept
|
||||
if err := dao.SysDept.Ctx(ctx).Master().Where("tenant_id", tenantID).Where("dept_id", deptID).Scan(&dept); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dept == nil || dept.Status != "0" {
|
||||
return []int64{}, nil
|
||||
}
|
||||
if tenantID == template.TenantId && deptID == template.DeptId {
|
||||
break
|
||||
}
|
||||
if dept.DeptRole == consts.SuperAdminRoleId {
|
||||
return nil, gerror.Newf("机构%d不能使用超级管理员角色作为代理权限上限,请核对机构角色绑定", dept.DeptId)
|
||||
}
|
||||
if dept.DeptRole <= 0 {
|
||||
return nil, gerror.Newf("机构%d未关联默认角色,请联系平台管理员核对机构角色绑定", dept.DeptId)
|
||||
}
|
||||
var role *entity.SysRole
|
||||
if err := dao.SysRole.Ctx(ctx).Master().Where("tenant_id", tenantID).Where("dept_id", deptID).Where("role_id", dept.DeptRole).Where("status", "0").Scan(&role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if role == nil {
|
||||
return nil, gerror.Newf("机构%d默认角色不存在、已停用或归属不匹配,请联系平台管理员核对", dept.DeptId)
|
||||
}
|
||||
deptID = dept.ParentId
|
||||
}
|
||||
if tenantID == template.TenantId && !seen[template.DeptId] {
|
||||
return []int64{}, nil
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package rolesync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/rabbitmq"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
||||
)
|
||||
|
||||
var publishRoleSync = func(ctx context.Context, message *model.AgentRoleSyncMessage) error {
|
||||
return rabbitmq.Publish(ctx, consts.QueueAgentRoleSync, message)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package tdengine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcfg"
|
||||
)
|
||||
|
||||
func TestGetConfigDatabaseRouting(t *testing.T) {
|
||||
previous := g.Cfg().GetAdapter()
|
||||
t.Cleanup(func() { g.Cfg().SetAdapter(previous) })
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
fields string
|
||||
fallback string
|
||||
daily string
|
||||
monthly string
|
||||
}{
|
||||
{"defaults", "", "iotlcm_daily", "iotlcm_daily", "iotlcm_daily"},
|
||||
{"legacy", " dbName: legacy\n", "legacy", "legacy", "legacy"},
|
||||
{"split", " dbName: legacy\n dailyDbName: iotlcm_daily\n monthlyDbName: iotlcm_monthly\n", "legacy", "iotlcm_daily", "iotlcm_monthly"},
|
||||
{"daily_only", " dbName: legacy\n dailyDbName: iotlcm_daily\n", "legacy", "iotlcm_daily", "legacy"},
|
||||
{"monthly_only", " dbName: legacy\n monthlyDbName: iotlcm_monthly\n", "legacy", "legacy", "iotlcm_monthly"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
adapter, err := gcfg.NewAdapterContent("tsdb:\n database: TdEngine\n tdengine:\n batchSize: 20\n" + test.fields)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.Cfg().SetAdapter(adapter)
|
||||
config := GetConfig(context.Background())
|
||||
if config.DbName != test.fallback {
|
||||
t.Fatalf("fallback=%q want=%q", config.DbName, test.fallback)
|
||||
}
|
||||
for _, stable := range []string{config.DailyStable, config.PoolDailyStable, config.PoolCardCountDailyStable} {
|
||||
if got := config.DbNameFor(stable); got != test.daily {
|
||||
t.Errorf("%s database=%q want=%q", stable, got, test.daily)
|
||||
}
|
||||
}
|
||||
for _, stable := range []string{config.MonthlyStable, config.PoolMonthlyStable, config.PoolCardCountMonthlyStable} {
|
||||
if got := config.DbNameFor(stable); got != test.monthly {
|
||||
t.Errorf("%s database=%q want=%q", stable, got, test.monthly)
|
||||
}
|
||||
}
|
||||
if got := config.DbNameFor("other_stable"); got != test.fallback {
|
||||
t.Errorf("unknown stable database=%q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabaseRoutingEmptySplitNames(t *testing.T) {
|
||||
config := Config{DbName: "legacy", DailyStable: "daily", MonthlyStable: "monthly"}
|
||||
for _, stable := range []string{"daily", "monthly", "other"} {
|
||||
if got := config.DbNameFor(stable); got != "legacy" {
|
||||
t.Errorf("%s database=%q", stable, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user