Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1707 from advancedtelematic/ref/config
Browse files Browse the repository at this point in the history
ref: moving all configs definition into a single file
  • Loading branch information
pattivacek committed Jul 7, 2020
2 parents ce3f819 + 85ff81d commit 689b6b0
Show file tree
Hide file tree
Showing 75 changed files with 402 additions and 468 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ function(aktualizr_source_file_checks)
endif()
endfunction()


# Use C++11, but without GNU or other extensions
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
Expand All @@ -319,6 +318,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

############### BUILD RULES
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/src/libaktualizr)
include_directories(${PROJECT_SOURCE_DIR}/third_party/googletest/googletest/include)
include_directories(${JSONCPP_INCLUDE_DIRS})
Expand Down
1 change: 1 addition & 0 deletions docs/doxygen/Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ WARN_LOGFILE =
INPUT = @CMAKE_SOURCE_DIR@/docs \
@CMAKE_SOURCE_DIR@/docs/doxygen \
@CMAKE_SOURCE_DIR@/include \
@CMAKE_SOURCE_DIR@/include/libaktualizr \
@CMAKE_SOURCE_DIR@/src/aktualizr_get \
@CMAKE_SOURCE_DIR@/src/aktualizr_info \
@CMAKE_SOURCE_DIR@/src/aktualizr_lite \
Expand Down
244 changes: 244 additions & 0 deletions include/libaktualizr/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#ifndef CONFIG_H_
#define CONFIG_H_

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "utilities/types.h"
#include "utilities/utils.h"

// Try to keep the order of config options the same as in Config::writeToStream()
// and Config::updateFromPropertyTree() in config.cc.

