Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit 3d2421a

Browse files
committed
Application Insights
MySql Provider
1 parent fb8faec commit 3d2421a

46 files changed

Lines changed: 7731 additions & 132 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Backend/Jp.Infra.CrossCutting.Identity/Jp.Infra.CrossCutting.Identity.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<Compile Remove="Migrations\**" />
9+
<EmbeddedResource Remove="Migrations\**" />
10+
<None Remove="Migrations\**" />
11+
</ItemGroup>
12+
713
<ItemGroup>
814
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
915
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.1.3" />
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
1116
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.2">
1217
<PrivateAssets>all</PrivateAssets>
1318
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Jp.Infra.CrossCutting.Identity.Entities.Identity;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace Jp.Infra.CrossCutting.IdentityServer.Configuration
9+
{
10+
public static class IdentityServerConfig
11+
{
12+
public static IIdentityServerBuilder AddIdentityServer(this IServiceCollection services,
13+
IConfiguration configuration, IHostingEnvironment environment, ILogger logger)
14+
{
15+
16+
var builder = services.AddIdentityServer(
17+
options =>
18+
{
19+
options.Events.RaiseErrorEvents = true;
20+
options.Events.RaiseInformationEvents = true;
21+
options.Events.RaiseFailureEvents = true;
22+
options.Events.RaiseSuccessEvents = true;
23+
})
24+
.AddAspNetIdentity<UserIdentity>();
25+
26+
builder.AddSigninCredentialFromConfig(configuration.GetSection("CertificateOptions"), logger, environment);
27+
28+
return builder;
29+
}
30+
31+
}
32+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System.IO;
2+
using System.Security.Cryptography.X509Certificates;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace Jp.Infra.CrossCutting.IdentityServer.Configuration
9+
{
10+
/// <summary>
11+
/// Impl of adding a signin key for identity server 4,
12+
/// with an appsetting.json configuration look similar to:
13+
/// "SigninKeyCredentials": {
14+
/// "KeyType": "KeyFile",
15+
/// "FileName": "C:\\certificates\\idsv4.pfx",
16+
/// "KeyStorePath": ""
17+
/// }
18+
/// </summary>
19+
public static class SigninCredentialExtension
20+
{
21+
private const string File = nameof(File);
22+
private const string Store = nameof(Store);
23+
private const string Temporary = nameof(Temporary);
24+
private const string Environment = nameof(Environment);
25+
26+
private const string FileName = nameof(FileName);
27+
private const string FilePassword = nameof(FilePassword);
28+
private const string KeyStoreIssuer = nameof(KeyStoreIssuer);
29+
30+
public static IIdentityServerBuilder AddSigninCredentialFromConfig(
31+
this IIdentityServerBuilder builder, IConfigurationSection options, ILogger logger, IHostingEnvironment env)
32+
{
33+
string keyType = System.Environment.GetEnvironmentVariable("CERTIFICATE_TYPE");
34+
logger.LogInformation($"SigninCredentialExtension keyType is {keyType}");
35+
36+
switch (keyType)
37+
{
38+
case Temporary:
39+
logger.LogInformation($"SigninCredentialExtension adding Temporary Signing Credential");
40+
builder.AddDeveloperSigningCredential(true);
41+
break;
42+
43+
case File:
44+
AddCertificateFromFile(builder, options, logger, env);
45+
break;
46+
47+
case Store:
48+
AddCertificateFromStore(builder, options, logger);
49+
break;
50+
51+
case Environment:
52+
AddCertificateFromEnvironment(builder, logger);
53+
break;
54+
}
55+
56+
return builder;
57+
}
58+
59+
private static void AddCertificateFromEnvironment(IIdentityServerBuilder builder, ILogger logger)
60+
{
61+
logger.LogInformation("Taking Certificate from Environment");
62+
var file = System.Environment.GetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path");
63+
var password = System.Environment.GetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Password");
64+
if (System.IO.File.Exists(file))
65+
{
66+
logger.LogInformation("Taked Certificate from Environment");
67+
builder.AddSigningCredential(new X509Certificate2(file, password, X509KeyStorageFlags.MachineKeySet));
68+
}
69+
else
70+
{
71+
logger.LogError($"SigninCredentialExtension cannot find key file {System.Environment.GetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path")}");
72+
}
73+
}
74+
75+
private static void AddCertificateFromStore(IIdentityServerBuilder builder,
76+
IConfigurationSection options, ILogger logger)
77+
{
78+
var keyIssuer = options.GetValue<string>(KeyStoreIssuer);
79+
logger.LogInformation($"SigninCredentialExtension adding key from store by {keyIssuer}");
80+
81+
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
82+
store.Open(OpenFlags.ReadOnly);
83+
84+
var certificates = store.Certificates.Find(X509FindType.FindByIssuerName, keyIssuer, true);
85+
86+
if (certificates.Count > 0)
87+
builder.AddSigningCredential(certificates[0]);
88+
else
89+
logger.LogError("A matching key couldn't be found in the store");
90+
}
91+
92+
private static void AddCertificateFromFile(IIdentityServerBuilder builder,
93+
IConfigurationSection options, ILogger logger, IHostingEnvironment env)
94+
{
95+
var keyFileName = options.GetValue<string>(FileName);
96+
var keyFilePassword = options.GetValue<string>(FilePassword);
97+
98+
if (System.IO.File.Exists(Path.Combine(env.ContentRootPath, keyFileName)))
99+
{
100+
logger.LogInformation($"SigninCredentialExtension adding key from file {keyFileName}");
101+
builder.AddSigningCredential(new X509Certificate2(Path.Combine(env.ContentRootPath, keyFileName), keyFilePassword, X509KeyStorageFlags.MachineKeySet));
102+
}
103+
else
104+
{
105+
logger.LogError($"SigninCredentialExtension cannot find key file {keyFileName}");
106+
}
107+
}
108+
}
109+
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<Compile Remove="Migrations\**" />
9+
<EmbeddedResource Remove="Migrations\**" />
10+
<None Remove="Migrations\**" />
11+
</ItemGroup>
12+
13+
14+
<ItemGroup>
15+
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="2.1.0" />
816
<PackageReference Include="IdentityServer4.EntityFramework" Version="2.1.1" />
9-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
1017
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.2">
1118
<PrivateAssets>all</PrivateAssets>
1219
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1320
</PackageReference>
1421
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
1522
</ItemGroup>
1623

24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\Jp.Infra.CrossCutting.Identity\Jp.Infra.CrossCutting.Identity.csproj" />
27+
</ItemGroup>
28+
1729
</Project>

src/Backend/Jp.Infra.Data/Jp.Infra.Data.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
8+
79
<ItemGroup>
8-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.2" />
911
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.2">
11-
<PrivateAssets>all</PrivateAssets>
12-
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
13-
</PackageReference>
12+
1413
</ItemGroup>
1514

1615
<ItemGroup>

src/Backend/Jp.UserManagement/Configuration/IdentityConfig.cs renamed to src/Backend/Jp.Infra.Migrations.MySql.Identity/Configuration/IdentityConfig.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using Jp.Infra.CrossCutting.Identity.Context;
34
using Jp.Infra.CrossCutting.Identity.Entities.Identity;
45
using Jp.Infra.Data.Context;
@@ -7,17 +8,18 @@
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
910

10-
namespace Jp.UserManagement.Configuration
11+
namespace Jp.Infra.Migrations.MySql.Identity.Configuration
1112
{
1213
public static class IdentityConfig
1314
{
14-
public static IServiceCollection AddIdentity(this IServiceCollection services, IConfiguration configuration)
15+
public static IServiceCollection AddIdentityMySql(this IServiceCollection services, IConfiguration configuration)
1516
{
16-
var connectionString = Environment.GetEnvironmentVariable("SQLSERVER_CONNECTION") ?? configuration.GetConnectionString("SSOConnection");
17+
var connectionString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION") ?? configuration.GetConnectionString("SSOConnection");
18+
var migrationsAssembly = typeof(IdentityConfig).GetTypeInfo().Assembly.GetName().Name;
1719

18-
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
19-
services.AddDbContext<JpContext>(options => options.UseSqlServer(connectionString));
20-
services.AddDbContext<EventStoreSQLContext>(options => options.UseSqlServer(connectionString));
20+
services.AddEntityFrameworkMySql().AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));
21+
services.AddDbContext<JpContext>(options => options.UseMySql(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));
22+
services.AddDbContext<EventStoreSQLContext>(options => options.UseMySql(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));
2123

2224
services.AddIdentity<UserIdentity, UserIdentityRole>()
2325
.AddEntityFrameworkStores<ApplicationDbContext>()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Jp.Infra.CrossCutting.Identity\Jp.Infra.CrossCutting.Identity.csproj" />
17+
<ProjectReference Include="..\Jp.Infra.Data\Jp.Infra.Data.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)