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

Commit

Permalink
OTA-2488: Move Managed and Virtual secondaries out of Uptane namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Sul <ext-mykhaylo.sul@here.com>
  • Loading branch information
Mike Sul committed Jul 1, 2019
1 parent 5146f19 commit f55f554
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 55 deletions.
1 change: 0 additions & 1 deletion docs/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Options for Uptane.
| `repo_server` | | Image repository server URL. If empty, set to `tls.server` with `/repo` appended.
| `key_source` | `"file"` | Where to read the device's private key from. Options: `"file"`, `"pkcs11"`.
| `key_type` | `"RSA2048"` | Type of cryptographic keys to use. Options: `"ED25519"`, `"RSA2048"`, `"RSA3072"` or `"RSA4096"`.
| `secondary_configs_dir` | `""` | Directory containing individual secondary json configuration files. Example here: link:{aktualizr-github-url}/config/secondary/virtualsec.json[]. This is currently only used for virtual secondaries and will be soon replaced entirely by `secondary_configs_file`.
| `secondary_config_file` | `""` | A path to a json file containing configuration of Secondary ECU(s) to be registered with the primary. Example here: link:{aktualizr-github-url}/config/posix-secondary-config.json[].
| `force_install_completion`| false | Forces installation completion. Causes a system reboot in case of an ostree package manager. Emulates a reboot in case of a fake package manager.
|==========================================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/aktualizr_primary/secondary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static SecondaryFactoryRegistry sec_factory_registry = {
{VirtualSecondaryConfig::Type,
[](const SecondaryConfig& config) {
auto virtual_sec_cgf = dynamic_cast<const VirtualSecondaryConfig&>(config);
return Secondaries({std::make_shared<Uptane::VirtualSecondary>(virtual_sec_cgf)});
return Secondaries({std::make_shared<VirtualSecondary>(virtual_sec_cgf)});
}},
// {
// Add another secondary factory here
Expand Down
8 changes: 4 additions & 4 deletions src/libaktualizr/primary/aktualizr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TEST(Aktualizr, AddSecondary) {

Primary::VirtualSecondaryConfig ecu_config = virtual_configuration(temp_dir.Path());

aktualizr.AddSecondary(std::make_shared<Uptane::VirtualSecondary>(ecu_config));
aktualizr.AddSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config));

aktualizr.Initialize();

Expand All @@ -156,7 +156,7 @@ TEST(Aktualizr, AddSecondary) {
EXPECT_EQ(expected_ecus.size(), 0);

ecu_config.ecu_serial = "ecuserial4";
auto sec4 = std::make_shared<Uptane::VirtualSecondary>(ecu_config);
auto sec4 = std::make_shared<Primary::VirtualSecondary>(ecu_config);
EXPECT_THROW(aktualizr.AddSecondary(sec4), std::logic_error);
}

Expand All @@ -180,7 +180,7 @@ TEST(Aktualizr, DeviceInstallationResult) {

Primary::VirtualSecondaryConfig ecu_config = virtual_configuration(temp_dir.Path());

aktualizr.AddSecondary(std::make_shared<Uptane::VirtualSecondary>(ecu_config));
aktualizr.AddSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config));

aktualizr.Initialize();

