Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
177 lines
5.5 KiB
Go
177 lines
5.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package properties
|
|
|
|
import (
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/request"
|
|
)
|
|
|
|
// Value audit actions, recorded as the "action" meta on the audit record
|
|
// emitted by a group's ValueAuditSink. Exported so group-specific sinks can
|
|
// branch on the action type.
|
|
const (
|
|
ValueAuditActionCreate = "create"
|
|
ValueAuditActionUpdate = "update"
|
|
ValueAuditActionUpsert = "upsert"
|
|
ValueAuditActionDelete = "delete"
|
|
ValueAuditActionDeleteForTarget = "delete_for_target"
|
|
ValueAuditActionDeleteForField = "delete_for_field"
|
|
)
|
|
|
|
// ValueAuditEvent describes one successful property value change passed to a
|
|
// group's ValueAuditSink. Prev and Current are nil for bulk-delete actions
|
|
// (delete_for_target, delete_for_field). Each group decides which fields to
|
|
// include in its audit record.
|
|
type ValueAuditEvent struct {
|
|
Action string
|
|
TargetType string
|
|
TargetID string
|
|
FieldID string
|
|
ValueID string
|
|
Prev *model.PropertyValue
|
|
Current *model.PropertyValue
|
|
}
|
|
|
|
// ValueAuditSink emits one audit record for a successful property value change.
|
|
// Each property group registers its own sink so the properties package stays
|
|
// independent of the audit subsystem.
|
|
type ValueAuditSink func(rctx request.CTX, e ValueAuditEvent)
|
|
|
|
// PropertyValueAuditHook audits value writes for registered property groups.
|
|
// Every write path funnels through the generic value write path, so auditing
|
|
// lives here rather than in each API handler. Post-hooks run after the store
|
|
// write. Groups without a registered sink are not audited.
|
|
type PropertyValueAuditHook struct {
|
|
BasePropertyHook
|
|
sinks map[string]ValueAuditSink
|
|
}
|
|
|
|
var _ PropertyHook = (*PropertyValueAuditHook)(nil)
|
|
|
|
// NewPropertyValueAuditHook creates a value audit hook. Call RegisterGroup to
|
|
// opt groups in with a sink callback.
|
|
func NewPropertyValueAuditHook() *PropertyValueAuditHook {
|
|
return &PropertyValueAuditHook{
|
|
sinks: make(map[string]ValueAuditSink),
|
|
}
|
|
}
|
|
|
|
// RegisterGroup registers an audit sink for the given property group ID.
|
|
func (h *PropertyValueAuditHook) RegisterGroup(groupID string, sink ValueAuditSink) {
|
|
h.sinks[groupID] = sink
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) sinkFor(groupID string) ValueAuditSink {
|
|
return h.sinks[groupID]
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) emit(rctx request.CTX, groupID string, e ValueAuditEvent) {
|
|
sink := h.sinkFor(groupID)
|
|
if sink == nil {
|
|
return
|
|
}
|
|
sink(rctx, e)
|
|
}
|
|
|
|
// auditWrite emits an audit record for a successful write.
|
|
func (h *PropertyValueAuditHook) auditWrite(rctx request.CTX, action string, value *model.PropertyValue) {
|
|
if value == nil {
|
|
return
|
|
}
|
|
h.emit(rctx, value.GroupID, ValueAuditEvent{
|
|
Action: action,
|
|
TargetType: value.TargetType,
|
|
TargetID: value.TargetID,
|
|
FieldID: value.FieldID,
|
|
Current: value,
|
|
})
|
|
}
|
|
|
|
// auditCreate emits a create audit record. A create always introduces a new
|
|
// value (a conflicting create fails), so there is no no-op to suppress.
|
|
func (h *PropertyValueAuditHook) auditCreate(rctx request.CTX, value *model.PropertyValue) {
|
|
if value == nil {
|
|
return
|
|
}
|
|
h.emit(rctx, value.GroupID, ValueAuditEvent{
|
|
Action: ValueAuditActionCreate,
|
|
TargetType: value.TargetType,
|
|
TargetID: value.TargetID,
|
|
FieldID: value.FieldID,
|
|
Current: value,
|
|
})
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostCreatePropertyValue(rctx request.CTX, value *model.PropertyValue) error {
|
|
h.auditCreate(rctx, value)
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostCreatePropertyValues(rctx request.CTX, values []*model.PropertyValue) error {
|
|
for _, v := range values {
|
|
h.auditCreate(rctx, v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostUpdatePropertyValue(rctx request.CTX, value *model.PropertyValue) error {
|
|
h.auditWrite(rctx, ValueAuditActionUpdate, value)
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostUpdatePropertyValues(rctx request.CTX, values []*model.PropertyValue) error {
|
|
for _, v := range values {
|
|
h.auditWrite(rctx, ValueAuditActionUpdate, v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostUpsertPropertyValue(rctx request.CTX, value *model.PropertyValue) error {
|
|
h.auditWrite(rctx, ValueAuditActionUpsert, value)
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostUpsertPropertyValues(rctx request.CTX, values []*model.PropertyValue) error {
|
|
for _, v := range values {
|
|
h.auditWrite(rctx, ValueAuditActionUpsert, v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostDeletePropertyValue(rctx request.CTX, groupID, valueID string, deleted *model.PropertyValue) error {
|
|
event := ValueAuditEvent{
|
|
Action: ValueAuditActionDelete,
|
|
ValueID: valueID,
|
|
}
|
|
emitGroupID := groupID
|
|
if deleted != nil {
|
|
event.TargetType = deleted.TargetType
|
|
event.TargetID = deleted.TargetID
|
|
event.FieldID = deleted.FieldID
|
|
event.Prev = deleted
|
|
emitGroupID = deleted.GroupID
|
|
}
|
|
|
|
h.emit(rctx, emitGroupID, event)
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostDeletePropertyValuesForTarget(rctx request.CTX, groupID, targetType, targetID string) error {
|
|
h.emit(rctx, groupID, ValueAuditEvent{
|
|
Action: ValueAuditActionDeleteForTarget,
|
|
TargetType: targetType,
|
|
TargetID: targetID,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (h *PropertyValueAuditHook) PostDeletePropertyValuesForField(rctx request.CTX, groupID, fieldID string) error {
|
|
h.emit(rctx, groupID, ValueAuditEvent{
|
|
Action: ValueAuditActionDeleteForField,
|
|
FieldID: fieldID,
|
|
})
|
|
return nil
|
|
}
|