Skip to content

Commit 04de150

Browse files
axies20AlirexaaCopilotdavidfowlaaronpowell
authored
Add Keycloak with Postgres integration (#811)
* **Add Keycloak with Postgres integration** - Introduced a new project `CommunityToolkit.Aspire.Keycloak.Postgress` for configuring Keycloak with PostgreSQL. - Added an extension method to simplify Keycloak and Postgres database resource integration. - Updated solution and package references for Aspire dependencies. * Refactor Keycloak Postgres extension for improved configuration - Enhanced parameter descriptions in XML documentation. - Updated method to support asynchronous operations. - Introduced additional environment variables for finer-grained database configuration. - Added cancellation token support for improved task management. * Refactor Keycloak Postgres extension for streamlined configuration - Simplified XML documentation and parameter descriptions. - Removed exceptions for parent resource validation. - Replaced manual environment variable setup with connection string parsing using `NpgsqlConnectionStringBuilder`. - Introduced optional port parameter for database configuration. * Update src/CommunityToolkit.Aspire.Keycloak.Postgress/KeycloakPostgresExtension.cs Co-authored-by: Alireza Baloochi <alireza.baloochi1380@gmail.com> * Refactor Keycloak Postgres extension and restructure project - Renamed `CommunityToolkit.Aspire.Keycloak.Postgress` to `CommunityToolkit.Aspire.Keycloak.Extensions.Postgres`. - Refactored methods to improve flexibility, including support for different configurations (e.g., development, credentials). - Introduced a dedicated test project `CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.Tests`. - Updated solution, dependencies, and project references accordingly. * Update src/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres/KeycloakPostgresExtension.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres/KeycloakPostgresExtension.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres/KeycloakPostgresExtension.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Remove Postgres-specific Keycloak extension and merge functionality into core Keycloak extensions - Deleted `CommunityToolkit.Aspire.Keycloak.Extensions.Postgres`. - Integrated Postgres configuration methods into `CommunityToolkit.Aspire.Keycloak.Extensions`. - Updated project and solution references to reflect the restructuring. - Standardized package versioning using `AspireVersion`. * Update tests/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.Tests/CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.Tests.csproj Co-authored-by: David Fowler <davidfowl@gmail.com> * Refactor Keycloak Postgres extension for code reuse and improved flexibility - Extracted `WithPostgresData` helper method to reduce redundant code. - Improved default credential handling for Username and Password parameters. - Updated XML documentation for clarity and consistency. * Move Keycloak Postgres extension tests into core Keycloak extensions - Removed `CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.Tests`. - Added `WithPostgres` tests to `CommunityToolkit.Aspire.Keycloak.Extensions.Tests`. - Updated solution and project references to reflect restructuring. * Add README and comprehensive tests for Keycloak PostgreSQL integration - Introduced detailed `README.md` explaining the Keycloak PostgreSQL Aspire extension's features, usage, and API. - Added extensive unit tests in `KeycloakExtensionTests` for validating default credentials, explicit parameters, server parameters, and XA transaction enablement. - Refactored `WithPostgres` method to streamline environment variable configuration and enhance maintainability. * Remove outdated comment in KeycloakExtensionTests regarding server parameter expectations * Add integration tests for Keycloak with Postgres and update project references - Introduced `KeycloakWithPostgresIntegrationTest` for verifying Keycloak's behavior with PostgreSQL. - Updated `WithPostgres` method to include a wait condition for database readiness. - Added project reference to `CommunityToolkit.Aspire.Testing` for testing utilities. * Add Keycloak Postgres example projects and enhance Aspire integration - Added example projects under `examples/keycloak-postgres` for showcasing Keycloak integration with PostgreSQL in development and production environments. - Introduced `AppHost` projects with service defaults for common functionalities like OpenTelemetry, resilience, and health checks. - Updated `CommunityToolkit.Aspire.Keycloak.Extensions` with a description and additional package tags for better discoverability. - Registered the new example projects in the solution file. * Remove unused Moq package reference from Keycloak extension tests project * Update Keycloak Postgres example projects and dependencies - Added conditional handling for `RequireHttpsMetadata` in development environments for both Dev and Prod projects. - Updated `Aspire.Hosting` and `Aspire.Keycloak.Authentication` package versions to 9.4.2. - Standardized XML formatting across example project files. * Update Aspire package version references to use variable-based approach - Switched `Aspire.Hosting` and related package versions to reference `$(AspireVersion)` for consistency. - Downgraded `Aspire.Keycloak.Authentication` from `9.4.2-preview.1.25428.12` to `9.4.1-preview.1.25408.4`. * Update Directory.Packages.props and Keycloak tests for new package versions and refactoring * Rename Keycloak Extensions and Examples to use "Hosting" for consistency - Updated namespace and project names from `CommunityToolkit.Aspire.Keycloak.Extensions` to `CommunityToolkit.Aspire.Keycloak.Hosting.Extensions`. - Adjusted all associated file paths, project references, and solution entries. - Renamed example projects under `examples/keycloak-postgres` to align with the namespace changes. - Verified updates in integration and unit test coverage. * Rename Keycloak Extensions to "Hosting" for consistency - Updated namespaces, project names, and file paths from `CommunityToolkit.Aspire.Keycloak.Extensions` to `CommunityToolkit.Aspire.Keycloak.Hosting.Extensions`. - Adjusted test projects, README files, and integration tests accordingly. - Verified alignment with example projects and ensured test coverage. * Update Keycloak PostgreSQL extensions for hosting model compliance - Refactored namespaces to align with `Aspire.Hosting`. - Simplified parameter handling in `WithPostgres` methods. - Updated README with adjusted examples and clarified usage notes. - Enhanced project metadata and descriptions for consistency. - Added missing project references in test projects to ensure coverage. * Enhance XML docs for `WithPostgres` methods and fix project references - Extended and clarified XML documentation for `WithPostgres` methods to improve developer understanding. - Removed unused `<TargetFrameworks />` from example project files. - Corrected project references in `AppHost` to align with the updated namespace and file paths. - Cleaned up unused `using` directives in `AppHost.cs`. * Rename Keycloak Hosting Extensions to Aspire.Hosting for consistency - Updated namespaces, project names, and file paths to align with `Aspire.Hosting` naming convention. - Renamed example projects in `examples/keycloak-postgres` to reflect changes. - Corrected project references, solution entries, and adjusted test projects accordingly. - Simplified `WithPostgres` methods with optional username and password handling. * Fixing the naming again and some build issues * Adding the tests in * Fixing more bad changes * Dealing with more build errors --------- Co-authored-by: Alireza Baloochi <alireza.baloochi1380@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: David Fowler <davidfowl@gmail.com> Co-authored-by: Aaron Powell <me@aaron-powell.com>
1 parent 4958e4a commit 04de150

26 files changed

+806
-1
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
Hosting.GoFeatureFlag.Tests,
3434
Hosting.Golang.Tests,
3535
Hosting.Java.Tests,
36+
Hosting.Keycloak.Extensions.Tests,
3637
Hosting.k6.Tests,
3738
Hosting.LavinMQ.Tests,
3839
Hosting.MailPit.Tests,

CommunityToolkit.Aspire.slnx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<Project Path="examples/k6/CommunityToolkit.Aspire.Hosting.k6.AppHost/CommunityToolkit.Aspire.Hosting.k6.AppHost.csproj" />
5959
<Project Path="examples/k6/CommunityToolkit.Aspire.Hosting.k6.ServiceDefaults/CommunityToolkit.Aspire.Hosting.k6.ServiceDefaults.csproj" />
6060
</Folder>
61+
<Folder Name="/examples/keycloak-postgres/">
62+
<Project Path="examples\keycloak-postgres\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.AppHost\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.AppHost.csproj" Type="C#" />
63+
<Project Path="examples\keycloak-postgres\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Dev\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Dev.csproj" Type="C#" />
64+
<Project Path="examples\keycloak-postgres\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Prod\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Prod.csproj" Type="C#" />
65+
<Project Path="examples\keycloak-postgres\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.ServiceDefaults\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.ServiceDefaults.csproj" Type="C#" />
66+
</Folder>
6167
<Folder Name="/examples/lavinmq/">
6268
<Project Path="examples/lavinmq/CommunityToolkit.Aspire.Hosting.LavinMQ.AppHost/CommunityToolkit.Aspire.Hosting.LavinMQ.AppHost.csproj" />
6369
<Project Path="examples/lavinmq/CommunityToolkit.Aspire.Hosting.LavinMQ.MassTransit/CommunityToolkit.Aspire.Hosting.LavinMQ.MassTransit.csproj" />
@@ -156,8 +162,8 @@
156162
</Folder>
157163
<Folder Name="/examples/surrealdb/">
158164
<Project Path="examples/surrealdb/CommunityToolkit.Aspire.Hosting.SurrealDb.ApiService/CommunityToolkit.Aspire.Hosting.SurrealDb.ApiService.csproj" />
159-
<Project Path="examples/surrealdb/CommunityToolkit.Aspire.Hosting.SurrealDb.ServiceDefaults/CommunityToolkit.Aspire.Hosting.SurrealDb.ServiceDefaults.csproj" />
160165
<Project Path="examples/surrealdb/CommunityToolkit.Aspire.Hosting.SurrealDb.AppHost/CommunityToolkit.Aspire.Hosting.SurrealDb.AppHost.csproj" />
166+
<Project Path="examples/surrealdb/CommunityToolkit.Aspire.Hosting.SurrealDb.ServiceDefaults/CommunityToolkit.Aspire.Hosting.SurrealDb.ServiceDefaults.csproj" />
161167
</Folder>
162168
<Folder Name="/src/">
163169
<Project Path="src/CommunityToolkit.Aspire.EventStore/CommunityToolkit.Aspire.EventStore.csproj" />
@@ -205,6 +211,7 @@
205211
<Project Path="src/CommunityToolkit.Aspire.OllamaSharp/CommunityToolkit.Aspire.OllamaSharp.csproj" />
206212
<Project Path="src/CommunityToolkit.Aspire.RavenDB.Client/CommunityToolkit.Aspire.RavenDB.Client.csproj" />
207213
<Project Path="src/CommunityToolkit.Aspire.SurrealDb/CommunityToolkit.Aspire.SurrealDb.csproj" />
214+
<Project Path="src\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.csproj" Type="C#" />
208215
</Folder>
209216
<Folder Name="/src/Dapr/">
210217
<Project Path="src/CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis/CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis.csproj" />
@@ -249,6 +256,7 @@
249256
<Project Path="tests/CommunityToolkit.Aspire.Hosting.Sqlite.Tests/CommunityToolkit.Aspire.Hosting.Sqlite.Tests.csproj" />
250257
<Project Path="tests/CommunityToolkit.Aspire.Hosting.SqlServer.Extensions.Tests/CommunityToolkit.Aspire.Hosting.SqlServer.Extensions.Tests.csproj" />
251258
<Project Path="tests/CommunityToolkit.Aspire.Hosting.SurrealDb.Tests/CommunityToolkit.Aspire.Hosting.SurrealDb.Tests.csproj" />
259+
<Project Path="tests/CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Tests/CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Tests.csproj" Type="C#" />
252260
<Project Path="tests/CommunityToolkit.Aspire.MassTransit.RabbitMQ.Tests/CommunityToolkit.Aspire.MassTransit.RabbitMQ.Tests.csproj" />
253261
<Project Path="tests/CommunityToolkit.Aspire.Meilisearch.Tests/CommunityToolkit.Aspire.Meilisearch.Tests.csproj" />
254262
<Project Path="tests/CommunityToolkit.Aspire.Microsoft.Data.Sqlite.Tests/CommunityToolkit.Aspire.Microsoft.Data.Sqlite.Tests.csproj" />

Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
<AspireMajorVersion>9</AspireMajorVersion>
1515
<AspireVersion>$(AspireMajorVersion).5.0</AspireVersion>
1616
<AspireAppHostSdkVersion>$(AspireVersion)</AspireAppHostSdkVersion>
17+
<AspirePreviewSuffix>preview.1.25474.7</AspirePreviewSuffix>
1718
<AspNetCoreVersion>9.0.0</AspNetCoreVersion>
1819
<DotNetExtensionsVersion>9.0.9</DotNetExtensionsVersion>
1920
<OpenTelemetryVersion>1.12.0</OpenTelemetryVersion>
2021
<TestContainersVersion>4.7.0</TestContainersVersion>
2122
<MEAIVersion>9.9.0</MEAIVersion>
2223
<IsPackable>false</IsPackable>
24+
<MoqVersion>4.20.72</MoqVersion>
2325

2426
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
2527
<SharedDir>$(RepoRoot)src\Shared</SharedDir>

Directory.Packages.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
45
</PropertyGroup>
56
<ItemGroup Label="Aspire Packages">
67
<!-- Aspire packages -->
@@ -105,6 +106,9 @@
105106
<PackageVersion Include="xunit.extensibility.execution" Version="2.9.3" />
106107
<PackageVersion Include="Microsoft.DotNet.XUnitExtensions" Version="9.0.0-beta.24568.1" />
107108
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
109+
<PackageVersion Include="Aspire.Hosting.Keycloak" Version="$(AspireVersion)-$(AspirePreviewSuffix)" />
110+
<PackageVersion Include="Aspire.Keycloak.Authentication" Version="$(AspireVersion)-$(AspirePreviewSuffix)" />
111+
<PackageVersion Include="Moq" Version="$(MoqVersion)" />
108112
<!-- Build dependencies -->
109113
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
110114
<!-- Testcontainers packages -->
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Aspire.Hosting;
2+
3+
var builder = DistributedApplication.CreateBuilder(args);
4+
5+
var postgres = builder.AddPostgres("keycloak-postgres-dev");
6+
var dbDev = postgres.AddDatabase("db-dev");
7+
8+
var keycloakDev = builder.AddKeycloak("keycloak-dev")
9+
.WithPostgres(dbDev);
10+
11+
var dbUserName = builder.AddParameter("db-username", "postgres");
12+
var dbPassword = builder.AddParameter("db-password", "Postgres!123");
13+
14+
var postgresProd = builder.AddPostgres("postgres-prod",
15+
dbUserName, dbPassword);
16+
17+
var dbProd = postgresProd.AddDatabase("db-prod");
18+
19+
var keycloakProd = builder.AddKeycloak("keycloak-prod")
20+
.WithPostgres(dbProd, dbUserName, dbPassword);
21+
22+
23+
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Keycloak_Extensions_Dev>("project-dev")
24+
.WithReference(keycloakDev)
25+
.WaitFor(keycloakDev);
26+
27+
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Keycloak_Extensions_Prod>("project-prod")
28+
.WithReference(keycloakProd)
29+
.WaitFor(keycloakProd);
30+
builder.Build().Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Sdk Name="Aspire.AppHost.Sdk" Version="$(AspireAppHostSdkVersion)"/>
4+
5+
<PropertyGroup>
6+
<OutputType>Exe</OutputType>
7+
<UserSecretsId>e5e65289-cf91-4bda-b628-28a68fb90841</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Aspire.Hosting"/>
12+
<PackageReference Include="Aspire.Hosting.AppHost"/>
13+
<PackageReference Include="Aspire.Hosting.Keycloak"/>
14+
<PackageReference Include="Aspire.Hosting.PostgreSQL"/>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.csproj" IsAspireProjectResource="false" />
19+
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Dev\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Dev.csproj" />
20+
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Prod\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.Prod.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:17044;http://localhost:15216",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"DOTNET_ENVIRONMENT": "Development",
12+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21202",
13+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22139"
14+
}
15+
},
16+
"http": {
17+
"commandName": "Project",
18+
"dotnetRunMessages": true,
19+
"launchBrowser": true,
20+
"applicationUrl": "http://localhost:15216",
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development",
23+
"DOTNET_ENVIRONMENT": "Development",
24+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19292",
25+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20099"
26+
}
27+
}
28+
}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<ItemGroup>
4+
<PackageReference Include="Aspire.Keycloak.Authentication" />
5+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
6+
</ItemGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.ServiceDefaults\CommunityToolkit.Aspire.Hosting.Keycloak.Extensions.ServiceDefaults.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@CommunityToolkit.Aspire.Hosting.Keycloak.Extensions_HostAddress = http://localhost:5030
2+
3+
GET {{CommunityToolkit.Aspire.Hosting.Keycloak.Extensions_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###

0 commit comments

Comments
 (0)