Rewrite password masking

This commit is contained in:
fvanroie 2021-05-01 01:40:32 +02:00
parent 9db554e6d7
commit 60df747785
2 changed files with 81 additions and 47 deletions

View File

@ -3,10 +3,9 @@
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
#include "ArduinoJson.h"
#include "StreamUtils.h" // For EEPromStream #include "StreamUtils.h" // For EEPromStream
#include "hasp_conf.h" #include "hasplib.h"
#include "hasp_config.h" #include "hasp_config.h"
#include "hasp_debug.h" #include "hasp_debug.h"
@ -18,8 +17,6 @@
//#include "hasp_gpio.h" included in conf //#include "hasp_gpio.h" included in conf
//#include "hasp_eeprom.h" //#include "hasp_eeprom.h"
#include "hasp/hasp.h"
#include "hasp/hasp_dispatch.h"
#if HASP_USE_EEPROM > 0 #if HASP_USE_EEPROM > 0
#include "EEPROM.h" #include "EEPROM.h"
@ -76,24 +73,59 @@ bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringH
return false; return false;
} }
void configStartDebug(bool setupdebug, String& configFile) void configSetupDebug(JsonDocument& settings)
{
debugSetupWithoutLogging(settings[FPSTR(FP_DEBUG)]);
debugStart(); // Debug started, now we can use it; HASP header sent
}
void configStorePasswords(JsonDocument& settings, String& wifiPass, String& mqttPass, String& httpPass)
{
const __FlashStringHelper* pass = F("pass");
wifiPass = settings[FPSTR(FP_WIFI)][pass].as<String>();
mqttPass = settings[FPSTR(FP_MQTT)][pass].as<String>();
httpPass = settings[FPSTR(FP_HTTP)][pass].as<String>();
}
void configRestorePasswords(JsonDocument& settings, String& wifiPass, String& mqttPass, String& httpPass)
{
const __FlashStringHelper* pass = F("pass");
if(!settings[FPSTR(FP_WIFI)][pass].isNull()) settings[FPSTR(FP_WIFI)][pass] = wifiPass;
if(!settings[FPSTR(FP_MQTT)][pass].isNull()) settings[FPSTR(FP_MQTT)][pass] = mqttPass;
if(!settings[FPSTR(FP_HTTP)][pass].isNull()) settings[FPSTR(FP_HTTP)][pass] = httpPass;
}
void configMaskPasswords(JsonDocument& settings)
{
String passmask = F(D_PASSWORD_MASK);
configRestorePasswords(settings, passmask, passmask, passmask);
}
DeserializationError configParseFile(String& configFile, JsonDocument& settings)
{ {
if(setupdebug) {
debugStart(); // Debug started, now we can use it; HASP header sent
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
LOG_INFO(TAG_CONF, F("SPI flash FS mounted")); File file = HASP_FS.open(configFile, "r");
filesystemInfo(); DeserializationError result;
filesystemList();
#endif if(file) {
size_t size = file.size();
if(size > 1024) {
LOG_ERROR(TAG_CONF, F("Config file size is too large"));
return DeserializationError::NoMemory;
}
result = deserializeJson(settings, file);
file.close();
return result;
} }
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 return DeserializationError::InvalidInput;
LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), configFile.c_str());
#else #else
LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), "EEPROM"); return DeserializationError::NotSupported;
#endif #endif
} }
void configRead(JsonDocument& settings, bool setupdebug = false) DeserializationError configRead(JsonDocument& settings, bool setupdebug = false)
{ {
String configFile((char*)0); String configFile((char*)0);
configFile.reserve(32); configFile.reserve(32);
@ -101,40 +133,34 @@ void configRead(JsonDocument& settings, bool setupdebug = false)
DeserializationError error; DeserializationError error;
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
File file = HASP_FS.open(configFile, "r"); error = configParseFile(configFile, settings);
if(!error) {
String output, wifiPass, mqttPass, httpPass;
if(file) { /* Load Debug params */
size_t size = file.size(); if(setupdebug) {
if(size > 1024) { configSetupDebug(settings); // Now we can use log
LOG_ERROR(TAG_CONF, F("Config file size is too large")); LOG_INFO(TAG_CONF, F("SPI flash FS mounted"));
return;
filesystemInfo();
filesystemList();
} }
error = deserializeJson(settings, file); LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), configFile.c_str());
file.close(); configStorePasswords(settings, wifiPass, mqttPass, httpPass);
if(!error) { // Output settings in log with masked passwords
/* Load Debug params */ configMaskPasswords(settings);
if(setupdebug) { serializeJson(settings, output);
debugPreSetup(settings[FPSTR(FP_DEBUG)]); LOG_VERBOSE(TAG_CONF, output.c_str());
}
configStartDebug(setupdebug, configFile);
// show settings in log configRestorePasswords(settings, wifiPass, mqttPass, httpPass);
String output; LOG_INFO(TAG_CONF, F(D_FILE_LOADED), configFile.c_str());
serializeJson(settings, output);
String passmask = F(D_PASSWORD_MASK);
const __FlashStringHelper* pass = F("pass");
output.replace(settings[FPSTR(FP_HTTP)][pass].as<String>(), passmask);
output.replace(settings[FPSTR(FP_MQTT)][pass].as<String>(), passmask);
output.replace(settings[FPSTR(FP_WIFI)][pass].as<String>(), passmask);
LOG_VERBOSE(TAG_CONF, output.c_str());
LOG_INFO(TAG_CONF, F(D_FILE_LOADED), configFile.c_str());
if(setupdebug) debugSetup(); if(setupdebug) debugSetup();
return; return error;
}
} }
#else #else
#if HASP_USE_EEPROM > 0 #if HASP_USE_EEPROM > 0
@ -145,15 +171,18 @@ void configRead(JsonDocument& settings, bool setupdebug = false)
#endif #endif
// File does not exist or error reading file // File does not exist or error reading file
if(setupdebug) { if(setupdebug) configSetupDebug(settings); // Now we can use log
debugPreSetup(settings[FPSTR(FP_DEBUG)]);
}
configStartDebug(setupdebug, configFile);
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
LOG_ERROR(TAG_CONF, F(D_FILE_LOAD_FAILED), configFile.c_str()); LOG_ERROR(TAG_CONF, F(D_FILE_LOAD_FAILED), configFile.c_str());
#endif #endif
configFile = F("EEPROM");
LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), configFile.c_str());
LOG_INFO(TAG_CONF, F(D_FILE_LOADED), configFile.c_str());
return error;
} }
/* /*
void configBackupToEeprom() void configBackupToEeprom()
{ {
@ -309,7 +338,9 @@ void configWrite()
File file = HASP_FS.open(configFile, "w"); File file = HASP_FS.open(configFile, "w");
if(file) { if(file) {
LOG_TRACE(TAG_CONF, F(D_FILE_SAVING), configFile.c_str()); LOG_TRACE(TAG_CONF, F(D_FILE_SAVING), configFile.c_str());
size_t size = serializeJson(doc, file); WriteBufferingStream bufferedFile(file, 256);
size_t size = serializeJson(doc, bufferedFile);
bufferedFile.flush();
file.close(); file.close();
if(size > 0) { if(size > 0) {
LOG_INFO(TAG_CONF, F(D_FILE_SAVED), configFile.c_str()); LOG_INFO(TAG_CONF, F(D_FILE_SAVED), configFile.c_str());

View File

@ -18,6 +18,8 @@ void configStart(void);
void configStop(void); void configStop(void);
/* ===== Special Event Processors ===== */ /* ===== Special Event Processors ===== */
DeserializationError configParseFile(String& configFile, JsonDocument& settings);
bool configRead(String configFile, JsonDocument& settings, bool setupdebug = false);
void configWrite(void); void configWrite(void);
void configOutput(const JsonObject& settings, uint8_t tag = TAG_CONF); void configOutput(const JsonObject& settings, uint8_t tag = TAG_CONF);
bool configClearEeprom(void); bool configClearEeprom(void);
@ -26,6 +28,7 @@ bool configClearEeprom(void);
bool configSet(int8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); bool configSet(int8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name);
bool configSet(uint8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); bool configSet(uint8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name);
bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name);
void configMaskPasswords(JsonDocument& settings);
/* ===== Read/Write Configuration ===== */ /* ===== Read/Write Configuration ===== */
void configSetConfig(JsonObject& settings); void configSetConfig(JsonObject& settings);