You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: examples/README-authz.md
+79-29
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,13 @@
1
-
## Token based authorization with Keycloak Authorization Services
1
+
## Token Based Authorization with Keycloak Authorization Services
2
2
3
3
Once the Kafka Broker has obtained an access token by using Strimzi Kafka OAuth for authentication, it is possible to use centrally managed authorization rules to enforce access restrictions onto Kafka Clients.
4
4
For this Strimzi Kafka OAuth supports the use of Keycloak Authorization Services.
5
5
6
6
A custom authorizer has to be configured on the Kafka Broker to take advantage of Authorization Services REST endpoints available on Keycloak, which provide a list of granted permissions on resources for authenticated users.
7
7
The list is fetched once, and enforced locally on the Kafka Broker for each user session in order to provide fast authorization decisions.
8
8
9
-
## Authorization example
9
+
10
+
## Building the Example Project
10
11
11
12
Before using the example, we first need to build the project, and prepare resources.
12
13
@@ -21,22 +22,30 @@ Now build the project, and prepare resources:
21
22
22
23
We are now ready to start up the containers and see Keycloak Authorization Services in action.
23
24
25
+
26
+
## Starting Up the Containers
27
+
24
28
First make sure any existing containers with the same name are removed, otherwise we might use previous configurations:
25
29
26
30
docker rm keycloak kafka zookeeper
27
31
28
32
Let's start up all the containers with authorization configured, and we'll then perform any manual step, and explain how everything works.
-f kafka-oauth-strimzi/compose-authz.yml up --build
31
36
32
37
When everything starts up without errors we should have one instance of `keycloak` listening on localhost:8080.
33
-
You can login to the admin console by opening `http://localhost:8080/auth/admin` and using `admin` as both username, and a password.
34
38
35
-
In upper left corner under Keycloak icon you should see `Master` selected as a current realm.
39
+
40
+
## Using Keycloak Admin Console to Configure Authorization
41
+
42
+
You can login to the Admin Console by opening `http://localhost:8080/auth/admin` and using `admin` as both username, and a password.
43
+
44
+
In the upper left corner under Keycloak icon you should see `Master` selected as a current realm.
36
45
Moving the mouse pointer over it should reveal two additional realms - `Demo` and `Kafka-authz`.
37
46
38
47
For this example we are interested in the `kafka-authz` realm.
39
-
Selecting it will open the `Realm Settings` for `kafka-authz` realm.
48
+
Selecting it will open the `Realm Settings` for the `kafka-authz` realm.
40
49
Next to `Realm Settings` there are other sections we are interested in - `Groups`, `Roles`, `Clients` and `Users`.
41
50
42
51
Under `Groups` we can see several groups that can be used to mark users as having some permissions.
@@ -61,15 +70,18 @@ Clients `team-a-client`, and `team-b-client` are confidential clients representi
61
70
The authorization configuration is defined in `kafka` client under `Authorization` tab.
62
71
This tab becomes visible when `Authorization Enabled` is turned on under `Settings` tab.
63
72
64
-
If we take a look under `Resources` sub-tab we'll see the list of resource definitions.
73
+
74
+
## Authorization Services - Resources, Authorization Scopes, Policies and Permissions
75
+
76
+
If we take a look under `Resources` sub-tab of `Authorization` tab, we'll see the list of resource definitions.
65
77
These are resource specifiers - patterns in a specific format, that are used to target policies to specific resources.
66
78
The format is quite simple. For example:
67
79
68
-
kafka-cluster:cluster-1,Topic:a_* ... targets only topics in kafka cluster 'cluster-1' with names starting with 'a_'
80
+
-`kafka-cluster:cluster-1,Topic:a_*` ... targets only topics in kafka cluster 'cluster-1' with names starting with 'a_'
69
81
70
82
If `kafka-cluster:XXX` segment is not present, the specifier targets any cluster.
71
83
72
-
Group:x_* ... targets all consumer groups on any cluster with names starting with 'x_'
84
+
-`Group:x_*` ... targets all consumer groups on any cluster with names starting with 'x_'
73
85
74
86
The possible resource types mirror the Kafka authorization model (Topic, Group, Cluster).
75
87
@@ -102,6 +114,7 @@ Let's click on `Dev Team A` policy. We see that it matches all users that have a
102
114
Similarly, the `Dev Team B ...` permissions perform matching using `Dev Team B` policy which also uses realm role to match allowed users - in this case those with realm role `Dev Team B`.
103
115
The `Dev Team B ...` permissions grant users `Describe` and `Read` on `Topic:x_*`, and `Group:x_*` resources, effectively giving matching users and clients the ability to read from topics, and update the consumed offsets for topics and consumer groups that have names starting with 'x_'.
104
116
117
+
## Targeting Permissions - Clients and Roles vs. Users and Groups
105
118
106
119
In Keycloak, confidential clients with 'service accounts' enabled can authenticate to the server in their own name using a clientId and a secret.
107
120
This is convenient for microservices which typically act in their own name, and not as agents of a particular user (like a web site would, for example).
@@ -114,6 +127,9 @@ Performing cluster management is usually done interactively - in person - using
114
127
It makes sense to require the user to log-in, before using the resulting access token to authenticate to Kafka Broker.
115
128
In this case the access token represents the specific user, rather than the client application.
116
129
130
+
131
+
## Authorization in Action Using CLI Clients
132
+
117
133
Before continuing, there is one setting we need to check.
118
134
Due to [a little bug in Keycloak](https://issues.redhat.com/browse/KEYCLOAK-12640) the realm is at this point misconfigured, and we have to fix the configuration manually.
119
135
Under `Clients` / `kafka` / `Authorization` / `Settings` make sure the `Decision Strategy` is set to `Affirmative`, and NOT to `Unanimous`. Click `Save` after fixing it.
@@ -149,10 +165,14 @@ Make sure the necessary classes are on the classpath:
This gives us an error like: 'Not authorized to access group: console-consumer-55841'.
181
206
182
207
The reason is that we have to override the default consumer group name - `Dev Team A` only has access to consumer groups that have names starting with 'a_'.
183
208
Let's set custom consumer group name that starts with 'a_'
But that will fail with `Cluster authorization failed.` error, because this operation requires cluster level permissions which `team-a-client` does not have.
206
239
240
+
241
+
### Client with Different Permissions
242
+
207
243
Let's prepare a configuration for `team-b-client`:
208
244
209
245
```
@@ -226,14 +262,16 @@ The `Dev Team B` users have full access to topics beginning with 'b_' on Kafka c
226
262
Let's try produce some messages to topic 'a_messages' as `team-b-client`:
0 commit comments