MM-51321 Refactor audit log application. (#22481)

* Refactor audit log application.

* Fix incorrect API Usage for Auditing

* Fixups.

* Rename Object audit to Auditable. Some small fixes.

---------

Co-authored-by: Daniel Schalla <daniel@schalla.me>
Этот коммит содержится в:
Christopher Speller
2023-03-16 09:50:00 -07:00
коммит произвёл GitHub
родитель 9a38a52392
Коммит 7804debb7f
49 изменённых файлов: 554 добавлений и 308 удалений

Просмотреть файл

@@ -37,7 +37,7 @@ func createCommand(c *Context, w http.ResponseWriter, r *http.Request) {
}
auditRec := c.MakeAuditRecord("createCommand", audit.Fail)
auditRec.AddEventParameter("command", cmd)
audit.AddEventParameterAuditable(auditRec, "command", &cmd)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
@@ -56,7 +56,6 @@ func createCommand(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec.Success()
c.LogAudit("success")
auditRec.AddMeta("command", rcmd)
auditRec.AddEventResultState(rcmd)
auditRec.AddEventObjectType("command")
@@ -79,17 +78,17 @@ func updateCommand(c *Context, w http.ResponseWriter, r *http.Request) {
}
auditRec := c.MakeAuditRecord("updateCommand", audit.Fail)
auditRec.AddEventParameter("command", cmd)
audit.AddEventParameterAuditable(auditRec, "command", &cmd)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
oldCmd, err := c.App.GetCommand(c.Params.CommandId)
if err != nil {
auditRec.AddMeta("command_id", c.Params.CommandId)
audit.AddEventParameter(auditRec, "command_id", c.Params.CommandId)
c.SetCommandNotFoundError()
return
}
auditRec.AddMeta("command", oldCmd)
auditRec.AddEventPriorState(oldCmd)
if cmd.TeamId != oldCmd.TeamId {
c.Err = model.NewAppError("updateCommand", "api.command.team_mismatch.app_error", nil, "user_id="+c.AppContext.Session().UserId, http.StatusBadRequest)
@@ -139,7 +138,7 @@ func moveCommand(c *Context, w http.ResponseWriter, r *http.Request) {
}
auditRec := c.MakeAuditRecord("moveCommand", audit.Fail)
auditRec.AddEventParameter("command_move_request", cmr)
audit.AddEventParameter(auditRec, "command_move_request", cmr.TeamId)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
@@ -148,7 +147,7 @@ func moveCommand(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err = appErr
return
}
auditRec.AddMeta("team", newTeam)
audit.AddEventParameterAuditable(auditRec, "team", newTeam)
if !c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), newTeam.Id, model.PermissionManageSlashCommands) {
c.LogAudit("fail - inappropriate permissions")
@@ -161,7 +160,7 @@ func moveCommand(c *Context, w http.ResponseWriter, r *http.Request) {
c.SetCommandNotFoundError()
return
}
auditRec.AddMeta("command", cmd)
auditRec.AddEventPriorState(cmd)
if !c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), cmd.TeamId, model.PermissionManageSlashCommands) {
c.LogAudit("fail - inappropriate permissions")
@@ -191,7 +190,7 @@ func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) {
}
auditRec := c.MakeAuditRecord("deleteCommand", audit.Fail)
auditRec.AddEventParameter("command_id", c.Params.CommandId)
audit.AddEventParameter(auditRec, "command_id", c.Params.CommandId)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
@@ -200,7 +199,7 @@ func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) {
c.SetCommandNotFoundError()
return
}
auditRec.AddMeta("command", cmd)
auditRec.AddEventPriorState(cmd)
if !c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), cmd.TeamId, model.PermissionManageSlashCommands) {
c.LogAudit("fail - inappropriate permissions")
@@ -222,7 +221,6 @@ func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
auditRec.AddEventResultState(cmd)
auditRec.AddEventObjectType("command")
auditRec.Success()
c.LogAudit("success")
@@ -323,8 +321,7 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec := c.MakeAuditRecord("executeCommand", audit.Fail)
defer c.LogAuditRec(auditRec)
auditRec.AddEventParameter("command_args", commandArgs)
auditRec.AddMeta("commandargs", commandArgs)
audit.AddEventParameterAuditable(auditRec, "command_args", &commandArgs)
// checks that user is a member of the specified channel, and that they have permission to use slash commands in it
if !c.App.SessionHasPermissionToChannel(c.AppContext, *c.AppContext.Session(), commandArgs.ChannelId, model.PermissionUseSlashCommands) {
@@ -358,8 +355,6 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
commandArgs.SiteURL = c.GetSiteURLHeader()
commandArgs.Session = *c.AppContext.Session()
auditRec.AddMeta("commandargs", commandArgs) // overwrite in case teamid changed. TODO do we need to log this too? is the original commandArgs not enough
response, err := c.App.ExecuteCommand(c.AppContext, &commandArgs)
if err != nil {
c.Err = err
@@ -456,12 +451,12 @@ func regenCommandToken(c *Context, w http.ResponseWriter, r *http.Request) {
cmd, err := c.App.GetCommand(c.Params.CommandId)
if err != nil {
auditRec.AddMeta("command_id", c.Params.CommandId)
audit.AddEventParameter(auditRec, "command_id", c.Params.CommandId)
c.SetCommandNotFoundError()
return
}
auditRec.AddMeta("command", cmd)
auditRec.AddEventParameter("command_id", c.Params.CommandId)
auditRec.AddEventPriorState(cmd)
audit.AddEventParameter(auditRec, "command_id", c.Params.CommandId)
if !c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), cmd.TeamId, model.PermissionManageSlashCommands) {
c.LogAudit("fail - inappropriate permissions")
@@ -482,7 +477,7 @@ func regenCommandToken(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err = err
return
}
auditRec.AddEventResultState(rcmd)
auditRec.Success()
c.LogAudit("success")