Owner review 2, R11 asked for more log settings. The file sink now writes daily or hourly files, continues a full file in numbered parts and prunes by age and by the total size (never the file being written); its options change while it runs. Server: the file and console levels are separate and read by the logging filters at every line, so the admin panel's new Logs group applies without a restart; the engine's level follows the lower of the two. This also fixes a bug: the file logger was fixed at Information, so CHRONOS_LOG_LEVEL=Debug or Trace never reached the files. New variables CHRONOS_LOG_CONSOLE_LEVEL, CHRONOS_LOG_RETENTION_DAYS, CHRONOS_LOG_MAX_FILE_MB, CHRONOS_LOG_MAX_TOTAL_MB, CHRONOS_LOG_ROTATION, CHRONOS_AUDIT_RETENTION_DAYS and CHRONOS_DIAGNOSTICS_LOG_MB lock their settings like the others; the diagnostics zip keeps the configured share of each log. Desktop: Settings > Advanced gains Log level, Keep logs, File size, Logs total, Rotation and Diagnostics logs, applied at every save; Advanced's Reset section restores them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit cc875784e4952ee0e4c807b9d9c18496d3d5a8c3)
92 lines
5.1 KiB
C#
92 lines
5.1 KiB
C#
using System.IO.Compression;
|
||
using System.Net;
|
||
using System.Text.Json;
|
||
using Chronos.Core.Model;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Server.Tests.Api;
|
||
|
||
[Category("server")]
|
||
public sealed class DiagnosticsTests(TestContext context)
|
||
{
|
||
private const string DbPassword = "db-secret-pw-42";
|
||
private const string ConnectionPassword = "correct horse, \"battery\" staple";
|
||
|
||
[Test]
|
||
public async Task TheBundleHoldsReportsLogsAndFailuresWithoutSecrets()
|
||
{
|
||
await using var server = await TestServer.StartAsync(context, o => o.DbPassword = DbPassword);
|
||
var admin = await server.AdminAsync();
|
||
// Only reported, never opened: the test server already runs on its own database.
|
||
server.Options.DbConnectionString = $"Host=db;Username=chronos;Password={ConnectionPassword};SslMode=prefer";
|
||
Directory.CreateDirectory(server.Options.LogPath);
|
||
await File.WriteAllTextAsync(Path.Combine(server.Options.LogPath, "chronos-server-test.log"),
|
||
$"connecting with Password={DbPassword}\nAuthorization: Bearer {TestServer.Password}\n");
|
||
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
|
||
await server.Database.Jobs.EnqueueAsync(job);
|
||
await server.Database.Jobs.SetStateAsync(job.Id, JobState.Failed, "rejected: token=" + DbPassword + " for db");
|
||
|
||
using var response = await admin.GetAsync("/api/v1/server/diagnostics");
|
||
await response.ExpectAsync(HttpStatusCode.OK);
|
||
Assert.Equal("application/zip", response.Content.Headers.ContentType?.MediaType);
|
||
await using var body = await response.Content.ReadAsStreamAsync();
|
||
using var zip = new ZipArchive(body, ZipArchiveMode.Read);
|
||
var texts = zip.Entries.ToDictionary(e => e.FullName, Read);
|
||
|
||
Assert.Contains("environment.txt", texts.Keys);
|
||
Assert.Contains("encoders.txt", texts.Keys);
|
||
Assert.Contains("logs/chronos-server-test.log", texts.Keys);
|
||
foreach (var (name, text) in texts)
|
||
{
|
||
Assert.False(text.Contains(DbPassword, StringComparison.Ordinal), $"{name} leaks the database password");
|
||
Assert.False(text.Contains(TestServer.AdminPassword, StringComparison.Ordinal), $"{name} leaks the admin password");
|
||
Assert.False(text.Contains(TestServer.Password, StringComparison.Ordinal), $"{name} leaks a user password");
|
||
Assert.False(text.Contains("horse", StringComparison.Ordinal) || text.Contains("staple", StringComparison.Ordinal), $"{name} leaks the connection-string password");
|
||
}
|
||
Assert.Contains("Chronos Server ", texts["environment.txt"]);
|
||
using (var settings = JsonDocument.Parse(texts["settings.json"]))
|
||
{
|
||
Assert.Equal("[removed]", settings.RootElement.GetProperty("DbPassword").GetString());
|
||
Assert.Equal("[removed]", settings.RootElement.GetProperty("AdminPassword").GetString());
|
||
Assert.Equal("Host=db;Username=chronos;Password=[removed];SslMode=prefer", settings.RootElement.GetProperty("DbConnectionString").GetString());
|
||
}
|
||
using (var failed = JsonDocument.Parse(texts["jobs-failed.json"]))
|
||
{
|
||
var entry = failed.RootElement.EnumerateArray().Single();
|
||
Assert.Equal(("Render", "rejected: token=[removed]"), (entry.GetProperty("kind").GetString(), entry.GetProperty("error").GetString()));
|
||
}
|
||
|
||
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
|
||
using var refused = await viewer.GetAsync("/api/v1/server/diagnostics");
|
||
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||
Assert.NotEmpty((await server.Database.Audit.ListAsync(null, "server_diagnostics", 10, null)).Items);
|
||
}
|
||
|
||
[Test]
|
||
public async Task TheBundleKeepsTheEndOfEachLogTheSettingsAskFor()
|
||
{
|
||
// Owner review 2, R11: Settings › Logs › Diagnostics logs, 1 MB here.
|
||
await using var server = await TestServer.StartAsync(context, o => o.DiagnosticsLogMb = 1);
|
||
var admin = await server.AdminAsync();
|
||
var log = new System.Text.StringBuilder();
|
||
for (var i = 0; i < 30_000; i++) log.Append("line ").Append(i).Append(' ').Append('x', 90).Append('\n');
|
||
await File.WriteAllTextAsync(Path.Combine(server.Options.LogPath, "server-20200101.log"), log.ToString());
|
||
|
||
using var response = await admin.GetAsync("/api/v1/server/diagnostics");
|
||
await response.ExpectAsync(HttpStatusCode.OK);
|
||
await using var body = await response.Content.ReadAsStreamAsync();
|
||
using var zip = new ZipArchive(body, ZipArchiveMode.Read);
|
||
var tail = Read(zip.GetEntry("logs/server-20200101.log")!);
|
||
Assert.True(tail.StartsWith("[first ", StringComparison.Ordinal), "a cut log says so");
|
||
Assert.Contains("line 29999 ", tail);
|
||
// 1 MiB of the file, less the cut first line, with the note and the system's line ends (CRLF on Windows).
|
||
Assert.InRange(tail.Length, (1 << 20) - 200, (1 << 20) * 1.02);
|
||
}
|
||
|
||
private static string Read(ZipArchiveEntry entry)
|
||
{
|
||
using var reader = new StreamReader(entry.Open());
|
||
return reader.ReadToEnd();
|
||
}
|
||
}
|