Skip to content
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

[Keycloak Integration] How to setup database connection for Keycloak when deploying to Azure #8034

Open
ekomsctr opened this issue Mar 12, 2025 · 1 comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages

Comments

@ekomsctr
Copy link

Hello,

i'm trying to deploy my Aspire solution to the Azure cloud. I'm having some trouble when configuring Keycloak environment variables to connect to the deployed Azure PostgreSql (also a resource of the same solution).

I made a simple extension method for the KeycloakResource, which accepts the AzurePostgresFlexibleServerDatabaseResource i'm trying to configure:

public static IResourceBuilder<KeycloakResource> WithDatabase(this IResourceBuilder<KeycloakResource> builder, IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource> source)
{
    builder.WithEnvironment(async context =>
     {
         if (context.ExecutionContext.IsPublishMode)
         {
             context.EnvironmentVariables.Add("TEST_KC_DB", "");
             context.EnvironmentVariables.Add("TEST_KC_DB_PASSWORD", "");
             context.EnvironmentVariables.Add("TEST_KC_DB_URL", "");
             context.EnvironmentVariables.Add("TEST_KC_DB_USERNAME", "");
             context.EnvironmentVariables.Add("TEST_KC_HOSTNAME", "");
         }
         else
         {
             var dbConnection = await source.Resource.ConnectionStringExpression.GetValueAsync(CancellationToken.None);
             var connStringBuilder = new Npgsql.NpgsqlConnectionStringBuilder(dbConnection);
             var keycloakDb = source.Resource;

             context.EnvironmentVariables.Add("KC_DB", "postgres");
             context.EnvironmentVariables.Add("KC_DB_PASSWORD", connStringBuilder.Password);
             context.EnvironmentVariables.Add("KC_DB_URL", $"jdbc:postgresql://pgsql:5432/{keycloakDb.DatabaseName}");
             context.EnvironmentVariables.Add("KC_DB_USERNAME", connStringBuilder.Username);
             context.EnvironmentVariables.Add("KC_HOSTNAME", connStringBuilder.Host);
         }
     });

    return builder;
}

For local development, i've retrieved successfully the connection string at runtime, and everything runs smoothly, but i'm having trouble doing the same for when the solution gets deployed (using Visual Studio publish profile at this moment).

For reference, here are the configured resources in the AppHost:

KeycloakResource

var keycloak = builder.AddKeycloak("keycloak", port: 8080)
    .WithDataVolume()
    .WithAnnotation(new CommandLineArgsCallbackAnnotation(args =>
    {
        if (!builder.ExecutionContext.IsPublishMode)
        {
            args.Add("--log-level=DEBUG");
            args.Add("--log-console-color=true");
            args.Add("--log=console,file");
            args.Add("--features=scripts");
        }
    }))
    .WithExternalHttpEndpoints()
    .WithProxyEdgeConfiguration()
    .WithDatabase(keycloakdb)
    .WithLifetime(ContainerLifetime.Persistent)
    .WaitFor(keycloakdb);

AzurePostgresFlexibleServerDatabaseResource

var sql = builder.ExecutionContext.IsPublishMode ?
    builder.AddAzurePostgresFlexibleServer("pgsql").WithPasswordAuthentication() :
    builder.AddAzurePostgresFlexibleServer("pgsql").RunAsContainer(container =>
    {
        container
            .WithLifetime(ContainerLifetime.Persistent)
            .WithDataVolume(isReadOnly: false)
            .WithPgWeb(pgWeb => pgWeb.WithExternalHttpEndpoints().PublishAsContainer());
    });

var keycloakdb = sql.AddDatabase("keycloakdb", "KeycloakDb")
    .WithCreateCommand(true);

Any tips on improving this implementation and how to get the parameters i need when publishing?

Thank you in advance,
Roberto

@github-actions github-actions bot added the area-integrations Issues pertaining to Aspire Integrations packages label Mar 12, 2025
@davidfowl
Copy link
Member

Let me know if this helps dotnet/docs-aspire#2340 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
Development

No branches or pull requests

2 participants