public EncryptedMessageDeserialiser(DataProtectionConfiguration configuration) { var provider = DataProtectionProvider.Create(configuration); _protector = provider.CreateProtector("PatLite"); _newtonsoftMessageDeserialiser = new NewtonsoftMessageDeserialiser(); }
public static IServiceCollection AddBasePatLiteServices(this IServiceCollection services, IConfiguration configuration) { var senderSettings = new PatSenderSettings(); var subscriberConfiguration = new SubscriberConfiguration(); var dataProtectionConfiguration = new DataProtectionConfiguration(); configuration.GetSection("PatLite:Sender").Bind(senderSettings); configuration.GetSection("PatLite:Subscriber").Bind(subscriberConfiguration); configuration.GetSection("DataProtection").Bind(dataProtectionConfiguration); services.AddPatLite(subscriberConfiguration) .AddTransient <IEncryptedMessagePublisher>( provider => new EncryptedMessagePublisher( provider.GetRequiredService <IMessageSender>(), dataProtectionConfiguration, provider.GetRequiredService <MessageProperties>())) .AddPatSenderNetCoreLogAdapter() .AddTransient <IMessageSender, MessageSender>() .AddSingleton <IMessageGenerator, MessageGenerator>() .AddSingleton <MessageProperties, MessageProperties>() .AddSingleton(senderSettings) .AddSingleton <ICorrelationIdProvider, NewCorrelationIdProvider>() .AddTransient <IMessagePublisher>(provider => new MessagePublisher( provider.GetRequiredService <IMessageSender>(), provider.GetRequiredService <IMessageGenerator>(), GetAnnotatedMessageProperties(provider) )); return(services); }
/// <summary> /// Returns the EncryptedMessageDeserialiser for encrypted messages otherwise returns default NewtonsoftMessageDeserialiser /// </summary> /// <param name="dataProtectionConfiguration">Settings describing the keys to use for encryption / description</param> /// <returns>Factory method for obtaining appropriate deserialiser for the message</returns> public static Func <IServiceProvider, IMessageDeserialiser> EncryptedMessageDeserialiser( DataProtectionConfiguration dataProtectionConfiguration) { return(provider => provider.GetService <MessageContext>().MessageEncrypted ? new EncryptedMessageDeserialiser(dataProtectionConfiguration) : (IMessageDeserialiser) new NewtonsoftMessageDeserialiser()); }
public static IContainer Initialize(IConfigurationRoot configuration) { var senderSettings = new PatSenderSettings(); configuration.GetSection("PatLite:Sender").Bind(senderSettings); var subscriberConfiguration = new SubscriberConfiguration(); configuration.GetSection("PatLite:Subscriber").Bind(subscriberConfiguration); var statisticsConfiguration = new StatisticsReporterConfiguration(); configuration.GetSection("StatsD").Bind(statisticsConfiguration); var dataProtectionConfiguration = new DataProtectionConfiguration(); configuration.GetSection("DataProtection").Bind(dataProtectionConfiguration); var statsReporter = new StatisticsReporter(statisticsConfiguration); var loggerName = "IntegrationLogger"; Logging.InitLogger(loggerName); var container = new Container(x => { x.AddRegistry(new PatLiteRegistry(new PatLiteOptions { SubscriberConfiguration = subscriberConfiguration, RegisterDefaultLoggerWithName = "Pat" })); }); container.Configure(x => { x.Scan(scanner => { scanner.WithDefaultConventions(); scanner.AssemblyContainingType <IMessagePublisher>(); }); x.For <IStatisticsReporter>().Use(statsReporter); x.For <ICorrelationIdProvider>().Use(new LiteralCorrelationIdProvider(Guid.NewGuid().ToString())); x.For <IMessageDeserialiser>().Use(ctx => ctx.GetInstance <MessageContext>().MessageEncrypted ? new EncryptedMessageDeserialiser(ctx.GetInstance <DataProtectionConfiguration>()) : (IMessageDeserialiser) new NewtonsoftMessageDeserialiser()); x.For <PatSenderSettings>().Use(senderSettings); x.For <MessageReceivedNotifier <TestEvent> >().Use(new MessageReceivedNotifier <TestEvent>()); x.For <DataProtectionConfiguration>().Use(dataProtectionConfiguration); x.For <ILog>().Use(LogManager.GetLogger(loggerName, loggerName)); x.For <ILoggerFactory>().Use(context => new LoggerFactory()); }); return(container); }
public static IServiceCollection Initialize(IConfigurationRoot configuration) { var senderSettings = new PatSenderSettings(); configuration.GetSection("PatLite:Sender").Bind(senderSettings); var subscriberConfiguration = new SubscriberConfiguration(); configuration.GetSection("PatLite:Subscriber").Bind(subscriberConfiguration); var statisticsConfiguration = new StatisticsReporterConfiguration(); configuration.GetSection("StatsD").Bind(statisticsConfiguration); var dataProtectionConfiguration = new DataProtectionConfiguration(); configuration.GetSection("DataProtection").Bind(dataProtectionConfiguration); var loggerName = "IntegrationLogger-DotNetIoC"; Logging.InitLogger(loggerName); var serviceCollection = new ServiceCollection() .AddSingleton(senderSettings) .AddSingleton(subscriberConfiguration) .AddSingleton(statisticsConfiguration) .AddSingleton(dataProtectionConfiguration) .AddSingleton <IMessageGenerator, MessageGenerator>() .AddSingleton <MessageReceivedNotifier <TestEvent> >() .AddTransient <IEncryptedMessagePublisher>( provider => new EncryptedMessagePublisher( provider.GetRequiredService <IMessageSender>(), provider.GetRequiredService <DataProtectionConfiguration>(), new MessageProperties(Guid.NewGuid().ToString()))) .AddTransient <IMessagePublisher>( provider => new MessagePublisher( provider.GetRequiredService <IMessageSender>(), provider.GetRequiredService <IMessageGenerator>(), new MessageProperties(Guid.NewGuid().ToString()))) .AddTransient <IMessageSender, MessageSender>() .AddTransient <IStatisticsReporter, StatisticsReporter>() .AddLogging(b => b.AddDebug()) .AddTransient <ILog>(s => LogManager.GetLogger(loggerName, loggerName)) .AddPatLite(new PatLiteOptions { MessageDeserialiser = provider => provider.GetService <MessageContext>().MessageEncrypted ? new EncryptedMessageDeserialiser(provider.GetService <DataProtectionConfiguration>()) : (IMessageDeserialiser) new NewtonsoftMessageDeserialiser(), SubscriberConfiguration = subscriberConfiguration }) .AddHandlersFromAssemblyContainingType <DotNetIoC>(); return(serviceCollection); }
public EncryptedMessageGenerator(DataProtectionConfiguration configuration) { var provider = DataProtection.DataProtectionProvider.Create(configuration); _dataProtector = provider.CreateProtector("PatLite"); }
public EncryptedMessagePublisher(IMessageSender messageSender, DataProtectionConfiguration configuration, MessageProperties defaultMessageProperties) : base(messageSender, new EncryptedMessageGenerator(configuration), defaultMessageProperties) { }
public static void AddDataProtection <TDbContext>(this IServiceCollection services, DataProtectionConfiguration dataProtectionConfiguration, AzureKeyVaultConfiguration azureKeyVaultConfiguration) where TDbContext : DbContext, IDataProtectionKeyContext { var dataProtectionBuilder = services.AddDataProtection() .SetApplicationName("Skoruba.IdentityServer4") .PersistKeysToDbContext <TDbContext>(); if (dataProtectionConfiguration.ProtectKeysWithAzureKeyVault) { if (azureKeyVaultConfiguration.UseClientCredentials) { dataProtectionBuilder.ProtectKeysWithAzureKeyVault( new Uri(azureKeyVaultConfiguration.DataProtectionKeyIdentifier), new ClientSecretCredential(azureKeyVaultConfiguration.TenantId, azureKeyVaultConfiguration.ClientId, azureKeyVaultConfiguration.ClientSecret)); } else { dataProtectionBuilder.ProtectKeysWithAzureKeyVault(new Uri(azureKeyVaultConfiguration.DataProtectionKeyIdentifier), new DefaultAzureCredential()); } } }
public static void AddDataProtection <TDbContext>(this IServiceCollection services, DataProtectionConfiguration dataProtectionConfiguration, AzureKeyVaultConfiguration azureKeyVaultConfiguration) where TDbContext : DbContext, IDataProtectionKeyContext { var dataProtectionBuilder = services.AddDataProtection() .SetApplicationName("Skoruba.IdentityServer4") .PersistKeysToDbContext <TDbContext>(); if (dataProtectionConfiguration.ProtectKeysWithAzureKeyVault) { if (azureKeyVaultConfiguration.UseClientCredentials) { dataProtectionBuilder.ProtectKeysWithAzureKeyVault(azureKeyVaultConfiguration.DataProtectionKeyIdentifier, azureKeyVaultConfiguration.ClientId, azureKeyVaultConfiguration.ClientSecret); } else { var azureServiceTokenProvider = new AzureServiceTokenProvider(); var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback)); dataProtectionBuilder.ProtectKeysWithAzureKeyVault(keyVaultClient, azureKeyVaultConfiguration.DataProtectionKeyIdentifier); } } }