Aktualizr
C++ SOTA Client
config.h
1 #ifndef CONFIG_H_
2 #define CONFIG_H_
3 
4 #include <algorithm>
5 #include <iostream>
6 #include <string>
7 #include <vector>
8 
9 #include <boost/filesystem.hpp>
10 #include <boost/program_options.hpp>
11 #include <boost/property_tree/ini_parser.hpp>
12 
13 #include "libaktualizr/types.h"
14 
15 // Try to keep the order of config options the same as in Config::writeToStream()
16 // and Config::updateFromPropertyTree() in config.cc.
17 
18 struct LoggerConfig {
19  int loglevel{2};
20  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
21  void writeToStream(std::ostream& out_stream) const;
22 };
23 
24 // declare p11 types as incomplete so that the header can be used without libp11
25 struct PKCS11_ctx_st;
26 struct PKCS11_slot_st;
27 
28 struct P11Config {
29  boost::filesystem::path module;
30  std::string pass;
31  std::string uptane_key_id;
32  std::string tls_cacert_id;
33  std::string tls_pkey_id;
34  std::string tls_clientcert_id;
35 
36  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
37  void writeToStream(std::ostream& out_stream) const;
38 };
39 
40 struct TlsConfig {
41  std::string server;
42  boost::filesystem::path server_url_path;
43  CryptoSource ca_source{CryptoSource::kFile};
44  CryptoSource pkey_source{CryptoSource::kFile};
45  CryptoSource cert_source{CryptoSource::kFile};
46 
47  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
48  void writeToStream(std::ostream& out_stream) const;
49 };
50 
52  std::string server;
53  std::string p12_password;
54  std::string expiry_days{"36000"};
55  boost::filesystem::path provision_path;
56  ProvisionMode mode{ProvisionMode::kDefault};
57  std::string device_id;
58  std::string primary_ecu_serial;
59  std::string primary_ecu_hardware_id;
60  std::string ecu_registration_endpoint;
61 
62  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
63  void writeToStream(std::ostream& out_stream) const;
64 };
65 
66 struct UptaneConfig {
67  uint64_t polling_sec{10U};
68  std::string director_server;
69  std::string repo_server;
70  CryptoSource key_source{CryptoSource::kFile};
71  KeyType key_type{KeyType::kRSA2048};
72  bool force_install_completion{false};
73  boost::filesystem::path secondary_config_file;
74  uint64_t secondary_preinstall_wait_sec{600U};
75 
76  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
77  void writeToStream(std::ostream& out_stream) const;
78 };
79 
80 // TODO: move these to their corresponding headers
81 #define PACKAGE_MANAGER_NONE "none"
82 #define PACKAGE_MANAGER_OSTREE "ostree"
83 #define PACKAGE_MANAGER_OSTREEDOCKERAPP "ostree+docker-app"
84 
85 #ifdef BUILD_OSTREE
86 #define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_OSTREE
87 #else
88 #define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_NONE
89 #endif
90 
91 struct PackageConfig {
92  std::string type{PACKAGE_MANAGER_DEFAULT};
93 
94  // OSTree options
95  std::string os;
96  boost::filesystem::path sysroot;
97  std::string ostree_server;
98  boost::filesystem::path images_path{"/var/sota/images"};
99  boost::filesystem::path packages_file{"/usr/package.manifest"};
100 
101  // Options for simulation (to be used with "none")
102  bool fake_need_reboot{false};
103 
104  // for specialized configuration
105  std::map<std::string, std::string> extra;
106 
107  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
108  void writeToStream(std::ostream& out_stream) const;
109 };
110 
112  StorageType type{StorageType::kSqlite};
113  boost::filesystem::path path{"/var/sota"};
114 
115  // FS storage
116  utils::BasedPath uptane_metadata_path{"metadata"};
117  utils::BasedPath uptane_private_key_path{"ecukey.der"};
118  utils::BasedPath uptane_public_key_path{"ecukey.pub"};
119  utils::BasedPath tls_cacert_path{"root.crt"};
120  utils::BasedPath tls_pkey_path{"pkey.pem"};
121  utils::BasedPath tls_clientcert_path{"client.pem"};
122 
123  // SQLite storage
124  utils::BasedPath sqldb_path{"sql.db"}; // based on `/var/sota`
125 
126  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
127  void writeToStream(std::ostream& out_stream) const;
128 };
129 
130 struct ImportConfig {
131  boost::filesystem::path base_path{"/var/sota/import"};
132  utils::BasedPath uptane_private_key_path{""};
133  utils::BasedPath uptane_public_key_path{""};
134  utils::BasedPath tls_cacert_path{""};
135  utils::BasedPath tls_pkey_path{""};
136  utils::BasedPath tls_clientcert_path{""};
137 
138  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
139  void writeToStream(std::ostream& out_stream) const;
140 };
141 
142 /**
143  * @brief The TelemetryConfig struct
144  * Report device network information: IP address, hostname, MAC address.
145  */
147  bool report_network{true};
148  bool report_config{true};
149  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
150  void writeToStream(std::ostream& out_stream) const;
151 };
152 
153 enum class RollbackMode { kBootloaderNone = 0, kUbootGeneric, kUbootMasked };
154 std::ostream& operator<<(std::ostream& os, RollbackMode mode);
155 
157  RollbackMode rollback_mode{RollbackMode::kBootloaderNone};
158  boost::filesystem::path reboot_sentinel_dir{"/var/run/aktualizr-session"};
159  boost::filesystem::path reboot_sentinel_name{"need_reboot"};
160  std::string reboot_command{"/sbin/reboot"};
161 
162  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
163  void writeToStream(std::ostream& out_stream) const;
164 };
165 
166 // bundle some parts of the main config together
167 // Should be derived by calling Config::keymanagerConfig()
169  KeyManagerConfig() = delete; // only allow construction by initializer list
170  P11Config p11;
171  CryptoSource tls_ca_source;
172  CryptoSource tls_pkey_source;
173  CryptoSource tls_cert_source;
174  KeyType uptane_key_type;
175  CryptoSource uptane_key_source;
176 };
177 
178 /**
179  * @brief The BaseConfig class
180  */
181 class BaseConfig {
182  public:
183  virtual ~BaseConfig() = default;
184  void updateFromToml(const boost::filesystem::path& filename);
185  virtual void updateFromPropertyTree(const boost::property_tree::ptree& pt) = 0;
186 
187  protected:
188  void updateFromDirs(const std::vector<boost::filesystem::path>& configs);
189 
190  static void checkDirs(const std::vector<boost::filesystem::path>& configs) {
191  for (const auto& config : configs) {
192  if (!boost::filesystem::exists(config)) {
193  throw std::runtime_error("Config directory " + config.string() + " does not exist.");
194  }
195  }
196  }
197 
198  std::vector<boost::filesystem::path> config_dirs_ = {"/usr/lib/sota/conf.d", "/etc/sota/conf.d/"};
199 };
200 
201 /**
202  * Configuration object for an aktualizr instance running on a Primary ECU.
203  *
204  * This class is a parent to a series of smaller configuration objects for
205  * specific subsystems. Note that most other aktualizr-related tools have their
206  * own parent configuration objects with a reduced set of members.
207  */
208 class Config : public BaseConfig {
209  public:
210  Config();
211  explicit Config(const boost::program_options::variables_map& cmd);
212  explicit Config(const boost::filesystem::path& filename);
213  explicit Config(const std::vector<boost::filesystem::path>& config_dirs);
214 
215  KeyManagerConfig keymanagerConfig() const;
216 
217  void updateFromTomlString(const std::string& contents);
218  void postUpdateValues();
219  void writeToStream(std::ostream& sink) const;
220 
221  // Config data structures. Keep logger first so that it is taken into account
222  // while processing the others.
223  LoggerConfig logger;
224  P11Config p11;
225  TlsConfig tls;
226  ProvisionConfig provision;
227  UptaneConfig uptane;
228  PackageConfig pacman;
229  StorageConfig storage;
230  ImportConfig import;
231  TelemetryConfig telemetry;
232  BootloaderConfig bootloader;
233 
234  private:
235  void updateFromPropertyTree(const boost::property_tree::ptree& pt) override;
236  void updateFromCommandLine(const boost::program_options::variables_map& cmd);
237  bool loglevel_from_cmdline{false};
238 };
239 
240 std::ostream& operator<<(std::ostream& os, const Config& cfg);
241 
242 #endif // CONFIG_H_
ProvisionConfig
Definition: config.h:51
TlsConfig
Definition: config.h:40
types.h
BaseConfig
The BaseConfig class.
Definition: config.h:181
StorageConfig
Definition: config.h:111
utils::BasedPath
The BasedPath class Can represent an absolute or relative path, only readable through the BasePath::g...
Definition: types.h:31
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
P11Config
Definition: config.h:28
UptaneConfig
Definition: config.h:66
LoggerConfig
Definition: config.h:18
KeyManagerConfig
Definition: config.h:168
PackageConfig
Definition: config.h:91
TelemetryConfig
The TelemetryConfig struct Report device network information: IP address, hostname,...
Definition: config.h:146
BootloaderConfig
Definition: config.h:156
ImportConfig
Definition: config.h:130