Skip to content

Commit

Permalink
Pass certificate into MQTT client service
Browse files Browse the repository at this point in the history
  • Loading branch information
willsawyerrrr committed Oct 11, 2024
1 parent 3b7210d commit 60da924
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 3 additions & 4 deletions Mortein/Mqtt/Extensions/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ namespace Mortein.Mqtt.Extensions;
/// </summary>
public static class ServiceCollectionExtension
{
// TODO: read from somewhere other than the local filesystem.
private static readonly X509Certificate2 certificate = new("/workspaces/api/api.pfx");

/// <summary>
/// Registers a hosted MQTT client as a service in the <see cref="IServiceCollection" />.
/// </summary>
///
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
///
/// <param name="certificate">The <see cref="X509Certificate2"/> with which to authenticate.</param>
///
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddMqttClientHostedService(this IServiceCollection services)
public static IServiceCollection AddMqttClientHostedService(this IServiceCollection services, X509Certificate2 certificate)
{
services.AddMqttClientServiceWithConfig(optionsBuilder =>
{
Expand Down
9 changes: 8 additions & 1 deletion Mortein/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Mortein.Types;
using NodaTime.Serialization.SystemTextJson;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;

namespace Mortein;

Expand All @@ -20,6 +21,12 @@ public class Startup(IConfiguration configuration)
/// </summary>
public IConfiguration Configuration { get; } = configuration;

// TODO: read from somewhere other than the local filesystem.
private static X509Certificate2 GetAwsMqttCertificate()
{
return new("/workspaces/api/api.pfx");
}

/// <summary>
/// Add services to the container.
/// </summary>
Expand All @@ -37,7 +44,7 @@ public void ConfigureServices(IServiceCollection services)
});
services.AddDbContext<DatabaseContext>();
services.AddEndpointsApiExplorer();
services.AddMqttClientHostedService();
services.AddMqttClientHostedService(GetAwsMqttCertificate());
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("openapi", new OpenApiInfo()
Expand Down

0 comments on commit 60da924

Please sign in to comment.