Skip to content

Commit 4cfb359

Browse files
committed
feat: newer keycloak and pulumi
1 parent 3851ce0 commit 4cfb359

File tree

7 files changed

+90
-73
lines changed

7 files changed

+90
-73
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
1010
## Unreleased
1111

12-
* None yet!
12+
* Update keycloak image to quay
13+
* Update auth examples for new keycloak handling
14+
* Upgrade pulumi and keycloak setup
1315

1416
## [0.27.2] - 10/06/2024
1517

Craftsman/Builders/Auth/UserPolicyHandlerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private async Task<string[]> GetRoles()
8282
.Where(x => x.User.Identifier == nameIdentifier)
8383
.Select(x => x.Role.Value)
8484
.ToArray()
85-
: Array.Empty<string>();
85+
: [];
8686
8787
if (roles.Length == 0)
8888
throw new NoRolesAssignedException();

Craftsman/Builders/AuthServer/ClientExtensionsBuilder.cs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,62 @@ public void Create(string solutionDirectory, string projectBaseName)
2121

2222
private static string GetFileText(string classNamespace)
2323
{
24-
return @$"namespace {classNamespace};
24+
// language=csharp
25+
return $$"""
26+
namespace {{classNamespace}};
2527
26-
using Pulumi;
27-
using Pulumi.Keycloak.OpenId;
28+
using Pulumi;
29+
using Pulumi.Keycloak.OpenId;
2830
29-
public static class ClientExtensions
30-
{{
31-
public static void ExtendDefaultScopes(this Client client, params Output<string>[] scopeNames)
32-
{{
33-
var defaultScopes = client.Name.Apply(clientName =>
34-
new ClientDefaultScopes($""default-scopes-for-{{clientName}}"", new ClientDefaultScopesArgs()
35-
{{
36-
RealmId = client.RealmId,
37-
ClientId = client.Id,
38-
DefaultScopes =
39-
{{
40-
""openid"",
41-
""profile"",
42-
""email"",
43-
""roles"",
44-
""web-origins"",
45-
scopeNames,
46-
}},
47-
}})
48-
);
49-
}}
50-
51-
public static void AddAudienceMapper(this Client client, string audience)
52-
{{
53-
var audienceMapper = client.Name.Apply(clientName =>
54-
new AudienceProtocolMapper($""audienceMapper-{{clientName}}-{{audience}}"", new AudienceProtocolMapperArgs
55-
{{
56-
RealmId = client.RealmId,
57-
ClientId = client.Id,
58-
IncludedCustomAudience = audience,
59-
Name = $""{{audience}}-Mapping""
60-
}})
61-
);
62-
}}
63-
}}";
31+
public static class ClientExtensions
32+
{
33+
public static void ExtendDefaultScopes(this Client client, params string[] scopeNames)
34+
{
35+
var scopeList = new InputList<string>()
36+
{
37+
"openid",
38+
"profile",
39+
"email",
40+
"roles",
41+
"web-origins"
42+
};
43+
foreach (var scopeName in scopeNames)
44+
{
45+
scopeList.Add(scopeName);
46+
}
47+
48+
var clientDefaultScopes = new ClientDefaultScopes($"client_default_scopes_{client.GetResourceName()}", new ClientDefaultScopesArgs
49+
{
50+
RealmId = client.RealmId,
51+
ClientId = client.Id,
52+
DefaultScopes = scopeList,
53+
});
54+
}
55+
56+
public static void AddAudienceMapper(this Client client, string audience)
57+
{
58+
var audienceMapper = new AudienceProtocolMapper($"audience_mapper_{client.GetResourceName()}", new AudienceProtocolMapperArgs
59+
{
60+
RealmId = client.RealmId,
61+
ClientId = client.Id,
62+
IncludedCustomAudience = audience,
63+
Name = $"{audience}-Mapping"
64+
});
65+
}
66+
67+
// example tenant mapper
68+
// public static void AddTenantMapper(this Client client)
69+
// {
70+
// var userAttributeMapper = new UserAttributeProtocolMapper($"tenant_mapper_{client.GetResourceName()}", new()
71+
// {
72+
// RealmId = client.RealmId,
73+
// ClientId = client.Id,
74+
// Name = "tenant-mapper",
75+
// UserAttribute = "organization-id",
76+
// ClaimName = "organization_id"
77+
// });
78+
// }
79+
}
80+
""";
6481
}
6582
}

