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

Add KeycloakRealmResource and AddRealm method #7120

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/Aspire.Hosting.Keycloak/KeycloakRealmResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting.Keycloak;

/// <summary>
/// Represents a Keycloak Realm resource.
/// </summary>
public sealed class KeycloakRealmResource : Resource, IResourceWithParent<KeycloakResource>, IResourceWithConnectionString
{
private EndpointReference? _parentEndpoint;
private EndpointReferenceExpression? _parentUrl;

/// <summary>
/// Initializes a new instance of the <see cref="KeycloakRealmResource"/> class.
/// </summary>
/// <param name="name">The name of the realm resource.</param>
/// <param name="realmName">The name of the realm.</param>
/// <param name="parent">The Keycloak server resource associated with this database.</param>
public KeycloakRealmResource(string name, string realmName, KeycloakResource parent) : base(name)
{
ArgumentException.ThrowIfNullOrWhiteSpace(realmName);
ArgumentNullException.ThrowIfNull(parent);

RealmName = realmName;
RealmPath = $"realms/{realmName}";
Parent = parent;
}

private EndpointReferenceExpression ParentUrl => _parentUrl ??= ParentEndpoint.Property(EndpointProperty.Url);

/// <summary>
/// Gets the parent endpoint reference.
/// </summary>
public EndpointReference ParentEndpoint => _parentEndpoint ??= new(Parent, "http");

/// <inheritdoc/>
public ReferenceExpression ConnectionStringExpression => ReferenceExpression.Create($"{ParentUrl}/{RealmPath}/");

/// <summary>
/// Gets the base address of the realm.
/// </summary>
public string RealmPath { get; }

/// <summary>
/// Gets the issuer expression for the Keycloak realm.
/// </summary>
public ReferenceExpression IssuerUrlExpression => ReferenceExpression.Create($"{ParentUrl}/{RealmPath}");

/// <summary>
/// Gets or sets the metadata address for the Keycloak realm.
/// </summary>
public string MetadataAddress => ".well-known/openid-configuration";

/// <summary>
/// Gets the metadata address expression for the Keycloak realm.
/// </summary>
public ReferenceExpression MetadataAddressExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{MetadataAddress}");

/// <summary>
/// Gets or sets the 'authorization_endpoint' for the Keycloak realm.
/// </summary>
public string AuthorizationEndpoint => "protocol/openid-connect/auth";

/// <summary>
/// Gets the 'authorization_endpoint' expression for the Keycloak realm.
/// </summary>
public ReferenceExpression AuthorizationEndpointExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{AuthorizationEndpoint}");

/// <summary>
/// Gets or sets the 'token_endpoint' for the Keycloak realm.
/// </summary>
public string TokenEndpoint => "protocol/openid-connect/token";

/// <summary>
/// Gets the 'token_endpoint' expression for the Keycloak realm.
/// </summary>
public ReferenceExpression TokenEndpointExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{TokenEndpoint}");

/// <summary>
/// Gets or sets the 'introspection_endpoint' for the Keycloak realm.
/// </summary>
public string IntrospectionEndpoint => "protocol/openid-connect/token/introspect";

/// <summary>
/// Gets the 'introspection_endpoint' expression for the Keycloak realm.
/// </summary>
public ReferenceExpression IntrospectionEndpointExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{IntrospectionEndpoint}");

/// <summary>
/// Gets or sets 'user_info_endpoint' for the Keycloak realm.
/// </summary>
public string UserInfoEndpoint => "protocol/openid-connect/userinfo";

/// <summary>
/// Gets 'user_info_endpoint' expression for the Keycloak realm.
/// </summary>
public ReferenceExpression UserInfoEndpointExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{UserInfoEndpoint}");

/// <summary>
/// Gets or sets the 'end_session_endpoint' for the Keycloak realm.
/// </summary>
public string EndSessionEndpoint => "protocol/openid-connect/logout";

/// <summary>
/// Gets the 'end_session_endpoint' expression for the Keycloak realm.
/// </summary>
public ReferenceExpression EndSessionEndpointExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{EndSessionEndpoint}");

/// <summary>
/// Gets or sets the 'registration_endpoint' for the Keycloak realm.
/// </summary>
public string RegistrationEndpoint => "clients-registrations/openid-connect";

/// <summary>
/// Gets the 'registration_endpoint' expression for the Keycloak realm.
/// </summary>
public ReferenceExpression RegistrationEndpointExpression => ReferenceExpression.Create($"{ConnectionStringExpression}{RegistrationEndpoint}");

/// <inheritdoc/>
public KeycloakResource Parent { get; }

/// <summary>
/// Gets the name of the realm.
/// </summary>
public string RealmName { get; }
}
22 changes: 22 additions & 0 deletions src/Aspire.Hosting.Keycloak/KeycloakResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,26 @@ public static IResourceBuilder<KeycloakResource> WithRealmImport(

throw new InvalidOperationException($"The realm import file or directory '{importFullPath}' does not exist.");
}

