Skip to content

Commit

Permalink
log to file, secure admin endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Oct 27, 2024
1 parent 97a7bf7 commit 12ee2c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

deploy:
environment:
name: AZ-TEST
name: AZ-Test
url: https://test.rubberduckvba.com
permissions:
contents: none
Expand Down
5 changes: 5 additions & 0 deletions rubberduckvba.Server/Api/Admin/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hangfire;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using rubberduckvba.com.Server.ContentSynchronization;
Expand All @@ -7,13 +8,15 @@
namespace rubberduckvba.com.Server.Api.Admin;


[Authorize("github")]
[ApiController]
public class AdminController(ConfigurationOptions options, IBackgroundJobClient backgroundJob, ILogger<AdminController> logger) : ControllerBase
{
/// <summary>
/// Enqueues a job that updates xmldoc content from the latest release/pre-release tags.
/// </summary>
/// <returns>The unique identifier of the enqueued job.</returns>
[Authorize("github")]
[HttpPost("admin/update/xmldoc")]
public async ValueTask<IActionResult> UpdateXmldocContent()
{
Expand All @@ -28,6 +31,7 @@ public async ValueTask<IActionResult> UpdateXmldocContent()
/// Enqueues a job that gets the latest release/pre-release tags and their respective assets, and updates the installer download stats.
/// </summary>
/// <returns>The unique identifier of the enqueued job.</returns>
[Authorize("github")]
[HttpPost("admin/update/tags")]
public async ValueTask<IActionResult> UpdateTagMetadata()
{
Expand All @@ -38,6 +42,7 @@ public async ValueTask<IActionResult> UpdateTagMetadata()
return await ValueTask.FromResult(Ok(jobId));
}

[Authorize("github")]
[HttpGet("admin/config/current")]
public async ValueTask<IActionResult> Config()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,28 @@ public void TraceBlockCompletion(IDataflowBlock block, string name)
break;
}
Logger.LogTrace(Context.Parameters, $"{GetType().Name} | ✅ Dataflow block completion task completed | {name} ({block.GetType().Name}) | {status}");
})
.ContinueWith(t =>
{
var details = Blocks.Select(
block => $"{(block.Value.Completion.IsCompletedSuccessfully ? "✔️" :
block.Value.Completion.IsFaulted ? "✖️" :
block.Value.Completion.IsCanceled ? "" : "🕑")} {block.Key} : {block.Value.Completion.Status}");
Logger.LogTrace(Context.Parameters, "Pipeline block completion status details" + Environment.NewLine + string.Join(Environment.NewLine, details));
}));
}).ContinueWith(t =>
{
var details = Blocks.Select(
block => $"{(
block.Value.Completion.IsCompletedSuccessfully ? "✔️"
: block.Value.Completion.IsFaulted ? "✖️"
: block.Value.Completion.IsCanceled ? ""
: "🕑")} {block.Key} : {block.Value.Completion.Status}");
Logger.LogTrace(Context.Parameters, "Pipeline block completion status details" + Environment.NewLine + string.Join(Environment.NewLine, details));
}));
}

public void FaultPipelineBlock(IDataflowBlock block, Exception exception)
{
block.Fault(exception);
Logger.LogWarning(Context.Parameters, $"{GetType().Name} | ⚠️ Block ({block.GetType().Name}) was faulted");

if (Exception is null)
{
Exception = exception;
Result.AddException(exception);

block.Fault(exception);
Logger.LogWarning(Context.Parameters, $"{GetType().Name} | ⚠️ Block ({block.GetType().Name}) was faulted");

try
{
Logger.LogWarning(Context.Parameters, $"{GetType().Name} | ⚠️ Cancelling token source");
Expand All @@ -77,6 +78,10 @@ public void FaultPipelineBlock(IDataflowBlock block, Exception exception)
Logger.LogWarning(Context.Parameters, $"{GetType().Name} | ⚠️ Token source disposed: cancellation already happened");
}
}
else
{
Exception = new AggregateException([Exception, exception]);
}
}

protected void ThrowIfCancellationRequested() => Token.ThrowIfCancellationRequested();
Expand Down
10 changes: 10 additions & 0 deletions rubberduckvba.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ private static void ConfigureLogging(IServiceCollection services)
(NLog.LogLevel MinLevel, string, Target Target)[] targets = [
(NLog.LogLevel.Trace, "*", new DebuggerTarget("DebuggerLog")),
(NLog.LogLevel.Info, "rubberduckvba.*", new FileTarget("FileLog")
{
FileName = "rubberduckvba.Server.log",
DeleteOldFileOnStartup = true,
CreateDirs = true,
ArchiveEvery = FileArchivePeriod.Day,
ArchiveOldFileOnStartup = true,
EnableFileDelete = true,
MaxArchiveDays = 10,
}),
(NLog.LogLevel.Error, "*", new EventLogTarget("EventLog")
{
Source = "rubberduckvba.Server",
Expand Down
2 changes: 1 addition & 1 deletion rubberduckvba.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"AutoRetryAttempts": 2,
"AutoRetryDelaySeconds": [ 2, 5, 10, 30, 60 ],
"HeartbeatIntervalMinutes": 1,
"CancellationCheckIntervalSeconds": 5,
"CancellationCheckIntervalSeconds": 300,
"ConfigureHangfireServerOnStartup": true,
"CreateUpdateInstallerDownloadsJob": true,
"CreateUpdateXmldocContentJob": true,
Expand Down

0 comments on commit 12ee2c6

Please sign in to comment.