konnected-io / konnected-security

Konnected connects wired sensors and switches to SmartThings, Home Assistant, Hubitat and OpenHAB
https://konnected.io
Apache License 2.0
416 stars 322 forks source link

Lock endpoint and structured logger #142

Closed kit-klein closed 3 years ago

kit-klein commented 3 years ago

This PR creates a /lock endpoint that can be used to lock and unlock the device settings. It also includes the structured logger implementation ported over from the pro board.

Lock Endpoint

When the lock is active any attempt to update the /settings endpoint will fail with a 409 status.

NOTE: the settings lock DOES NOT currently prevent restart or restore operations on the /settings endpoint.

A GET on /lock will return the state of the device

{ "state": "locked" } or

{ "state": "unlocked" } A PUT on /lock will attempt to lock the settings. Requests must include a JSON body with a pwd member. The value of pwd should be a password which is used to validate, or create/store a signature in the device_config file. Ex request body...

{ "pwd": "SuPeR SeCrEt" } If the lock is active - the value pwd must be the same as the value used when the lock was created. A match will unlock the device. If the lock is not active, a lock will be created using the value of pwd.

The response body of PUT upon success is the new state of the device (same format as returned by GET).

If an error occurs the /lock endpoint returns an appropriate http status. 400 - pwd member missing 403 - incorrect value for pwd

Structured Logger

This PR migrates the lfs modules to utilize a logger. Log output format shifts from

Heap:   41336   HTTP:   Starting server at http://192.168.1.213:9794
Heap:   41160   Loaded:         server
Heap:   39272   REST Endpoint:  nil
Heap:   39240   Loaded:         application
Heap:   36840   UPnP:   Sent SSDP NOTIFY

to

[INFO  14.593499, 39136]: HTTP:  Starting server at http://192.168.1.213:9794
[INFO  14.609761, 39240]: Loaded:  server
[INFO  14.819109, 37200]: REST Endpoint: nil
[INFO  14.834246, 37904]: Loaded:  application
[INFO  29.765033, 36416]: UPnP: Sent SSDP NOTIFY

Note that all logs now include a level, timestamp, and heap. This differs a bit from the pro board as the debug module, which exposes lines and files, isn't included in the 8266 implementation.

Using the logger seems to impact HEAP reports by about 1300 bytes. It doesn't seem it impact performance in any other notable way.