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

Commit

Permalink
OTA-2488: Make Virtual Secondary use the new configuration way. Remov…
Browse files Browse the repository at this point in the history
…e the old SecondaryConfig from libaktualizr

Signed-off-by: Mike Sul <ext-mykhaylo.sul@here.com>
  • Loading branch information
Mike Sul committed Jun 28, 2019
1 parent 12f8558 commit 5146f19
Show file tree
Hide file tree
Showing 40 changed files with 462 additions and 522 deletions.
3 changes: 2 additions & 1 deletion src/aktualizr_primary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ set(TARGET aktualizr)
set(SOURCES main.cc secondary_config.h secondary_config.cc secondary.h secondary.cc)

add_executable(${TARGET} ${SOURCES})
target_link_libraries(${TARGET} aktualizr_static_lib aktualizr-posix ${AKTUALIZR_EXTERNAL_LIBS})
target_link_libraries(${TARGET} aktualizr_static_lib aktualizr-posix virtual_secondary ${AKTUALIZR_EXTERNAL_LIBS})
target_include_directories(${TARGET} PUBLIC
${PROJECT_SOURCE_DIR}/src/libaktualizr-posix
${PROJECT_SOURCE_DIR}/src/virtual_secondary
${PROJECT_SOURCE_DIR}/third_party)

aktualizr_source_file_checks(${SOURCES})
Expand Down
5 changes: 5 additions & 0 deletions src/aktualizr_primary/secondary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ static SecondaryFactoryRegistry sec_factory_registry = {
auto ip_sec_cgf = dynamic_cast<const IPSecondariesConfig&>(config);
return createIPSecondaries(ip_sec_cgf);
}},
{VirtualSecondaryConfig::Type,
[](const SecondaryConfig& config) {
auto virtual_sec_cgf = dynamic_cast<const VirtualSecondaryConfig&>(config);
return Secondaries({std::make_shared<Uptane::VirtualSecondary>(virtual_sec_cgf)});
}},
// {
// Add another secondary factory here
// }
Expand Down
7 changes: 7 additions & 0 deletions src/aktualizr_primary/secondary_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,11 @@ void JsonConfigParser::createIPSecondariesCfg(Configs& configs, Json::Value& jso
configs.push_back(resultant_cfg);
}

void JsonConfigParser::createVirtualSecondariesCfg(Configs& configs, Json::Value& json_virtual_sec_cfg) {
for (const auto& json_config : json_virtual_sec_cfg) {
auto virtual_config = std::make_shared<VirtualSecondaryConfig>(json_config);
configs.push_back(virtual_config);
}
}

} // namespace Primary
16 changes: 5 additions & 11 deletions src/aktualizr_primary/secondary_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
#include <boost/filesystem.hpp>
#include <unordered_map>

