diff --git a/coap/README.md b/coap/README.md index 854e9339244..ee9c090bc28 100644 --- a/coap/README.md +++ b/coap/README.md @@ -64,4 +64,4 @@ MF_THINGS_URL=[Things service URL] MF_NATS_URL=[NATS instance URL] MF_COAP_ADAPT ## Usage If CoAP adapter is running locally (on default 5683 port), a valid URL would be: `coap://localhost/channels//messages?authorization=`. -Since CoAP protocol does not support `Authorization` header (option) and options have limited size, in order to send CoAP messages, valid `authorization` value must be present in `Uri-Query` option. +Since CoAP protocol does not support `Authorization` header (option) and options have limited size, in order to send CoAP messages, valid `authorization` value (a valid Thing key) must be present in `Uri-Query` option. diff --git a/docs/authentication.md b/docs/authentication.md new file mode 100644 index 00000000000..69c1c2d7621 --- /dev/null +++ b/docs/authentication.md @@ -0,0 +1,85 @@ +## Authentication using Mainflux keys +By default, Mainflux uses Mainflux keys for authentication. The Мainflux key is a secret key that's generated at the Thing creation. In order to authenticate, the Thing needs to send its key with the message. The way the key is passed depends on the protocol used to send a message and differs from adapter to adapter. For more details on how this key is passed around, please check out [messaging section](https://mainflux.readthedocs.io/en/latest/messaging). +This is the default Mainflux authentication mechanism and this method is used if the composition is started using the following command: + +```bash +docker-compose -f docker/docker-compose.yml up +``` + +## Mutual authentication + +In most of the cases, HTTPS, WSS, MQTTS or secure CoAP are secure enough. However, sometimes you might need an even more secure connection. Mainflux supports mutual TLS authentication (_mTLS_) based on [X.509 certificates](https://tools.ietf.org/html/rfc5280). By default, the TLS protocol only proves the identity of the server to the client using the X.509 certificate and the authentication of the client to the server is left to the application layer. TLS also offers client-to-server authentication using client-side X.509 authentication. This is called two-way or mutual authentication. Mainflux currently supports mTLS over HTTP, WS, and MQTT protocols. In order to run Docker composition with mTLS turned on, you can execute the following command from the project root: + +```bash +AUTH=x509 docker-compose -f docker/docker-compose.yml up -d +``` + +Mutual authentication includes client-side certificates. Certificates can be generated using the simple script provided [here](http://www.github.com/mainflux/mainflux/tree/master/docker/ssl/Makefile). In order to create a valid certificate, you need to create Mainflux thing using the process described in the [provisioning section](provisioning.md). After that, you need to fetch created thing key. Thing key will be used to create x.509 certificate for the corresponding thing. To create a certificate, execute the following commands: + +```bash +cd docker/ssl +make ca +make server_cert +make thing_cert KEY= CRT_FILE_NAME= +``` +These commands use [OpenSSL](https://www.openssl.org/) tool, so please make sure that you have it installed and set up before running these commands. + + - Command `make ca` will generate a self-signed certificate that will later be used as a CA to sign other generated certificates. CA will expire in 3 years. + - Command `make server_cert` will generate and sign (with previously created CA) server cert, which will expire after 1000 days. This cert is used as a Mainflux server-side certificate in usual TLS flow to establish HTTPS, WSS, or MQTTS connection. + - Command `make thing_cert` will finally generate and sign a client-side certificate and private key for the thing. + +In this example `` represents key of the thing, and `` represents the name of the certificate and key file which will be saved in `docker/ssl/certs` directory. Generated Certificate will expire after 2 years. The key must be stored in the x.509 certificate `CN` field. This script is created for testing purposes and is not meant to be used in production. We strongly recommend avoiding self-signed certificates and using a certificate management tool such as [Vault](https://www.vaultproject.io/) for the production. + +Once you have created CA and server-side cert, you can spin the composition using: + +```bash +AUTH=x509 docker-compose -f docker/docker-compose.yml up -d +``` + +Then, you can create user and provision things and channels. Now, in order to send a message from the specific thing to the channel, you need to connect thing to the channel and generate corresponding client certificate using aforementioned commands. To publish a message to the channel, thing should send following request: + +### HTTPS +```bash +curl -s -S -i --cacert docker/ssl/certs/ca.crt --cert docker/ssl/certs/.crt --key docker/ssl/certs/.key --insecure -X POST -H "Content-Type: application/senml+json" https://localhost/http/channels//messages -d '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]' +``` + +### MQTTS + +#### Publish +```bash +mosquitto_pub -u -P -t channels//messages -h localhost --cafile docker/ssl/certs/ca.crt --cert docker/ssl/certs/.crt --key docker/ssl/certs/.key -m '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]' +``` + +#### Subscribe +``` +mosquitto_sub -u -P --cafile docker/ssl/certs/ca.crt --cert docker/ssl/certs/.crt --key docker/ssl/certs/.key -t channels//messages -h localhost +``` + +### WSS +```javascript +const WebSocket = require('ws'); + +// Do not verify self-signed certificates if you are using one. +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' + +// Replace and with real values. +const ws = new WebSocket('wss://localhost/ws/channels//messages?authorization=', +// This is ClientOptions object that contains client cert and client key in the form of string. You can easily load these strings from cert and key files. +{ + cert: `-----BEGIN CERTIFICATE-----....`, + key: `-----BEGIN RSA PRIVATE KEY-----.....` +}) + +ws.on('open', () => { + ws.send('something') +}) + +ws.on('message', (data) => { + console.log(data) +}) +ws.on('error', (e) => { + console.log(e) +}) +``` + +As you can see, `Authorization` header does not have to be present in the HTTP request, since the key is present in the certificate. However, if you pass `Authorization` header, it _must be the same as the key in the cert_. In the case of MQTTS, `password` filed in CONNECT message _must match the key from the certificate_. In the case of WSS, `Authorization` header or `authorization` query parameter _must match cert key_. \ No newline at end of file diff --git a/docs/bootstrap.md b/docs/bootstrap.md index 0f24539e807..eb4bae65626 100644 --- a/docs/bootstrap.md +++ b/docs/bootstrap.md @@ -8,7 +8,237 @@ Mainflux platform supports bootstrapping process, but some of the preconditions > Bootstrapping and provisioning are two different procedures. Provisioning refers to entities management while bootstrapping is related to entity configuration. -![bootstrapping flow](img/bs_flow.gif) + + +
+ +
+ ### Configuration diff --git a/docs/img/bootstrap/1.png b/docs/img/bootstrap/1.png new file mode 100644 index 00000000000..c6741c6539d Binary files /dev/null and b/docs/img/bootstrap/1.png differ diff --git a/docs/img/bootstrap/2.png b/docs/img/bootstrap/2.png new file mode 100644 index 00000000000..f404fd454fd Binary files /dev/null and b/docs/img/bootstrap/2.png differ diff --git a/docs/img/bootstrap/3.png b/docs/img/bootstrap/3.png new file mode 100644 index 00000000000..4e8492e6b4a Binary files /dev/null and b/docs/img/bootstrap/3.png differ diff --git a/docs/img/bootstrap/4.png b/docs/img/bootstrap/4.png new file mode 100644 index 00000000000..a23bac56db6 Binary files /dev/null and b/docs/img/bootstrap/4.png differ diff --git a/docs/img/bootstrap/5.png b/docs/img/bootstrap/5.png new file mode 100644 index 00000000000..077232b8f0f Binary files /dev/null and b/docs/img/bootstrap/5.png differ diff --git a/docs/img/bootstrap/6.png b/docs/img/bootstrap/6.png new file mode 100644 index 00000000000..11f7f7dd742 Binary files /dev/null and b/docs/img/bootstrap/6.png differ diff --git a/docs/img/bs_flow.gif b/docs/img/bs_flow.gif deleted file mode 100644 index 605f96d5a32..00000000000 Binary files a/docs/img/bs_flow.gif and /dev/null differ diff --git a/docs/security.md b/docs/security.md index dd27a680305..65728c58323 100644 --- a/docs/security.md +++ b/docs/security.md @@ -1,12 +1,6 @@ -# **SECURING COMMUNICATION** +## Server configuration -By default gRPC communication is not secure as Mainflux system is most often run in a private network behind the reverse proxy. - -However, TLS can be activated and configured. - -# Server configuration - -## Securing PostgreSQL connections +### Securing PostgreSQL connections By default, Mainflux will connect to Postgres using insecure transport. If a secured connection is required, you can select the SSL mode and set paths to any extra certificates and keys needed. @@ -24,8 +18,13 @@ If a secured connection is required, you can select the SSL mode and set paths t Supported database connection modes are: `disabled` (default), `required`, `verify-ca` and `verify-full`. ## Securing gRPC +By default gRPC communication is not secure as Mainflux system is most often run in a private network behind the reverse proxy. + +However, TLS can be activated and configured. + +### Server configuration -### Users +#### Users If either the cert or key is not set, the server will use insecure transport. @@ -33,7 +32,7 @@ If either the cert or key is not set, the server will use insecure transport. `MF_USERS_SERVER_KEY` the path to the server key in pem format. -### Things +#### Things If either the cert or key is not set, the server will use insecure transport. @@ -41,91 +40,14 @@ If either the cert or key is not set, the server will use insecure transport. `MF_THINGS_SERVER_KEY` the path to the server key in pem format. -## Client configuration +### Client configuration If you wish to secure the gRPC connection to `things` and `users` services you must define the CAs that you trust. This does not support mutual certificate authentication. -### HTTP Adapter +#### Adapter configuration -`MF_HTTP_ADAPTER_CA_CERTS` - the path to a file that contains the CAs in PEM format. If not set, the default connection will be insecure. If it fails to read the file, the adapter will fail to start up. +`MF_HTTP_ADAPTER_CA_CERTS`, `MF_MQTT_ADAPTER_CA_CERTS`, `MF_WS_ADAPTER_CA_CERTS`, `MF_COAP_ADAPTER_CA_CERTS` - the path to a file that contains the CAs in PEM format. If not set, the default connection will be insecure. If it fails to read the file, the adapter will fail to start up. -### Things +#### Things `MF_THINGS_CA_CERTS` - the path to a file that contains the CAs in PEM format. If not set, the default connection will be insecure. If it fails to read the file, the service will fail to start up. - -# Mutual authentication - -In the most of the cases, HTTPS, WSS, MQTTS or secure CoAP are secure enough. However, sometimes you might need even more secure connection. Mainflux supports mutual TLS authentication (_mTLS_) based on (X.509 certificates)[https://tools.ietf.org/html/rfc5280]. By default the TLS protocol only proves the identity of the server to the client using X.509 certificate and the authentication of the client to the server is left to the application layer. TLS also offers client-to-server authentication using client-side X.509 authentication. This is called two-way or mutual authentication. Mainflux currently supports mTLS over HTTP, WS, and MQTT protocols. In order to run Docker composition with mTLS turned on, you can execute following command from the project root: - -```bash -AUTH=x509 docker-compose -f docker/docker-compose.yml up -d -``` - -Mutual authentication includes client side certificates. Certificates can be generated using simple script provided (here)[http://www.github.com/mainflux/mainflux/tree/master/docker/ssl/Makefile]. In order to create a valid certificate, you need to create Mainflux thing using the process described in the [provisioning section](provisioning.md). After that, you need to fetch created thing key. Thing key will be used to create x.509 certificate for corresponding thing. TO create certificate, execute following commands: - -```bash -cd docker/ssl -make ca -make server_cert -make thing_cert KEY= CRT_FILE_NAME= -``` -These commands use (OpenSSL)[https://www.openssl.org/] tool, so please make sure that you have it installed and set up before running these commands. - - - Command `make ca` wil generate self-signed certificate that will later be used as a CA to sign other generated certificates. CA will expire in 3 years. - - Command `make server_cert` will generated and sign (with previously created CA) server cert, which will expire after 1000 days. This cert is used as a Mainflux server-side certificate in usual TLS flow to establish HTTPS, WSS, or MQTTS connection. - - Command `make thing_cert` wil finally generate and sign client-side certificate and private key for the thing. - -In this example `` represents key of the thing, and `` represents name of the certificate and key file which will be saved in `docker/ssl/certs` directory. Generated Certificate will expire after 2 years. The key must be stored in the x.509 certificate "CN" field. This script is created for the testing purposes and is not meant to be used in production. We strongly recommend avoiding self-signed certificates and using certificate management tool such as (Vault)[https://www.vaultproject.io/] for the production. - -Once you have created CA and server-side cert, you can spin the composition using: - -```bash -AUTH=x509 docker-compose -f docker/docker-compose.yml up -d -``` - -Then, you can create user and provision things and channels. Now, in order to send a message from the specific thing to the channel, you need to connect thing to the channel and generate corresponding client certificate using aforementioned commands. To publish a message to the channel, thing should send following request: - -_HTTPS:_ -```bash -curl -s -S -i --cacert docker/ssl/certs/ca.crt --cert docker/ssl/certs/.crt --key docker/ssl/certs/.key --insecure -X POST -H "Content-Type: application/senml+json" https://localhost/http/channels//messages -d '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]' -``` - -_MQTTS_: - -###### PUBLISH -```bash -mosquitto_pub -u -P -t channels//messages -h localhost --cafile docker/ssl/certs/ca.crt --cert docker/ssl/certs/.crt --key docker/ssl/certs/.key -m '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]' -``` -###### SUBSCRIBE -``` -mosquitto_sub -u -P --cafile docker/ssl/certs/ca.crt --cert docker/ssl/certs/.crt --key docker/ssl/certs/.key -t channels//messages -h localhost -``` - -_WSS:_ -```javascript -const WebSocket = require('ws'); - -// Do not verify self-signed certificates if you are using one. -process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' - -// Replace and with real values. -const ws = new WebSocket('wss://localhost/ws/channels//messages?authorization=', -// This is ClientOptions object that contains client cert and client key in the form of string. You can easily load these strings from cert and key files. -{ - cert: `-----BEGIN CERTIFICATE-----....`, - key: `-----BEGIN RSA PRIVATE KEY-----.....` -}) - -ws.on('open', () => { - ws.send('something') -}) - -ws.on('message', (data) => { - console.log(data) -}) -ws.on('error', (e) => { - console.log(e) -}) -``` - -As you can see, `Authorization` header does not have to be present in the the HTTP request, since the key is present in the certificate. However, if yoy pass `Authorization` header, it _must be the same as the key in the cert_. In the case of MQTTS, `password` filed in CONNECT message _must match the key from the certificate_. In the case of WSS, `Authorization` header or `authorization` query parameter _must match cert key_. \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 5415de4c42a..96a863d364e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -5,7 +5,7 @@ # SPDX-License-Identifier: Apache-2.0 # -copyright: Copyright (c) 2015-2018 Mainflux +copyright: Copyright (c) 2015-2019 Mainflux repo_url: https://github.com/mainflux/mainflux site_description: Mainflux IoT System site_name: Mainflux @@ -34,6 +34,7 @@ pages: - Storage: storage.md - LoRa: lora.md - Security: security.md +- Authentication: authentication.md - CLI: cli.md - Bootstrap: bootstrap.md - Developer's Guide: dev-guide.md