mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 19:04:19 +00:00
refactor: trim MQTT observer config and credential masking
This commit is contained in:
1 parent
e2699a94bd
commit
1dca9e728c
6 files changed
+29
-76
No files matched your search
+18
-17
@@ -106,8 +106,8 @@ get mqttN.audience
|
||||
get mqttN.owner
|
||||
get mqttN.email
|
||||
get mqttN.username
|
||||
get mqttN.password # configured/not set only
|
||||
get mqttN.token # configured/not set only
|
||||
get mqttN.password
|
||||
get mqttN.token
|
||||
get mqttN.topic
|
||||
get mqtt.iata
|
||||
get mqtt.origin
|
||||
@@ -119,27 +119,28 @@ get mqtt.packets
|
||||
get mqtt.raw
|
||||
get wifi.status # connection, IP, RSSI
|
||||
get wifi.ssid
|
||||
get wifi.pwd
|
||||
get wifi.powersave
|
||||
```
|
||||
|
||||
`N` is the one-based slot number (`1` or `2` in this build). The worker uses
|
||||
a compile-time slot array; `MQTT_OBSERVER_MAX_BROKERS` defaults to 2 and may be
|
||||
raised up to 6 at build time. Six broker records are reserved
|
||||
in `/mqtt_prefs`, so raising the active count up to 6 does not change the config
|
||||
format or require a migration. Increasing the reserved storage capacity beyond
|
||||
six would require a format migration. The observer does not include presets,
|
||||
packet allowlists, TX uplink, neighbor queries, WebConfig, SNMP, or alerting. It reports all MeshCore packet types received when the
|
||||
shared RX and packet/raw switches are enabled.
|
||||
`N` is the one-based slot number (`1` or `2` in this build). The worker and
|
||||
`/mqtt_prefs` config support two broker slots. Adding more slots requires a
|
||||
config format version change. The observer does not include presets, packet
|
||||
allowlists, TX uplink, neighbor queries, WebConfig, SNMP, or alerting. It reports
|
||||
all MeshCore packet types received when the shared RX and packet/raw switches
|
||||
are enabled.
|
||||
|
||||
SSID/password values are the rest of the command line; do not quote them. An
|
||||
empty value clears a string setting. Settings are saved lazily about three
|
||||
seconds after a change. The experimental `/mqtt_prefs` record is versioned and
|
||||
CRC-checked. Its six reserved broker records keep the persisted layout stable
|
||||
when `MQTT_OBSERVER_MAX_BROKERS` is raised up to 6. No migration from the earlier
|
||||
prototype format is included because this observer has not been deployed and
|
||||
there are no device configs to preserve. The filesystem is not encrypted; an
|
||||
administrator with filesystem or firmware access can recover credentials.
|
||||
Commands pass through the repeater's existing CLI permission boundary.
|
||||
seconds after a change. The experimental `/mqtt_prefs` record is version 4 and
|
||||
CRC-checked; the two broker records make its payload 1,508 bytes (1,517 bytes
|
||||
including the record header and CRC). No migration from earlier prototype
|
||||
formats is included because this observer has not been deployed and there are
|
||||
no device configs to preserve. Credential values are returned by `get` commands
|
||||
and appear in fleet-script serial logs; protect CLI access and logs accordingly.
|
||||
The filesystem is not encrypted; an administrator with filesystem or firmware
|
||||
access can recover credentials. Commands pass through the repeater's existing
|
||||
CLI permission boundary.
|
||||
|
||||
## Topics and payloads
|
||||
|
||||
|
||||
@@ -1077,7 +1077,7 @@ bool MQTTObserver::handleGetCommand(const char* command, char* reply) {
|
||||
_config.wifi_power_save == 2 ? "max" : "min";
|
||||
else if (strcmp(command, "get wifi.ssid") == 0)
|
||||
value = _config.wifi_ssid[0] ? _config.wifi_ssid : "not set";
|
||||
else value = _config.wifi_password[0] ? "configured" : "not set";
|
||||
else value = _config.wifi_password[0] ? _config.wifi_password : "not set";
|
||||
snprintf(reply, CLI_REPLY_MAX, "%s", value);
|
||||
portEXIT_CRITICAL(&_config_mux);
|
||||
return true;
|
||||
@@ -1148,9 +1148,9 @@ bool MQTTObserver::handleGetCommand(const char* command, char* reply) {
|
||||
else if (MQTT_GET_IS("owner")) result = broker.owner[0] ? broker.owner : "not configured";
|
||||
else if (MQTT_GET_IS("email")) result = broker.email[0] ? broker.email : "not configured";
|
||||
else if (MQTT_GET_IS("enabled")) result = broker.enabled ? "on" : "off";
|
||||
else if (MQTT_GET_IS("password")) result = broker.password[0] ? "configured" : "not set";
|
||||
else if (MQTT_GET_IS("username")) result = broker.username[0] ? "configured" : "not set";
|
||||
else if (MQTT_GET_IS("token")) result = broker.token[0] ? "configured" : "not set";
|
||||
else if (MQTT_GET_IS("password")) result = broker.password[0] ? broker.password : "not set";
|
||||
else if (MQTT_GET_IS("username")) result = broker.username[0] ? broker.username : "not set";
|
||||
else if (MQTT_GET_IS("token")) result = broker.token[0] ? broker.token : "not set";
|
||||
if (result && result != value) {
|
||||
snprintf(value, sizeof(value), "%s", result);
|
||||
result = value;
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
#define MQTT_OBSERVER_MAX_BROKERS 2 // Active broker clients configured by mqttN.*
|
||||
#endif
|
||||
|
||||
// Reserve the full slot array so increasing the active count does not move the record layout.
|
||||
static const size_t MQTT_OBSERVER_CONFIG_BROKER_CAPACITY = 6;
|
||||
// The persisted config stores only the broker slots supported by this format.
|
||||
static const size_t MQTT_OBSERVER_CONFIG_BROKER_CAPACITY = 2;
|
||||
|
||||
#ifndef MQTT_OBSERVER_QUEUE_CAPACITY
|
||||
#define MQTT_OBSERVER_QUEUE_CAPACITY 8 // Captured packet events shared by all broker slots
|
||||
@@ -51,7 +51,7 @@ struct MQTTObserverConfig {
|
||||
|
||||
namespace MQTTObserverConfigCodec {
|
||||
|
||||
static const uint8_t kVersion = 3;
|
||||
static const uint8_t kVersion = 4;
|
||||
static const size_t kCommonPayloadSize = 6 + 33 + 65 + 4 + 33 + 65;
|
||||
static const size_t kBrokerPayloadSize = 1 + 2 + 129 + 65 + 65 + 65 + 129 + 65 + 65 + 65;
|
||||
static const size_t kPayloadSize = kCommonPayloadSize +
|
||||
|
||||
@@ -25,28 +25,6 @@ inline bool parseSlot(const char* command, const char* operation, size_t max_slo
|
||||
return true;
|
||||
}
|
||||
|
||||
// True when a `set` line carries a credential value that must not reach the
|
||||
// fleet-script log: `set wifi.pwd <...>` and `set mqttN.password|username|token
|
||||
// <...>`. `get` lines are never masked — their replies hold only
|
||||
// configured/not-set state, and the log is more useful with them visible.
|
||||
inline bool isCredentialSetting(const char* line) {
|
||||
if (!line) return false;
|
||||
while (*line == ' ') line++;
|
||||
if (strncmp(line, "set ", 4) != 0) return false;
|
||||
line += 4;
|
||||
if (strncmp(line, "wifi.pwd", 8) == 0 && (line[8] == 0 || line[8] == ' ')) return true;
|
||||
if (strncmp(line, "mqtt", 4) != 0) return false;
|
||||
line += 4;
|
||||
while (*line >= '0' && *line <= '9') line++;
|
||||
if (*line++ != '.') return false;
|
||||
static const char* const fields[] = {"password", "username", "token"};
|
||||
for (size_t i = 0; i < sizeof(fields) / sizeof(fields[0]); i++) {
|
||||
size_t len = strlen(fields[i]);
|
||||
if (strncmp(line, fields[i], len) == 0 && (line[len] == 0 || line[len] == ' ')) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace MQTTObserverCommandPolicy
|
||||
|
||||
namespace MQTTObserverQueuePolicy {
|
||||
|
||||
@@ -1414,11 +1414,6 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
|
||||
// ran. `key` is the job's !id key string from the queued script itself.
|
||||
static void execCliLine(void* ctx, const char* key, char* line, char* reply) {
|
||||
// logged before dispatch: the handlers tokenise the line in place
|
||||
#ifdef WITH_MQTT_OBSERVER
|
||||
if (MQTTObserverCommandPolicy::isCredentialSetting(line)) {
|
||||
Serial.printf("cli[%s] <credential setting>\n", key);
|
||||
} else
|
||||
#endif
|
||||
Serial.printf("cli[%s] %s\n", key, line);
|
||||
((MyMesh*)ctx)->handleCommand(0, line, reply);
|
||||
if (reply[0]) Serial.printf(" -> %s\n", reply);
|
||||
|
||||
@@ -31,9 +31,11 @@ TEST(MQTTObserverConfig, RoundTripsTwoBrokerSettings) {
|
||||
config.brokers[1].enabled = 1;
|
||||
strcpy(config.brokers[1].server, "mqtt://second.example");
|
||||
strcpy(config.brokers[1].username, "second-user");
|
||||
config.brokers[5].enabled = 1;
|
||||
strcpy(config.brokers[5].server, "mqtt://reserved-slot.example");
|
||||
|
||||
EXPECT_EQ(MQTT_OBSERVER_CONFIG_BROKER_CAPACITY, 2u);
|
||||
EXPECT_EQ(MQTTObserverConfigCodec::kVersion, 4);
|
||||
EXPECT_EQ(MQTTObserverConfigCodec::kPayloadSize, 1508u);
|
||||
EXPECT_EQ(MQTTObserverRecord::kRecordSize, 1517u);
|
||||
uint8_t payload[MQTTObserverConfigCodec::kPayloadSize];
|
||||
ASSERT_EQ(MQTTObserverConfigCodec::encode(config, payload, sizeof(payload)), sizeof(payload));
|
||||
MQTTObserverConfig decoded;
|
||||
@@ -54,8 +56,6 @@ TEST(MQTTObserverConfig, RoundTripsTwoBrokerSettings) {
|
||||
EXPECT_EQ(decoded.brokers[0].port, 8883);
|
||||
EXPECT_STREQ(decoded.brokers[1].server, "mqtt://second.example");
|
||||
EXPECT_STREQ(decoded.brokers[1].username, "second-user");
|
||||
EXPECT_EQ(decoded.brokers[5].enabled, 1);
|
||||
EXPECT_STREQ(decoded.brokers[5].server, "mqtt://reserved-slot.example");
|
||||
}
|
||||
|
||||
TEST(MQTTObserverConfig, RejectsInvalidValuesAndIntegrityChanges) {
|
||||
@@ -86,8 +86,6 @@ TEST(MQTTObserverRecord, ChecksVersionLengthAndChecksum) {
|
||||
strcpy(original.brokers[0].server, "mqtts://broker.example");
|
||||
original.brokers[1].enabled = 1;
|
||||
strcpy(original.brokers[1].server, "mqtt://second.example");
|
||||
original.brokers[5].enabled = 1;
|
||||
strcpy(original.brokers[5].server, "mqtt://reserved.example");
|
||||
strcpy(original.iata, "SEA");
|
||||
uint8_t record[MQTTObserverRecord::kRecordSize];
|
||||
ASSERT_EQ(MQTTObserverRecord::encode(original, record, sizeof(record)), sizeof(record));
|
||||
@@ -96,7 +94,6 @@ TEST(MQTTObserverRecord, ChecksVersionLengthAndChecksum) {
|
||||
ASSERT_TRUE(MQTTObserverRecord::decode(record, sizeof(record), decoded));
|
||||
EXPECT_STREQ(decoded.brokers[0].server, original.brokers[0].server);
|
||||
EXPECT_STREQ(decoded.brokers[1].server, original.brokers[1].server);
|
||||
EXPECT_STREQ(decoded.brokers[5].server, original.brokers[5].server);
|
||||
EXPECT_STREQ(decoded.iata, original.iata);
|
||||
EXPECT_FALSE(MQTTObserverRecord::decode(record, sizeof(record) - 1, decoded));
|
||||
|
||||
@@ -216,24 +213,6 @@ TEST(MQTTObserverCommandPolicy, ParsesNumberedSlotsAndRejectsUnnumberedBrokerSet
|
||||
slot, property));
|
||||
}
|
||||
|
||||
TEST(MQTTObserverCommandPolicy, MasksCredentialSettingsAndOnlySetLines) {
|
||||
EXPECT_TRUE(MQTTObserverCommandPolicy::isCredentialSetting("set wifi.pwd hunter2"));
|
||||
EXPECT_TRUE(MQTTObserverCommandPolicy::isCredentialSetting(" set mqtt1.password secret"));
|
||||
EXPECT_TRUE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt2.token abc"));
|
||||
EXPECT_TRUE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt1.username bob"));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting(nullptr));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("set wifi.ssid home"));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt1.server mqtt://x"));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt1.port 1883"));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt.iata SEA"));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt1.passwordless x"));
|
||||
// over-mask a mistyped slot rather than risk leaking the value into the log
|
||||
EXPECT_TRUE(MQTTObserverCommandPolicy::isCredentialSetting("set mqtt3.password x"));
|
||||
// get lines only ever echo configured/not-set state; leave them visible
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("get mqtt1.password"));
|
||||
EXPECT_FALSE(MQTTObserverCommandPolicy::isCredentialSetting("get wifi.pwd"));
|
||||
}
|
||||
|
||||
TEST(MQTTObserverQueuePolicy, FlushesOnlyAfterLongDisconnectAndRetriesBoundedly) {
|
||||
EXPECT_FALSE(MQTTObserverQueuePolicy::shouldFlushDisconnected(60099, 100));
|
||||
EXPECT_TRUE(MQTTObserverQueuePolicy::shouldFlushDisconnected(60100, 100));
|
||||
|
||||
Reference in new issue
Block a user