-
Notifications
You must be signed in to change notification settings - Fork 119
[WIP] Add Keycloak with Postgres integration #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
max2020204
wants to merge
30
commits into
CommunityToolkit:main
Choose a base branch
from
max2020204:KeycloakPostgres
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0df225d
**Add Keycloak with Postgres integration**
max2020204 2e07fa8
Refactor Keycloak Postgres extension for improved configuration
max2020204 1bf7ea7
Merge branch 'main' into KeycloakPostgres
max2020204 90621f4
Refactor Keycloak Postgres extension for streamlined configuration
max2020204 7a4fc17
Update src/CommunityToolkit.Aspire.Keycloak.Postgress/KeycloakPostgre…
max2020204 9b11f66
Refactor Keycloak Postgres extension and restructure project
max2020204 03cbc0a
Merge branch 'main' into KeycloakPostgres
max2020204 53c7ca1
Update src/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres/Keycl…
max2020204 9c913c5
Update src/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres/Keycl…
max2020204 d77acaf
Update src/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres/Keycl…
max2020204 fcfad00
Merge remote-tracking branch 'origin/main' into KeycloakPostgres
max2020204 6cd7d93
Merge branch 'main' into KeycloakPostgres
max2020204 d4720a9
Remove Postgres-specific Keycloak extension and merge functionality i…
max2020204 3962aa1
Merge remote-tracking branch 'origin/KeycloakPostgres' into KeycloakP…
max2020204 beb9fe9
Merge branch 'main' into KeycloakPostgres
max2020204 e01ce34
Update tests/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.Tes…
max2020204 19c6f9a
Refactor Keycloak Postgres extension for code reuse and improved flex…
max2020204 31a647b
Move Keycloak Postgres extension tests into core Keycloak extensions
max2020204 cdd4b15
Merge branch 'main' into KeycloakPostgres
max2020204 1ba736e
Add README and comprehensive tests for Keycloak PostgreSQL integration
max2020204 1efdbbc
Merge remote-tracking branch 'origin/KeycloakPostgres' into KeycloakP…
max2020204 afb6c43
Merge branch 'main' into KeycloakPostgres
max2020204 993aa3e
Merge branch 'main' into KeycloakPostgres
max2020204 6eeee8c
Remove outdated comment in KeycloakExtensionTests regarding server pa…
max2020204 8514cab
Merge remote-tracking branch 'origin/KeycloakPostgres' into KeycloakP…
max2020204 7513876
Add integration tests for Keycloak with Postgres and update project r…
max2020204 8d7af9f
Add Keycloak Postgres example projects and enhance Aspire integration
max2020204 2c66428
Remove unused Moq package reference from Keycloak extension tests pro…
max2020204 d504e4c
Update Keycloak Postgres example projects and dependencies
max2020204 5db02af
Update Aspire package version references to use variable-based approach
max2020204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...erviceDefaults/CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.ServiceDefaults.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireSharedProject>true</IsAspireSharedProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App"/> | ||
|
||
<PackageReference Include="Microsoft.Extensions.Http.Resilience"/> | ||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery"/> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol"/> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting"/> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore"/> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Http"/> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime"/> | ||
</ItemGroup> | ||
|
||
</Project> |
128 changes: 128 additions & 0 deletions
128
...ostgres/CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.ServiceDefaults/Extensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.ServiceDefaults; | ||
|
||
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. | ||
// This project should be referenced by each service project in your solution. | ||
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults | ||
public static class Extensions | ||
{ | ||
private const string HealthEndpointPath = "/health"; | ||
private const string AlivenessEndpointPath = "/alive"; | ||
|
||
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.ConfigureOpenTelemetry(); | ||
|
||
builder.AddDefaultHealthChecks(); | ||
|
||
builder.Services.AddServiceDiscovery(); | ||
|
||
builder.Services.ConfigureHttpClientDefaults(http => | ||
{ | ||
// Turn on resilience by default | ||
http.AddStandardResilienceHandler(); | ||
|
||
// Turn on service discovery by default | ||
http.AddServiceDiscovery(); | ||
}); | ||
|
||
// Uncomment the following to restrict the allowed schemes for service discovery. | ||
// builder.Services.Configure<ServiceDiscoveryOptions>(options => | ||
// { | ||
// options.AllowedSchemes = ["https"]; | ||
// }); | ||
|
||
return builder; | ||
} | ||
|
||
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) | ||
where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.Logging.AddOpenTelemetry(logging => | ||
{ | ||
logging.IncludeFormattedMessage = true; | ||
logging.IncludeScopes = true; | ||
}); | ||
|
||
builder.Services.AddOpenTelemetry() | ||
.WithMetrics(metrics => | ||
{ | ||
metrics.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddRuntimeInstrumentation(); | ||
}) | ||
.WithTracing(tracing => | ||
{ | ||
tracing.AddSource(builder.Environment.ApplicationName) | ||
.AddAspNetCoreInstrumentation(tracing => | ||
// Exclude health check requests from tracing | ||
tracing.Filter = context => | ||
!context.Request.Path.StartsWithSegments(HealthEndpointPath) | ||
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) | ||
) | ||
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) | ||
//.AddGrpcClientInstrumentation() | ||
.AddHttpClientInstrumentation(); | ||
}); | ||
|
||
builder.AddOpenTelemetryExporters(); | ||
|
||
return builder; | ||
} | ||
|
||
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) | ||
where TBuilder : IHostApplicationBuilder | ||
{ | ||
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); | ||
|
||
if (useOtlpExporter) | ||
{ | ||
builder.Services.AddOpenTelemetry().UseOtlpExporter(); | ||
} | ||
|
||
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) | ||
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) | ||
//{ | ||
// builder.Services.AddOpenTelemetry() | ||
// .UseAzureMonitor(); | ||
//} | ||
|
||
return builder; | ||
} | ||
|
||
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) | ||
where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.Services.AddHealthChecks() | ||
// Add a default liveness check to ensure app is responsive | ||
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); | ||
|
||
return builder; | ||
} | ||
|
||
public static WebApplication MapDefaultEndpoints(this WebApplication app) | ||
{ | ||
// Adding health checks endpoints to applications in non-development environments has security implications. | ||
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
// All health checks must pass for app to be considered ready to accept traffic after starting | ||
app.MapHealthChecks(HealthEndpointPath); | ||
|
||
// Only health checks tagged with the "live" tag must pass for app to be considered alive | ||
app.MapHealthChecks(AlivenessEndpointPath, | ||
new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") }); | ||
} | ||
|
||
return app; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
examples/keycloak-postgres/CommunityToolkit.Aspire.Keycloak.Extensions.AppHost/AppHost.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Aspire.Hosting; | ||
using CommunityToolkit.Aspire.Keycloak.Extensions; | ||
|
||
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var postgres = builder.AddPostgres("keycloak-postgres-dev"); | ||
var dbDev = postgres.AddDatabase("db-dev"); | ||
|
||
var keycloakDev = builder.AddKeycloak("keycloak-dev") | ||
.WithPostgres(dbDev); | ||
|
||
var dbUserName = builder.AddParameter("db-username", "postgres"); | ||
var dbPassword = builder.AddParameter("db-password", "Postgres!123"); | ||
|
||
var postgresProd = builder.AddPostgres("postgres-prod", | ||
dbUserName, dbPassword); | ||
|
||
var dbProd = postgresProd.AddDatabase("db-prod"); | ||
|
||
var keycloakProd = builder.AddKeycloak("keycloak-prod") | ||
.WithPostgres(dbProd, dbUserName, dbPassword); | ||
|
||
|
||
builder.AddProject<Projects.CommunityToolkit_Aspire_Keycloak_Extensions_Dev>("project-dev") | ||
.WithReference(keycloakDev) | ||
.WaitFor(keycloakDev); | ||
|
||
builder.AddProject<Projects.CommunityToolkit_Aspire_Keycloak_Extensions_Prod>("project-prod") | ||
.WithReference(keycloakProd) | ||
.WaitFor(keycloakProd); | ||
builder.Build().Run(); |
26 changes: 26 additions & 0 deletions
26
...re.Keycloak.Extensions.AppHost/CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Sdk Name="Aspire.AppHost.Sdk" Version="$(AspireAppHostSdkVersion)"/> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<UserSecretsId>e5e65289-cf91-4bda-b628-28a68fb90841</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting"/> | ||
<PackageReference Include="Aspire.Hosting.AppHost"/> | ||
<PackageReference Include="Aspire.Hosting.Keycloak"/> | ||
<PackageReference Include="Aspire.Hosting.PostgreSQL"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Keycloak.Extensions\CommunityToolkit.Aspire.Keycloak.Extensions.csproj" IsAspireProjectResource="false"/> | ||
<ProjectReference Include="..\CommunityToolkit.Aspire.Keycloak.Extensions.Dev\CommunityToolkit.Aspire.Keycloak.Extensions.Dev.csproj"/> | ||
<ProjectReference Include="..\CommunityToolkit.Aspire.Keycloak.Extensions.Prod\CommunityToolkit.Aspire.Keycloak.Extensions.Prod.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
29 changes: 29 additions & 0 deletions
29
...stgres/CommunityToolkit.Aspire.Keycloak.Extensions.AppHost/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:17044;http://localhost:15216", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21202", | ||
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22139" | ||
} | ||
}, | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:15216", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19292", | ||
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20099" | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...es/keycloak-postgres/CommunityToolkit.Aspire.Keycloak.Extensions.AppHost/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning", | ||
"Aspire.Hosting.Dcp": "Warning" | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...kit.Aspire.Keycloak.Extensions.Dev/CommunityToolkit.Aspire.Keycloak.Extensions.Dev.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<TargetFrameworks /> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Keycloak.Authentication" /> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.ServiceDefaults\CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.ServiceDefaults.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
6 changes: 6 additions & 0 deletions
6
...tyToolkit.Aspire.Keycloak.Extensions.Dev/CommunityToolkit.Aspire.Keycloak.Extensions.http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@CommunityToolkit.Aspire.Keycloak.Extensions_HostAddress = http://localhost:5030 | ||
|
||
GET {{CommunityToolkit.Aspire.Keycloak.Extensions_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
47 changes: 47 additions & 0 deletions
47
examples/keycloak-postgres/CommunityToolkit.Aspire.Keycloak.Extensions.Dev/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using CommunityToolkit.Aspire.Keycloak.Extensions.AppHost.ServiceDefaults; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
builder.AddServiceDefaults(); | ||
|
||
builder.Services.AddAuthentication() | ||
.AddKeycloakJwtBearer("keycloak-dev", "master", jwt => | ||
{ | ||
if (builder.Environment.IsDevelopment()) | ||
{ | ||
//for development only | ||
jwt.RequireHttpsMetadata = false; | ||
} | ||
|
||
|
||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
|
||
app.UseHttpsRedirection(); | ||
|
||
var summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
app.MapGet("/weatherforecast", () => | ||
{ | ||
var forecast = Enumerable.Range(1, 5).Select(index => | ||
new WeatherForecast | ||
( | ||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
Random.Shared.Next(-20, 55), | ||
summaries[Random.Shared.Next(summaries.Length)] | ||
)) | ||
.ToArray(); | ||
return forecast; | ||
}) | ||
.WithName("GetWeatherForecast"); | ||
|
||
app.Run(); | ||
|
||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | ||
{ | ||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to introduce a MSBuild variable for the preview version of Aspire packages.