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(
BasedPath& 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 BasedPath bp{StripQuotesFromStrings(value.get())};
106 template <
typename T>
107 inline void CopySubtreeFromConfig(T& dest,
const std::string& subtree_name,
const boost::property_tree::ptree& pt) {
108 auto subtree = pt.get_child_optional(subtree_name);
109 if (subtree.is_initialized()) {
110 dest.updateFromPropertyTree(subtree.get());
113 dest.updateFromPropertyTree(boost::property_tree::ptree());
117 template <
typename T>
118 inline void WriteSectionToStream(T& sec,
const std::string& section_name, std::ostream& os) {
119 os << std::boolalpha;
120 os <<
"[" << section_name <<
"]\n";
121 sec.writeToStream(os);
128 void updateFromToml(
const boost::filesystem::path& filename) {
129 LOG_INFO <<
"Reading config: " << filename;
130 if (!boost::filesystem::exists(filename)) {
131 throw std::runtime_error(
"Config file " + filename.string() +
" does not exist.");
133 boost::property_tree::ptree pt;
134 boost::property_tree::ini_parser::read_ini(filename.string(), pt);
135 updateFromPropertyTree(pt);
137 virtual void updateFromPropertyTree(
const boost::property_tree::ptree& pt) = 0;
140 void updateFromDirs(
const std::vector<boost::filesystem::path>& configs) {
141 std::map<std::string, boost::filesystem::path> configs_map;
142 for (
const auto& config : configs) {
143 if (!boost::filesystem::exists(config)) {
146 if (boost::filesystem::is_directory(config)) {
147 for (
const auto& config_file : Utils::getDirEntriesByExt(config,
".toml")) {
148 configs_map[config_file.filename().string()] = config_file;
151 configs_map[config.filename().string()] = config;
154 for (
const auto& config_file : configs_map) {
155 updateFromToml(config_file.second);
159 void checkDirs(
const std::vector<boost::filesystem::path>& configs) {
160 for (
const auto& config : configs) {
161 if (!boost::filesystem::exists(config)) {
162 throw std::runtime_error(
"Config directory " + config.string() +
" does not exist.");
167 std::vector<boost::filesystem::path> config_dirs_ = {
"/usr/lib/sota/conf.d",
"/etc/sota/conf.d/"};
170 #endif // CONFIG_UTILS_H_