Craftsman/Builders/AuthServer/RealmBuildBuilder.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void Create(string solutionDirectory, string projectBaseName, string temp
2323

2424
private static string GetFileText(string classNamespace, string realmName, List<AuthServerTemplate.AuthClient> clients, string solutionDirectory, string projectBaseName)
2525
{
26-
var realm = @$"var realm = new Realm(""{realmName}-realm"", new RealmArgs
26+
var realm = @$"var realm = new Keycloak.Realm(""{realmName}-realm"", new Keycloak.RealmArgs
2727
{{
2828
RealmName = ""{realmName}"",
2929
RegistrationAllowed = true,
@@ -62,16 +62,16 @@ private static string GetFileText(string classNamespace, string realmName, List<
6262
using {extensionsClassPath.ClassNamespace};
6363
using {factoryClassPath.ClassNamespace};
6464
using Pulumi;
65-
using Pulumi.Keycloak;
6665
using Pulumi.Keycloak.Inputs;
66+
using Keycloak = Pulumi.Keycloak;
6767
6868
class RealmBuild : Stack
6969
{{
7070
public RealmBuild()
7171
{{
7272
{realm}{scopesString}{clientsString}
7373
74-
var bob = new User(""bob"", new UserArgs
74+
var bob = new Keycloak.User(""bob"", new Keycloak.UserArgs
7575
{{
7676
RealmId = realm.Id,
7777
Username = ""bob"",
@@ -86,7 +86,7 @@ public RealmBuild()
8686
}},
8787
}});
8888
89-
var alice = new User(""alice"", new UserArgs
89+
var alice = new Keycloak.User(""alice"", new Keycloak.UserArgs
9090
{{
9191
RealmId = realm.Id,
9292
Username = ""alice"",
@@ -120,7 +120,10 @@ private static string GetNewClientString(AuthServerTemplate.AuthClient client)
120120
string redirectUris = GetRedirectUris(client);
121121
string webOrigins = GetCors(client.AllowedCorsOrigins);
122122

123-
var scopeStringList = client.Scopes.Select(scope => $@"{GetScopeVarName(scope)}.Name");
123+
var scopeStringList = client.Scopes.Select(scope =>
124+
$"""
125+
"{scope}"
126+
""");
124127
var clientScopesToAdd = string.Join(",", scopeStringList);
125128

126129
var mapperString = string.Join("", client.Scopes.Select(scope => $@"

Craftsman/Builders/Docker/DockerComposeBuilders.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,19 @@ public void AddAuthServerToDockerCompose(string solutionDirectory, AuthServerTem
333333
- keycloak-data:/var/lib/postgresql/data
334334
335335
keycloak:
336-
image: sleighzy/keycloak:latest
336+
image: quay.io/keycloak/keycloak:latest
337337
environment:
338-
DB_VENDOR: POSTGRES
339-
DB_ADDR: keycloakdb
340-
DB_DATABASE: keycloak
341-
DB_USER: keycloak
342-
DB_PASSWORD: password
343-
DB_SCHEMA: public
344-
KEYCLOAK_USER: {template.Username}
345-
KEYCLOAK_PASSWORD: {template.Password}
346-
KEYCLOAK_HTTP_PORT: 8080
347-
# Uncomment the line below if you want to specify JDBC parameters. The parameter below is just an example,
348-
# and it shouldn't be used in production without knowledge. It is highly recommended that you read the
349-
# PostgreSQL JDBC driver documentation in order to use it.
350-
#JDBC_PARAMS: ""ssl=true""
338+
KC_DB: postgres
339+
KC_DB_URL_HOST: keycloakdb
340+
KC_DB_URL_DATABASE: keycloak
341+
KC_DB_USERNAME: keycloak
342+
KC_DB_PASSWORD: password
343+
KEYCLOAK_ADMIN: {template.Username}
344+
KEYCLOAK_ADMIN_PASSWORD: {template.Password}
351345
ports:
352-
- {template.Port}:8080
346+
- '{template.Port}:8080'
347+
command:
348+
- start-dev
353349
depends_on:
354350
- keycloakdb
355351
";

Craftsman/Builders/Projects/AuthServerProjBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public static string ProjectFileText()
2323
</PropertyGroup>
2424
2525
<ItemGroup>
26-
<PackageReference Include=""Pulumi"" Version=""3.*"" />
27-
<PackageReference Include=""Pulumi.Keycloak"" Version=""4.11.0"" />
26+
<PackageReference Include=""Pulumi.Keycloak"" Version=""5.3.5"" />
2827
</ItemGroup>
2928
3029
</Project>";

Craftsman/Commands/NewExampleCommand.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ private static string ComplexTemplate(string name)
266266
AsValueObject: MonetaryAmount
267267
Environment:
268268
AuthSettings:
269-
Authority: http://localhost:3255/auth/realms/DevRealm
269+
Authority: http://localhost:3881/realms/DevRealm
270270
Audience: the_kitchen_company
271-
AuthorizationUrl: http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/auth
272-
TokenUrl: http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/token
271+
AuthorizationUrl: http://localhost:3881/realms/DevRealm/protocol/openid-connect/auth
272+
TokenUrl: http://localhost:3881/realms/DevRealm/protocol/openid-connect/token
273273
ClientId: recipe_management.swagger
274274
ClientSecret: 974d6f71-d41b-4601-9a7a-a33081f80687
275275
BrokerSettings:
@@ -303,7 +303,7 @@ private static string ComplexTemplate(string name)
303303
AuthServer:
304304
Name: KeycloakPulumi
305305
RealmName: DevRealm
306-
Port: 3255
306+
Port: 3881
307307
Clients:
308308
- Id: recipe_management.postman.machine
309309
Name: RecipeManagement Postman Machine
@@ -430,10 +430,10 @@ private static string AuthTemplate(string name)
430430
Type: DateOnly?
431431
Environment:
432432
AuthSettings:
433-
Authority: http://localhost:3255/auth/realms/DevRealm
433+
Authority: http://localhost:3881/realms/DevRealm
434434
Audience: the_kitchen_company
435-
AuthorizationUrl: http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/auth
436-
TokenUrl: http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/token
435+
AuthorizationUrl: http://localhost:3881/realms/DevRealm/protocol/openid-connect/auth
436+
TokenUrl: http://localhost:3881/realms/DevRealm/protocol/openid-connect/token
437437
ClientId: recipe_management.swagger
438438
ClientSecret: 974d6f71-d41b-4601-9a7a-a33081f80687";
439439
}
@@ -558,16 +558,16 @@ private static string AuthServerTemplate(string name)
558558
Type: DateOnly?
559559
Environment:
560560
AuthSettings:
561-
Authority: http://localhost:3255/auth/realms/DevRealm
561+
Authority: http://localhost:3881/realms/DevRealm
562562
Audience: the_kitchen_company
563-
AuthorizationUrl: http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/auth
564-
TokenUrl: http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/token
563+
AuthorizationUrl: http://localhost:3881/realms/DevRealm/protocol/openid-connect/auth
564+
TokenUrl: http://localhost:3881/realms/DevRealm/protocol/openid-connect/token
565565
ClientId: recipe_management.swagger
566566
ClientSecret: 974d6f71-d41b-4601-9a7a-a33081f80687
567567
AuthServer:
568568
Name: KeycloakPulumi
569569
RealmName: DevRealm
570-
Port: 3255
570+
Port: 3881
571571
Clients:
572572
- Id: recipe_management.postman.machine
573573
Name: RecipeManagement Postman Machine

0 commit comments

Comments
 (0)