-
Notifications
You must be signed in to change notification settings - Fork 32
/
KeycloakResource.cs
77 lines (66 loc) · 2.52 KB
/
KeycloakResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Aspire.Hosting.Publishing;
namespace Aspire.Hosting;
internal static class KeycloakHostingExtensions
{
private const int DefaultContainerPort = 8080;
public static IResourceBuilder<TResource> WithReference<TResource>(this IResourceBuilder<TResource> builder,
IResourceBuilder<KeycloakResource> keycloakBuilder,
string env)
where TResource : IResourceWithEnvironment
{
builder.WithReference(keycloakBuilder);
builder.WithEnvironment(env, keycloakBuilder.Resource.ClientSecret);
return builder;
}
public static IResourceBuilder<KeycloakResource> AddKeycloakContainer(
this IDistributedApplicationBuilder builder,
string name,
int? port = null,
string? tag = null)
{
var keycloakContainer = new KeycloakResource(name)
{
ClientSecret = Guid.NewGuid().ToString("N")
};
var keycloak = builder
.AddResource(keycloakContainer)
.WithAnnotation(new ContainerImageAnnotation { Registry = "quay.io", Image = "keycloak/keycloak", Tag = tag ?? "latest" })
.WithHttpEndpoint(port: port, targetPort: DefaultContainerPort)
.WithEnvironment("KEYCLOAK_ADMIN", "admin")
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", "admin")
.WithEnvironment("WEBAPP_CLIENT_SECRET", keycloakContainer.ClientSecret);
if (builder.ExecutionContext.IsRunMode)
{
keycloak.WithArgs("start-dev");
}
else
{
keycloak.WithArgs("start");
}
return keycloak;
}
public static IResourceBuilder<KeycloakResource> ImportRealms(this IResourceBuilder<KeycloakResource> builder, string source)
{
builder
.WithBindMount(source, "/opt/keycloak/data/import")
.WithAnnotation(new CommandLineArgsCallbackAnnotation(args =>
{
// TODO: This could be cleaned up to make it properly compose with any other callers who customize args
args.Clear();
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode)
{
args.Add("start-dev");
}
else
{
args.Add("start");
}
args.Add("--import-realm");
}));
return builder;
}
}
internal class KeycloakResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
public string? ClientSecret { get; set; }
}