Skip to content

Commit

Permalink
Properly handle security key casing in zwave_js (home-assistant#2208)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Co-authored-by: Joakim Sørensen <[email protected]>
  • Loading branch information
raman325 and ludeeus committed Sep 30, 2021
1 parent cb754bc commit c1bc77b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions zwave_js/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 2 additions & 3 deletions zwave_js/config.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -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)",
Expand Down
28 changes: 23 additions & 5 deletions zwave_js/rootfs/etc/cont-init.d/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 "
Expand All @@ -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
Expand All @@ -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."
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zwave_js/rootfs/etc/services.d/zwave_js/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c1bc77b

Please sign in to comment.