Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions SS14.Labeller/HealthChecks/GitHubApiHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using SS14.Labeller.GitHubApi;

namespace SS14.Labeller.HealthChecks;

public class GitHubApiKeyHealthCheck(IHttpClientFactory httpClientFactory, IMemoryCache cache) : IHealthCheck
{
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default
)
{
var result = await cache.GetOrCreateAsync(
"GHKeyHealthCheck",
async entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5);
return await CheckHealthAsyncInternal(cancellationToken);
}
);

return result;
}

private async Task<HealthCheckResult> CheckHealthAsyncInternal(CancellationToken cancellationToken)
{
try
{
var client = httpClientFactory.CreateClient(nameof(IGitHubApiClient));

// Make a simple, low-impact request to verify the key
var response = await client.GetAsync("https://api.github.com/user", cancellationToken);

if (response.IsSuccessStatusCode)
{
return HealthCheckResult.Healthy("GitHub API key is valid.");
}

if (response.StatusCode is System.Net.HttpStatusCode.Unauthorized or System.Net.HttpStatusCode.Forbidden)
{
return HealthCheckResult.Unhealthy("GitHub API key is invalid or lacks necessary permissions.");
}

return HealthCheckResult.Degraded($"GitHub API returned an unexpected status code: {response.StatusCode}");
}
catch (HttpRequestException ex)
{
return HealthCheckResult.Unhealthy("Failed to connect to GitHub API", exception: ex);
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy("An unexpected error occurred during GitHub API key health check", exception: ex);
}
Comment on lines +30 to +54

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing ex.Message just pass ex itself so it also includes the full stacktrace. (Considering this endpoint is supposed to be internal)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that, but serializer that we are using (which is the only available by default w/ NAOT) is not going to show anything anyways, not even that text - just healthy/unhealthy. So probably fine by now. Later tho i would better like that thing blocking requests from the outside on the level of reverse proxy.

}
}
9 changes: 3 additions & 6 deletions SS14.Labeller/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SS14.Labeller.Configuration;
using SS14.Labeller.Endpoints;
using SS14.Labeller.Handlers;
using SS14.Labeller.Middlewares;
using SS14.Labeller.Models;

[module:DapperAot]

Expand All @@ -31,6 +25,9 @@ public static void Main(string[] args)
app.UseHttpLogging();

app.MapGet("/", () => Results.Ok("Nik is a cat!"));

app.MapHealthChecks("/health");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you gate this behind some form of auth? Maybe a basic-auth check that is configured via appsettings? We wouldn't want people spamming this endpoint and getting us ratelimited that way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smort! true.
actually i was not expecting to have this in open field
hmm. i need to do cache for gh status, so we won't re-request it too often anyways!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache added, i propose leave it as is - having basic auth just for that endpoint seems off.


app.MapGithubWebhook();

app.Run();
Expand Down
9 changes: 9 additions & 0 deletions SS14.Labeller/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SS14.Labeller.Labelling;
using SS14.Labeller.Repository;
using System.Net.Http.Headers;
using SS14.Labeller.HealthChecks;
using Polly;
using Polly.Extensions.Http;

Expand Down Expand Up @@ -80,6 +81,14 @@ public static void RegisterDependencies(this IServiceCollection service, IConfig
sp => sp.GetServices<RequestHandlerBase>()
.ToDictionary(x => x.CanHandleType)
);

service.AddHealthChecks()
.AddCheck<GitHubApiKeyHealthCheck>(
"GitHub API Key",
tags: ["github", "api-key"]
);

service.AddMemoryCache();
}

private static IAsyncPolicy<HttpResponseMessage> GetDiscourseRetryPolicy(IServiceProvider sp)
Expand Down
Loading