-
Notifications
You must be signed in to change notification settings - Fork 129
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
0df225d
2e07fa8
1bf7ea7
90621f4
7a4fc17
9b11f66
03cbc0a
53c7ca1
9c913c5
d77acaf
fcfad00
6cd7d93
d4720a9
3962aa1
beb9fe9
e01ce34
19c6f9a
31a647b
cdd4b15
1ba736e
1efdbbc
afb6c43
993aa3e
6eeee8c
8514cab
7513876
8d7af9f
2c66428
d504e4c
5db02af
c5b5d8d
5424687
fe16c1f
50db669
9d0a369
96e174f
fedcf87
6c968b4
b1d9793
a7422fe
bbbb8d4
9055d7b
81bb900
4a801e9
cab3674
1e5d94d
5961fec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,13 @@ | |
</PropertyGroup> | ||
<ItemGroup Label="Aspire Packages"> | ||
<!-- Aspire packages --> | ||
<PackageVersion Include="Aspire.Hosting" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting" Version="9.4.1" /> | ||
<PackageVersion Include="Aspire.Hosting.AppHost" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.Azure.Storage" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.Dapr" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.Azure.AppContainers" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.Azure.Redis" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.Keycloak" Version="9.4.1-preview.1.25408.4" /> | ||
|
||
<PackageVersion Include="Aspire.Hosting.NodeJS" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.Python" Version="$(AspireVersion)" /> | ||
|
@@ -18,6 +19,7 @@ | |
<PackageVersion Include="Aspire.Hosting.MongoDB" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.MySql" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="$(AspireVersion)" /> | ||
<PackageVersion Include="Moq" Version="4.20.72" /> | ||
|
||
</ItemGroup> | ||
<ItemGroup Label="Core Packages"> | ||
<!-- AspNetCore packages --> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
axies20 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting" /> | ||
<PackageReference Include="Aspire.Hosting.Keycloak" /> | ||
<PackageReference Include="Aspire.Hosting.PostgreSQL" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,104 @@ | ||||||
using Aspire.Hosting; | ||||||
using Aspire.Hosting.ApplicationModel; | ||||||
using Npgsql; | ||||||
|
||||||
namespace CommunityToolkit.Aspire.Keycloak.Extensions.Postgres; | ||||||
|
||||||
/// <summary> | ||||||
/// Provides extension methods for integrating Keycloak resources with PostgreSQL. | ||||||
/// </summary> | ||||||
public static class KeycloakPostgresExtension | ||||||
{ | ||||||
/// Configures a Keycloak resource to use a Postgres database by setting appropriate environment variables. | ||||||
axies20 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
/// <param name="builder"> | ||||||
/// The resource builder for the Keycloak resource. | ||||||
/// </param> | ||||||
/// <param name="database"> | ||||||
/// The resource builder for the Postgres database resource. | ||||||
/// </param> | ||||||
/// <param name="app"> | ||||||
/// The distributed application builder for adding parameters like username and password for the Postgres database. | ||||||
/// </param> | ||||||
/// <returns> | ||||||
/// The updated resource builder for the Keycloak resource. | ||||||
/// </returns> | ||||||
private static void WithPostgres(this IResourceBuilder<KeycloakResource> builder, | ||||||
|
||||||
IResourceBuilder<PostgresDatabaseResource> database) | ||||||
{ | ||||||
ArgumentNullException.ThrowIfNull(builder); | ||||||
ArgumentNullException.ThrowIfNull(database); | ||||||
|
||||||
PostgresServerResource pgServer = database.Resource.Parent; | ||||||
EndpointReference ep = pgServer.GetEndpoint("tcp"); | ||||||
|
||||||
string dbName = database.Resource.Name; | ||||||
|
||||||
ReferenceExpression jdbcUrl = ReferenceExpression.Create( | ||||||
$"jdbc:postgresql://{ep.Property(EndpointProperty.Host)}:" + | ||||||
$"{ep.Property(EndpointProperty.Port)}/{dbName}"); | ||||||
|
||||||
builder.WithEnvironment("KC_DB", "postgres") | ||||||
|
||||||
.WithEnvironment("KC_DB_URL", jdbcUrl); | ||||||
} | ||||||
|
||||||
/// Configures a Keycloak resource to use a Postgres database in a development environment by setting appropriate | ||||||
/// environment variables and custom connection details. | ||||||
axies20 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
/// <param name="builder"> | ||||||
/// The resource builder for the Keycloak resource. | ||||||
/// </param> | ||||||
/// <param name="database"> | ||||||
/// The resource builder for the Postgres database resource. | ||||||
/// </param> | ||||||
/// <param name="port"> | ||||||
/// The port for connecting to the Postgres database. Defaults to 5432. | ||||||
/// </param> | ||||||
/// <returns> | ||||||
/// The updated resource builder for the Keycloak resource. | ||||||
/// </returns> | ||||||
public static IResourceBuilder<KeycloakResource> WithPostgresDev(this IResourceBuilder<KeycloakResource> builder, | ||||||
|
||||||
IResourceBuilder<PostgresDatabaseResource> database, int port = 5432) | ||||||
{ | ||||||
WithPostgres(builder, database); | ||||||
|
||||||
database.OnConnectionStringAvailable(async (dataResource, _, cancellationToken) => | ||||||
{ | ||||||
NpgsqlConnectionStringBuilder npg = | ||||||
new(await dataResource.ConnectionStringExpression.GetValueAsync(cancellationToken)); | ||||||
builder.WithEnvironment("KC_DB", "postgres") | ||||||
.WithEnvironment("KC_DB", "postgres") | ||||||
|
builder.WithEnvironment("KC_DB", "postgres") | |
.WithEnvironment("KC_DB", "postgres") |
Copilot uses AI. Check for mistakes.
axies20 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting.Keycloak" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/> | ||
<PackageReference Include="xunit" Version="2.9.2"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/> | ||
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\CommunityToolkit.Aspire.Keycloak.Extensions.Postgres\CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| ||
|
||
namespace CommunityToolkit.Aspire.Keycloak.Extensions.Postgres.Tests; | ||
|
||
public partial class KeycloakExtensionPostgressTests | ||
{ | ||
|
||
} |
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.
I assume this is just done when you were testing, but ensure you roll that back and use the MSBuild variable
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.
I had error
0>CommunityToolkit.Aspire.Keycloak.Postgres.csproj: Error NU1605 : Warning As Error: Detected package downgrade: Aspire.Hosting from 9.4.1 to 9.4.0. Reference the package directly from the project to select a different version.
CommunityToolkit.Aspire.Keycloak.Postgres -> Aspire.Hosting.Keycloak 9.4.1-preview.1.25408.4 -> Aspire.Hosting (>= 9.4.1)
CommunityToolkit.Aspire.Keycloak.Postgres -> Aspire.Hosting (>= 9.4.0)
0>------- Finished building project: CommunityToolkit.Aspire.Keycloak.Postgres. Succeeded: False. Errors: 1. Warnings: 0
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.
The
main
branch is now updated to 9.4.1 Aspire.