Re-implement tar log

This commit is contained in:
Timothy Baldridge 2022-02-09 11:24:31 -07:00
parent 9fd2542e77
commit 927f20401f
4 changed files with 63 additions and 10 deletions

View File

@ -9,7 +9,10 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.Server.DataModels;
using Wabbajack.Server.DTOs;
namespace Wabbajack.BuildServer;
@ -27,6 +30,9 @@ public class ApiKeyAuthenticationHandler : AuthenticationHandler<ApiKeyAuthentic
private readonly DTOSerializer _dtos;
private readonly AppSettings _settings;
private readonly AuthorKeys _authorKeys;
private readonly Task<HashSet<string>> _tarKeys;
private readonly Metrics _metricsStore;
private readonly TarLog _tarLog;
public ApiKeyAuthenticationHandler(
IOptionsMonitor<ApiKeyAuthenticationOptions> options,
@ -35,8 +41,13 @@ public class ApiKeyAuthenticationHandler : AuthenticationHandler<ApiKeyAuthentic
UrlEncoder encoder,
ISystemClock clock,
DTOSerializer dtos,
Metrics metricsStore,
TarLog tarlog,
AppSettings settings) : base(options, logger, encoder, clock)
{
_tarLog = tarlog;
_metricsStore = metricsStore;
_dtos = dtos;
_authorKeys = authorKeys;
_settings = settings;
@ -49,20 +60,18 @@ public class ApiKeyAuthenticationHandler : AuthenticationHandler<ApiKeyAuthentic
//await LogRequest(metricsKey);
if (metricsKey != default)
{
/*
if (await _sql.IsTarKey(metricsKey))
if (await _tarLog.Contains(metricsKey))
{
await _sql.IngestMetric(new Metric
await _metricsStore.Ingest(new Metric()
{
Action = "TarKey",
Subject = "Auth",
Subject = metricsKey,
Action = "tarlog",
MetricsKey = metricsKey,
Timestamp = DateTime.UtcNow
UserAgent = Request.Headers.UserAgent
});
await Task.Delay(TimeSpan.FromSeconds(60));
throw new Exception("Error, lipsum timeout of the cross distant cloud.");
return AuthenticateResult.Fail("Error, lipsum timeout of the cross distant cloud.");
}
*/
}
var authorKey = Request.Headers[ApiKeyHeaderName].FirstOrDefault();

View File

@ -11,6 +11,8 @@ public class AppSettings
}
public bool TestMode { get; set; }
public string AuthorAPIKeyFile { get; set; }
public string TarKeyFile { get; set; }
public string WabbajackBuildServerUri { get; set; } = "https://build.wabbajack.org/";
public string MetricsKeyHeader { get; set; } = "x-metrics-key";
public string TempFolder { get; set; }

View File

@ -90,7 +90,7 @@ public class MetricsController : ControllerBase
[HttpGet]
[Route("dump")]
public async Task GetMetrics([FromQuery] string action, [FromQuery] string from, [FromQuery] string? to)
public async Task GetMetrics([FromQuery] string action, [FromQuery] string from, [FromQuery] string? to, [FromQuery] string? subject)
{
var parser = new Parser();
@ -100,15 +100,18 @@ public class MetricsController : ControllerBase
var fromDate = parser.Parse(from).Start;
var records = _metricsStore.GetRecords(fromDate!.Value, toDate!.Value, action);
Response.Headers.ContentType = "application/json";
await foreach (var record in records)
{
if (!string.IsNullOrWhiteSpace(subject) && !record.Subject.Contains(subject))
continue;
await JsonSerializer.SerializeAsync(Response.Body, record);
await Response.Body.WriteAsync(EOL);
}
}
[HttpGet]
[Route("report")]
[ResponseCache(Duration = 60 * 60 * 4, VaryByQueryKeys = new [] {"action", "from", "to"})]

View File

@ -0,0 +1,39 @@
using Wabbajack.BuildServer;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
namespace Wabbajack.Server.DataModels;
public class TarLog
{
private Task<HashSet<string>> _tarKeys;
private readonly AppSettings _settings;
public TarLog(AppSettings settings)
{
_settings = settings;
Load();
}
private void Load()
{
if (_settings.TarKeyFile.ToAbsolutePath().FileExists())
{
_tarKeys = Task.Run(async () => await _settings.TarKeyFile.ToAbsolutePath()
.ReadAllLinesAsync()
.Select(line => line.Trim())
.ToHashSetAsync());
}
else
{
_tarKeys = Task.Run(async () => new HashSet<string>());
}
}
public async Task<bool> Contains(string metricsKey)
{
return (await _tarKeys).Contains(metricsKey);
}
}