forked from eclipse-sdv-blueprints/fleet-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-config-hono.sh
executable file
·293 lines (268 loc) · 10.3 KB
/
create-config-hono.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
# SPDX-FileCopyrightText: 2023 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
# This script can be used to provision a tenant and a device to an existing
# Hono instance.
#
# By default, the device will be provisioned to the Hono sandbox.
#
set -e
HONO_HOST="hono.eclipseprojects.io"
HONO_REGISTRY_PORT="28443"
HONO_TENANT_ID=""
HONO_TENANT_CA=""
HONO_DEVICE_ID=""
HONO_DEVICE_PASSWORD=""
HONO_DEVICE_CERT=""
HONO_DEVICE_KEY=""
HONO_KAFKA_BROKERS=""
HONO_KAFKA_SECURE_PORT=9094
HONO_KAFKA_USER="hono"
HONO_KAFKA_PASSWORD="hono-secret"
TRUST_STORE_PATH="/etc/ssl/certs/ca-certificates.crt"
ENABLE_HOSTNAME_VALIDATION="true"
OUT_DIR="."
PROVISION_TO_HONO=""
print_usage() {
cmd_name=$(basename "$0")
cat <<EOF >&2
Usage: ${cmd_name} OPTIONS ...
OPTIONS
-h | --help Display this usage information.
-t | --tenant ID The identifier of the tenant to create the device for.
--tenant-ca PATH The path to a PEM file containing an X.509 certificate which should be set as the tenant's
trust anchor for authenticating devices using a client certificate.
--device-id ID The identifier of the device to create.
--device-pwd PWD The password that the device needs to use for authenticating to Hono's MQTT adapter.
--device-cert PATH The path to a PEM file containing an X.509 certificate that the device uses for authenticating
to Hono's MQTT adapter.
--device-key PATH The path to a PEM file containing the private key that the device uses for authenticating to
Hono's MQTT adapter.
-H | --host HOST The host name or IP address of Hono's device registry. [${HONO_HOST}]
-p | --port PORT The TLS port of the Hono device registry. [${HONO_REGISTRY_PORT}]
--kafka-brokers LIST A comma separated list of host name/IP address:port tuples of the Kafka broker(s) to consume
messages from. [${HONO_HOST}:${HONO_KAFKA_SECURE_PORT}]
--kafka-user USER The username to use for authenticating to the Kafka broker(s) to consume messages from. [${HONO_KAFKA_USER}]
--kafka-pwd PWD The password to use for authenticating to the Kafka broker(s) to consume messages from. [${HONO_KAFKA_PASSWORD}]
--out-dir PATH The path to the folder to write configuration files to. [${OUT_DIR}]
--provision Also provision device information to Hono's Device Registry.
--trust-store-path PATH The path to a file that contains PEM encoded trusted root certificates. [${TRUST_STORE_PATH}]
--disable-hostname-validation Disables matching of server certificates against the hostname/IP address used by a client to
connect to the server.
Examples:
Create configuration files in current working directory and provision device information to Hono Sandbox
${cmd_name} --tenant my-test-tenant --device-id my-device --device-pwd verysecret --provision
Create configuration files in home directory
${cmd_name} -t my-test-tenant --device-id my-device --device-pwd verysecret -o ~/
EOF
}
error_and_exit() {
echo "ERROR: $1" >&2
echo
print_usage
exit 1
}
provision_device() {
REGISTRY_URI="https://${HONO_HOST}:${HONO_REGISTRY_PORT}/v1"
OPENSSL_CMD=$(which openssl)
TRUST_ANCHOR_DEF=""
if [[ -n ${HONO_TENANT_CA} ]]; then
if [[ -z "${OPENSSL_CMD}" ]]; then
echo "Could not find openssl command, skipping registration of trust anchor with tenant ..."
else
CERT=$(openssl x509 -in "${HONO_TENANT_CA}" -outform PEM | sed /^---/d | sed -z 's/\n//g')
TRUST_ANCHOR_DEF='{"cert": "'${CERT}'"}'
fi
fi
curl --silent --show-error --fail -X POST -H "content-type: application/json" --data-binary '{
"trusted-ca": ['"${TRUST_ANCHOR_DEF}"'],
"ext": {
"messaging-type": "kafka"
}
}' "${REGISTRY_URI}/tenants/${HONO_TENANT_ID}"
echo "Successfully created tenant [${HONO_TENANT_ID}] in Hono registry [${REGISTRY_URI}]"
curl --silent --show-error --fail -X POST "${REGISTRY_URI}/devices/${HONO_TENANT_ID}/${HONO_DEVICE_ID}"
echo "Successfully added vehicle device [${HONO_DEVICE_ID}] to tenant [${HONO_TENANT_ID}]"
if [[ -n "${HONO_DEVICE_PASSWORD}" ]]; then
curl --silent --show-error --fail -X PUT -H "content-type: application/json" --data-binary '[{
"type": "hashed-password",
"auth-id": "'"${HONO_DEVICE_ID}"'",
"secrets": [{
"pwd-plain": "'"${HONO_DEVICE_PASSWORD}"'"
}]
}]' "${REGISTRY_URI}/credentials/${HONO_TENANT_ID}/${HONO_DEVICE_ID}"
echo "Successfully created hashed password credentials for vehicle device [${HONO_DEVICE_ID}]"
fi
if [[ -n "${HONO_DEVICE_CERT}" ]]; then
if [[ -z "${OPENSSL_CMD}" ]]; then
echo "Could not find openssl command, skipping registration of X.509 certificate based credentials ..."
else
CERT=$(openssl x509 -in "${HONO_DEVICE_CERT}" -outform PEM | sed /^---/d | sed -z 's/\n//g')
curl --silent --show-error --fail -X PUT -H "content-type: application/json" --data-binary '[{
"type": "x509-cert",
"cert": "'"${CERT}"'",
"secrets": [{}]
}]' "${REGISTRY_URI}/credentials/${HONO_TENANT_ID}/${HONO_DEVICE_ID}"
echo "Successfully created X.509 client certificate based credentials for vehicle device [${HONO_DEVICE_ID}]"
fi
fi
}
# shellcheck source=create-config-functions.sh
source "${0%/*}/create-config-functions.sh"
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-h | --help )
print_usage
exit 1
;;
-t | --tenant )
shift; HONO_TENANT_ID=$1
;;
--tenant-ca )
shift; HONO_TENANT_CA=$1
;;
--device-id )
shift; HONO_DEVICE_ID=$1
;;
--device-pwd )
shift; HONO_DEVICE_PASSWORD=$1
;;
--device-cert )
shift; HONO_DEVICE_CERT=$1
;;
--device-key )
shift; HONO_DEVICE_KEY=$1
;;
-H | --host )
shift; HONO_HOST=$1
;;
-p | --port )
shift; HONO_REGISTRY_PORT=$1
;;
--kafka-brokers )
shift; HONO_KAFKA_BROKERS=$1
;;
--kafka-user )
shift; HONO_KAFKA_USER=$1
;;
--kafka-pwd )
shift; HONO_KAFKA_PASSWORD=$1
;;
--trust-store-path )
shift; TRUST_STORE_PATH=$1
;;
-o | --out-dir )
shift; OUT_DIR=$1
;;
--provision )
PROVISION_TO_HONO=1
;;
--disable-hostname-validation )
ENABLE_HOSTNAME_VALIDATION="false"
;;
*)
echo "Ignoring unknown option: $1"
echo "Run with flag -h for usage"
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
if [[ -z "${HONO_TENANT_ID}" ]]; then
error_and_exit "Missing required option: tenant"
fi
if [[ -z "${HONO_DEVICE_ID}" ]]; then
error_and_exit "Missing required option: device-id"
fi
if [[ -z "${HONO_DEVICE_PASSWORD}" && -z ${HONO_DEVICE_CERT} ]]; then
error_and_exit "Missing required option: device-pwd | device-cert"
fi
if [[ -n "${HONO_DEVICE_CERT}" ]]; then
if [[ -z "${HONO_DEVICE_KEY}" ]]; then
error_and_exit "Missing required option: device-key"
elif [[ ! -e ${HONO_DEVICE_CERT} || ! -r ${HONO_DEVICE_CERT} ]]; then
error_and_exit "Cannot read client certificate from ${HONO_DEVICE_CERT}"
fi
fi
if [[ -n "${HONO_DEVICE_KEY}" ]]; then
if [[ -z "${HONO_DEVICE_CERT}" ]]; then
error_and_exit "Missing required option: device-cert"
elif [[ ! -e ${HONO_DEVICE_KEY} || ! -r ${HONO_DEVICE_KEY} ]]; then
error_and_exit "Cannot read client key from ${HONO_DEVICE_KEY}"
fi
fi
if [[ -z "${HONO_KAFKA_BROKERS}" ]]; then
HONO_KAFKA_BROKERS=${HONO_HOST}:${HONO_KAFKA_SECURE_PORT}
fi
CONFIG_DIR_BASE="${OUT_DIR}/config/hono"
CONFIG_DIR_FMS_CONSUMER="${CONFIG_DIR_BASE}/fms-consumer"
mkdir -p "${CONFIG_DIR_FMS_CONSUMER}"
CONFIG_DIR_FMS_FORWARDER="${CONFIG_DIR_BASE}/fms-forwarder"
mkdir -p "${CONFIG_DIR_FMS_FORWARDER}"
if [[ -n "${PROVISION_TO_HONO}" ]]; then
provision_device
fi
# create kafka client properties file for use with the FMS Consumer running in the back end
KAFKA_PROPERTIES_FILE="${CONFIG_DIR_FMS_CONSUMER}/kafka.properties"
echo "Creating Kafka client properties file ${KAFKA_PROPERTIES_FILE} ..."
cat <<EOF > "${KAFKA_PROPERTIES_FILE}"
bootstrap.servers=${HONO_KAFKA_BROKERS}
group.id=fms-demo-consumer
enable.partition.eof=false
session.timeout.ms=6000
enable.auto.commit=true
security.protocol=SASL_SSL
sasl.mechanism=SCRAM-SHA-512
sasl.username=${HONO_KAFKA_USER}
sasl.password=${HONO_KAFKA_PASSWORD}
ssl.ca.location=${TRUST_STORE_PATH}
EOF
if [[ "${ENABLE_HOSTNAME_VALIDATION}" == "false" ]]; then
echo "ssl.endpoint.identification.algorithm=none" >> "${KAFKA_PROPERTIES_FILE}"
fi
# create file with environment variables that the FMS Forwarder running in the vehicle
# will use to configure its connection to Hono's MQTT adapter
MQTT_PROPS_FILE="${CONFIG_DIR_BASE}/mqtt-client.env"
if [[ -n "${HONO_DEVICE_CERT}" ]]; then
create_mqtt_client_env_with_cert \
"${MQTT_PROPS_FILE}" \
"mqtts://${HONO_HOST}:8883" \
"${HONO_DEVICE_CERT}" \
"${HONO_DEVICE_KEY}" \
"${TRUST_STORE_PATH}" \
"${ENABLE_HOSTNAME_VALIDATION}"
else
create_mqtt_client_env \
"${MQTT_PROPS_FILE}" \
"mqtts://${HONO_HOST}:8883" \
"${HONO_DEVICE_ID}@${HONO_TENANT_ID}" \
"${HONO_DEVICE_PASSWORD}" \
"${TRUST_STORE_PATH}" \
"${ENABLE_HOSTNAME_VALIDATION}"
fi
# create file with environment variables to be used with Docker Compose when
# starting the services:
#
# docker compose --env-file hono.env ...
#
create_docker_compose_env \
"${CONFIG_DIR_BASE}/hono.env" \
"${CONFIG_DIR_FMS_CONSUMER}" \
"kafka.properties" \
"hono.telemetry.${HONO_TENANT_ID}" \
"${CONFIG_DIR_FMS_FORWARDER}" \
"${MQTT_PROPS_FILE}"