MM-22785 audit server CLI (#14144)
Add auditing to server CLI. Also: - simplify auditing in API layer - reduce number of AddMeta calls - have models serialize themselves - more consistent field naming
Этот коммит содержится в:
46
app/audit.go
46
app/audit.go
@@ -5,6 +5,7 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/user"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/audit"
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
@@ -33,6 +34,51 @@ func (a *App) GetAuditsPage(userId string, page int, perPage int) (model.Audits,
|
||||
return a.Srv().Store.Audit().Get(userId, page*perPage, perPage)
|
||||
}
|
||||
|
||||
// LogAuditRec logs an audit record using default CLILevel.
|
||||
func (a *App) LogAuditRec(rec *audit.Record, err error) {
|
||||
a.LogAuditRecWithLevel(rec, CLILevel, err)
|
||||
}
|
||||
|
||||
// LogAuditRecWithLevel logs an audit record using specified Level.
|
||||
func (a *App) LogAuditRecWithLevel(rec *audit.Record, level audit.Level, err error) {
|
||||
if rec == nil {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
if appErr, ok := err.(*model.AppError); ok {
|
||||
rec.AddMeta("err", appErr.Error())
|
||||
rec.AddMeta("code", appErr.StatusCode)
|
||||
} else {
|
||||
rec.AddMeta("err", err)
|
||||
}
|
||||
rec.Fail()
|
||||
}
|
||||
a.Srv().Audit.LogRecord(level, *rec)
|
||||
}
|
||||
|
||||
// MakeAuditRecord creates a audit record pre-populated with defaults.
|
||||
func (a *App) MakeAuditRecord(event string, initialStatus string) *audit.Record {
|
||||
var userID string
|
||||
user, err := user.Current()
|
||||
if err == nil {
|
||||
userID = fmt.Sprintf("%s:%s", user.Uid, user.Username)
|
||||
}
|
||||
|
||||
rec := &audit.Record{
|
||||
APIPath: "",
|
||||
Event: event,
|
||||
Status: initialStatus,
|
||||
UserID: userID,
|
||||
SessionID: "",
|
||||
Client: fmt.Sprintf("server %s-%s", model.BuildNumber, model.BuildHash),
|
||||
IPAddress: "",
|
||||
Meta: audit.Meta{audit.KeyClusterID: a.GetClusterId()},
|
||||
}
|
||||
rec.AddMetaTypeConverter(model.AuditModelTypeConv)
|
||||
|
||||
return rec
|
||||
}
|
||||
|
||||
func (s *Server) configureAudit(adt *audit.Audit) {
|
||||
adt.OnQueueFull = s.onAuditTargetQueueFull
|
||||
adt.OnError = s.onAuditError
|
||||
|
||||
Ссылка в новой задаче
Block a user