struct LoggerConfig {
int loglevel{2};
void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

// declare p11 types as incomplete so that the header can be used without libp11
struct PKCS11_ctx_st;
struct PKCS11_slot_st;

struct P11Config {
boost::filesystem::path module;
std::string pass;
std::string uptane_key_id;
std::string tls_cacert_id;
std::string tls_pkey_id;
std::string tls_clientcert_id;

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct TlsConfig {
std::string server;
boost::filesystem::path server_url_path;
CryptoSource ca_source{CryptoSource::kFile};
CryptoSource pkey_source{CryptoSource::kFile};
CryptoSource cert_source{CryptoSource::kFile};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct ProvisionConfig {
std::string server;
std::string p12_password;
std::string expiry_days{"36000"};
boost::filesystem::path provision_path;
ProvisionMode mode{ProvisionMode::kDefault};
std::string device_id;
std::string primary_ecu_serial;
std::string primary_ecu_hardware_id;
std::string ecu_registration_endpoint;

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct UptaneConfig {
uint64_t polling_sec{10U};
std::string director_server;
std::string repo_server;
CryptoSource key_source{CryptoSource::kFile};
KeyType key_type{KeyType::kRSA2048};
bool force_install_completion{false};
boost::filesystem::path secondary_config_file;
uint64_t secondary_preinstall_wait_sec{600U};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

// TODO: move these to their corresponding headers
#define PACKAGE_MANAGER_NONE "none"
#define PACKAGE_MANAGER_OSTREE "ostree"
#define PACKAGE_MANAGER_DEBIAN "debian"
#define PACKAGE_MANAGER_ANDROID "android"
#define PACKAGE_MANAGER_OSTREEDOCKERAPP "ostree+docker-app"

#ifdef BUILD_OSTREE
#define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_OSTREE
#else
#define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_NONE
#endif

struct PackageConfig {
std::string type{PACKAGE_MANAGER_DEFAULT};

// OSTree options
std::string os;
boost::filesystem::path sysroot;
std::string ostree_server;
boost::filesystem::path images_path{"/var/sota/images"};
boost::filesystem::path packages_file{"/usr/package.manifest"};

// Options for simulation (to be used with "none")
bool fake_need_reboot{false};

// for specialized configuration
std::map<std::string, std::string> extra;

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct StorageConfig {
StorageType type{StorageType::kSqlite};
boost::filesystem::path path{"/var/sota"};

// FS storage
BasedPath uptane_metadata_path{"metadata"};
BasedPath uptane_private_key_path{"ecukey.der"};
BasedPath uptane_public_key_path{"ecukey.pub"};
BasedPath tls_cacert_path{"root.crt"};
BasedPath tls_pkey_path{"pkey.pem"};
BasedPath tls_clientcert_path{"client.pem"};

// SQLite storage
BasedPath sqldb_path{"sql.db"}; // based on `/var/sota`

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct ImportConfig {
boost::filesystem::path base_path{"/var/sota/import"};
BasedPath uptane_private_key_path{""};
BasedPath uptane_public_key_path{""};
BasedPath tls_cacert_path{""};
BasedPath tls_pkey_path{""};
BasedPath tls_clientcert_path{""};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

/**
* @brief The TelemetryConfig struct
* Report device network information: IP address, hostname, MAC address.
*/
struct TelemetryConfig {
bool report_network{true};
bool report_config{true};
void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

enum class RollbackMode { kBootloaderNone = 0, kUbootGeneric, kUbootMasked };
std::ostream& operator<<(std::ostream& os, RollbackMode mode);

struct BootloaderConfig {
RollbackMode rollback_mode{RollbackMode::kBootloaderNone};
boost::filesystem::path reboot_sentinel_dir{"/var/run/aktualizr-session"};
boost::filesystem::path reboot_sentinel_name{"need_reboot"};
std::string reboot_command{"/sbin/reboot"};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

// bundle some parts of the main config together
// Should be derived by calling Config::keymanagerConfig()
struct KeyManagerConfig {
KeyManagerConfig() = delete; // only allow construction by initializer list
P11Config p11;
CryptoSource tls_ca_source;
CryptoSource tls_pkey_source;
CryptoSource tls_cert_source;
KeyType uptane_key_type;
CryptoSource uptane_key_source;
};

/**
* @brief The BaseConfig class
*/
class BaseConfig {
public:
virtual ~BaseConfig() = default;
void updateFromToml(const boost::filesystem::path& filename);
virtual void updateFromPropertyTree(const boost::property_tree::ptree& pt) = 0;

protected:
void updateFromDirs(const std::vector<boost::filesystem::path>& configs);

static void checkDirs(const std::vector<boost::filesystem::path>& configs) {
for (const auto& config : configs) {
if (!boost::filesystem::exists(config)) {
throw std::runtime_error("Config directory " + config.string() + " does not exist.");
}
}
}

std::vector<boost::filesystem::path> config_dirs_ = {"/usr/lib/sota/conf.d", "/etc/sota/conf.d/"};
};

/**
* Configuration object for an aktualizr instance running on a Primary ECU.
*
* This class is a parent to a series of smaller configuration objects for
* specific subsystems. Note that most other aktualizr-related tools have their
* own parent configuration objects with a reduced set of members.
*/
class Config : public BaseConfig {
public:
Config();
explicit Config(const boost::program_options::variables_map& cmd);
explicit Config(const boost::filesystem::path& filename);
explicit Config(const std::vector<boost::filesystem::path>& config_dirs);

KeyManagerConfig keymanagerConfig() const;

void updateFromTomlString(const std::string& contents);
void postUpdateValues();
void writeToStream(std::ostream& sink) const;

// Config data structures. Keep logger first so that it is taken into account
// while processing the others.
LoggerConfig logger;
P11Config p11;
TlsConfig tls;
ProvisionConfig provision;
UptaneConfig uptane;
PackageConfig pacman;
StorageConfig storage;
ImportConfig import;
TelemetryConfig telemetry;
BootloaderConfig bootloader;

private:
void updateFromPropertyTree(const boost::property_tree::ptree& pt) override;
void updateFromCommandLine(const boost::program_options::variables_map& cmd);
bool loglevel_from_cmdline{false};
};

std::ostream& operator<<(std::ostream& os, const Config& cfg);

#endif // CONFIG_H_
2 changes: 1 addition & 1 deletion src/aktualizr_get/get.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef AKTUALIZR_GET_HELPERS
#define AKTUALIZR_GET_HELPERS

#include "config/config.h"
#include "libaktualizr/config.h"

std::string aktualizrGet(Config &config, const std::string &url);

Expand Down
3 changes: 2 additions & 1 deletion src/aktualizr_get/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

#include "config/config.h"
#include "get.h"
#include "libaktualizr/config.h"
#include "logging/logging.h"
#include "utilities/aktualizr_version.h"

namespace bpo = boost::program_options;
Expand Down
5 changes: 1 addition & 4 deletions src/aktualizr_info/aktualizr_info_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
#include <boost/program_options.hpp>
#include <boost/property_tree/ini_parser.hpp>

#include "bootloader/bootloader_config.h"
#include "logging/logging_config.h"
#include "package_manager/packagemanagerconfig.h"
#include "storage/storage_config.h"
#include "libaktualizr/config.h"
#include "utilities/config_utils.h"

// Try to keep the order of config options the same as in
Expand Down
2 changes: 1 addition & 1 deletion src/aktualizr_info/aktualizr_info_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <boost/asio/io_service.hpp>
#include <boost/process.hpp>

#include "config/config.h"
#include "libaktualizr/config.h"
#include "storage/sqlstorage.h"
#include "test_utils.h"
#include "utilities/utils.h"
Expand Down
2 changes: 1 addition & 1 deletion src/aktualizr_lite/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

#include "config/config.h"
#include "helpers.h"
#include "libaktualizr/config.h"
#include "utilities/aktualizr_version.h"

namespace bpo = boost::program_options;
Expand Down
2 changes: 1 addition & 1 deletion src/aktualizr_primary/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <boost/property_tree/ini_parser.hpp>
#include <boost/signals2.hpp>

#include "config/config.h"
#include "libaktualizr/config.h"
#include "logging/logging.h"
#include "primary/aktualizr.h"
#include "primary/aktualizr_helpers.h"
Expand Down
7 changes: 1 addition & 6 deletions src/aktualizr_secondary/aktualizr_secondary_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
#include <boost/program_options.hpp>
#include <boost/property_tree/ini_parser.hpp>

#include "bootloader/bootloader_config.h"
#include "crypto/keymanager_config.h"
#include "crypto/p11_config.h"
#include "logging/logging_config.h"
#include "package_manager/packagemanagerconfig.h"
#include "storage/storage_config.h"
#include "libaktualizr/config.h"
#include "utilities/config_utils.h"

// Try to keep the order of config options the same as in
Expand Down
2 changes: 1 addition & 1 deletion src/cert_provider/cert_provider_shared_cred_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <boost/format.hpp>

#include "cert_provider_test.h"
#include "config/config.h"
#include "libaktualizr/config.h"
#include "utilities/utils.h"

static boost::filesystem::path CERT_PROVIDER_PATH;
Expand Down
2 changes: 1 addition & 1 deletion src/cert_provider/cert_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <boost/format.hpp>

#include "cert_provider_test.h"
#include "config/config.h"
#include "crypto/crypto.h"
#include "libaktualizr/config.h"
#include "utilities/utils.h"

static boost::filesystem::path CERT_PROVIDER_PATH;
Expand Down
2 changes: 1 addition & 1 deletion src/cert_provider/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "json/json.h"

#include "bootstrap/bootstrap.h"
#include "config/config.h"
#include "crypto/crypto.h"
#include "http/httpclient.h"
#include "libaktualizr/config.h"
#include "logging/logging.h"
#include "utilities/aktualizr_version.h"
#include "utilities/utils.h"
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr-c/test/api-test-utils/api-test-utils.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "api-test-utils.h"

#include <boost/process.hpp>
#include "config/config.h"
#include "libaktualizr/config.h"
#include "test_utils.h"
#include "utilities/utils.h"

Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr-c/test/api-test-utils/api-test-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#ifdef __cplusplus
#include <boost/process.hpp>
#include "config/config.h"
#include "libaktualizr/config.h"

using Config = Config;
using FakeHttpServer = boost::process::child;
Expand Down
Loading

0 comments on commit 689b6b0

Please sign in to comment.