Skip to content

reddec/fluent-amqp

Repository files navigation

Fluent AMQP

FA

Docker Automated build Telegram Sender Snap Status donate Download

Library that provides fluent and easy wrapper over streadway-amqp API. Adds such features like:

  • Reconnectiong. Will restore all defined infrastructure
  • Non-blocking processing of messages
  • Optional auto-requeue (with delay)
  • Signing and verifiying messages by public/private pair

API documentation

Signing and verification

Hash algorithm (x509) SHA512 with RSA, sign - SHA512.

The signer (producer) should use private key to sign content of message body and message id.

The validator (consumer) should use public certificate to validate content of message and message id against signature and should drops invalid or duplicated messages.

The sign should be put to a header (X-Signature by default: see DefaultSignatureHeader constant in godoc) as binary object (not hex or base64 encoded).

DATA = BYTES(ID) ... BYTES(BODY)
# SIGN via PKCS#1 v1.5
SIGN_HEADER_BODY = SIGN_SHA512(PRIVATE_KEY, DATA)

States

image

Default message

Message by default has:

  • Delivery type - persistent
  • Time - current time in UTC

Templates

amqp-recv supports templating output -o template. Template content read from STDIN.

Root template object is a amqp.Delivery with functions from Sprig plus additional methods like:

  • asText - converts bytes to string
  1. Basic example:

Print same as -o plain.

echo '{{- .Body | asText -}}' | amqp-recv -o template ...
  1. Notification to telegram

Use combination of basic CLI utils and templates.

TOKEN="xxxyyyy"        # BotFather token for Telegram (see here: https://t.me/BotFather)
CHAT_ID="1234567"      # Target Telegram chat ID (find yours: https://t.me/MyTelegramID_bot)
QUEUE="notification"   # queue name should be defined if persistent required
EXCHANGE="amqp.topic"  # source of notification
TYPE="topic"           # exchange type
TOPIC="#"              # specify subject that will be sent over telegram (# - everything)

while true; do
  echo -n -e 'Subject: {{.RoutingKey}}\n\n{{.Body | asText}}' | amqp-recv -o template -Q $QUEUE -e $EXCHANGE -k $TYPE "$TOPIC" > message.txt
  curl -f -X POST --data "text=$(cat message.txt)" --data "chat_id=${CHAT_ID}" "https://api.telegram.org/bot${TOKEN}/sendMessage" || exit 1
done

Command line utilities

  • amqp-exec - CGI like daemon to listen message and run executable (and send reply)
  • amqp-recv - Receive message from AMQP (like cat command)
  • amqp-send - Send message to AMQP (like wall command)

Installation

Get it from the Snap Store

  • snapcraft: fluent-amqp

    after installation commands are available with fluent-amqp. prefixes (i.e. fluent-amqp.amqp-exec)

  • pre-build binaries for all major platform

  • From bintray repository for most debian-based distribution (trusty, xenial, bionic, buster, wheezy):

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
echo "deb https://dl.bintray.com/reddec/debian {distribution} main" | sudo tee -a /etc/apt/sources.list
sudo apt install amqp-send amqp-exec amqp-recv