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

Commit

Permalink
Merge RepoType with RepositoryTYpe
Browse files Browse the repository at this point in the history
Signed-off-by: Serhiy Stetskovych <patriotyk@gmail.com>
  • Loading branch information
patriotyk committed Dec 12, 2018
1 parent 14f3a9e commit c1e65d6
Show file tree
Hide file tree
Showing 28 changed files with 205 additions and 207 deletions.
8 changes: 4 additions & 4 deletions src/aktualizr_info/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ int main(int argc, char **argv) {
std::string director_targets;
std::string images_root;
std::string images_targets;
bool has_metadata = storage->loadLatestRoot(&director_root, Uptane::RepositoryType::Director);
storage->loadLatestRoot(&images_root, Uptane::RepositoryType::Images);
storage->loadNonRoot(&director_targets, Uptane::RepositoryType::Director, Uptane::Role::Targets());
storage->loadNonRoot(&images_targets, Uptane::RepositoryType::Images, Uptane::Role::Targets());
bool has_metadata = storage->loadLatestRoot(&director_root, Uptane::RepositoryType::Director());
storage->loadLatestRoot(&images_root, Uptane::RepositoryType::Image());
storage->loadNonRoot(&director_targets, Uptane::RepositoryType::Director(), Uptane::Role::Targets());
storage->loadNonRoot(&images_targets, Uptane::RepositoryType::Image(), Uptane::Role::Targets());

std::string device_id;
if (!storage->loadDeviceId(&device_id)) {
Expand Down
2 changes: 1 addition & 1 deletion src/aktualizr_repo/director_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class DirectorRepo : public Repo {
public:
DirectorRepo(boost::filesystem::path path, const std::string &expires, std::string correlation_id)
: Repo(RepoType::Type::kDirector, std::move(path), expires, std::move(correlation_id)) {}
: Repo(Uptane::RepositoryType::Director(), std::move(path), expires, std::move(correlation_id)) {}
void addTarget(const std::string &target_name, const Json::Value &target, const std::string &hardware_id,
const std::string &ecu_serial);
void signTargets();
Expand Down
2 changes: 1 addition & 1 deletion src/aktualizr_repo/image_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ImageRepo : public Repo {
public:
ImageRepo(boost::filesystem::path path, const std::string &expires, std::string correlation_id)
: Repo(RepoType::Type::kImage, std::move(path), expires, std::move(correlation_id)) {}
: Repo(Uptane::RepositoryType::Image(), std::move(path), expires, std::move(correlation_id)) {}
void addImage(const boost::filesystem::path &image_path);
};

Expand Down
4 changes: 2 additions & 2 deletions src/aktualizr_repo/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ int main(int argc, char **argv) {
std::istream_iterator<char> end;
std::string text_to_sign(it, end);

Repo base_repo(RepoType(vm["repotype"].as<std::string>()), vm["path"].as<boost::filesystem::path>(),
expiration_time, correlation_id);
Repo base_repo(Uptane::RepositoryType(vm["repotype"].as<std::string>()),
vm["path"].as<boost::filesystem::path>(), expiration_time, correlation_id);

auto json_to_sign = Utils::parseJSON(text_to_sign);
if (json_to_sign == Json::nullValue) {
Expand Down
7 changes: 4 additions & 3 deletions src/aktualizr_repo/repo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

#include "repo.h"

Repo::Repo(RepoType repo_type, boost::filesystem::path path, const std::string &expires, std::string correlation_id)
Repo::Repo(Uptane::RepositoryType repo_type, boost::filesystem::path path, const std::string &expires,
std::string correlation_id)
: repo_type_(repo_type), path_(std::move(path)), correlation_id_(std::move(correlation_id)) {
expiration_time_ = getExpirationTime(expires);
if (boost::filesystem::exists(path_)) {
Expand Down Expand Up @@ -127,7 +128,7 @@ void Repo::generateRepo(KeyType key_type) {
targets["version"] = 1;
targets["targets"] = Json::objectValue;
LOG_ERROR << "repo: " << repo_type_.toString();
if (repo_type_ == RepoType::Type::kDirector && correlation_id_ != "") {
if (repo_type_ == Uptane::RepositoryType::Director() && correlation_id_ != "") {
targets["custom"]["correlationId"] = correlation_id_;
}
std::string signed_targets = Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Targets(), targets));
Expand Down Expand Up @@ -164,7 +165,7 @@ void Repo::generateRepo(KeyType key_type) {
timestamp["meta"]["snapshot.json"]["version"] = 1;
Utils::writeFile(repo_dir / "timestamp.json",
Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Snapshot(), timestamp)));
if (repo_type_ == RepoType::Type::kDirector) {
if (repo_type_ == Uptane::RepositoryType::Director()) {
Utils::writeFile(path_ / "repo/director/manifest", std::string()); // just empty file to work with put method
}
}
Expand Down
29 changes: 3 additions & 26 deletions src/aktualizr_repo/repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,10 @@ struct KeyPair {
std::string private_key;
};

class RepoType {
public:
enum class Type { kDirector = 0, kImage };
RepoType(RepoType::Type type) { type_ = type; }
RepoType(const std::string &repo_type) {
if (repo_type == "director") {
type_ = RepoType::Type::kDirector;
} else if (repo_type == "image") {
type_ = RepoType::Type::kImage;
} else {
throw std::runtime_error(std::string("incorrect repo type: ") + repo_type);
}
}
Type type_;
bool operator==(const RepoType &other) { return type_ == other.type_; }
std::string toString() {
if (type_ == RepoType::Type::kDirector) {
return "director";
} else {
return "image";
}
}
};

class Repo {
public:
Repo(RepoType repo_type, boost::filesystem::path path, const std::string &expires, std::string correlation_id);
Repo(Uptane::RepositoryType repo_type, boost::filesystem::path path, const std::string &expires,
std::string correlation_id);
void generateRepo(KeyType key_type = KeyType::kRSA2048);
Json::Value getTarget(const std::string &target_name);
Json::Value signTuf(const Uptane::Role &role, const Json::Value &json);
Expand All @@ -50,7 +27,7 @@ class Repo {
void generateKeyPair(KeyType key_type, const Uptane::Role &key_name);
std::string getExpirationTime(const std::string &expires);
void readKeys();
RepoType repo_type_;
Uptane::RepositoryType repo_type_;
boost::filesystem::path path_;
std::string correlation_id_;
std::string expiration_time_;
Expand Down
10 changes: 5 additions & 5 deletions src/aktualizr_secondary/aktualizr_secondary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ bool AktualizrSecondary::putMetadataResp(const Uptane::RawMetaPack& meta_pack) {
detected_attack_.clear();

// TODO: proper partial verification
root_ = Uptane::Root(Uptane::RepositoryType::Director, Utils::parseJSON(meta_pack.director_root), root_);
Uptane::Targets targets(Uptane::RepositoryType::Director, Utils::parseJSON(meta_pack.director_targets), root_);
root_ = Uptane::Root(Uptane::RepositoryType::Director(), Utils::parseJSON(meta_pack.director_root), root_);
Uptane::Targets targets(Uptane::RepositoryType::Director(), Utils::parseJSON(meta_pack.director_targets), root_);
if (meta_targets_.version() > targets.version()) {
detected_attack_ = "Rollback attack detected";
return true;
Expand All @@ -88,16 +88,16 @@ bool AktualizrSecondary::putMetadataResp(const Uptane::RawMetaPack& meta_pack) {
target_ = std_::make_unique<Uptane::Target>(*it);
}
}
storage_->storeRoot(meta_pack.director_root, Uptane::RepositoryType::Director, Uptane::Version(root_.version()));
storage_->storeNonRoot(meta_pack.director_targets, Uptane::RepositoryType::Director, Uptane::Role::Targets());
storage_->storeRoot(meta_pack.director_root, Uptane::RepositoryType::Director(), Uptane::Version(root_.version()));
storage_->storeNonRoot(meta_pack.director_targets, Uptane::RepositoryType::Director(), Uptane::Role::Targets());

return true;
}

int32_t AktualizrSecondary::getRootVersionResp(bool director) const {
std::string root_meta;
if (!storage_->loadLatestRoot(&root_meta,
(director) ? Uptane::RepositoryType::Director : Uptane::RepositoryType::Images)) {
(director) ? Uptane::RepositoryType::Director() : Uptane::RepositoryType::Image())) {
LOG_ERROR << "Could not load root metadata";
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/aktualizr_secondary/aktualizr_secondary_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ AktualizrSecondaryCommon::AktualizrSecondaryCommon(const AktualizrSecondaryConfi

// Load Root keys from storage
std::string root;
storage_->loadLatestRoot(&root, Uptane::RepositoryType::Director);
storage_->loadLatestRoot(&root, Uptane::RepositoryType::Director());
if (root.size() > 0) {
LOG_DEBUG << "Loading root.json:" << root;
root_ = Uptane::Root(Uptane::RepositoryType::Director, Utils::parseJSON(root));
root_ = Uptane::Root(Uptane::RepositoryType::Director(), Utils::parseJSON(root));
} else {
LOG_INFO << "No root.json in local storage, defaulting will accept the first root.json provided";
root_ = Uptane::Root(Uptane::Root::Policy::kAcceptAll);
Expand Down
14 changes: 7 additions & 7 deletions src/aktualizr_secondary/opcuaserver_secondary_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ void OpcuaServerSecondaryDelegate::handleAllMetaDataFilesReceived(opcuabridge::S
secondary_->detected_attack_.clear();

std::string root_str;
secondary_->storage_->loadLatestRoot(&root_str, Uptane::RepositoryType::Director);
secondary_->root_ = Uptane::Root(Uptane::RepositoryType::Director, Utils::parseJSON(root_str));
secondary_->storage_->loadLatestRoot(&root_str, Uptane::RepositoryType::Director());
secondary_->root_ = Uptane::Root(Uptane::RepositoryType::Director(), Utils::parseJSON(root_str));

std::string targets_str;
secondary_->storage_->loadNonRoot(&targets_str, Uptane::RepositoryType::Director, Uptane::Role::Targets());
secondary_->storage_->loadNonRoot(&targets_str, Uptane::RepositoryType::Director(), Uptane::Role::Targets());
secondary_->meta_targets_ = Uptane::Targets(Utils::parseJSON(targets_str));

try {
// TODO: proper root metadata rotation
secondary_->root_ = Uptane::Root(Uptane::RepositoryType::Director,
secondary_->root_ = Uptane::Root(Uptane::RepositoryType::Director(),
Utils::parseJSON(received_meta_pack_.director_root), secondary_->root_);
Uptane::Targets targets(Uptane::RepositoryType::Director, Utils::parseJSON(received_meta_pack_.director_targets),
Uptane::Targets targets(Uptane::RepositoryType::Director(), Utils::parseJSON(received_meta_pack_.director_targets),
secondary_->root_);
if (secondary_->meta_targets_.version() > targets.version()) {
secondary_->detected_attack_ = "Rollback attack detected";
Expand All @@ -99,9 +99,9 @@ void OpcuaServerSecondaryDelegate::handleAllMetaDataFilesReceived(opcuabridge::S
LOG_ERROR << "Uptane security check: " << ex.what();
return;
}
secondary_->storage_->storeRoot(received_meta_pack_.director_root, Uptane::RepositoryType::Director,
secondary_->storage_->storeRoot(received_meta_pack_.director_root, Uptane::RepositoryType::Director(),
Uptane::Version(secondary_->root_.version()));
secondary_->storage_->storeNonRoot(received_meta_pack_.director_targets, Uptane::RepositoryType::Director,
secondary_->storage_->storeNonRoot(received_meta_pack_.director_targets, Uptane::RepositoryType::Director(),
Uptane::Role::Targets());
}

Expand Down
12 changes: 6 additions & 6 deletions src/aktualizr_secondary/update_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ TEST(aktualizr_secondary_protocol, DISABLED_manual_update) {
auto storage2 = INvStorage::newStorage(storage2_config);

Uptane::RawMetaPack metadata;
EXPECT_TRUE(storage2->loadLatestRoot(&metadata.director_root, Uptane::RepositoryType::Director));
EXPECT_TRUE(storage2->loadLatestRoot(&metadata.director_root, Uptane::RepositoryType::Director()));
EXPECT_TRUE(
storage2->loadNonRoot(&metadata.director_targets, Uptane::RepositoryType::Director, Uptane::Role::Targets()));
EXPECT_TRUE(storage2->loadLatestRoot(&metadata.image_root, Uptane::RepositoryType::Images));
EXPECT_TRUE(storage2->loadNonRoot(&metadata.image_targets, Uptane::RepositoryType::Images, Uptane::Role::Targets()));
storage2->loadNonRoot(&metadata.director_targets, Uptane::RepositoryType::Director(), Uptane::Role::Targets()));
EXPECT_TRUE(storage2->loadLatestRoot(&metadata.image_root, Uptane::RepositoryType::Image()));
EXPECT_TRUE(storage2->loadNonRoot(&metadata.image_targets, Uptane::RepositoryType::Image(), Uptane::Role::Targets()));
EXPECT_TRUE(
storage2->loadNonRoot(&metadata.image_timestamp, Uptane::RepositoryType::Images, Uptane::Role::Timestamp()));
storage2->loadNonRoot(&metadata.image_timestamp, Uptane::RepositoryType::Image(), Uptane::Role::Timestamp()));
EXPECT_TRUE(
storage2->loadNonRoot(&metadata.image_snapshot, Uptane::RepositoryType::Images, Uptane::Role::Snapshot()));
storage2->loadNonRoot(&metadata.image_snapshot, Uptane::RepositoryType::Image(), Uptane::Role::Snapshot()));

std::string firmware = Utils::readFile(temp_dir.Path() / "firmware.bin");

Expand Down
Loading

0 comments on commit c1e65d6

Please sign in to comment.