2017-02-14 23:51:47 +08:00
|
|
|
# RabbitMQ HTTP Authn/Authz Backend Examples
|
|
|
|
|
2018-07-30 05:51:34 +08:00
|
|
|
## Overview
|
|
|
|
|
|
|
|
This directory provides a number of community contributed example applications that target
|
|
|
|
different platforms and frameworks:
|
|
|
|
|
|
|
|
* Python and Django
|
|
|
|
* Java and Spring Boot
|
2019-02-27 22:38:44 +08:00
|
|
|
* Kotlin and Spring Boot
|
2018-07-30 05:51:34 +08:00
|
|
|
* C# and ASP.NET Web API
|
2024-01-22 15:52:56 +08:00
|
|
|
* C# and ASP.NET Core 8.0
|
2019-02-27 22:38:44 +08:00
|
|
|
* PHP
|
2018-07-30 05:51:34 +08:00
|
|
|
|
2017-02-16 21:04:27 +08:00
|
|
|
## Python Example
|
2017-01-12 20:06:32 +08:00
|
|
|
|
2020-03-01 00:40:50 +08:00
|
|
|
`rabbitmq_auth_backend_django` is a very minimalistic [Django](https://www.djangoproject.com/) application
|
2017-01-12 20:06:32 +08:00
|
|
|
that rabbitmq-auth-backend-http can authenticate against. It's really
|
|
|
|
not designed to be anything other than an example.
|
|
|
|
|
2017-02-14 23:51:47 +08:00
|
|
|
### Running the Example
|
2017-01-12 20:06:32 +08:00
|
|
|
|
2019-02-28 02:42:48 +08:00
|
|
|
Run
|
|
|
|
|
|
|
|
``` shell
|
|
|
|
start.sh
|
|
|
|
```
|
|
|
|
|
2019-06-27 02:56:21 +08:00
|
|
|
to launch it after [installing Django](https://docs.djangoproject.com/en/2.1/intro/install/).
|
|
|
|
You may need to hack `start.sh` if you are not running Debian or Ubuntu.
|
2017-01-12 20:06:32 +08:00
|
|
|
|
|
|
|
The app will use a local SQLite database. It uses the standard
|
|
|
|
Django authentication database. All users get access to all vhosts and
|
|
|
|
resources.
|
|
|
|
|
2019-06-27 02:56:21 +08:00
|
|
|
The app recognises two users (to make the setup easier): `admin` and `someuser`.
|
|
|
|
Passwords for those users do not matter. user `admin` as tagged as `administrator`.
|
2019-06-27 00:43:25 +08:00
|
|
|
|
2017-02-14 23:51:47 +08:00
|
|
|
### HTTP Endpoint Examples
|
2017-01-12 20:06:32 +08:00
|
|
|
|
2018-07-30 05:51:34 +08:00
|
|
|
`urls.py` and `auth/views.py` are the main modules that describe HTTP routes and
|
|
|
|
views (endpoints).
|
2017-02-14 23:51:47 +08:00
|
|
|
|
2017-02-16 21:04:27 +08:00
|
|
|
|
|
|
|
## Spring Boot Example
|
2017-02-14 23:51:47 +08:00
|
|
|
|
|
|
|
`rabbitmq_auth_backend_spring_boot` is a simple [Spring Boot](https://projects.spring.io/spring-boot/)
|
|
|
|
application that rabbitmq-auth-backend-http can authenticate against. It's really
|
|
|
|
not designed to be anything other than an example.
|
|
|
|
|
2018-07-30 05:45:33 +08:00
|
|
|
### Running the Example
|
2017-02-14 23:51:47 +08:00
|
|
|
|
|
|
|
Import the example as a Maven project in your favorite IDE or run it directly from the command line:
|
|
|
|
|
2018-04-01 04:51:21 +08:00
|
|
|
``` shell
|
2019-02-28 02:42:48 +08:00
|
|
|
mvn spring-boot:run
|
2018-04-01 04:51:21 +08:00
|
|
|
```
|
|
|
|
|
2017-02-14 23:51:47 +08:00
|
|
|
The application listens on the 8080 port.
|
|
|
|
|
|
|
|
### HTTP Endpoint Examples
|
|
|
|
|
|
|
|
Have a look at the `AuthBackendHttpController`. There's only one user: `guest`,
|
|
|
|
with the `guest` password. This implementation also checks the
|
2018-04-01 04:51:21 +08:00
|
|
|
routing key starts with an `a` when publishing to a topic exchange
|
2017-02-16 21:04:27 +08:00
|
|
|
or consuming from a topic. (an example of [topic authorisation](http://next.rabbitmq.com/access-control.html#topic-authorisation)).
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2019-02-28 02:42:48 +08:00
|
|
|
### rabbitmq.config Example
|
|
|
|
|
|
|
|
Below is a [RabbitMQ config file](http://www.rabbitmq.com/configure.html) example to go with this
|
|
|
|
example:
|
|
|
|
|
|
|
|
``` ini
|
|
|
|
auth_backends.1 = http
|
|
|
|
|
|
|
|
auth_http.http_method = post
|
|
|
|
auth_http.user_path = http://localhost:8080/auth/user
|
|
|
|
auth_http.vhost_path = http://localhost:8080/auth/vhost
|
|
|
|
auth_http.resource_path = http://localhost:8080/auth/resource
|
2019-06-03 20:26:29 +08:00
|
|
|
auth_http.topic_path = http://localhost:8080/auth/topic
|
2019-02-28 02:42:48 +08:00
|
|
|
```
|
2018-07-30 05:45:33 +08:00
|
|
|
|
2018-11-02 06:10:26 +08:00
|
|
|
## Spring Boot Kotlin Example
|
|
|
|
|
|
|
|
`rabbitmq_auth_backend_spring_boot_kotlin` is a simple [Spring Boot](https://projects.spring.io/spring-boot/)
|
|
|
|
application written in Kotlin that rabbitmq-auth-backend-http can authenticate against. It's really
|
|
|
|
not designed to be anything other than an example.
|
|
|
|
It contains examples with recommended POST methods and example RabbitMQ configuration.
|
|
|
|
It can be run the same way as the above example.
|
|
|
|
|
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
## ASP.NET Web API Example
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
`rabbitmq_auth_backend_webapi_dotnet` is a very minimalistic ASP.NET Web API application
|
|
|
|
the plugin can authenticate against. It's really
|
|
|
|
**not designed to be anything other than an example**.
|
2017-10-10 18:33:16 +08:00
|
|
|
|
|
|
|
### Running the Example
|
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
Open the WebApiHttpAuthService.csproj in Visual Studio 2017, More details about prerequisites can be found below.
|
|
|
|
|
2018-07-30 05:45:33 +08:00
|
|
|
As with other examples, RabbitMQ [authentication and authorization backends](http://www.rabbitmq.com/access-control.html) must be configured
|
|
|
|
to use this plugin and the endpoints provided by this example app.
|
2017-10-11 04:00:19 +08:00
|
|
|
|
|
|
|
Then Build the solution and run it from Visual Studio.
|
|
|
|
`Controllers/AuthController.cs` contains the authentication and authorization logic.
|
|
|
|
By default All users get access to all vhosts and resources.
|
|
|
|
User "authuser" will be denied access.
|
2017-10-10 18:33:16 +08:00
|
|
|
|
|
|
|
### HTTP Endpoint Examples
|
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
Have a look at `AuthController`.
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
### Development Environment
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
This example was developed using
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
* .NET Framework 4.5
|
|
|
|
* Visual Studio 2017
|
|
|
|
* Windows 10 and IIS v10.0
|
2018-04-01 04:51:21 +08:00
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
It is possible to build and run service from Visual Studio browse the endpoint without using IIS.
|
|
|
|
Port number may vary but will likely be `62190`.
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
When the example is hosted on IIS, port 80 will be used by default.
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2024-01-22 15:52:56 +08:00
|
|
|
## ASP.NET Core 8.0 Example
|
2018-07-29 20:12:43 +08:00
|
|
|
|
2018-07-30 05:45:33 +08:00
|
|
|
`rabbitmq_auth_backend_webapi_dotnetcore` is a modification of the `rabbitmq_auth_backend_webapi_dotnet` example
|
2024-01-22 15:52:56 +08:00
|
|
|
designed for ASP.NET Core 8.0. It's very similar to the original version but it also adds some static typing
|
2018-07-30 05:45:33 +08:00
|
|
|
for requests and responses.
|
2018-07-29 20:12:43 +08:00
|
|
|
|
|
|
|
### Running the Example
|
|
|
|
|
2024-01-22 15:52:56 +08:00
|
|
|
Open the solution file, `RabbitMqAuthBackendHttp.sln` in Visual Studio 2022 version 17.8 or later.
|
2018-07-29 20:12:43 +08:00
|
|
|
|
2018-07-30 05:45:33 +08:00
|
|
|
As with other examples, RabbitMQ [authentication and authorization backends](http://www.rabbitmq.com/access-control.html) must be configured
|
|
|
|
to use this plugin and the endpoints provided by this example app.
|
2018-07-29 20:12:43 +08:00
|
|
|
|
2018-07-30 05:45:33 +08:00
|
|
|
Then build the solution and run it from Visual Studio.
|
2022-11-22 15:43:12 +08:00
|
|
|
You can try the example with Swagger UI (http://localhost:5000/swagger/index.html)
|
|
|
|
|
|
|
|
* `Controllers/AuthController.cs` contains the authentication and authorization logic.
|
|
|
|
* By default All users get access to all vhosts and resources. User "authuser" will be denied access.
|
2018-07-29 20:12:43 +08:00
|
|
|
|
|
|
|
### HTTP Endpoint Examples
|
|
|
|
|
|
|
|
Have a look at `AuthController`.
|
|
|
|
|
|
|
|
### Development Environment
|
|
|
|
|
|
|
|
This example was developed using
|
|
|
|
|
2024-01-22 15:52:56 +08:00
|
|
|
* .NET SDK 8.0
|
|
|
|
* Visual Studio 2022 version 17.8 or Visual Studio Code
|
2018-07-29 20:12:43 +08:00
|
|
|
* Windows 10
|
2019-02-28 02:42:48 +08:00
|
|
|
|
2018-07-29 20:12:43 +08:00
|
|
|
It is possible to build and run service from Visual Studio using IIS or from Visual Studio or Visual Studio Code using cross-platform server Kestrel.
|
|
|
|
|
2022-11-22 15:43:12 +08:00
|
|
|
### rabbitmq.config Example
|
|
|
|
|
|
|
|
Below is a [RabbitMQ config file](https://www.rabbitmq.com/configure.html) example to go with this
|
|
|
|
example:
|
|
|
|
|
|
|
|
``` ini
|
|
|
|
auth_backends.1 = http
|
|
|
|
|
|
|
|
auth_http.http_method = post
|
|
|
|
auth_http.user_path = http://localhost:5000/auth/user
|
|
|
|
auth_http.vhost_path = http://localhost:5000/auth/vhost
|
|
|
|
auth_http.resource_path = http://localhost:5000/auth/resource
|
|
|
|
auth_http.topic_path = http://localhost:5000/auth/topic
|
|
|
|
```
|
2018-07-30 05:45:33 +08:00
|
|
|
|
2019-02-27 22:38:44 +08:00
|
|
|
## PHP Example
|
2018-03-31 16:47:51 +08:00
|
|
|
|
2018-04-01 04:51:21 +08:00
|
|
|
`rabbitmq_auth_backend_php` is a minimalistic PHP application that this plugin can authenticate against.
|
2018-03-31 16:47:51 +08:00
|
|
|
It's really not designed to be anything other than an example.
|
|
|
|
|
|
|
|
### Running the Example
|
|
|
|
|
2018-04-01 04:51:21 +08:00
|
|
|
The example requires PHP >= 5.4 and [Composer](https://getcomposer.org/).
|
2018-03-31 16:47:51 +08:00
|
|
|
|
|
|
|
The `rabbitmq-auth-backend-http-php` library depend on `symfony/security` and `symfony/http-foundation` components.
|
|
|
|
Go to the `rabbitmq_auth_backend_php` folder and run `composer install`.
|
|
|
|
|
2019-02-28 02:42:48 +08:00
|
|
|
``` shell
|
2018-04-01 04:51:21 +08:00
|
|
|
cd rabbitmq_auth_backend_php/
|
|
|
|
composer install
|
2018-03-31 16:47:51 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
Now you can run the PHP 5.4 server (server at http://127.0.0.1:8080)
|
|
|
|
|
2019-02-28 02:42:48 +08:00
|
|
|
``` shell
|
2018-04-01 04:51:21 +08:00
|
|
|
composer start
|
2018-03-31 16:47:51 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
Ensure the log file is writable `rabbitmq-auth-backend-http/examples/rabbitmq_auth_backend_php/var/log.log`.
|
|
|
|
|
2018-04-01 04:51:21 +08:00
|
|
|
Go to `http://localhost:8080/user.php?username=Anthony&password=anthony-password`, all work properly if you see `Allow administrator`
|
|
|
|
|
2018-03-31 16:47:51 +08:00
|
|
|
|
|
|
|
### HTTP Endpoint Examples
|
|
|
|
|
|
|
|
Have a look at the `bootstrap.php`. By default this example implement the same authorization rules than RabbitMQ.
|
|
|
|
|
|
|
|
Users list:
|
|
|
|
|
|
|
|
| User | password | is admin | Vhost | Configure regex | Write regex | Read regex | tags |
|
|
|
|
|--|--|--|--|--|--|--|--|
|
|
|
|
| Anthony | anthony-password | ✔️ | All | All | All | All | administrator |
|
|
|
|
| James | bond | | / | .* | .* | .* | management |
|
|
|
|
| Roger | rabbit | | | | | | monitoring |
|
|
|
|
| bunny | bugs | | | | | | policymaker |
|
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
### rabbitmq.config Example
|
|
|
|
|
|
|
|
Below is a [RabbitMQ config file](http://www.rabbitmq.com/configure.html) example to go with this
|
|
|
|
example:
|
2017-10-10 18:33:16 +08:00
|
|
|
|
2018-04-01 04:51:21 +08:00
|
|
|
``` ini
|
|
|
|
auth_backends.1 = internal
|
2018-03-31 16:47:51 +08:00
|
|
|
auth_backends.2 = http
|
2019-02-28 02:42:48 +08:00
|
|
|
|
2018-04-01 04:51:21 +08:00
|
|
|
auth_http.user_path = http://localhost:62190/auth/user.php
|
|
|
|
auth_http.vhost_path = http://localhost:62190/auth/vhost.php
|
|
|
|
auth_http.resource_path = http://localhost:62190/auth/resource.php
|
2018-03-31 16:47:51 +08:00
|
|
|
auth_http.topic_path = http://localhost:62190/auth/topic.php
|
|
|
|
```
|
|
|
|
|
2017-10-11 04:00:19 +08:00
|
|
|
See [RabbitMQ Access Control guide](http://www.rabbitmq.com/access-control.html) for more information.
|
2019-06-27 00:43:25 +08:00
|
|
|
|
2019-06-27 02:56:21 +08:00
|
|
|
## Running with Docker Compose
|
2019-06-27 00:43:25 +08:00
|
|
|
|
2019-06-27 02:56:21 +08:00
|
|
|
An example node can be started using a provided `docker-compose.yml` file that sets up RabbitMQ.
|
|
|
|
There's also a file that sets up the Django example above:
|
2019-06-27 00:43:25 +08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
docker-compose -f docker-compose.yml -f rabbitmq_auth_backend_django/docker-compose.yml up --build
|
|
|
|
```
|
|
|
|
|
2019-06-27 02:56:21 +08:00
|
|
|
Another file, `docker/nodered/docker-compose.yml`, will run [nodered](https://nodered.org/) on port 1880
|
|
|
|
with a configured MQTT client that will connect to RabbitMQ and perform basic operations that will trigger
|
|
|
|
requests to the example service:
|
2019-06-27 00:43:25 +08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
docker-compose -f docker-compose.yml -f rabbitmq_auth_backend_django/docker-compose.yml -f docker/nodered/docker-compose.yml up --build
|
|
|
|
```
|
|
|
|
|
2019-06-27 02:56:21 +08:00
|
|
|
Edit the provided [config file](docker/rabbitmq.conf) and enable caching and logging settings.
|