优化结构

This commit is contained in:
Yvan
2026-08-28 14:54:06 +08:00
parent 56f9440a6b
commit ae5345de14
15 changed files with 188 additions and 93 deletions
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
integrationbiz "kra/internal/biz/integration"
"strings"
"kra/internal/service/dto"
)
@@ -38,13 +39,13 @@ func (s *IntegrationConfigService) List(ctx context.Context, kind string) ([]*dt
enabled = config.Enabled
delete(byProvider, definition.Provider)
}
result = append(result, &dto.IntegrationConfigResponse{Kind: definition.Kind, Provider: definition.Provider, Name: definition.Name, Description: definition.Description, Enabled: enabled, Configured: configured, Config: values, Fields: integrationFieldsDTO(definition.Fields)})
result = append(result, &dto.IntegrationConfigResponse{Kind: definition.Kind, Provider: definition.Provider, Name: definition.Name, Description: definition.Description, Enabled: enabled, Configured: configured, Config: redactIntegrationJSON(values), Fields: integrationFieldsDTO(definition.Fields)})
}
for _, config := range configs {
if _, exists := byProvider[config.Provider]; !exists {
continue
}
result = append(result, &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: config.Values})
result = append(result, &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: redactIntegrationJSON(config.Values)})
}
return result, nil
}
@@ -55,16 +56,44 @@ func (s *IntegrationConfigService) Find(ctx context.Context, kind, provider stri
return nil, err
}
definition, found := integrationbiz.IntegrationDefinition(config.Kind, config.Provider)
response := &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: config.Values}
response := &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: redactIntegrationJSON(config.Values)}
if found {
response.Name = definition.Name
response.Description = definition.Description
response.Fields = integrationFieldsDTO(definition.Fields)
response.Config = mergeConfigJSON(definition.Defaults, config.Values)
response.Config = redactIntegrationJSON(mergeConfigJSON(definition.Defaults, config.Values))
}
return response, nil
}
func redactIntegrationJSON(raw json.RawMessage) json.RawMessage {
var value any
if json.Unmarshal(raw, &value) != nil {
return raw
}
var walk func(any)
walk = func(v any) {
switch x := v.(type) {
case map[string]any:
for k, item := range x {
n := strings.ToLower(strings.ReplaceAll(k, "-", "_"))
if strings.Contains(n, "secret") || strings.Contains(n, "password") || strings.Contains(n, "private") || strings.Contains(n, "credential") || strings.Contains(n, "token") || strings.HasSuffix(n, "_key") || strings.Contains(n, "cert") || strings.Contains(n, "p12") {
x[k] = "******"
} else {
walk(item)
}
}
case []any:
for _, item := range x {
walk(item)
}
}
}
walk(value)
out, _ := json.Marshal(value)
return out
}
func (s *IntegrationConfigService) Save(ctx context.Context, kind, provider string, req *dto.IntegrationConfigRequest) error {
if req == nil {
return s.uc.Save(ctx, nil)