Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
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_DEBIAN "debian"
84 #define PACKAGE_MANAGER_ANDROID "android"
85 #define PACKAGE_MANAGER_OSTREEDOCKERAPP "ostree+docker-app"
86 
87 #ifdef BUILD_OSTREE
88 #define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_OSTREE
89 #else
90 #define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_NONE
91 #endif
92 
93 struct PackageConfig {
94  std::string type{PACKAGE_MANAGER_DEFAULT};
95 
96  // OSTree options
97  std::string os;
98  boost::filesystem::path sysroot;
99  std::string ostree_server;
100  boost::filesystem::path images_path{"/var/sota/images"};
101  boost::filesystem::path packages_file{"/usr/package.manifest"};
102 
103  // Options for simulation (to be used with "none")
104  bool fake_need_reboot{false};
105 
106  // for specialized configuration
107  std::map<std::string, std::string> extra;
108 
109  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
110  void writeToStream(std::ostream& out_stream) const;
111 };
112 
114  StorageType type{StorageType::kSqlite};
115  boost::filesystem::path path{"/var/sota"};
116 
117  // FS storage
118  utils::BasedPath uptane_metadata_path{"metadata"};
119  utils::BasedPath uptane_private_key_path{"ecukey.der"};
120  utils::BasedPath uptane_public_key_path{"ecukey.pub"};
121  utils::BasedPath tls_cacert_path{"root.crt"};
122  utils::BasedPath tls_pkey_path{"pkey.pem"};
123  utils::BasedPath tls_clientcert_path{"client.pem"};
124 
125  // SQLite storage
126  utils::BasedPath sqldb_path{"sql.db"}; // based on `/var/sota`
127 
128  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
129  void writeToStream(std::ostream& out_stream) const;
130 };
131 
132 struct ImportConfig {
133  boost::filesystem::path base_path{"/var/sota/import"};
134  utils::BasedPath uptane_private_key_path{""};
135  utils::BasedPath uptane_public_key_path{""};
136  utils::BasedPath tls_cacert_path{""};
137  utils::BasedPath tls_pkey_path{""};
138  utils::BasedPath tls_clientcert_path{""};
139 
140  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
141  void writeToStream(std::ostream& out_stream) const;
142 };
143 
144 /**
145  * @brief The TelemetryConfig struct
146  * Report device network information: IP address, hostname, MAC address.
147  */
149  bool report_network{true};
150  bool report_config{true};
151  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
152  void writeToStream(std::ostream& out_stream) const;
153 };
154 
155 enum class RollbackMode { kBootloaderNone = 0, kUbootGeneric, kUbootMasked };
156 std::ostream& operator<<(std::ostream& os, RollbackMode mode);
157 
159  RollbackMode rollback_mode{RollbackMode::kBootloaderNone};
160  boost::filesystem::path reboot_sentinel_dir{"/var/run/aktualizr-session"};
161  boost::filesystem::path reboot_sentinel_name{"need_reboot"};
162  std::string reboot_command{"/sbin/reboot"};
163 
164  void updateFromPropertyTree(const boost::property_tree::ptree& pt);
165  void writeToStream(std::ostream& out_stream) const;
166 };
167 
168 // bundle some parts of the main config together
169 // Should be derived by calling Config::keymanagerConfig()
171  KeyManagerConfig() = delete; // only allow construction by initializer list
172  P11Config p11;
173  CryptoSource tls_ca_source;
174  CryptoSource tls_pkey_source;
175  CryptoSource tls_cert_source;
176  KeyType uptane_key_type;
177  CryptoSource uptane_key_source;
178 };
179 
180 /**
181  * @brief The BaseConfig class
182  */
183 class BaseConfig {
184  public:
185  virtual ~BaseConfig() = default;
186  void updateFromToml(const boost::filesystem::path& filename);
187  virtual void updateFromPropertyTree(const boost::property_tree::ptree& pt) = 0;
188 
189  protected:
190  void updateFromDirs(const std::vector<boost::filesystem::path>& configs);
191 
192  static void checkDirs(const std::vector<boost::filesystem::path>& configs) {
193  for (const auto& config : configs) {
194  if (!boost::filesystem::exists(config)) {
195  throw std::runtime_error("Config directory " + config.string() + " does not exist.");
196  }
197  }
198  }
199 
200  std::vector<boost::filesystem::path> config_dirs_ = {"/usr/lib/sota/conf.d", "/etc/sota/conf.d/"};
201 };
202 
203 /**
204  * Configuration object for an aktualizr instance running on a Primary ECU.
205  *
206  * This class is a parent to a series of smaller configuration objects for
207  * specific subsystems. Note that most other aktualizr-related tools have their
208  * own parent configuration objects with a reduced set of members.
209  */
210 class Config : public BaseConfig {
211  public:
212  Config();
213  explicit Config(const boost::program_options::variables_map& cmd);
214  explicit Config(const boost::filesystem::path& filename);
215  explicit Config(const std::vector<boost::filesystem::path>& config_dirs);
216 
217  KeyManagerConfig keymanagerConfig() const;
218 
219  void updateFromTomlString(const std::string& contents);
220  void postUpdateValues();
221  void writeToStream(std::ostream& sink) const;
222 
223  // Config data structures. Keep logger first so that it is taken into account
224  // while processing the others.
225  LoggerConfig logger;
226  P11Config p11;
227  TlsConfig tls;
228  ProvisionConfig provision;
229  UptaneConfig uptane;
230  PackageConfig pacman;
231  StorageConfig storage;
232  ImportConfig import;
233  TelemetryConfig telemetry;
234  BootloaderConfig bootloader;
235 
236  private:
237  void updateFromPropertyTree(const boost::property_tree::ptree& pt) override;
238  void updateFromCommandLine(const boost::program_options::variables_map& cmd);
239  bool loglevel_from_cmdline{false};
240 };
241 
242 std::ostream& operator<<(std::ostream& os, const Config& cfg);
243 
244 #endif // CONFIG_H_
The BaseConfig class.
Definition: config.h:183
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:210
The TelemetryConfig struct Report device network information: IP address, hostname, MAC address.
Definition: config.h:148
The BasedPath class Can represent an absolute or relative path, only readable through the BasePath::g...
Definition: types.h:29