Skip to content

Commit 11cff89

Browse files
author
Ubuntu
committed
Add field to trigger config shadow persistance updates
1 parent dad506a commit 11cff89

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

source/config/Config.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,8 @@ void PlainConfig::SampleShadow::SerializeToObject(Crt::JsonObject &object) const
18831883

18841884
constexpr char PlainConfig::ConfigShadow::JSON_ENABLE_CONFIG_SHADOW[];
18851885
constexpr char PlainConfig::ConfigShadow::CLI_ENABLE_CONFIG_SHADOW[];
1886+
constexpr char PlainConfig::ConfigShadow::JSON_CONFIG_SHADOW_PERSISTENT_UPDATE[];
1887+
18861888

18871889
bool PlainConfig::ConfigShadow::LoadFromJson(const Crt::JsonView &json)
18881890
{
@@ -1892,6 +1894,12 @@ bool PlainConfig::ConfigShadow::LoadFromJson(const Crt::JsonView &json)
18921894
enabled = json.GetBool(jsonKey);
18931895
}
18941896

1897+
jsonKey = JSON_CONFIG_SHADOW_PERSISTENT_UPDATE;
1898+
if (json.ValueExists(jsonKey))
1899+
{
1900+
persistentUpdate = json.GetBool(jsonKey);
1901+
}
1902+
18951903
return true;
18961904
}
18971905

@@ -1913,6 +1921,7 @@ bool PlainConfig::ConfigShadow::Validate() const
19131921
void PlainConfig::ConfigShadow::SerializeToObject(Crt::JsonObject &object) const
19141922
{
19151923
object.WithBool(JSON_ENABLE_CONFIG_SHADOW, enabled);
1924+
object.WithBool(JSON_CONFIG_SHADOW_PERSISTENT_UPDATE, persistentUpdate);
19161925
}
19171926

19181927
constexpr char PlainConfig::SecureElement::CLI_ENABLE_SECURE_ELEMENT[];
@@ -3139,6 +3148,7 @@ bool Config::ExportDefaultSetting(const string &file)
31393148
PlainConfig::SampleShadow::JSON_SAMPLE_SHADOW_OUTPUT_FILE,
31403149
PlainConfig::JSON_KEY_CONFIG_SHADOW,
31413150
PlainConfig::ConfigShadow::JSON_ENABLE_CONFIG_SHADOW,
3151+
PlainConfig::ConfigShadow::JSON_CONFIG_SHADOW_PERSISTENT_UPDATE,
31423152
PlainConfig::JSON_KEY_SECURE_ELEMENT,
31433153
PlainConfig::SecureElement::JSON_ENABLE_SECURE_ELEMENT,
31443154
PlainConfig::SecureElement::JSON_PKCS11_LIB,

source/config/Config.h

+2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,10 @@ namespace Aws
372372

373373
static constexpr char CLI_ENABLE_CONFIG_SHADOW[] = "--enable-config-shadow";
374374
static constexpr char JSON_ENABLE_CONFIG_SHADOW[] = "enabled";
375+
static constexpr char JSON_CONFIG_SHADOW_PERSISTENT_UPDATE[] = "persistent-update";
375376

376377
bool enabled{false};
378+
bool persistentUpdate{false};
377379
};
378380
ConfigShadow configShadow;
379381

source/shadow/ConfigShadow.cpp

+30-9
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,37 @@ void ConfigShadow::resetClientConfigWithJSON(
374374
}
375375
}
376376

377-
// Save the updated configuration to a local file for persistence
378-
JsonObject jsonObj;
379-
config.SerializeToObject(jsonObj);
380-
381-
const char* jsonStr = jsonObj.View().WriteReadable().c_str();
382-
LOGM_INFO(TAG, "Updating device configuration files with the following: %s", jsonStr);
377+
if (desiredJsonView.ValueExists(PlainConfig::JSON_KEY_CONFIG_SHADOW) &&
378+
deltaJsonView.ValueExists(PlainConfig::JSON_KEY_CONFIG_SHADOW))
379+
{
380+
PlainConfig::ConfigShadow configShadow;
381+
configShadow.LoadFromJson(desiredJsonView.GetJsonObject(PlainConfig::JSON_KEY_CONFIG_SHADOW));
382+
if (configShadow.Validate())
383+
{
384+
config.configShadow = configShadow;
385+
}
386+
else
387+
{
388+
LOGM_WARN(
389+
TAG,
390+
"Config shadow contains invalid configurations in %s feature, aborting this feature's configuration "
391+
"update now. Please check the error logs for more information",
392+
PlainConfig::JSON_KEY_CONFIG_SHADOW);
393+
}
394+
}
383395

384-
string userConfig = FileUtils::ExtractExpandedPath(Config::DEFAULT_CONFIG_FILE);
385-
updateLocalConfigFile(config, userConfig.c_str());
386-
updateLocalConfigFile(config, Config::DEFAULT_ROOT_CONFIG_FILE);
396+
// Save the updated configuration to a local file for persistence if required
397+
if(config.configShadow.persistentUpdate)
398+
{
399+
JsonObject jsonObj;
400+
config.SerializeToObject(jsonObj);
401+
const char* jsonStr = jsonObj.View().WriteReadable().c_str();
402+
LOGM_INFO(TAG, "Updating device configuration files with the following config shadow update: %s", jsonStr);
403+
404+
string userConfig = FileUtils::ExtractExpandedPath(Config::DEFAULT_CONFIG_FILE);
405+
updateLocalConfigFile(config, userConfig.c_str());
406+
updateLocalConfigFile(config, Config::DEFAULT_ROOT_CONFIG_FILE);
407+
}
387408
}
388409

389410
void ConfigShadow::updateShadowWithLocalConfig(Iotshadow::IotShadowClient iotShadowClient, PlainConfig &config)

0 commit comments

Comments
 (0)