|
| 1 | +--- |
| 2 | +title: Use an explicit forward proxy and Keycloak as OAuth 2.0 server |
| 3 | +displayed_sidebar: docsSidebar |
| 4 | +--- |
| 5 | +<!-- |
| 6 | +Copyright (c) 2005-2024 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 7 | +
|
| 8 | +All rights reserved. This program and the accompanying materials |
| 9 | +are made available under the terms of the under the Apache License, |
| 10 | +Version 2.0 (the "License”); you may not use this file except in compliance |
| 11 | +with the License. You may obtain a copy of the License at |
| 12 | +
|
| 13 | +https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | +Unless required by applicable law or agreed to in writing, software |
| 16 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +See the License for the specific language governing permissions and |
| 19 | +limitations under the License. |
| 20 | +--> |
| 21 | + |
| 22 | +# Use an explicit forward proxy and Keycloak as OAuth 2.0 server |
| 23 | + |
| 24 | +:::warning |
| 25 | +To run this example you need to use the commercial [Tanzu RabbitMQ docker image](https://techdocs.broadcom.com/us/en/vmware-tanzu/data-solutions/tanzu-rabbitmq-oci/4-0/tanzu-rabbitmq-oci-image/overview.html). Support for **forward proxy** is a commercial feature. |
| 26 | +::: |
| 27 | + |
| 28 | +This guide explains how to set up OAuth 2.0 for RabbitMQ to access the Authorization Server via an explicit forward proxy. |
| 29 | + |
| 30 | +* Access the RabbitMQ Management UI using a browser through OAuth2 Proxy |
| 31 | + |
| 32 | +```plain |
| 33 | + [ Keycloak ] |
| 34 | + /|\ |
| 35 | + | |
| 36 | + 2.http request (*) | [ RabbitMQ ] |
| 37 | + [ forward-proxy ] <----1. http request (*)--- [ http ] |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | +RabbitMQ establishes an HTTP connection with Keycloak via the forward-proxy in any of |
| 42 | +these situations: |
| 43 | + |
| 44 | +- You have configured `auth_oauth2.issuer` so that RabbitMQ downloads the OpenID configuration via the OpenID discovery endpoint. |
| 45 | +- You have configured `auth_oauth2.issuer` or `auth_oauth2.jwks_url` so that RabbitMQ downloads the tokens' signing keys. |
| 46 | + |
| 47 | +## Prerequisites for Using OAuth 2 vith a forward proxy |
| 48 | + |
| 49 | +* Docker |
| 50 | +* make |
| 51 | +* 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. |
| 52 | +* The following entries in your /etc/hosts file. Without these entries you will get DNS errors in the browser. |
| 53 | +``` |
| 54 | +localhost keycloak rabbitmq forward-proxy |
| 55 | +``` |
| 56 | + |
| 57 | +:::info |
| 58 | +`make start-keycloak` will |
| 59 | +generate the TLS certificate and private keys as necessary. These certificates have an expiration date. |
| 60 | + |
| 61 | +In you see any error messages that hint at expired or invalid certificates, stop Keycloak, run `make clean-certs` to regenerate the certificates and private keys, |
| 62 | +and then restart Keycloak and the proxy. |
| 63 | +::: |
| 64 | + |
| 65 | +## Deploy Keycloak |
| 66 | + |
| 67 | +Deploy keycloak on its own network called `keycloak_net` by running: |
| 68 | + |
| 69 | +```bash |
| 70 | +PROVIDER_NETWORK=keycloak_net make start-keycloak |
| 71 | +``` |
| 72 | + |
| 73 | +To access Keycloak Management UI, go to https://keycloak:8443/ and enter `admin` as the username and password. |
| 74 | + |
| 75 | +There is a dedicated **Keycloak realm** called `Test` configured as follows: |
| 76 | + |
| 77 | +* [rsa](https://keycloak:8443/admin/master/console/#/realms/test/keys) signing-key |
| 78 | +* [rsa provider]https://keycloak:8443/admin/master/console/#/realms/test/keys/providers) |
| 79 | +* `rabbitmq-proxy-client` client |
| 80 | + |
| 81 | +## Start Forward Proxy |
| 82 | + |
| 83 | +Deploy and start the forward-proxy in two networks, `keycloak_net` and `rabbitmq_net`, by running: |
| 84 | + |
| 85 | +```bash |
| 86 | +PROVIDER_NETWORK=keycloak_net make start-forward-proxy |
| 87 | +``` |
| 88 | + |
| 89 | +The forward proxy is configured by using [httpd.conf](https://github.com/rabbitmq/rabbitmq-oauth2-tutorial/tree/next/conf/forward-proxy/httpd/httpd.conf). This type of configuration inserts the access token into the HTTP **Authorization** header. |
| 90 | + |
| 91 | + |
| 92 | +## Start RabbitMQ |
| 93 | + |
| 94 | +Deploy RabbitMQ in its own network `rabbitmq_net` and start it by running: |
| 95 | + |
| 96 | +``` |
| 97 | +export IMAGE=<Tanzu RabbitMQ OCI image name> |
| 98 | +export IMAGE_TAG=<Tanzu RabbitMQ OCI image tag> |
| 99 | +MODE=forward-proxy OAUTH_PROVIDER=keycloak make start-rabbitmq |
| 100 | +``` |
| 101 | + |
| 102 | +## Access [management UI](./management/) |
| 103 | + |
| 104 | +Go to https://rabbitmq:15671/, proceed to login, and enter the credentials |
| 105 | +`rabbit_admin` as the username and `rabbit_admin` as the password when Keycloak prompts you. |
| 106 | +You will be redirected back to RabbitMQ management UI. |
| 107 | + |
| 108 | +The management UI running in the browser goes straight to keycloak. |
| 109 | +In other words, it does not go via the forward-proxy. If you want the management UI to |
| 110 | +go via the forward-proxy, you must configure the browser. That is beyond |
| 111 | +the scope of this example. |
| 112 | + |
| 113 | +However, in order to validate the token the management UI received from keycloak, RabbitMQ has to connect to keycloak via the forward-proxy. This is necessary in order to download the signing keys and to download the OpenID configuration if you only configured the `issuer` URL. |
| 114 | + |
| 115 | +## Access Management API |
| 116 | + |
| 117 | +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). |
| 118 | + |
| 119 | +```bash |
| 120 | +make curl-keycloak url=https://localhost:15671/api/overview client_id=mgt_api_client secret=LWOuYqJ8gjKg3D2U8CJZDuID3KiRZVDa realm=test |
| 121 | +``` |
0 commit comments