Skip to content

Commit ce66114

Browse files
feat(Keycloak): Add API to import a realm configuration file (#1526)
Co-authored-by: Andre Hofmeister <[email protected]>
1 parent 642c5e9 commit ce66114

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/Testcontainers.Keycloak/KeycloakBuilder.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ public KeycloakBuilder WithPassword(string password)
6060
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", password);
6161
}
6262

63+
/// <summary>
64+
/// Configures Keycloak to import a realm configuration file during startup.
65+
/// </summary>
66+
/// <remarks>
67+
/// The file will be copied to the <c>/opt/keycloak/data/import/</c> directory
68+
/// inside the container:
69+
/// https://www.keycloak.org/server/importExport#_importing_a_realm_during_startup.
70+
/// </remarks>
71+
/// <param name="realmConfigurationFilePath">The local path to the realm configuration file (JSON).</param>
72+
/// <returns>A configured instance of <see cref="KeycloakBuilder" />.</returns>
73+
public KeycloakBuilder WithRealm(string realmConfigurationFilePath)
74+
{
75+
return WithCommand("--import-realm")
76+
.WithResourceMapping(realmConfigurationFilePath, "/opt/keycloak/data/import/");
77+
}
78+
6379
/// <inheritdoc />
6480
public override KeycloakContainer Build()
6581
{

tests/Testcontainers.Keycloak.Tests/KeycloakContainerTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,28 @@ public KeycloakV26Configuration()
8585
{
8686
}
8787
}
88+
89+
[UsedImplicitly]
90+
public sealed class KeycloakRealmConfiguration : KeycloakContainerTest
91+
{
92+
public KeycloakRealmConfiguration()
93+
: base(new KeycloakBuilder().WithRealm("realm-export.json").Build())
94+
{
95+
}
96+
97+
[Fact]
98+
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
99+
public async Task AllRealmsAreEnabled()
100+
{
101+
// Given
102+
var keycloakClient = new KeycloakClient(_keycloakContainer.GetBaseAddress(), KeycloakBuilder.DefaultUsername, KeycloakBuilder.DefaultPassword);
103+
104+
// When
105+
var realms = await keycloakClient.GetRealmsAsync("master", TestContext.Current.CancellationToken)
106+
.ConfigureAwait(true);
107+
108+
// Then
109+
Assert.Collection(realms, realm => Assert.True(realm.Enabled), realm => Assert.True(realm.Enabled));
110+
}
111+
}
88112
}

tests/Testcontainers.Keycloak.Tests/Testcontainers.Keycloak.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
<ProjectReference Include="../../src/Testcontainers.Keycloak/Testcontainers.Keycloak.csproj"/>
1717
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
1818
</ItemGroup>
19+
<ItemGroup>
20+
<None Update="realm-export.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
1924
</Project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"realm": "test-realm",
3+
"enabled": true,
4+
"clients": [
5+
{
6+
"clientId": "test-client",
7+
"secret": "secret",
8+
"enabled": true,
9+
"protocol": "openid-connect",
10+
"publicClient": false,
11+
"directAccessGrantsEnabled": true,
12+
"serviceAccountsEnabled": true,
13+
"name": "Test Client",
14+
"attributes": {
15+
"jwt.client.claims": "true"
16+
},
17+
"protocolMappers": [
18+
{
19+
"name": "audience",
20+
"protocol": "openid-connect",
21+
"protocolMapper": "oidc-audience-mapper",
22+
"consentRequired": false,
23+
"config": {
24+
"included.client.audience": "test-client",
25+
"id.token.claim": "true",
26+
"access.token.claim": "true"
27+
}
28+
}
29+
]
30+
}
31+
],
32+
"users": [
33+
{
34+
"username": "admin",
35+
"enabled": true,
36+
"emailVerified": true,
37+
"credentials": [
38+
{
39+
"type": "password",
40+
"value": "admin",
41+
"temporary": false
42+
}
43+
],
44+
"email": "[email protected]"
45+
}
46+
]
47+
}

0 commit comments

Comments
 (0)