mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Audit logging - new schema added, old schema removed. * fix linter error by running goimports * Address review comments * Address review comments * Example usage of new audit logging API for the updateUserAuth call * fixed unit test on auditing updating user record * Changed the `TestUpdateConfigDiffInAuditRecord` testcase---it failed, because this PR changes how the `meta` field is serialized into the audit log records. * fix linter error * use string constants for record keys * new audit api calls for api4/bot * `Auditable` interface implementations for model classes * New audit calls for channel api * New audit calls for channel_local * renamed receivers for required style reasons * New audit calls for api4/command * renamed receiver * New audit calls for api4/command_local * renamed receiver * fix unit test to reflect changes in the Auditable implementation of the user class * new audit calls for compliance * new audit calls for configs * remove auditRec.addMeta from updateConfig and patchConfig * new audit calls for config_local * new audit calls * new audit calls for ldap, license apis * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * fix linter error * fixed linter error * fixed "user update" test * Don't include all of config when audit logging config changes. Also fix unit test on TestUpdateConfigDiffInAuditRecord * address review comments * Added Auditable() method for UserPatch * Fix duplicative method declaration from merge * Fix styling and API changes issues introduced with merge * Fix broken test Co-authored-by: Daniel Schalla <daniel@schalla.me>
163 lines
4.8 KiB
Go
163 lines
4.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package api4
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/avct/uasurfer"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/audit"
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
)
|
|
|
|
func (api *API) InitCompliance() {
|
|
api.BaseRoutes.Compliance.Handle("/reports", api.APISessionRequired(createComplianceReport)).Methods("POST")
|
|
api.BaseRoutes.Compliance.Handle("/reports", api.APISessionRequired(getComplianceReports)).Methods("GET")
|
|
api.BaseRoutes.Compliance.Handle("/reports/{report_id:[A-Za-z0-9]+}", api.APISessionRequired(getComplianceReport)).Methods("GET")
|
|
api.BaseRoutes.Compliance.Handle("/reports/{report_id:[A-Za-z0-9]+}/download", api.APISessionRequiredTrustRequester(downloadComplianceReport)).Methods("GET")
|
|
}
|
|
|
|
func createComplianceReport(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
var job model.Compliance
|
|
if jsonErr := json.NewDecoder(r.Body).Decode(&job); jsonErr != nil {
|
|
c.SetInvalidParam("compliance")
|
|
return
|
|
}
|
|
|
|
auditRec := c.MakeAuditRecord("createComplianceReport", audit.Fail)
|
|
auditRec.AddEventParameter("compliance", job)
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionCreateComplianceExportJob) {
|
|
c.SetPermissionError(model.PermissionCreateComplianceExportJob)
|
|
return
|
|
}
|
|
|
|
job.UserId = c.AppContext.Session().UserId
|
|
|
|
rjob, err := c.App.SaveComplianceReport(&job)
|
|
if err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
|
|
auditRec.Success()
|
|
auditRec.AddEventResultState(rjob)
|
|
auditRec.AddEventObjectType("compliance")
|
|
auditRec.AddMeta("compliance_id", rjob.Id)
|
|
auditRec.AddMeta("compliance_desc", rjob.Desc)
|
|
c.LogAudit("")
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
if err := json.NewEncoder(w).Encode(rjob); err != nil {
|
|
mlog.Warn("Error while writing response", mlog.Err(err))
|
|
}
|
|
}
|
|
|
|
func getComplianceReports(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionReadComplianceExportJob) {
|
|
c.SetPermissionError(model.PermissionReadComplianceExportJob)
|
|
return
|
|
}
|
|
|
|
auditRec := c.MakeAuditRecord("getComplianceReports", audit.Fail)
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
crs, err := c.App.GetComplianceReports(c.Params.Page, c.Params.PerPage)
|
|
if err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
|
|
auditRec.Success()
|
|
if err := json.NewEncoder(w).Encode(crs); err != nil {
|
|
mlog.Warn("Error while writing response", mlog.Err(err))
|
|
}
|
|
}
|
|
|
|
func getComplianceReport(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
c.RequireReportId()
|
|
if c.Err != nil {
|
|
return
|
|
}
|
|
|
|
auditRec := c.MakeAuditRecord("getComplianceReport", audit.Fail)
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionReadComplianceExportJob) {
|
|
c.SetPermissionError(model.PermissionReadComplianceExportJob)
|
|
return
|
|
}
|
|
|
|
auditRec.AddEventParameter("report_id", c.Params.ReportId)
|
|
job, err := c.App.GetComplianceReport(c.Params.ReportId)
|
|
if err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
|
|
auditRec.Success()
|
|
auditRec.AddMeta("compliance_id", job.Id)
|
|
auditRec.AddMeta("compliance_desc", job.Desc)
|
|
|
|
if err := json.NewEncoder(w).Encode(job); err != nil {
|
|
mlog.Warn("Error while writing response", mlog.Err(err))
|
|
}
|
|
}
|
|
|
|
func downloadComplianceReport(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
c.RequireReportId()
|
|
if c.Err != nil {
|
|
return
|
|
}
|
|
|
|
auditRec := c.MakeAuditRecord("downloadComplianceReport", audit.Fail)
|
|
defer c.LogAuditRec(auditRec)
|
|
auditRec.AddEventParameter("compliance_id", c.Params.ReportId)
|
|
|
|
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionDownloadComplianceExportResult) {
|
|
c.SetPermissionError(model.PermissionDownloadComplianceExportResult)
|
|
return
|
|
}
|
|
|
|
job, err := c.App.GetComplianceReport(c.Params.ReportId)
|
|
if err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
auditRec.AddEventResultState(job)
|
|
auditRec.AddEventObjectType("compliance")
|
|
|
|
reportBytes, err := c.App.GetComplianceFile(job)
|
|
if err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
auditRec.AddMeta("length", len(reportBytes))
|
|
|
|
c.LogAudit("downloaded " + job.Desc)
|
|
|
|
w.Header().Set("Cache-Control", "max-age=2592000, private")
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(reportBytes)))
|
|
w.Header().Del("Content-Type") // Content-Type will be set automatically by the http writer
|
|
|
|
// attach extra headers to trigger a download on IE, Edge, and Safari
|
|
ua := uasurfer.Parse(r.UserAgent())
|
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename=\""+job.JobName()+".zip\"")
|
|
|
|
if ua.Browser.Name == uasurfer.BrowserIE || ua.Browser.Name == uasurfer.BrowserSafari {
|
|
// trim off anything before the final / so we just get the file's name
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
}
|
|
|
|
auditRec.Success()
|
|
|
|
w.Write(reportBytes)
|
|
}
|