1 #ifndef CONFIG_UTILS_H_ 2 #define CONFIG_UTILS_H_ 6 #include <boost/property_tree/ini_parser.hpp> 8 #include "logging/logging.h" 27 inline T StripQuotesFromStrings(
const T& value);
30 inline std::string StripQuotesFromStrings<std::string>(
const std::string& value) {
31 return Utils::stripQuotes(value);
35 inline T StripQuotesFromStrings(
const T& value) {
40 inline T addQuotesToStrings(
const T& value);
43 inline std::string addQuotesToStrings<std::string>(
const std::string& value) {
44 return Utils::addQuotes(value);
48 inline T addQuotesToStrings(
const T& value) {
53 inline void writeOption(std::ostream& sink,
const T&
data,
const std::string& option_name) {
54 sink << option_name <<
" = " << addQuotesToStrings(
data) <<
"\n";
58 inline void CopyFromConfig(T& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
59 boost::optional<T> value = pt.get_optional<T>(option_name);
60 if (value.is_initialized()) {
61 dest = StripQuotesFromStrings(value.get());
66 inline void CopyFromConfig(KeyType& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
67 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
68 if (value.is_initialized()) {
69 std::string key_type{StripQuotesFromStrings(value.get())};
70 if (key_type ==
"RSA2048") {
71 dest = KeyType::kRSA2048;
72 }
else if (key_type ==
"RSA3072") {
73 dest = KeyType::kRSA3072;
74 }
else if (key_type ==
"RSA4096") {
75 dest = KeyType::kRSA4096;
76 }
else if (key_type ==
"ED25519") {
77 dest = KeyType::kED25519;
79 dest = KeyType::kUnknown;
85 inline void CopyFromConfig(CryptoSource& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
86 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
87 if (value.is_initialized()) {
88 std::string crypto_source{StripQuotesFromStrings(value.get())};
89 if (crypto_source ==
"pkcs11") {
90 dest = CryptoSource::kPkcs11;
92 dest = CryptoSource::kFile;
98 inline void CopyFromConfig(
RunningMode& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
99 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
100 if (value.is_initialized()) {
101 dest = RunningModeFromString(StripQuotesFromStrings(value.get()));
106 inline void CopyFromConfig(
BasedPath& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
107 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
108 if (value.is_initialized()) {
109 BasedPath bp{StripQuotesFromStrings(value.get())};
114 template <
typename T>
115 inline void CopySubtreeFromConfig(T& dest,
const std::string& subtree_name,
const boost::property_tree::ptree& pt) {
116 auto subtree = pt.get_child_optional(subtree_name);
117 if (subtree.is_initialized()) {
118 dest.updateFromPropertyTree(subtree.get());
121 dest.updateFromPropertyTree(boost::property_tree::ptree());
125 template <
typename T>
126 inline void WriteSectionToStream(T& sec,
const std::string& section_name, std::ostream& os) {
127 os << std::boolalpha;
128 os <<
"[" << section_name <<
"]\n";
129 sec.writeToStream(os);
136 void updateFromToml(
const boost::filesystem::path& filename) {
137 LOG_INFO <<
"Reading config: " << filename;
138 if (!boost::filesystem::exists(filename)) {
139 throw std::runtime_error(
"Config file " + filename.string() +
" does not exist.");
141 boost::property_tree::ptree pt;
142 boost::property_tree::ini_parser::read_ini(filename.string(), pt);
143 updateFromPropertyTree(pt);
145 virtual void updateFromPropertyTree(
const boost::property_tree::ptree& pt) = 0;
148 void updateFromDirs(
const std::vector<boost::filesystem::path>& configs) {
149 std::map<std::string, boost::filesystem::path> configs_map;
150 for (
const auto& config : configs) {
151 if (!boost::filesystem::exists(config)) {
154 if (boost::filesystem::is_directory(config)) {
155 for (
const auto& config_file : Utils::glob((config /
"*.toml").
string())) {
156 configs_map[config_file.filename().string()] = config_file;
159 configs_map[config.filename().string()] = config;
162 for (
const auto& config_file : configs_map) {
163 updateFromToml(config_file.second);
167 void checkDirs(
const std::vector<boost::filesystem::path>& configs) {
168 for (
const auto& config : configs) {
169 if (!boost::filesystem::exists(config)) {
170 throw std::runtime_error(
"Config directory " + config.string() +
" does not exist.");
175 std::vector<boost::filesystem::path> config_dirs_ = {
"/usr/lib/sota/conf.d",
"/etc/sota/conf.d/"};
178 #endif // CONFIG_UTILS_H_
RunningMode
Execution mode to run aktualizr in.