Skip to content

Commit 5383aef

Browse files
committed
[TNZDOC-664] Language and formatting edits
1 parent 8debcad commit 5383aef

File tree

2 files changed

+132
-108
lines changed

2 files changed

+132
-108
lines changed

docs/oauth2-examples-keycloak.md

Lines changed: 87 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ See the License for the specific language governing permissions and
1919
limitations under the License.
2020
-->
2121

22-
# Use Keycloak as OAuth 2.0 server
22+
## Use Keycloak as OAuth 2.0 server
2323

24-
This guide explains how to set up OAuth 2.0 for RabbitMQ
25-
and Keycloak as Authorization Server using the following flows:
24+
This guide explains how to set up OAuth 2.0 for RabbitMQ and Keycloak as Authorization Server using
25+
the following flows:
2626

2727
* Access [management UI](./management/) via a browser
2828
* Access management HTTP API
2929
* Application authentication and authorization
3030

3131
## Keycloak JWT payloads
3232

33-
Keycloak may issue two types of JWT payloads.
33+
Keycloak can issue two types of JWT payloads.
3434

35-
One type of payload is found in a [Requesting Party Token](./oauth2#requesting-party-token).
36-
RabbitMQ supports this type of token and it extracts the scopes from it.
37-
You do not need to configure anything.
35+
One type of payload is found in a [Requesting Party Token](./oauth2#requesting-party-token).
36+
RabbitMQ supports this type of token and it extracts the scopes from it. You do not need to
37+
configure anything.
3838

39-
A second type of payload is the following.
39+
The second type of payload is the following:
4040

41-
```json
41+
```json
4242
{
4343
"realm_access": {
4444
"roles": [
@@ -63,111 +63,130 @@ A second type of payload is the following.
6363
```
6464

6565
:::info
66-
The claim `roles` is not strictily
67-
speaking part of Keycloak official claims. Instead, it is a custom claim configured
68-
by the user via the Keycloak administration console.
66+
The claim `roles` is not, strictly speaking, part of Keycloak official claims. Instead, it is a
67+
custom claim configured by the user from the Keycloak administration console.
6968
:::
7069

71-
RabbitMQ does not read the scopes from this token unless you configure it to do so.
72-
For instance, to configure RabbitMQ to extract the scopes from `roles` under `realm_access` claim,
73-
you add the following configuration variable:
70+
RabbitMQ does not read the scopes from this token unless you configure it to do so. For example, to
71+
configure RabbitMQ to extract the scopes from `roles` under the `realm_access` claim, add the
72+
following configuration variable:
7473

7574
```json
7675
auth_oauth2.additional_scopes_key = realm_access.roles
7776
```
7877

79-
To configure RabbitMQ to also read from `resource_access` claim, you modify the previous
80-
configuration as follows:
78+
To configure RabbitMQ to also read from `resource_access` claim, edit the previous configuration as
79+
follows:
8180

8281
```json
83-
auth_oauth2.additional_scopes_key = realm_access.roles resource_access.account.roles
82+
auth_oauth2.additional_scopes_key = realm_access.roles resource_access.account.roles
8483
```
8584

86-
And finally, if you also want to use the scopes in the claim `roles`, you modify
87-
the previous configuration:
85+
And finally, if you also want to use the scopes in the claim `roles`, you edit the previous
86+
configuration:
8887

8988
```json
90-
auth_oauth2.additional_scopes_key = roles realm_access.roles resource_access.account.roles
89+
auth_oauth2.additional_scopes_key = roles realm_access.roles resource_access.account.roles
9190
```
9291

93-
RabbitMQ reads the scopes from all those sources.
92+
RabbitMQ reads the scopes from all those sources.
9493

9594
## Prerequisites to follow this guide
9695

9796
* Docker
9897
* make
99-
* A local clone of a [GitHub repository](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/tree/next) for branch `next` that contains all the configuration files and scripts used on this example
98+
* A local clone of a
99+
[GitHub repository](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/tree/next) for branch
100+
`next` that contains all the configuration files and scripts used on this example
100101
* Add the following entry to `/etc/hosts`:
101-
```
102-
localhost keycloak rabbitmq
103-
```
102+
103+
```console
104+
localhost keycloak rabbitmq
105+
```
104106

105107
## Deploy Keycloak
106108

107-
1. First, deploy **Keycloak**. It comes preconfigured with all the required scopes, users and clients.
109+
1. First, deploy Keycloak. It comes preconfigured with all the required scopes, users, and clients.
108110

109-
2. Run the following command to start **Keycloak** server:
111+
2. Start the Keycloak server by running:
110112

111-
```bash
112-
make start-keycloak
113-
```
113+
```bash
114+
make start-keycloak
115+
```
114116

115-
There is a dedicated **Keycloak realm** called `Test` configured as follows:
117+
There is a dedicated Keycloak realm called `Test` configured as follows:
116118

117-
* A [rsa](https://keycloak:8443/admin/master/console/#/test/realm-settings/keys) signing key. Use `admin`:`admin`
118-
when prompted for credentials to access the Keycloak Administration page
119+
* A [rsa](https://keycloak:8443/admin/master/console/#/test/realm-settings/keys) signing key. Use
120+
`admin`:`admin` when prompted for credentials to access the Keycloak Administration page
119121
* A [rsa provider](https://keycloak:8443/admin/master/console/#/test/realm-settings/keys/providers)
120-
* Three clients: `rabbitmq-client-code` for the rabbitmq management UI, `mgt_api_client` to access via the
121-
management api and `producer` to access via AMQP protocol.
122-
122+
* Three clients: `rabbitmq-client-code` for the RabbitMQ management UI, `mgt_api_client` to access
123+
via the management API and `producer` to access via the AMQP protocol.
123124

124125
## Start RabbitMQ
125126

126-
Run the command below to start RabbitMQ configured with the **Keycloak** server we started in the previous section: This is the [rabbitmq.conf](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/blob/next/conf/keycloak/rabbitmq.conf) used for **Keycloak**.
127+
Run the command below to start RabbitMQ configured with the `Keycloak` server we started in the
128+
previous section: This is the
129+
[rabbitmq.conf](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/blob/next/conf/keycloak/rabbitmq.conf)
130+
used for Keycloak.
131+
127132
```bash
128133
export MODE=keycloak
129134
make start-rabbitmq
130135
```
131136

132137
:::info
133-
RabbitMQ is deployed with TLS enabled and Keycloak is configured with the corresponding `redirect_url` which uses https.
138+
RabbitMQ is deployed with TLS enabled and Keycloak is configured with the corresponding `redirect_url`
139+
which uses HTTPS.
134140
:::
135141

136-
:::important
137-
RabbitMQ is configured to read the scopes from the custom claim [extra_scope](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/blob/next/conf/keycloak/rabbitmq.conf#L11) and
138-
by default from the standard claim `scope`.
139-
However, if your scopes are deep in a map/list structure like `authorization.permissions.scopes`
140-
or under `realm_access.roles` or `resource_access.account.roles`, you can configure
141-
RabbitMQ to use those locations instead. See the section [Use a different token field for the scope](./oauth2#use-different-token-field) for more information.
142+
:::important
143+
RabbitMQ is configured to read the scopes from the custom claim
144+
[extra_scope](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/blob/next/conf/keycloak/rabbitmq.conf#L11)
145+
and by default from the standard claim `scope`.
146+
However, if your scopes are deep in a map/list structure such as `authorization.permissions.scopes`,
147+
or under `realm_access.roles` or `resource_access.account.roles`, you can configure RabbitMQ to use
148+
those locations instead. For more information, see the section
149+
[Use a different token field for the scope](./oauth2#use-different-token-field).
142150
:::
143151

144-
## Access Management api
152+
## Access Management API
145153

146-
To access the management api run the following command. It uses the client [mgt_api_client](https://keycloak:8443/admin/master/console/#/test/clients/c5be3c24-0c88-4672-a77a-79002fcc9a9d/settings) which has the scope [rabbitmq.tag:administrator](https://keycloak:8443/admin/master/console/#/test/client-scopes/f6e6dd62-22bf-4421-910e-e6070908764c/settings).
154+
To access the management api run the following command. It uses the client
155+
[mgt_api_client](https://keycloak:8443/admin/master/console/#/test/clients/c5be3c24-0c88-4672-a77a-79002fcc9a9d/settings)
156+
that has the scope
157+
[rabbitmq.tag:administrator](https://keycloak:8443/admin/master/console/#/test/client-scopes/f6e6dd62-22bf-4421-910e-e6070908764c/settings).
147158

148159
```bash
149160
make curl-keycloak url=https://localhost:15671/api/overview client_id=mgt_api_client secret=LWOuYqJ8gjKg3D2U8CJZDuID3KiRZVDa realm=test
150161
```
151162

152163
## Application authentication and authorization with PerfTest
153164

154-
To test OAuth 2.0 authentication with AMQP protocol you are going to use RabbitMQ PerfTest tool which uses RabbitMQ Java Client.
165+
To test OAuth 2.0 authentication with the AMQP protocol you use the RabbitMQ PerfTest tool, which
166+
uses RabbitMQ Java Client.
155167

156-
First you obtain the token and pass it as a parameter to the make target `start-perftest-producer-with-token`.
168+
First you obtain the token and pass it as a parameter to the make target
169+
`start-perftest-producer-with-token`.
157170

158171
```bash
159172
make start-perftest-producer-with-token PRODUCER=producer TOKEN=$(bin/keycloak/token producer kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn test)
160173
```
161174

162-
**NOTE**: Initializing an application with a token has one drawback: the application cannot use the connection beyond the lifespan of the token. See the next section where you demonstrate how to refresh the token.
175+
:::info
176+
Initializing an application with a token has one drawback: the application cannot use the connection
177+
beyond the lifespan of the token. See the next section where you demonstrate how to refresh the token.
178+
:::
163179

164180
## Application authentication and authorization with Pika
165181

166-
In the following information, OAuth 2.0 authentication is tested with the AMQP protocol and the Pika library. These tests specifically demonstrate how to refresh a token on a live AMQP connection.
182+
In the following information, OAuth 2.0 authentication is tested with the AMQP protocol and the Pika
183+
library. These tests specifically demonstrate how to refresh a token on a live AMQP connection.
167184

168-
The sample Python application [can be found on GitHub](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/tree/next/pika-client).
185+
The sample Python application is
186+
[in GitHub](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/tree/next/pika-client).
169187

170188
To run this sample code proceed as follows:
189+
171190
```bash
172191
python3 --version
173192
pip install pika
@@ -183,40 +202,42 @@ source venv/bin/activate
183202
```
184203
:::
185204

186-
Note: Ensure you install pika 1.3
205+
:::important
206+
Ensure that you install pika 1.3.
207+
:::
187208

188209
## Access [management UI](./management/)
189210

190211
1. Go to https://localhost:15671.
191-
2. Click on the single button on the page which redirects to **Keycloak** to authenticate.
192-
3. Enter `rabbit_admin` and `rabbit_admin` and you should be redirected back to RabbitMQ Management fully logged in.
212+
2. Click on the single button on the page which redirects to Keycloak to authenticate.
213+
3. Enter `rabbit_admin` and `rabbit_admin` and you should be redirected back to RabbitMQ Management
214+
fully logged in.
193215

194-
195-
## Stop keycloak
216+
## Stop Keycloak
196217

197218
```bash
198219
make stop-keycloak
199220
```
200221

201222
## Notes about setting up Keycloak
202223

203-
### Configure Client
224+
### Configure client
204225

205226
For backend applications which uses **Client Credentials flow**, you can create a **Client** with:
206227

207-
* **Access Type** : `public`
228+
* **Access Type**: `public`
208229
* Turn off `Standard Flow`, `Implicit Flow`, and `Direct Access Grants`
209230
* With **Service Accounts Enabled** on. If it is not enabled you do not have the tab `Credentials`
210231
* In the `Credentials` tab, you have the `client id`
211232

233+
### Configure client scopes
212234

213-
### Configure Client scopes
214-
215-
*Default Client Scope* are scopes automatically granted to every token. Whereas *Optional Client Scope* are
216-
scopes which are only granted if they are explicitly requested during the authorization/token request flow.
217-
235+
*Default Client Scope* are scopes automatically granted to every token. Whereas
236+
*Optional Client Scope* are scopes which are only granted if they are explicitly requested during
237+
the authorization/token request flow.
218238

219239
### Include appropriate aud claim
220240

221-
You must configure a **Token Mapper** of type **Hardcoded claim** with the value of rabbitmq's *resource_server_id**.
222-
You can configure **Token Mapper** either to a **Client scope** or to a **Client**.
241+
You must configure a **Token Mapper** of type **Hardcoded claim** with the value of RabbitMQ's
242+
`resource_server_id`. You can configure **Token Mapper** either to a **Client scope** or to a
243+
**Client**.

0 commit comments

Comments
 (0)