[MM-56339] Audit logs: on login add UserId and SessionId to audit's Actor field (#27446)

* on login add UserId and SessionId to audit's Actor field to match logout

* lint

* simplify to add only userId and sessionId

* AddToEventActor -> AddUser/SessionToEventActor

* fill in missing session data when logging the audit record

* why did it bump that? reverting.

* make modules-tidy

* trigger build

* add more context to the comment
Этот коммит содержится в:
Christopher Poile
2024-07-08 18:56:54 -04:00
коммит произвёл GitHub
родитель 9f312f48b5
Коммит 9b2f20210b
7 изменённых файлов: 110 добавлений и 18 удалений

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

@@ -184,6 +184,47 @@ func TestCreateUserAudit(t *testing.T) {
require.NotContains(t, string(data), password)
}
func TestUserLoginAudit(t *testing.T) {
logFile, err := os.CreateTemp("", "adv.log")
require.NoError(t, err)
defer os.Remove(logFile.Name())
os.Setenv("MM_EXPERIMENTALAUDITSETTINGS_FILEENABLED", "true")
os.Setenv("MM_EXPERIMENTALAUDITSETTINGS_FILENAME", logFile.Name())
defer os.Unsetenv("MM_EXPERIMENTALAUDITSETTINGS_FILEENABLED")
defer os.Unsetenv("MM_EXPERIMENTALAUDITSETTINGS_FILENAME")
options := []app.Option{app.WithLicense(model.NewTestLicense("advanced_logging"))}
th := SetupWithServerOptions(t, options)
defer th.TearDown()
_, err = th.Client.Logout(context.Background())
require.NoError(t, err)
user, resp, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password)
require.NoError(t, err)
CheckOKStatus(t, resp)
assert.Equal(t, th.BasicUser.Id, user.Id)
sess, resp, err := th.Client.GetSessions(context.Background(), user.Id, "")
require.NoError(t, err)
CheckOKStatus(t, resp)
assert.Len(t, sess, 1)
assert.Equal(t, th.BasicUser.Id, sess[0].UserId)
// Forcing a flush before attempting to read log's content.
err = th.Server.Audit.Flush()
require.NoError(t, err)
require.NoError(t, logFile.Sync())
data, err := io.ReadAll(logFile)
require.NoError(t, err)
require.NotEmpty(t, data)
// ensure we are auditing the user_id and session_id
require.Contains(t, string(data), fmt.Sprintf("\"event_name\":\"login\",\"status\":\"success\",\"actor\":{\"user_id\":\"%s\",\"session_id\":\"%s\"", user.Id, sess[0].Id))
}
func TestCreateUserInputFilter(t *testing.T) {
th := Setup(t)
defer th.TearDown()