Expand Down Expand Up @@ -1005,7 +1005,7 @@ TEST(Aktualizr, FullMultipleSecondaries) {
UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
UptaneTestCommon::addDefaultSecondary(conf, temp_dir2, "sec_serial2", "sec_hwid2");
if (boost::filesystem::exists(conf.uptane.secondary_config_file)) {
aktualizr.AddSecondary(std::make_shared<Uptane::VirtualSecondary>(
aktualizr.AddSecondary(std::make_shared<Primary::VirtualSecondary>(
Primary::VirtualSecondaryConfig::create_from_file(conf.uptane.secondary_config_file)));
}
std::function<void(std::shared_ptr<event::BaseEvent> event)> f_cb = process_events_FullMultipleSecondaries;
Expand Down
12 changes: 6 additions & 6 deletions src/libaktualizr/primary/uptane_key_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class UptaneKey_Check_Test {
// Verify that each secondary has valid keys.
std::map<Uptane::EcuSerial, std::shared_ptr<Uptane::SecondaryInterface> >::iterator it;
for (it = sota_client->secondaries.begin(); it != sota_client->secondaries.end(); it++) {
std::shared_ptr<Uptane::ManagedSecondary> managed =
boost::polymorphic_pointer_downcast<Uptane::ManagedSecondary>(it->second);
std::shared_ptr<Primary::ManagedSecondary> managed =
boost::polymorphic_pointer_downcast<Primary::ManagedSecondary>(it->second);
std::string public_key;
std::string private_key;
EXPECT_TRUE(managed->loadKeys(&public_key, &private_key));
Expand Down Expand Up @@ -117,8 +117,8 @@ TEST(UptaneKey, CheckAllKeys) {
initKeyTests(config, ecu_config1, ecu_config2, temp_dir, http->tls_server);
auto storage = INvStorage::newStorage(config.storage);
auto sota_client = UptaneTestCommon::newTestClient(config, storage, http);
sota_client->addNewSecondary(std::make_shared<Uptane::VirtualSecondary>(ecu_config1));
sota_client->addNewSecondary(std::make_shared<Uptane::VirtualSecondary>(ecu_config2));
sota_client->addNewSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config1));
sota_client->addNewSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config2));
EXPECT_NO_THROW(sota_client->initialize());
UptaneKey_Check_Test::checkKeyTests(storage, sota_client);
}
Expand All @@ -140,8 +140,8 @@ TEST(UptaneKey, RecoverWithoutKeys) {
{
auto storage = INvStorage::newStorage(config.storage);
auto sota_client = UptaneTestCommon::newTestClient(config, storage, http);
sota_client->addNewSecondary(std::make_shared<Uptane::VirtualSecondary>(ecu_config1));
sota_client->addNewSecondary(std::make_shared<Uptane::VirtualSecondary>(ecu_config2));
sota_client->addNewSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config1));
sota_client->addNewSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config2));
EXPECT_NO_THROW(sota_client->initialize());
UptaneKey_Check_Test::checkKeyTests(storage, sota_client);

