NikiforovAll / keycloak-authorization-services-dotnet

Authentication and Authorization with Keycloak and ASP.NET Core 🔐
https://nikiforovall.github.io/keycloak-authorization-services-dotnet/
MIT License
396 stars 95 forks source link

Is it possible to pass the database to Keycloak with aspire hosting? #113

Open nodew opened 1 month ago

nodew commented 1 month ago

What I want to do is as the follow:

var pg = builder.AddPostgres("postgresql");

var keycloak = builder
    .AddKeycloakContainer("keycloak")
    .WithDatabase(pg)
    ...
NikiforovAll commented 1 month ago

This feature is not implemented. However, you could use existing implementation and override arguments. Based on: https://www.keycloak.org/server/db

If we are talking about extending this library to support such a scenario, we can do that and I'm open for contribution.


Design Considerations

We could implement it as suggested in the original post, but there are downsides to that - taking a dependency on actual database components is, probably, not a good idea. Let's discuss it.

Solution 1 - take dependencies on Aspire components

Since Keycloak supports only a subset of databases, it should be easy to add support for main databases.

Solution 2 - adapter

We could provide a simple adapter. E.g:

var pg = builder.AddPostgres("postgresql");

var pga = new KeycloakDbAdapter(pg);

var keycloak = builder
    .AddKeycloakContainer("keycloak")
    .WithDatabase(pga);

The Adapter could define something like this:

public interface IKeycloakDbAdapter
{
    KeycloakCredential GetKeycloakCredential()
}