mirror of
https://github.com/mattermost/mattermost.git
synced 2026-08-29 02:28:16 +08:00
[MM-68457] Expose audit logging API via pluginapi.Client (#36232)
Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package pluginapi
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/plugin"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
)
|
||||
|
||||
// AuditService exposes methods to emit audit records through the Mattermost server audit pipeline.
|
||||
type AuditService struct {
|
||||
api plugin.API
|
||||
}
|
||||
|
||||
// Record logs an audit record using the default audit log level.
|
||||
//
|
||||
// Minimum server version: 10.10
|
||||
func (a *AuditService) Record(rec *model.AuditRecord) {
|
||||
a.api.LogAuditRec(rec)
|
||||
}
|
||||
|
||||
// RecordWithLevel logs an audit record with the given log level.
|
||||
//
|
||||
// Minimum server version: 10.10
|
||||
func (a *AuditService) RecordWithLevel(rec *model.AuditRecord, level mlog.Level) {
|
||||
a.api.LogAuditRecWithLevel(rec, level)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package pluginapi_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/plugin"
|
||||
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
|
||||
"github.com/mattermost/mattermost/server/public/pluginapi"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
)
|
||||
|
||||
func TestAuditService(t *testing.T) {
|
||||
t.Run("record", func(t *testing.T) {
|
||||
rec := plugin.MakeAuditRecord("test.event", model.AuditStatusSuccess)
|
||||
|
||||
api := &plugintest.API{}
|
||||
api.On("LogAuditRec", rec).Return()
|
||||
defer api.AssertExpectations(t)
|
||||
|
||||
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
||||
client.Audit.Record(rec)
|
||||
})
|
||||
|
||||
t.Run("record with level", func(t *testing.T) {
|
||||
rec := plugin.MakeAuditRecord("test.event", model.AuditStatusFail)
|
||||
level := mlog.LvlAuditCLI
|
||||
|
||||
api := &plugintest.API{}
|
||||
api.On("LogAuditRecWithLevel", rec, level).Return()
|
||||
defer api.AssertExpectations(t)
|
||||
|
||||
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
||||
client.Audit.RecordWithLevel(rec, level)
|
||||
})
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
type Client struct {
|
||||
api plugin.API
|
||||
|
||||
Audit AuditService
|
||||
Bot BotService
|
||||
Channel ChannelService
|
||||
Cluster ClusterService
|
||||
@@ -41,6 +42,7 @@ func NewClient(api plugin.API, driver plugin.Driver) *Client {
|
||||
return &Client{
|
||||
api: api,
|
||||
|
||||
Audit: AuditService{api: api},
|
||||
Bot: BotService{api: api},
|
||||
Channel: ChannelService{api: api},
|
||||
Cluster: ClusterService{api: api},
|
||||
|
||||
Reference in New Issue
Block a user