Expand Down
4 changes: 2 additions & 2 deletions src/libaktualizr/uptane/uptane_ci_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ TEST(UptaneCI, CheckKeys) {

std::map<Uptane::EcuSerial, std::shared_ptr<Uptane::SecondaryInterface> >::iterator it;
for (it = sota_client->secondaries.begin(); it != sota_client->secondaries.end(); it++) {
std::shared_ptr<Uptane::ManagedSecondary> managed_secondary =
std::dynamic_pointer_cast<Uptane::ManagedSecondary>(it->second);
std::shared_ptr<Primary::ManagedSecondary> managed_secondary =
std::dynamic_pointer_cast<Primary::ManagedSecondary>(it->second);
EXPECT_TRUE(managed_secondary);

std::string public_key;
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/uptane/uptane_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ TEST(Uptane, UptaneSecondaryAddSameSerial) {
auto storage = INvStorage::newStorage(config.storage);
auto sota_client = UptaneTestCommon::newTestClient(config, storage, http);
UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware_new");
EXPECT_THROW(sota_client->addNewSecondary(std::make_shared<Uptane::VirtualSecondary>(
EXPECT_THROW(sota_client->addNewSecondary(std::make_shared<Primary::VirtualSecondary>(
Primary::VirtualSecondaryConfig::create_from_file(config.uptane.secondary_config_file))),
std::runtime_error);
}
Expand Down
19 changes: 11 additions & 8 deletions src/virtual_secondary/managedsecondary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

#include <sys/stat.h>

namespace Uptane {
namespace Primary {

ManagedSecondary::ManagedSecondary(Primary::ManagedSecondaryConfig sconfig_in) : sconfig(std::move(sconfig_in)) {
// TODO: FIX
// loadMetadata(meta_pack);
Expand Down Expand Up @@ -60,15 +61,16 @@ void ManagedSecondary::Initialize() {
void ManagedSecondary::rawToMeta() {
// raw meta is trusted
current_meta.director_root =
Uptane::Root(RepositoryType::Director(), Utils::parseJSON(current_raw_meta.director_root));
Uptane::Root(Uptane::RepositoryType::Director(), Utils::parseJSON(current_raw_meta.director_root));
current_meta.director_targets = Uptane::Targets(Utils::parseJSON(current_raw_meta.director_targets));
current_meta.image_root = Uptane::Root(RepositoryType::Image(), Utils::parseJSON(current_raw_meta.image_root));
current_meta.image_root =
Uptane::Root(Uptane::RepositoryType::Image(), Utils::parseJSON(current_raw_meta.image_root));
current_meta.image_targets = Uptane::Targets(Utils::parseJSON(current_raw_meta.image_targets));
current_meta.image_timestamp = Uptane::TimestampMeta(Utils::parseJSON(current_raw_meta.image_timestamp));
current_meta.image_snapshot = Uptane::Snapshot(Utils::parseJSON(current_raw_meta.image_snapshot));
}

bool ManagedSecondary::putMetadata(const RawMetaPack &meta_pack) {
bool ManagedSecondary::putMetadata(const Uptane::RawMetaPack &meta_pack) {
// No verification is currently performed, we can add verification in future for testing purposes
detected_attack = "";

Expand Down Expand Up @@ -117,8 +119,8 @@ int ManagedSecondary::getRootVersion(const bool director) {
bool ManagedSecondary::putRoot(const std::string &root, const bool director) {
Uptane::Root &prev_root = (director) ? current_meta.director_root : current_meta.image_root;
std::string &prev_raw_root = (director) ? current_raw_meta.director_root : current_raw_meta.image_root;
Uptane::Root new_root =
Uptane::Root((director) ? RepositoryType::Director() : RepositoryType::Image(), Utils::parseJSON(root));
Uptane::Root new_root = Uptane::Root(
(director) ? Uptane::RepositoryType::Director() : Uptane::RepositoryType::Image(), Utils::parseJSON(root));

// No verification is currently performed, we can add verification in future for testing purposes
if (new_root.version() == prev_root.version() + 1) {
Expand Down Expand Up @@ -151,7 +153,7 @@ bool ManagedSecondary::sendFirmware(const std::shared_ptr<std::string> &data) {
return false;
}

std::vector<Hash>::const_iterator it;
std::vector<Uptane::Hash>::const_iterator it;
for (it = expected_target_hashes.begin(); it != expected_target_hashes.end(); it++) {
if (it->TypeString() == "sha256") {
if (boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(*data))) !=
Expand Down Expand Up @@ -227,4 +229,5 @@ bool ManagedSecondary::loadKeys(std::string *pub_key, std::string *priv_key) {
*pub_key = Utils::readFile(public_key_path.string());
return true;
}
} // namespace Uptane

} // namespace Primary
33 changes: 15 additions & 18 deletions src/virtual_secondary/managedsecondary.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef UPTANE_MANAGEDSECONDARY_H_
#define UPTANE_MANAGEDSECONDARY_H_
#ifndef PRIMARY_MANAGEDSECONDARY_H_
#define PRIMARY_MANAGEDSECONDARY_H_

#include <future>
#include <string>
Expand Down Expand Up @@ -34,30 +34,26 @@ class ManagedSecondaryConfig : public SecondaryConfig {
KeyType key_type{KeyType::kRSA2048};
};

} // namespace Primary

namespace Uptane {

// Managed secondary is an abstraction over virtual and other types of legacy
// (non-UPTANE) secondaries. They require all the UPTANE-related functionality
// to be implemented in aktualizr itself, so there's some shared code.

class ManagedSecondary : public SecondaryInterface {
class ManagedSecondary : public Uptane::SecondaryInterface {
public:
explicit ManagedSecondary(Primary::ManagedSecondaryConfig sconfig_in);
~ManagedSecondary() override = default;

void Initialize();

EcuSerial getSerial() override {
Uptane::EcuSerial getSerial() override {
if (!sconfig.ecu_serial.empty()) {
return EcuSerial(sconfig.ecu_serial);
return Uptane::EcuSerial(sconfig.ecu_serial);
}
return EcuSerial(public_key_.KeyId());
return Uptane::EcuSerial(public_key_.KeyId());
}
Uptane::HardwareIdentifier getHwId() override { return Uptane::HardwareIdentifier(sconfig.ecu_hardware_id); }
PublicKey getPublicKey() override { return public_key_; }
bool putMetadata(const RawMetaPack& meta_pack) override;
bool putMetadata(const Uptane::RawMetaPack& meta_pack) override;
int getRootVersion(bool director) override;
bool putRoot(const std::string& root, bool director) override;

Expand All @@ -75,11 +71,11 @@ class ManagedSecondary : public SecondaryInterface {

std::string detected_attack;
std::string expected_target_name;
std::vector<Hash> expected_target_hashes;
std::vector<Uptane::Hash> expected_target_hashes;
uint64_t expected_target_length{};

MetaPack current_meta;
RawMetaPack current_raw_meta;
Uptane::MetaPack current_meta;
Uptane::RawMetaPack current_raw_meta;
std::mutex install_mutex;

virtual bool storeFirmware(const std::string& target_name, const std::string& content) = 0;
Expand All @@ -89,9 +85,10 @@ class ManagedSecondary : public SecondaryInterface {
void rawToMeta();

// TODO: implement
void storeMetadata(const RawMetaPack& meta_pack) { (void)meta_pack; }
bool loadMetadata(RawMetaPack* meta_pack);
void storeMetadata(const Uptane::RawMetaPack& meta_pack) { (void)meta_pack; }
bool loadMetadata(Uptane::RawMetaPack* meta_pack);
};
} // namespace Uptane

#endif // UPTANE_MANAGEDSECONDARY_H_
} // namespace Primary

#endif // PRIMARY_MANAGEDSECONDARY_H_
2 changes: 1 addition & 1 deletion src/virtual_secondary/virtual_secondary_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PartialVerificationSecondaryTest : public ::testing::Test {
};

/* Create a virtual secondary for testing. */
TEST_F(VirtualSecondaryTest, Instantiation) { EXPECT_NO_THROW(Uptane::VirtualSecondary virtual_sec(config_)); }
TEST_F(VirtualSecondaryTest, Instantiation) { EXPECT_NO_THROW(Primary::VirtualSecondary virtual_sec(config_)); }

/* Partial verification secondaries generate and store public keys. */
TEST_F(PartialVerificationSecondaryTest, Uptane_get_key) {
Expand Down
6 changes: 2 additions & 4 deletions src/virtual_secondary/virtualsecondary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ void VirtualSecondaryConfig::dump(const boost::filesystem::path& file_full_path)
json_file.close();
}

} // namespace Primary

namespace Uptane {
VirtualSecondary::VirtualSecondary(Primary::VirtualSecondaryConfig sconfig_in)
: ManagedSecondary(std::move(sconfig_in)) {}

Expand Down Expand Up @@ -87,4 +84,5 @@ bool VirtualSecondary::getFirmwareInfo(std::string* target_name, size_t& target_

return true;
}
} // namespace Uptane

} // namespace Primary
12 changes: 5 additions & 7 deletions src/virtual_secondary/virtualsecondary.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef UPTANE_VIRTUALSECONDARY_H_
#define UPTANE_VIRTUALSECONDARY_H_
#ifndef PRIMARY_VIRTUALSECONDARY_H_
#define PRIMARY_VIRTUALSECONDARY_H_

#include <string>

Expand All @@ -20,9 +20,6 @@ class VirtualSecondaryConfig : public ManagedSecondaryConfig {
static const char* const Type;
};

} // namespace Primary

namespace Uptane {
class VirtualSecondary : public ManagedSecondary {
public:
explicit VirtualSecondary(Primary::VirtualSecondaryConfig sconfig_in);
Expand All @@ -32,6 +29,7 @@ class VirtualSecondary : public ManagedSecondary {
bool storeFirmware(const std::string& target_name, const std::string& content) override;
bool getFirmwareInfo(std::string* target_name, size_t& target_len, std::string* sha256hash) override;
};
} // namespace Uptane

#endif // UPTANE_VIRTUALSECONDARY_H_
} // namespace Primary

#endif // PRIMARY_VIRTUALSECONDARY_H_
4 changes: 2 additions & 2 deletions tests/uptane_test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct UptaneTestCommon {
std::shared_ptr<HttpInterface> http)
: Aktualizr(config, storage, http) {
if (boost::filesystem::exists(config.uptane.secondary_config_file)) {
AddSecondary(std::make_shared<Uptane::VirtualSecondary>(Primary::VirtualSecondaryConfig::create_from_file(config.uptane.secondary_config_file)));
AddSecondary(std::make_shared<Primary::VirtualSecondary>(Primary::VirtualSecondaryConfig::create_from_file(config.uptane.secondary_config_file)));
}
}

Expand All @@ -56,7 +56,7 @@ struct UptaneTestCommon {
SotaUptaneClient(config_in, storage_in, http_client, bootloader_in, report_queue_in, events_channel_in) {

if (boost::filesystem::exists(config_in.uptane.secondary_config_file)) {
addSecondary(std::make_shared<Uptane::VirtualSecondary>(Primary::VirtualSecondaryConfig::create_from_file(config_in.uptane.secondary_config_file)));
addSecondary(std::make_shared<Primary::VirtualSecondary>(Primary::VirtualSecondaryConfig::create_from_file(config_in.uptane.secondary_config_file)));
}
}
};
Expand Down

0 comments on commit f55f554

Please sign in to comment.