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

View File

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

View File

@ -90,7 +90,7 @@ public class MetricsController : ControllerBase
[HttpGet] [HttpGet]
[Route("dump")] [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(); var parser = new Parser();
@ -100,9 +100,12 @@ public class MetricsController : ControllerBase
var fromDate = parser.Parse(from).Start; var fromDate = parser.Parse(from).Start;
var records = _metricsStore.GetRecords(fromDate!.Value, toDate!.Value, action); var records = _metricsStore.GetRecords(fromDate!.Value, toDate!.Value, action);
Response.Headers.ContentType = "application/json"; Response.Headers.ContentType = "application/json";
await foreach (var record in records) await foreach (var record in records)
{ {
if (!string.IsNullOrWhiteSpace(subject) && !record.Subject.Contains(subject))
continue;
await JsonSerializer.SerializeAsync(Response.Body, record); await JsonSerializer.SerializeAsync(Response.Body, record);
await Response.Body.WriteAsync(EOL); await Response.Body.WriteAsync(EOL);

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);
}
}