mountservicelogic.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // udeServer: 启用服务(状态开关拨向"启用") —— 逐保护目录调外挂程序 /api/fuseMount 挂载。
  2. // 成功后 mount_status=mounted、status=启用;任一目录挂载失败则 mount_status=failed,
  3. // 失败信息聚合进 mountWarning 由前端弹黄色警告(软处理,不阻塞状态变更)。
  4. package logic
  5. import (
  6. "bytes"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "path"
  13. "strings"
  14. "udeServer/internal/audit"
  15. "udeServer/internal/svc"
  16. "udeServer/internal/types"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. type MountServiceLogic struct {
  20. logx.Logger
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. }
  24. func NewMountServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MountServiceLogic {
  25. return &MountServiceLogic{
  26. Logger: logx.WithContext(ctx),
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. }
  30. }
  31. // MountService 请求体与手动补刷同款:路径参数 id(types.ServiceIdReq)。
  32. // 挂载是幂等的:外挂注册表里"已挂且同密钥"的目录直接跳过,重复点启用零成本。
  33. func (l *MountServiceLogic) MountService(req *types.ServiceIdReq) (*types.ServiceResp, error) {
  34. if err := checkId(req.Id); err != nil {
  35. return nil, err
  36. }
  37. svc, err := findService(l.ctx, l.svcCtx.ServiceModel, req.Id)
  38. if err != nil {
  39. return nil, err
  40. }
  41. ip, port := svc.Ip.String, svc.Port // port 已是 int64
  42. if ip == "" || port == 0 {
  43. return nil, fmt.Errorf("外挂服务 IP/端口为空,无法下发挂载")
  44. }
  45. // 密钥内容:按服务配置的密钥标识(keyIden)查 t_key,与创建流程 notifyFuseMount 同源
  46. keyIden := svc.KeyIden.String
  47. if keyIden == "" {
  48. return nil, fmt.Errorf("服务未配置密钥标识(keyIden),无法下发挂载")
  49. }
  50. key, err := l.svcCtx.KeyModel.FindOneByIden(l.ctx, keyIden)
  51. if err != nil {
  52. return nil, fmt.Errorf("查询密钥(%s)失败: %w", keyIden, err)
  53. }
  54. ikey := key.Content.String
  55. if ikey == "" {
  56. return nil, fmt.Errorf("密钥(%s)内容为空,无法下发挂载", keyIden)
  57. }
  58. dirs, err := l.svcCtx.DirectoryModel.FindDirsByServiceId(l.ctx, req.Id)
  59. if err != nil {
  60. return nil, fmt.Errorf("查询保护目录失败: %w", err)
  61. }
  62. // 逐目录挂载:audit.DoSimple 记 type=2 审计日志,
  63. // doFuseRequest 注入 X-TOKEN=iden(外挂侧与环境变量 TOKEN 恒定时间比对)
  64. var msgs []string
  65. for _, mount := range dirs {
  66. // path.Clean(非 filepath!):发给远程 Linux 外挂必须保持 / 分隔符
  67. dirLinuxPath := path.Clean(mount)
  68. payload := map[string]interface{}{
  69. "backing": dirLinuxPath + "-cipher", // 密文目录 = 保护目录 + "-cipher" 后缀
  70. "mount": dirLinuxPath, // 挂载点 = 保护目录本身
  71. "ikey": ikey,
  72. }
  73. body, err := json.Marshal(payload)
  74. if err != nil {
  75. msgs = append(msgs, fmt.Sprintf("序列化挂载请求失败: %v", err))
  76. continue
  77. }
  78. baseURL := fmt.Sprintf("http://%s:%d/api/fuseMount", ip, port) // %d:port 是 int64
  79. if _, callErr := audit.DoSimple(l.svcCtx.SpanRecorder, l.ctx, "FuseMount", baseURL, body,
  80. func() (map[string]interface{}, int64, error) {
  81. resp, rerr := doFuseRequest(fuseMountClient, svc.Iden.String, http.MethodPost, baseURL, bytes.NewReader(body), "application/json")
  82. if rerr != nil {
  83. return nil, 0, fmt.Errorf("挂载目录 %s 失败(外挂程序未启动?): %w", dirLinuxPath, rerr)
  84. }
  85. raw, _ := io.ReadAll(resp.Body) // 读到 EOF 保证 TCP 连接复用 Keep-Alive
  86. resp.Body.Close()
  87. status := int64(resp.StatusCode)
  88. if status >= http.StatusBadRequest {
  89. return nil, status, fmt.Errorf("挂载目录 %s 失败,外挂程序返回 HTTP %d: %s", dirLinuxPath, status, string(raw))
  90. }
  91. return map[string]interface{}{"status": status, "body": string(raw)}, status, nil
  92. }); callErr != nil {
  93. msgs = append(msgs, callErr.Error())
  94. continue
  95. }
  96. l.Infof("fuse mount ok: %s -> %s", dirLinuxPath, dirLinuxPath+"-cipher")
  97. }
  98. // 挂载结果落库:全成功=mounted;任一失败=failed;没配保护目录=unmounted(无挂载可言)。
  99. // 启用状态与挂载结果挂钩:挂载成功才启用;任一失败(整体 failed)则保持停用,
  100. // 前端按响应里的 mountStatus/status 把开关弹回"停用"位。落库失败只记日志,不影响主流程。
  101. mountStatus := "mounted"
  102. if len(dirs) == 0 {
  103. mountStatus = "unmounted"
  104. }
  105. if len(msgs) > 0 {
  106. mountStatus = "failed"
  107. }
  108. finalStatus := "启用"
  109. if mountStatus == "failed" {
  110. finalStatus = "停用"
  111. }
  112. if uerr := l.svcCtx.ServiceModel.UpdateMountStatus(l.ctx, req.Id, mountStatus); uerr != nil {
  113. l.Errorf("update mount_status=%s failed(id=%d): %v", mountStatus, req.Id, uerr)
  114. }
  115. if uerr := l.svcCtx.ServiceModel.UpdateStatus(l.ctx, req.Id, finalStatus); uerr != nil {
  116. l.Errorf("update status=%s failed(id=%d): %v", finalStatus, req.Id, uerr)
  117. }
  118. updated, err := findService(l.ctx, l.svcCtx.ServiceModel, req.Id)
  119. if err != nil {
  120. return nil, err
  121. }
  122. resp := toServiceResp(updated)
  123. if len(msgs) > 0 {
  124. resp.MountWarning = strings.Join(msgs, "; ")
  125. }
  126. return resp, nil
  127. }