Skip to content

Commit 72f9c3e

Browse files
committed
Add keycloak example
0 parents  commit 72f9c3e

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

rbac-keycloak/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Keycloak setup
2+
3+
1. Run the example compose file provided in this directory
4+
2. Go to keycloak admin console at http://localhost:8080/admin and create a new realm (myrealm)
5+
3. Create users "admin" and "readonly", set passwords for them
6+
4. Create realms roles "ops" and "ronly". You can use client roles instead if you want to, but this guide won't cover this.
7+
5. Go to users, assign ops role to admin user and ronly role to readonly user
8+
9+
## Creating a client
10+
11+
Let's create an "openid connect" client:
12+
13+
1. Go to "clients" menu of "myrealm" realm
14+
2. Create a client, use name (client id) "kafbat-ui-client"
15+
3. Set "client authentication" to "on" and "authorization" to "on", allow the "standard" (authorization code) flow.
16+
4. Set the redirect url to http://localhost:9091/login/oauth2/code/keycloak
17+
Obtain client secret in "credentials" tab, save it somewhere
18+
5. Go to "client scopes" tab in our client -> kafbat-ui-client-dedicated -> mappers tab -> add mapper -> from predefined -> find "realm roles", edit added mapper, "token claim name" = "groups", "Claim JSON Type" = String, "Multivalued" = true
19+
20+
This is a simple setup with users assigned to roles directly. You can use groups with roles instead, but this guide doesn't cover it.
21+
22+
## Kafbat UI setup
23+
24+
1. In config.yml example provided, replace client-id and client-secret (obtained earlier) from keycloak
25+
2. Adjust the roles configuration if needed
26+
27+
When logging in, you should see the following in the logs:
28+
29+
```log
30+
DEBUG [reactor-http-nio-6] i.k.u.s.r.e.OauthAuthorityExtractor: Principal name is: [name surname]
31+
DEBUG [reactor-http-nio-6] i.k.u.s.r.e.OauthAuthorityExtractor: Token's groups: [default-roles-myrealm,ops,offline_access,uma_authorization]
32+
DEBUG [reactor-http-nio-6] i.k.u.s.r.e.OauthAuthorityExtractor: Matched group roles: [admins]
33+
```
34+
35+
User info is also available at http://localhost:9090/api/authorization endpoint.
36+
37+
Voilà!

rbac-keycloak/config.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
logging:
2+
level:
3+
org.springframework.security: TRACE
4+
io.kafbat.ui.service.rbac: TRACE
5+
6+
dynamic.config.enabled: true
7+
8+
server:
9+
port: 9090 # prevent clashes with keycloak ports
10+
11+
auth:
12+
type: OAUTH2
13+
oauth2:
14+
client:
15+
keycloak:
16+
clientId: kafbat-ui-client
17+
clientSecret: # CLIENT SECRET OBTAINED FROM KEYCLOAK
18+
scope: openid,roles
19+
client-name: keycloak
20+
provider: keycloak
21+
redirect-uri: http://localhost:9090/login/oauth2/code/keycloak
22+
authorization-grant-type: authorization_code
23+
issuer-uri: http://localhost:8080/realms/myrealm
24+
user-name-attribute: name
25+
custom-params:
26+
type: oauth
27+
roles-field: groups
28+
29+
rbac:
30+
roles:
31+
- name: "admins"
32+
clusters:
33+
- local
34+
subjects:
35+
- provider: oauth
36+
type: role
37+
value: "ops"
38+
39+
permissions:
40+
- resource: applicationconfig
41+
actions: all
42+
43+
- resource: clusterconfig
44+
actions: all
45+
46+
- resource: topic
47+
value: ".*"
48+
actions: all
49+
50+
- resource: consumer
51+
value: ".*"
52+
actions: all
53+
54+
- resource: schema
55+
value: ".*"
56+
actions: all
57+
58+
- resource: connect
59+
value: ".*"
60+
actions: all
61+
62+
- resource: ksql
63+
actions: all
64+
65+
- resource: acl
66+
actions: all
67+
68+
- resource: audit
69+
actions: all
70+
- name: "readonly"
71+
clusters:
72+
- local
73+
subjects:
74+
- provider: oauth
75+
type: role
76+
value: "ronly"
77+
permissions:
78+
- resource: clusterconfig
79+
actions: [ "view" ]
80+
81+
- resource: topic
82+
value: ".*"
83+
actions:
84+
- VIEW
85+
- MESSAGES_READ
86+
87+
- resource: consumer
88+
value: ".*"
89+
actions: [ view ]
90+
91+
- resource: schema
92+
value: ".*"
93+
actions: [ view ]
94+
95+
- resource: connect
96+
value: ".*"
97+
actions: [ view ]
98+
99+
- resource: acl
100+
actions: [ view ]

rbac-keycloak/docker-compose.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
version: '3.8'
3+
name: "kafbat-ui-keycloak"
4+
5+
services:
6+
7+
kafbat-ui:
8+
container_name: kafbat-ui
9+
image: ghcr.io/kafbat/kafka-ui:latest
10+
network_mode: host # WON'T WORK ON MACOS UNLESS ORBSTACK IS USED
11+
environment:
12+
SPRING_CONFIG_ADDITIONAL-LOCATION: /config.yml
13+
volumes:
14+
- ./config.yml:/config.yml # BEWARE, this might need to be an absolute path instead
15+
16+
keycloak:
17+
image: quay.io/keycloak/keycloak:25.0.0
18+
network_mode: host # WON'T WORK ON MACOS UNLESS ORBSTACK IS USED
19+
ports:
20+
- 8081:8080
21+
- 8082:8443 #ssl
22+
command: start-dev
23+
environment:
24+
KEYCLOAK_ADMIN: admin
25+
KEYCLOAK_ADMIN_PASSWORD: admin
26+
volumes:
27+
- /tmp/keycloak:/opt/keycloak/data/ # don't forget to save it somewhere, not in /tmp

0 commit comments

Comments
 (0)