日本語

How to use an MQTT broker with SSL/TLS authentication (client authentication)

Overview

This page describes how to connect from SINETStream to an MQTT broker that requires SSL/TLS two-way authentication.

The description will be made in the following order.

  1. Prerequisites
  2. Configurations on the Mosquitto MQTT broker (server side)
  3. Configurations on SINETStream (client side)
  4. Behavior on authentication errors

Prerequisites

Though the configuration and setting of an MQTT broker may vary, the following conditions are assumed for simplicity in this document.

(*1) Refer to How to create a certificate with a private certificate authority for details.

The following values are used in the examples.

In practice, use appropriate values for your environment.

Configurations on the MQTT broker (server side)

The following procedure is needed for the Mosquitto MQTT broker to perform SSL/TLS two-way authentication.

  1. Edit the MQTT broker’s configuration file
  2. Reload the configuration file

Edit the MQTT broker’s configuration file

Add the following lines to the MQTT broker’s configuration file /etc/mosquitto/mosquitto.conf.

per_listener_settings true
listener 8883
cafile /etc/pki/CA/cacert.pem
certfile /etc/pki/CA/certs/broker.crt
keyfile /etc/pki/CA/private/broker.key
require_certificate true

The meanings of the above settings are:

Reload the configuration file

Send a SIGHUP signal to reload the configuration file.

$ sudo killall -HUP mosquitto

Configurations on SINETStream (client side)

The following procedure is needed for SINETStream to connect to the MQTT broker with authentication.

  1. Prepare certificate
  2. Edit the SINETStream’s configuration file
  3. Create a program that uses SINETStream

Prepare certificate

The following certificates are required on the client side to use SSL/TLS two-way authentication.

Put the certificates created by a private CA etc. to your convenient location. SINETStream reads the certificate from the path specified in the configuration file.

Edit the SINETStream’s configuration file

An example of SINETStream’s configuration file is shown below.

service-mqtt-ssl:
  brokers: broker.example.org:9093
  type: mqtt
  topic: topic-001
  tls:
    ca_certs: /opt/certs/cacert.pem
    certfile: /home/user01/certs/client0.crt
    keyfile: /home/user01/certs/client0.key

The settings for brokers, type, topic, consistency, tls are identical to those without authentication. Settings related to SSL/TLS authentication are under tls:.

Create a program that uses SINETStream

Your program will be identical with or without SSL/TLS authentication. For example, a program that uses MessageWriter of the SINETStream’s Python API is shown below.

with sinetstream.MessageWriter(service='service-mqtt-ssl') as writer:
    writer.publish(b'message 001')

As you see, no code is written for authentication.

If you want to configure the authentication within your program, add parameters to the constructor arguments.

tls = {
    'ca_certs': '/opt/certs/cacert.pem',
    'certfile': '/home/user01/certs/client0.crt',
    'keyfile': '/home/user01/certs/client0.key',
}
with sinetstream.MessageWriter(service='service-mqtt', tls=tls) as writer:
    writer.publish(b'message 001')

Behavior on authentication errors

Python API

The methods listed below raises the sinetstream.error.ConnectionError exception when an authentication error occurs.

Java API

The methods listed below throws the jp.ad.sinet.stream.api.AuthenticationException exception when an authentication error occurs.