/// <summary>
/// Adds a Keycloak Realm to the application model from a <see cref="IResourceBuilder{KeycloakRealmResource}"/>.
/// </summary>
/// <param name="builder">The Keycloak server resource builder.</param>
/// <param name="name">The name of the realm.</param>
/// <param name="realmName">The name of the realm. If not provided, the resource name will be used.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{KeycloakRealmResource}"/>.</returns>
public static IResourceBuilder<KeycloakRealmResource> AddRealm(
this IResourceBuilder<KeycloakResource> builder,
string name,
string? realmName = null)
{
ArgumentNullException.ThrowIfNull(builder);

// Use the resource name as the realm name if it's not provided
realmName ??= name;

var keycloakRealm = new KeycloakRealmResource(name, realmName, builder.Resource);

return builder.ApplicationBuilder.AddResource(keycloakRealm);
}
}
68 changes: 68 additions & 0 deletions tests/Aspire.Hosting.Keycloak.Tests/KeycloakPublicApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,49 @@ public void CtorKeycloakResourceShouldThrowWhenAdminPasswordIsNull()
Assert.Equal(nameof(adminPassword), exception.ParamName);
}

[Fact]
public void CtorKeycloakRealmResourceShouldThrowWhenNameIsNull()
{
string name = null!;
var realmName = "realm1";
var builder = TestDistributedApplicationBuilder.Create();
var adminPassword = builder.AddParameter("Password");
var parent = new KeycloakResource("keycloak", default(ParameterResource?), adminPassword.Resource);

var action = () => new KeycloakRealmResource(name, realmName, parent);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void CtorMongoKeycloakRealmResourceShouldThrowWhenRealmNameIsNull()
{
var name = "keycloak";
string realmName = null!;
var builder = TestDistributedApplicationBuilder.Create();
var adminPassword = builder.AddParameter("Password");
var parent = new KeycloakResource("keycloak", default(ParameterResource?), adminPassword.Resource);

var action = () => new KeycloakRealmResource(name, realmName, parent);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(realmName), exception.ParamName);
}

[Fact]
public void CtorMongoKeycloakRealmResourceShouldThrowWhenDatabaseParentIsNull()
{
var name = "keycloak";
var realmName = "realm1";
KeycloakResource parent = null!;

var action = () => new KeycloakRealmResource(name, realmName, parent);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(parent), exception.ParamName);
}

[Fact]
public void AddKeycloakContainerShouldThrowWhenBuilderIsNull()
{
Expand Down Expand Up @@ -195,4 +238,29 @@ public void WithRealmImportFileAddsBindMountAnnotation(bool? isReadOnly)
Assert.Equal(ContainerMountType.BindMount, containerAnnotation.Type);
Assert.Equal(isReadOnly ?? false, containerAnnotation.IsReadOnly);
}

[Fact]
public void AddRealmShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<KeycloakResource> builder = null!;
const string name = "realm1";

var action = () => builder.AddRealm(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddRealmShouldThrowWhenNameIsNull()
{
var builderResource = TestDistributedApplicationBuilder.Create();
var MongoDB = builderResource.AddKeycloak("realm1");
string name = null!;

var action = () => MongoDB.AddRealm(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}
}