diff --git a/Moonlight/App/Models/Log/LogData.cs b/Moonlight/App/Models/Log/LogData.cs new file mode 100644 index 00000000..645a666f --- /dev/null +++ b/Moonlight/App/Models/Log/LogData.cs @@ -0,0 +1,7 @@ +namespace Moonlight.App.Models.Log; + +public class LogData +{ + public Type Type { get; set; } + public string Value { get; set; } +} \ No newline at end of file diff --git a/Moonlight/App/Services/DomainService.cs b/Moonlight/App/Services/DomainService.cs index b2b2b1fb..4bdbf9b3 100644 --- a/Moonlight/App/Services/DomainService.cs +++ b/Moonlight/App/Services/DomainService.cs @@ -169,7 +169,11 @@ public class DomainService })); } - await AuditLogService.Log(AuditLogType.AddDomainRecord, new[] { d.Id.ToString(), dnsRecord.Name }); + await AuditLogService.Log(AuditLogType.AddDomainRecord, x => + { + x.Add(d.Id); + x.Add(dnsRecord.Name); + }); } public async Task UpdateDnsRecord(Domain d, DnsRecord dnsRecord) @@ -199,7 +203,11 @@ public class DomainService })); } - await AuditLogService.Log(AuditLogType.UpdateDomainRecord, new[] { d.Id.ToString(), dnsRecord.Name }); + await AuditLogService.Log(AuditLogType.UpdateDomainRecord, x => + { + x.Add(d.Id); + x.Add(dnsRecord.Name); + }); } public async Task DeleteDnsRecord(Domain d, DnsRecord dnsRecord) @@ -210,7 +218,11 @@ public class DomainService await Client.Zones.DnsRecords.DeleteAsync(domain.SharedDomain.CloudflareId, dnsRecord.Id) ); - await AuditLogService.Log(AuditLogType.DeleteDomainRecord, new[] { d.Id.ToString(), dnsRecord.Name }); + await AuditLogService.Log(AuditLogType.DeleteDomainRecord, x => + { + x.Add(d.Id); + x.Add(dnsRecord.Name); + }); } private Domain EnsureData(Domain domain) diff --git a/Moonlight/App/Services/LogServices/AuditLogService.cs b/Moonlight/App/Services/LogServices/AuditLogService.cs index 7e32f588..ac9c8845 100644 --- a/Moonlight/App/Services/LogServices/AuditLogService.cs +++ b/Moonlight/App/Services/LogServices/AuditLogService.cs @@ -1,4 +1,5 @@ using Moonlight.App.Database.Entities.LogsEntries; +using Moonlight.App.Models.Log; using Moonlight.App.Models.Misc; using Moonlight.App.Repositories.LogEntries; using Moonlight.App.Services.Sessions; @@ -19,16 +20,18 @@ public class AuditLogService HttpContextAccessor = httpContextAccessor; } - public Task Log(AuditLogType type, params object[] data) + public Task Log(AuditLogType type, Action data) { var ip = GetIp(); + var al = new AuditLogParameters(); + data(al); var entry = new AuditLogEntry() { Ip = ip, Type = type, System = false, - JsonData = data.Length == 0 ? "" : JsonConvert.SerializeObject(data) + JsonData = al.Build() }; Repository.Add(entry); @@ -62,4 +65,23 @@ public class AuditLogService return HttpContextAccessor.HttpContext.Connection.RemoteIpAddress!.ToString(); } + + public class AuditLogParameters + { + private List Data = new List(); + + public void Add(object data) + { + Data.Add(new LogData() + { + Type = typeof(T), + Value = data.ToString() + }); + } + + internal string Build() + { + return JsonConvert.SerializeObject(Data); + } + } } \ No newline at end of file diff --git a/Moonlight/App/Services/ServerService.cs b/Moonlight/App/Services/ServerService.cs index 2f69fd7b..84522e8e 100644 --- a/Moonlight/App/Services/ServerService.cs +++ b/Moonlight/App/Services/ServerService.cs @@ -96,7 +96,11 @@ public class ServerService Action = rawSignal }); - await AuditLogService.Log(AuditLogType.ChangePowerState, new[] { server.Uuid.ToString(), rawSignal }); + await AuditLogService.Log(AuditLogType.ChangePowerState, x => + { + x.Add(server.Uuid); + x.Add(rawSignal); + }); } public async Task CreateBackup(Server server) @@ -126,7 +130,11 @@ public class ServerService }); await AuditLogService.Log(AuditLogType.CreateBackup, - new[] { serverData.Uuid.ToString(), backup.Uuid.ToString() }); + x => + { + x.Add(server.Uuid); + x.Add(backup.Uuid); + }); return backup; } @@ -164,7 +172,11 @@ public class ServerService }); await AuditLogService.Log(AuditLogType.RestoreBackup, - new[] { s.Uuid.ToString(), serverBackup.Uuid.ToString() }); + x => + { + x.Add(server.Uuid); + x.Add(serverBackup.Uuid); + }); } public async Task DeleteBackup(Server server, ServerBackup serverBackup) @@ -186,7 +198,11 @@ public class ServerService await MessageService.Emit("wings.backups.delete", backup); await AuditLogService.Log(AuditLogType.DeleteBackup, - new[] { serverBackup.Uuid.ToString(), serverBackup.Uuid.ToString() }); + x => + { + x.Add(server.Uuid); + x.Add(backup.Uuid); + }); } public async Task DownloadBackup(Server s, ServerBackup serverBackup) @@ -200,7 +216,11 @@ public class ServerService }); await AuditLogService.Log(AuditLogType.DownloadBackup, - new[] { serverBackup.Uuid.ToString(), serverBackup.Uuid.ToString() }); + x => + { + x.Add(server.Uuid); + x.Add(serverBackup.Uuid); + }); return $"https://{server.Node.Fqdn}:{server.Node.HttpPort}/download/backup?token={token}"; } @@ -305,7 +325,10 @@ public class ServerService StartOnCompletion = false }); - await AuditLogService.Log(AuditLogType.CreateServer, newServerData.Uuid.ToString()); + await AuditLogService.Log(AuditLogType.CreateServer, x => + { + x.Add(newServerData.Uuid); + }); return newServerData; } @@ -325,7 +348,10 @@ public class ServerService await WingsApiHelper.Post(server.Node, $"api/servers/{server.Uuid}/reinstall", null); - await AuditLogService.Log(AuditLogType.ReinstallServer, server.Uuid.ToString()); + await AuditLogService.Log(AuditLogType.ReinstallServer, x => + { + x.Add(server.Uuid); + }); } public async Task SftpServerLogin(int serverId, int id, string password) diff --git a/Moonlight/App/Services/TotpService.cs b/Moonlight/App/Services/TotpService.cs index 0e359fec..caffc7a7 100644 --- a/Moonlight/App/Services/TotpService.cs +++ b/Moonlight/App/Services/TotpService.cs @@ -1,4 +1,5 @@ -using Moonlight.App.Models.Misc; +using Moonlight.App.Database.Entities; +using Moonlight.App.Models.Misc; using Moonlight.App.Repositories; using Moonlight.App.Services.LogServices; using Moonlight.App.Services.Sessions; @@ -51,7 +52,10 @@ public class TotpService UserRepository.Update(user); - await AuditLogService.Log(AuditLogType.EnableTotp, user.Email); + await AuditLogService.Log(AuditLogType.EnableTotp, x => + { + x.Add(user.Email); + }); } public async Task EnforceTotpLogin() @@ -70,7 +74,10 @@ public class TotpService UserRepository.Update(user); - await AuditLogService.Log(AuditLogType.DisableTotp, user.Email); + await AuditLogService.Log(AuditLogType.DisableTotp,x => + { + x.Add(user.Email); + }); } private string GenerateSecret() diff --git a/Moonlight/App/Services/UserService.cs b/Moonlight/App/Services/UserService.cs index 2cf233ea..c70ae8e9 100644 --- a/Moonlight/App/Services/UserService.cs +++ b/Moonlight/App/Services/UserService.cs @@ -77,7 +77,10 @@ public class UserService }); await MailService.SendMail(user!, "register", values => {}); - await AuditLogService.Log(AuditLogType.Register, user.Email); + await AuditLogService.Log(AuditLogType.Register, x => + { + x.Add(user.Email); + }); return await GenerateToken(user); } @@ -125,7 +128,10 @@ public class UserService if (totpCodeValid) { - await AuditLogService.Log(AuditLogType.Login, email); + await AuditLogService.Log(AuditLogType.Login, x => + { + x.Add(email); + }); return await GenerateToken(user, true); } else @@ -136,7 +142,10 @@ public class UserService } else { - await AuditLogService.Log(AuditLogType.Login, email); + await AuditLogService.Log(AuditLogType.Login, x => + { + x.Add(email); + }); return await GenerateToken(user!, true); } } @@ -160,7 +169,10 @@ public class UserService values.Add("Location", "In your walls"); }); - await AuditLogService.Log(AuditLogType.ChangePassword, user.Email); + await AuditLogService.Log(AuditLogType.ChangePassword, x => + { + x.Add(user.Email); + }); } } @@ -218,7 +230,7 @@ public class UserService var newPassword = StringHelper.GenerateString(16); await ChangePassword(user, newPassword, true); - await AuditLogService.Log(AuditLogType.PasswordReset); + await AuditLogService.Log(AuditLogType.PasswordReset, x => {}); await MailService.SendMail(user, "passwordReset", values => { diff --git a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePassword.razor b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePassword.razor index b3ffcd8f..f9ee3ac8 100644 --- a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePassword.razor +++ b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePassword.razor @@ -3,6 +3,7 @@ @using Moonlight.App.Repositories @using Newtonsoft.Json @using Moonlight.App.Database.Entities +@using Moonlight.App.Models.Log @inject UserRepository UserRepository @@ -18,7 +19,7 @@
@if (User == null) { - Password change for @(Data[0]) + Password change for @(Data[0].Value) } else { @@ -38,18 +39,18 @@ public AuditLogEntry Entry { get; set; } private User? User; - private string[] Data; + private LogData[] Data; protected override void OnInitialized() { - Data = JsonConvert.DeserializeObject(Entry.JsonData)!; + Data = JsonConvert.DeserializeObject(Entry.JsonData)!; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { - User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0]); + User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0].Value); await InvokeAsync(StateHasChanged); } diff --git a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePowerState.razor b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePowerState.razor index 95400158..9dcafe02 100644 --- a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePowerState.razor +++ b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryChangePowerState.razor @@ -2,6 +2,7 @@ @using Moonlight.App.Helpers @using Newtonsoft.Json @using Moonlight.App.Database.Entities +@using Moonlight.App.Models.Log @using Moonlight.App.Repositories.Servers @inject ServerRepository ServerRepository @@ -18,11 +19,11 @@
@if (Server == null) { - Change power state for @(Data[0]) to @(Data[1]) + Change power state for @(Data[0].Value) to @(Data[1].Value) } else { - Change power state for @(Server.Name) to @(Data[1]) + Change power state for @(Server.Name) to @(Data[1].Value) }
@@ -38,18 +39,18 @@ public AuditLogEntry Entry { get; set; } private Server? Server; - private string[] Data; + private LogData[] Data; protected override void OnInitialized() { - Data = JsonConvert.DeserializeObject(Entry.JsonData)!; + Data = JsonConvert.DeserializeObject(Entry.JsonData)!; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { - Server = ServerRepository.Get().FirstOrDefault(x => x.Uuid == Guid.Parse(Data[0])); + Server = ServerRepository.Get().FirstOrDefault(x => x.Uuid == Guid.Parse(Data[0].Value)); await InvokeAsync(StateHasChanged); } diff --git a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryLogin.razor b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryLogin.razor index c215f4df..810a3568 100644 --- a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryLogin.razor +++ b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryLogin.razor @@ -3,6 +3,7 @@ @using Moonlight.App.Repositories @using Newtonsoft.Json @using Moonlight.App.Database.Entities +@using Moonlight.App.Models.Log @inject UserRepository UserRepository @@ -18,7 +19,7 @@
@if (User == null) { - New login for @(Data[0]) + New login for @(Data[0].Value) } else { @@ -38,18 +39,18 @@ public AuditLogEntry Entry { get; set; } private User? User; - private string[] Data; + private LogData[] Data; protected override void OnInitialized() { - Data = JsonConvert.DeserializeObject(Entry.JsonData)!; + Data = JsonConvert.DeserializeObject(Entry.JsonData)!; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { - User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0]); + User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0].Value); await InvokeAsync(StateHasChanged); } diff --git a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryRegister.razor b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryRegister.razor index 43cc89a3..846f0d86 100644 --- a/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryRegister.razor +++ b/Moonlight/Shared/Components/AuditLogEntrys/AuditLogEntryRegister.razor @@ -3,6 +3,7 @@ @using Moonlight.App.Repositories @using Newtonsoft.Json @using Moonlight.App.Database.Entities +@using Moonlight.App.Models.Log @inject UserRepository UserRepository @@ -18,7 +19,7 @@
@if (User == null) { - Register for @(Data[0]) + Register for @(Data[0].Value) } else { @@ -38,18 +39,18 @@ public AuditLogEntry Entry { get; set; } private User? User; - private string[] Data; + private LogData[] Data; protected override void OnInitialized() { - Data = JsonConvert.DeserializeObject(Entry.JsonData)!; + Data = JsonConvert.DeserializeObject(Entry.JsonData)!; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { - User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0]); + User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0].Value); await InvokeAsync(StateHasChanged); } diff --git a/Moonlight/Shared/Views/Profile/Security.razor b/Moonlight/Shared/Views/Profile/Security.razor index e648de9d..db0bf9f8 100644 --- a/Moonlight/Shared/Views/Profile/Security.razor +++ b/Moonlight/Shared/Views/Profile/Security.razor @@ -236,7 +236,7 @@ private async void Enable() { - await AuditLogService.Log(AuditLogType.EnableTotp, "Totp enabled"); + await AuditLogService.Log(AuditLogType.EnableTotp, x => x.Add("Totp enabled")); await TotpService.Enable(); TotpEnabled = await TotpService.GetEnabled(); TotpSecret = await TotpService.GetSecret(); @@ -262,7 +262,7 @@ private async void Disable() { - await AuditLogService.Log(AuditLogType.DisableTotp, "Totp disabled"); + await AuditLogService.Log(AuditLogType.DisableTotp, x => x.Add("Totp disabled")); await TotpService.Disable(); NavigationManager.NavigateTo(NavigationManager.Uri, true); } @@ -286,7 +286,7 @@ { await UserService.ChangePassword(User, Password); - await AuditLogService.Log(AuditLogType.PasswordChange, "The password has been set to a new one"); + await AuditLogService.Log(AuditLogType.PasswordChange, x => x.Add("The password has been set to a new one")); // Reload to make the user login again NavigationManager.NavigateTo(NavigationManager.Uri, true);