RabbitMQ

RabbitMQ Server

docker-compose.yml

version: "3"
services:
  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq-server
    volumes:
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
      - ./cert/ca.crt:/cert/ca.crt
      - ./cert/server.crt:/cert/server.crt
      - ./key/server.key:/key/server.key
    ports:
      - 5671:5671
      - 5672:5672
      - 15672:15672

rabbitmq.conf

listeners.ssl.default = 5671

default_user = alice
default_pass = alice@123
ssl_options.cacertfile = ./cert/ca.crt
ssl_options.certfile = ./cert/server.crt
ssl_options.keyfile = ./key/server.key
ssl_options.verify = verify_peer
ssl_options.fail_if_no_peer_cert = true
$ docker-compose -f docker-compose.yml up

Consumer

rabbitmq_consumer_credentials_auth.bal

import ballerina/io;
import ballerinax/rabbitmq;

listener rabbitmq:Listener securedEP = new ("localhost", 5671,
    auth = {
        username: "alice",
        password: "alice@123"
    },
    secureSocket = {
        cert: "./resources/cert/server.crt",
        key: {
            certFile: "./resources/cert/client.crt",
            keyFile: "./resources/key/client.key"
        },
        protocol: {
            name: rabbitmq:TLS
        }
    }
);

@rabbitmq:ServiceConfig {
    queueName: "demo.security"
}
isolated service rabbitmq:Service on securedEP {
    isolated remote function onMessage(rabbitmq:Message message) {
        string|error messageContent = string:fromBytes(message.content);
        if messageContent is string {
            io:println("Received message: ", messageContent);
        }
    }
}
$ bal run rabbitmq_consumer_credentials_auth.bal

Producer

rabbitmq_producer_credentials_auth.bal

import ballerina/io;
import ballerinax/rabbitmq;

final rabbitmq:Client securedEP = check new ("localhost", 5671,
    auth = {
        username: "alice",
        password: "alice@123"
    },
    secureSocket = {
        cert: "./resources/cert/server.crt",
        key: {
            certFile: "./resources/cert/client.crt",
            keyFile: "./resources/key/client.key"
        },
        protocol: {
            name: rabbitmq:TLS
        }
    }
);

public function main() returns error? {
    check securedEP->queueDeclare("demo.security");
    string message = "Hello, World!";
    check securedEP->publishMessage({
        content: message.toBytes(),
        routingKey: "demo.security"
    });
    check securedEP->close();
    io:println("Message published successfully.");
}
$ bal run rabbitmq_producer_credentials_auth.bal

Resources

ca.crt

-----BEGIN CERTIFICATE-----
MIIDPjCCAiYCCQDiEWXPiJfzKjANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJM
SzEQMA4GA1UECAwHV2VzdGVybjEQMA4GA1UEBwwHQ29sb21ibzENMAsGA1UECgwE
QVZJWDELMAkGA1UECwwCQ0ExEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0yMTA2MzAx
MjU2NTBaFw0zMTA2MjgxMjU2NTBaMGExCzAJBgNVBAYTAkxLMRAwDgYDVQQIDAdX
ZXN0ZXJuMRAwDgYDVQQHDAdDb2xvbWJvMQ0wCwYDVQQKDARBVklYMQswCQYDVQQL
DAJDQTESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEA0SlFT4SbEx2zGyzPTrdJSHOPdCEd7gsy6nheyIsjHECngqVVKYwH
Jq4c3RByWJfggvy/NP+mSqi7zCn+lZY1i39TpBmYxF/rhFYIEFNDB8d8pgvZ+0AR
3eB0baaYkkKsMokTjnX43AUm1i+nMTWTTy6thIvabEyT+YgdvoFEPOzQ9UsyeaQ2
bhryuZtQFYi4k3FjSONr8eKxdpjNjOgY03yi8BRW69e+s9Sa8jOMGhhphRpPeuSf
mJMEiRMtBlHsl1QkBRJs/l/rhr9M56CkDEsevG9EQaYsvW7hb1ChU13PksJ9uFWV
mclkwxFFvGXIwV3DJqR9qe/o43usO443XQIDAQABMA0GCSqGSIb3DQEBCwUAA4IB
AQCwzA0H0FCCIuO09gMrdtZIglbsGLRRRqmfXXRWNg63vOUSqSy50JucZtijRMTA
6lKDG1xZopBtI2G5P2SVDmVgwBiJYFxWGMlkF3k6Urid3HO1fQ6ljtmx21kOCtK+
0t++t2oRSpL1qXZkaTK29H4ExpHjlaxQSi9dYvnnPa/1RiIgjzUVt4SSwSI4x8LH
KLll0aRYel/nth7+K8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