namespace Primary {

class SecondaryConfig {
public:
SecondaryConfig(const char* type) : type_(type) {}
virtual const char* type() const { return type_; }
virtual ~SecondaryConfig() = default;
#include "primary/secondary_config.h"
#include "virtualsecondary.h"

private:
const char* const type_;
};
namespace Primary {

class IPSecondaryConfig {
public:
Expand Down Expand Up @@ -73,13 +66,14 @@ class JsonConfigParser : public SecondaryConfigParser {

private:
static void createIPSecondariesCfg(Configs& configs, Json::Value& json_ip_sec_cfg);
static void createVirtualSecondariesCfg(Configs& configs, Json::Value& json_virtual_sec_cfg);
// add here a factory method for another type of secondary config

private:
using SecondaryConfigFactoryRegistry = std::unordered_map<std::string, std::function<void(Configs&, Json::Value&)>>;

SecondaryConfigFactoryRegistry sec_cfg_factory_registry_ = {
{IPSecondariesConfig::Type, createIPSecondariesCfg}
{IPSecondariesConfig::Type, createIPSecondariesCfg}, {VirtualSecondaryConfig::Type, createVirtualSecondariesCfg}
// add here factory method for another type of secondary config
};

Expand Down
1 change: 1 addition & 0 deletions src/aktualizr_secondary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if(BUILD_OSTREE)
SOURCES uptane_test.cc
LIBRARIES aktualizr-posix
ARGS ${PROJECT_BINARY_DIR}/ostree_repo PROJECT_WORKING_DIRECTORY)
target_link_libraries(t_aktualizr_secondary_uptane virtual_secondary)
else(BUILD_OSTREE)
list(APPEND TEST_SOURCES uptane_test.cc)
endif(BUILD_OSTREE)
Expand Down
3 changes: 2 additions & 1 deletion src/aktualizr_secondary/uptane_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "config/config.h"
#include "httpfake.h"
#include "uptane_test_common.h"

std::shared_ptr<INvStorage> test_storage;
AktualizrSecondaryConfig test_config;
Expand Down Expand Up @@ -47,7 +48,7 @@ TEST(aktualizr_secondary_uptane, credentialsPassing) {

auto storage = INvStorage::newStorage(config.storage);

auto sota_client = SotaUptaneClient::newTestClient(config, storage, http);
auto sota_client = UptaneTestCommon::newTestClient(config, storage, http);
EXPECT_NO_THROW(sota_client->initialize());

std::string arch = sota_client->secondaryTreehubCredentials();
Expand Down
3 changes: 0 additions & 3 deletions src/libaktualizr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,3 @@ endif (BUILD_ISOTP)

target_include_directories(aktualizr_static_lib PUBLIC
$<TARGET_PROPERTY:package_manager,INCLUDE_DIRECTORIES>)

# To be removed once the refactoring is completed
target_link_libraries(aktualizr_static_lib virtual_secondary)
21 changes: 0 additions & 21 deletions src/libaktualizr/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void UptaneConfig::updateFromPropertyTree(const boost::property_tree::ptree& pt)
CopyFromConfig(key_type, "key_type", pt);
CopyFromConfig(force_install_completion, "force_install_completion", pt);
CopyFromConfig(secondary_config_file, "secondary_config_file", pt);
CopyFromConfig(secondary_configs_dir, "secondary_configs_dir", pt);
// uptane.secondary_configs is populated by processing secondary_configs_dir
}

void UptaneConfig::writeToStream(std::ostream& out_stream) const {
Expand All @@ -70,7 +68,6 @@ void UptaneConfig::writeToStream(std::ostream& out_stream) const {
writeOption(out_stream, key_type, "key_type");
writeOption(out_stream, force_install_completion, "force_install_completion");
writeOption(out_stream, secondary_config_file, "secondary_config_file");
writeOption(out_stream, secondary_configs_dir, "secondary_configs_dir");
}

/**
Expand Down Expand Up @@ -166,10 +163,6 @@ void Config::postUpdateValues() {
}
}

if (!uptane.secondary_configs_dir.empty()) {
readSecondaryConfigs(uptane.secondary_configs_dir);
}

LOG_TRACE << "Final configuration that will be used: \n" << (*this);
}

Expand Down Expand Up @@ -226,20 +219,6 @@ void Config::updateFromCommandLine(const boost::program_options::variables_map&
if (cmd.count("secondary-config-file") != 0) {
uptane.secondary_config_file = cmd["secondary_config_file"].as<boost::filesystem::path>();
}
if (cmd.count("secondary-configs-dir") != 0) {
uptane.secondary_configs_dir = cmd["secondary-configs-dir"].as<boost::filesystem::path>();
}
}

void Config::readSecondaryConfigs(const boost::filesystem::path& sconfigs_dir) {
if (!boost::filesystem::is_directory(sconfigs_dir)) {
LOG_ERROR << "Could not read secondary configs from " << sconfigs_dir << ": not a directory";
return;
}
for (const auto& config_file : Utils::getDirEntriesByExt(sconfigs_dir, ".json")) {
LOG_INFO << "Parsing secondary config: " << config_file;
uptane.secondary_configs.emplace_back(config_file);
}
}

void Config::writeToStream(std::ostream& sink) const {
Expand Down
5 changes: 1 addition & 4 deletions src/libaktualizr/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "package_manager/packagemanagerconfig.h"
#include "storage/storage_config.h"
#include "telemetry/telemetryconfig.h"
#include "uptane/secondaryconfig.h"
#include "utilities/config_utils.h"
#include "utilities/types.h"

Expand Down Expand Up @@ -58,9 +57,7 @@ struct UptaneConfig {
CryptoSource key_source{CryptoSource::kFile};
KeyType key_type{KeyType::kRSA2048};
bool force_install_completion{false};
boost::filesystem::path secondary_configs_dir;
boost::filesystem::path secondary_config_file;
std::vector<Uptane::SecondaryConfig> secondary_configs{};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
Expand Down Expand Up @@ -102,7 +99,7 @@ class Config : public BaseConfig {
private:
void updateFromPropertyTree(const boost::property_tree::ptree& pt) override;
void updateFromCommandLine(const boost::program_options::variables_map& cmd);
void readSecondaryConfigs(const boost::filesystem::path& sconfigs_dir);
// void readSecondaryConfigs(const boost::filesystem::path& sconfigs_dir);

std::vector<boost::filesystem::path> config_dirs_ = {"/usr/lib/sota/conf.d", "/etc/sota/conf.d/"};
bool loglevel_from_cmdline{false};
Expand Down
27 changes: 0 additions & 27 deletions src/libaktualizr/config/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,6 @@ TEST(config, ExtractCredentials) {
"D27E3E56BEF02AAA6D6FFEFDA5357458C477A8E891C5EADF4F04CE67BB5866A4");
}

/*
* Parse secondary config files in JSON format.
*/
TEST(config, SecondaryConfig) {
TemporaryDirectory temp_dir;
const std::string conf_path_str = (temp_dir.Path() / "config.toml").string();
TestUtils::writePathToConfig("tests/config/minimal.toml", conf_path_str, temp_dir.Path());

bpo::variables_map cmd;
bpo::options_description description("some text");
// clang-format off
description.add_options()
("secondary-configs-dir", bpo::value<boost::filesystem::path>(), "directory containing secondary ECU configuration files")
("config,c", bpo::value<std::vector<boost::filesystem::path> >()->composing(), "configuration file or directory");

// clang-format on
const char *argv[] = {"aktualizr", "--secondary-configs-dir", "config/secondary", "-c", conf_path_str.c_str()};
bpo::store(bpo::parse_command_line(5, argv, description), cmd);

Config conf(cmd);
EXPECT_EQ(conf.uptane.secondary_configs.size(), 1);
EXPECT_EQ(conf.uptane.secondary_configs[0].secondary_type, Uptane::SecondaryType::kVirtual);
EXPECT_EQ(conf.uptane.secondary_configs[0].ecu_hardware_id, "demo-virtual");
// If not provided, serial is not generated until SotaUptaneClient is initialized.
EXPECT_TRUE(conf.uptane.secondary_configs[0].ecu_serial.empty());
}

/**
* Start in device credential provisioning mode.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/libaktualizr/primary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set(SOURCES aktualizr.cc
reportqueue.cc
sotauptaneclient.cc)

set(HEADERS aktualizr.h
set(HEADERS secondary_config.h
aktualizr.h
events.h
initializer.h
reportqueue.h
Expand All @@ -12,7 +13,6 @@ set(HEADERS aktualizr.h


add_library(primary OBJECT ${SOURCES})
target_include_directories(primary PUBLIC ${PROJECT_SOURCE_DIR}/src/virtual_secondary)

add_aktualizr_test(NAME aktualizr SOURCES aktualizr_test.cc PROJECT_WORKING_DIRECTORY ARGS ${PROJECT_BINARY_DIR}/uptane_repos)
add_dependencies(t_aktualizr uptane_repo_full_no_correlation_id)
Expand All @@ -23,13 +23,16 @@ if (BUILD_OSTREE)
set_target_properties(t_aktualizr_fullostree PROPERTIES LINK_FLAGS -Wl,--export-dynamic)
add_dependencies(t_aktualizr_fullostree ostree_mock aktualizr-repo make_ostree_sysroot)
set_tests_properties(test_aktualizr_fullostree PROPERTIES ENVIRONMENT LD_PRELOAD=$<TARGET_FILE:ostree_mock>)
target_link_libraries(t_aktualizr_fullostree virtual_secondary)
else (BUILD_OSTREE)
aktualizr_source_file_checks(aktualizr_fullostree_test.cc)
endif (BUILD_OSTREE)

add_aktualizr_test(NAME reportqueue SOURCES reportqueue_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME emptytargets SOURCES empty_targets_test.cc PROJECT_WORKING_DIRECTORY
ARGS "$<TARGET_FILE:aktualizr-repo>")
target_link_libraries(t_emptytargets virtual_secondary)

add_aktualizr_test(NAME device_cred_prov SOURCES device_cred_prov_test.cc PROJECT_WORKING_DIRECTORY)
set_tests_properties(test_device_cred_prov PROPERTIES LABELS "crypto")

Expand Down
6 changes: 5 additions & 1 deletion src/libaktualizr/primary/aktualizr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <openssl/rand.h>
#include <sodium.h>

#include "primary/events.h"
#include "utilities/timer.h"

using std::make_shared;
Expand All @@ -24,7 +25,10 @@ Aktualizr::Aktualizr(Config &config, std::shared_ptr<INvStorage> storage_in, std
systemSetup();
sig_ = make_shared<boost::signals2::signal<void(shared_ptr<event::BaseEvent>)>>();
storage_ = std::move(storage_in);
uptane_client_ = SotaUptaneClient::newTestClient(config_, storage_, std::move(http_in), sig_);
std::shared_ptr<Bootloader> bootloader_in = std::make_shared<Bootloader>(config_.bootloader, *storage_);
std::shared_ptr<ReportQueue> report_queue_in = std::make_shared<ReportQueue>(config_, http_in);

uptane_client_ = std::make_shared<SotaUptaneClient>(config_, storage_, http_in, bootloader_in, report_queue_in, sig_);
}

void Aktualizr::systemSetup() {
Expand Down
37 changes: 5 additions & 32 deletions src/libaktualizr/primary/aktualizr.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,43 +158,16 @@ class Aktualizr {
*/
boost::signals2::connection SetSignalHandler(const std::function<void(std::shared_ptr<event::BaseEvent>)>& handler);

private:
FRIEND_TEST(Aktualizr, FullNoUpdates);
FRIEND_TEST(Aktualizr, DeviceInstallationResult);
FRIEND_TEST(Aktualizr, FullWithUpdates);
FRIEND_TEST(Aktualizr, FullWithUpdatesNeedReboot);
FRIEND_TEST(Aktualizr, FinalizationFailure);
FRIEND_TEST(Aktualizr, InstallationFailure);
FRIEND_TEST(Aktualizr, AutoRebootAfterUpdate);
FRIEND_TEST(Aktualizr, FullMultipleSecondaries);
FRIEND_TEST(Aktualizr, CheckNoUpdates);
FRIEND_TEST(Aktualizr, DownloadWithUpdates);
FRIEND_TEST(Aktualizr, DownloadFailures);
FRIEND_TEST(Aktualizr, InstallWithUpdates);
FRIEND_TEST(Aktualizr, ReportDownloadProgress);
FRIEND_TEST(Aktualizr, CampaignCheckAndControl);
FRIEND_TEST(Aktualizr, FullNoCorrelationId);
FRIEND_TEST(Aktualizr, ManifestCustom);
FRIEND_TEST(Aktualizr, APICheck);
FRIEND_TEST(Aktualizr, UpdateCheckCompleteError);
FRIEND_TEST(Aktualizr, PauseResumeQueue);
FRIEND_TEST(Aktualizr, AddSecondary);
FRIEND_TEST(Aktualizr, EmptyTargets);
FRIEND_TEST(Aktualizr, EmptyTargetsAfterInstall);
FRIEND_TEST(Aktualizr, FullOstreeUpdate);
FRIEND_TEST(Delegation, Basic);
FRIEND_TEST(Delegation, RevokeAfterCheckUpdates);
FRIEND_TEST(Delegation, RevokeAfterInstall);
FRIEND_TEST(Delegation, RevokeAfterDownload);
FRIEND_TEST(Delegation, IterateAll);

// This constructor is only used for tests
protected:
Aktualizr(Config& config, std::shared_ptr<INvStorage> storage_in, std::shared_ptr<HttpInterface> http_in);

std::shared_ptr<SotaUptaneClient> uptane_client_;

private:
static void systemSetup();

Config& config_;
std::shared_ptr<INvStorage> storage_;
std::shared_ptr<SotaUptaneClient> uptane_client_;
std::shared_ptr<event::Channel> sig_;
api::CommandQueue api_queue_;
};
Expand Down
5 changes: 2 additions & 3 deletions src/libaktualizr/primary/aktualizr_fullostree_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ TEST(Aktualizr, FullOstreeUpdate) {
boost::filesystem::remove(conf.bootloader.reboot_sentinel_dir / conf.bootloader.reboot_sentinel_name);

{
Aktualizr aktualizr(conf);

UptaneTestCommon::TestAktualizr aktualizr(conf);
aktualizr.Initialize();

result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
ASSERT_EQ(update_result.status, result::UpdateStatus::kNoUpdatesAvailable);

// check new version
const auto target = aktualizr.uptane_client_->package_manager_->getCurrent();
const auto target = aktualizr.uptane_client()->package_manager_->getCurrent();
EXPECT_EQ(target.sha256Hash(), new_rev);
}
}
Expand Down
Loading

0 comments on commit 5146f19

Please sign in to comment.