From c1bc77b9ae87f56bb6a142eabe7294499ab72ec9 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Thu, 30 Sep 2021 15:55:35 -0400 Subject: [PATCH] Properly handle security key casing in zwave_js (#2208) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Properly handle security key casing in zwave_js * Fix emulate_hardware * update changelog * declare missing variables * Add missing var from declaration * Always set network_key if unset * Don't flush to disk twice * Update zwave_js/rootfs/etc/cont-init.d/config.sh Co-authored-by: Joakim Sørensen Co-authored-by: Joakim Sørensen --- zwave_js/CHANGELOG.md | 5 ++++ zwave_js/config.json | 5 ++-- zwave_js/rootfs/etc/cont-init.d/config.sh | 28 +++++++++++++++++---- zwave_js/rootfs/etc/services.d/zwave_js/run | 2 +- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/zwave_js/CHANGELOG.md b/zwave_js/CHANGELOG.md index 658a276..7f032f6 100644 --- a/zwave_js/CHANGELOG.md +++ b/zwave_js/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.1.44 + +- Fix casing issues with security keys +- Fix `emulate_hardware` configuration option + ## 0.1.43 - Bump Z-Wave JS Server to 1.10.6 diff --git a/zwave_js/config.json b/zwave_js/config.json index 4a68ae4..16b517a 100644 --- a/zwave_js/config.json +++ b/zwave_js/config.json @@ -1,6 +1,6 @@ { "name": "Z-Wave JS", - "version": "0.1.43", + "version": "0.1.44", "slug": "zwave_js", "description": "Control a ZWave network with Home Assistant Z-Wave JS", "arch": ["amd64", "i386", "armhf", "armv7", "aarch64"], @@ -22,8 +22,7 @@ "s2_access_control_key": "", "s2_authenticated_key": "", "s2_unauthenticated_key": "", - "log_level": "info", - "network_key": "" + "log_level": "info" }, "schema": { "device": "device(subsystem=tty)", diff --git a/zwave_js/rootfs/etc/cont-init.d/config.sh b/zwave_js/rootfs/etc/cont-init.d/config.sh index 950859e..0fd90a7 100644 --- a/zwave_js/rootfs/etc/cont-init.d/config.sh +++ b/zwave_js/rootfs/etc/cont-init.d/config.sh @@ -3,6 +3,14 @@ # Generate Z-Wave JS config file # ============================================================================== declare network_key +declare network_key_upper +declare s0_legacy_key +declare s0_legacy +declare s2_access_control +declare s2_authenticated +declare s2_unauthenticated +declare log_level +declare flush_to_disk readonly DOCS_EXAMPLE_KEY_1="2232666D100F795E5BB17F0A1BB7A146" readonly DOCS_EXAMPLE_KEY_2="A97D2A51A6D4022998BEFC7B5DAE8EA1" @@ -14,7 +22,9 @@ if bashio::config.has_value 'network_key'; then # we don't know which one to pick so we have to exit. If they are both set # and do match, we don't need to do anything if bashio::config.has_value 's0_legacy_key'; then - if bashio::config.equals 's0_legacy_key' "$(bashio::config \"network_key\")"; then + network_key=$(bashio::string.upper "$(bashio::config 'network_key')") + s0_legacy_key=$(bashio::string.upper "$(bashio::config 's0_legacy_key')") + if [ "${network_key}" == "${s0_legacy_key}" ]; then bashio::log.info "Both 'network_key' and 's0_legacy_key' are set and match. All ok." else bashio::log.fatal "Both 'network_key' and 's0_legacy_key' are set to different values " @@ -26,8 +36,7 @@ if bashio::config.has_value 'network_key'; then # to migrate the key from 'network_key' to 's0_legacy_key' else bashio::log.info "Migrating \"network_key\" option to \"s0_legacy_key\"..." - network_key=$(bashio::string.upper "$(bashio::config 'network_key')") - bashio::addon.option s0_legacy_key "${network_key}" + bashio::addon.option s0_legacy_key "$(bashio::config 'network_key')" bashio::log.info "Flushing config to disk due to key migration..." bashio::addon.options > "/data/options.json" fi @@ -37,7 +46,8 @@ fi # keys for any missing keys. for key in "s0_legacy_key" "s2_access_control_key" "s2_authenticated_key" "s2_unauthenticated_key"; do network_key=$(bashio::config "${key}") - if [ "${network_key}" == "${DOCS_EXAMPLE_KEY_1}" ] || [ "${network_key}" == "${DOCS_EXAMPLE_KEY_2}" ] || [ "${network_key}" == "${DOCS_EXAMPLE_KEY_3}" ] || [ "${network_key}" == "${DOCS_EXAMPLE_KEY_4}" ]; then + network_key_upper=$(bashio::string.upper "${network_key}") + if [ "${network_key_upper}" == "${DOCS_EXAMPLE_KEY_1}" ] || [ "${network_key_upper}" == "${DOCS_EXAMPLE_KEY_2}" ] || [ "${network_key_upper}" == "${DOCS_EXAMPLE_KEY_3}" ] || [ "${network_key_upper}" == "${DOCS_EXAMPLE_KEY_4}" ]; then bashio::log.fatal bashio::log.fatal "The add-on detected that the Z-Wave network key used" bashio::log.fatal "is from the documented example." @@ -55,7 +65,15 @@ for key in "s0_legacy_key" "s2_access_control_key" "s2_authenticated_key" "s2_un bashio::exit.nok elif ! bashio::var.has_value "${network_key}"; then bashio::log.info "No ${key} is set, generating one..." - bashio::addon.option ${key} "$(hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random)" + network_key="$(hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random)" + bashio::addon.option ${key} "${network_key}" + flush_to_disk=1 + fi + + # If `network_key` is unset, we set it to match `s0_legacy_key` for backwards compatibility + if bashio::var.equals "${key}" "s0_legacy_key" && ! bashio::config.has_value "network_key"; then + bashio::log.info "No 'network_key' detected, setting it to 's0_legacy_key' for backwards compatibility" + bashio::addon.option network_key "${network_key}" flush_to_disk=1 fi done diff --git a/zwave_js/rootfs/etc/services.d/zwave_js/run b/zwave_js/rootfs/etc/services.d/zwave_js/run index 7fcd4f2..f28081f 100644 --- a/zwave_js/rootfs/etc/services.d/zwave_js/run +++ b/zwave_js/rootfs/etc/services.d/zwave_js/run @@ -6,7 +6,7 @@ SERIAL_DEVICE=$(bashio::config 'device') # Emulate serial Hardware for test & development if bashio::config.true 'emulate_hardware'; then - SERIAL_DEVICE="--mock" + SERIAL_DEVICE="--mock-driver" fi # Send out discovery information to Home Assistant