改为异步下发配置
This commit is contained in:
1 parent
8fc4ee2839
commit
0ec6cee36c
4 files changed
+30
-31
No files matched your search
@@ -49,20 +49,13 @@ func (h *SSHLoginHandler) GetConfig(c echo.Context) error {
|
||||
// POST /api/agents/:id/ssh-login/config
|
||||
func (h *SSHLoginHandler) UpdateConfig(c echo.Context) error {
|
||||
agentID := c.Param("id")
|
||||
if agentID == "" {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "探针ID不能为空",
|
||||
})
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "请求参数错误",
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
err := h.service.UpdateConfig(c.Request().Context(), agentID, req.Enabled)
|
||||
|
||||
@@ -33,9 +33,7 @@ func (h *TamperHandler) UpdateTamperConfig(c echo.Context) error {
|
||||
}
|
||||
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]interface{}{
|
||||
"message": "请求参数错误",
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
err := h.tamperService.UpdateConfig(c.Request().Context(), agentID, req.Enabled, req.Paths)
|
||||
|
||||
@@ -56,7 +56,8 @@ func (s *SSHLoginService) GetConfig(ctx context.Context, agentID string) (*model
|
||||
func (s *SSHLoginService) UpdateConfig(ctx context.Context, agentID string, enabled bool) error {
|
||||
// 保存配置到数据库
|
||||
config := models.SSHLoginConfigData{
|
||||
Enabled: enabled,
|
||||
Enabled: enabled,
|
||||
ApplyStatus: "pending",
|
||||
}
|
||||
|
||||
var agentForUpdate = models.Agent{
|
||||
@@ -68,7 +69,12 @@ func (s *SSHLoginService) UpdateConfig(ctx context.Context, agentID string, enab
|
||||
}
|
||||
|
||||
// 下发配置到 Agent
|
||||
return s.sendConfigToAgent(agentID, enabled)
|
||||
go func() {
|
||||
if err := s.sendConfigToAgent(agentID, enabled); err != nil {
|
||||
s.logger.Error("下发SSH登录监控配置到 Agent 失败", zap.String("agentId", agentID), zap.Error(err))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendConfigToAgent 下发配置到 Agent
|
||||
|
||||
@@ -89,8 +89,9 @@ func (s *TamperService) UpdateConfig(ctx context.Context, agentID string, enable
|
||||
|
||||
// 创建或更新配置
|
||||
newConfig := &models.TamperProtectConfigData{
|
||||
Enabled: enabled,
|
||||
Paths: paths,
|
||||
Enabled: enabled,
|
||||
Paths: paths,
|
||||
ApplyStatus: "pending",
|
||||
}
|
||||
|
||||
// 保存配置到数据库
|
||||
@@ -98,22 +99,23 @@ func (s *TamperService) UpdateConfig(ctx context.Context, agentID string, enable
|
||||
return err
|
||||
}
|
||||
|
||||
// 下发增量配置到探针
|
||||
if err := s.sendIncrementalConfigToAgent(agentID, added, removed); err != nil {
|
||||
s.logger.Warn("下发防篡改配置到探针失败",
|
||||
zap.String("agentId", agentID),
|
||||
zap.Strings("added", added),
|
||||
zap.Strings("removed", removed),
|
||||
zap.Error(err))
|
||||
// 不影响配置保存结果,只记录警告
|
||||
} else if len(added) > 0 || len(removed) > 0 {
|
||||
s.logger.Info("成功下发防篡改配置到探针",
|
||||
zap.String("agentId", agentID),
|
||||
zap.Strings("added", added),
|
||||
zap.Strings("removed", removed),
|
||||
zap.Int("totalPaths", len(paths)))
|
||||
}
|
||||
|
||||
go func() {
|
||||
// 下发增量配置到探针
|
||||
if err := s.sendIncrementalConfigToAgent(agentID, added, removed); err != nil {
|
||||
s.logger.Warn("下发防篡改配置到探针失败",
|
||||
zap.String("agentId", agentID),
|
||||
zap.Strings("added", added),
|
||||
zap.Strings("removed", removed),
|
||||
zap.Error(err))
|
||||
// 不影响配置保存结果,只记录警告
|
||||
} else if len(added) > 0 || len(removed) > 0 {
|
||||
s.logger.Info("成功下发防篡改配置到探针",
|
||||
zap.String("agentId", agentID),
|
||||
zap.Strings("added", added),
|
||||
zap.Strings("removed", removed),
|
||||
zap.Int("totalPaths", len(paths)))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user