Skip to content

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

@ekomsctr

Description

@ekomsctr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-integrationsIssues pertaining to Aspire Integrations packageskeycloakIssues related to keycloack integrations
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions