new audit log
This commit is contained in:
7
Moonlight/App/Models/Log/LogData.cs
Normal file
7
Moonlight/App/Models/Log/LogData.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Moonlight.App.Models.Log;
|
||||
|
||||
public class LogData
|
||||
{
|
||||
public Type Type { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
@@ -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<Domain>(d.Id);
|
||||
x.Add<DnsRecord>(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<Domain>(d.Id);
|
||||
x.Add<DnsRecord>(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<Domain>(d.Id);
|
||||
x.Add<DnsRecord>(dnsRecord.Name);
|
||||
});
|
||||
}
|
||||
|
||||
private Domain EnsureData(Domain domain)
|
||||
|
||||
@@ -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<AuditLogParameters> 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<LogData> Data = new List<LogData>();
|
||||
|
||||
public void Add<T>(object data)
|
||||
{
|
||||
Data.Add(new LogData()
|
||||
{
|
||||
Type = typeof(T),
|
||||
Value = data.ToString()
|
||||
});
|
||||
}
|
||||
|
||||
internal string Build()
|
||||
{
|
||||
return JsonConvert.SerializeObject(Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>(server.Uuid);
|
||||
x.Add<PowerSignal>(rawSignal);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<ServerBackup> 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>(server.Uuid);
|
||||
x.Add<ServerBackup>(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>(server.Uuid);
|
||||
x.Add<ServerBackup>(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>(server.Uuid);
|
||||
x.Add<ServerBackup>(backup.Uuid);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<string> 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>(server.Uuid);
|
||||
x.Add<ServerBackup>(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<Server>(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>(server.Uuid);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<Server> SftpServerLogin(int serverId, int id, string password)
|
||||
|
||||
@@ -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>(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>(user.Email);
|
||||
});
|
||||
}
|
||||
|
||||
private string GenerateSecret()
|
||||
|
||||
@@ -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>(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<User>(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<User>(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>(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 =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user