Skip to content

Commit

Permalink
Merge pull request #127 from uptane/tidy/repository-type-upstream
Browse files Browse the repository at this point in the history
Tidy up RepositoryType
  • Loading branch information
cajun-rat authored Dec 9, 2024
2 parents 6e90a8d + 9a666d4 commit 753818b
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 146 deletions.
2 changes: 0 additions & 2 deletions src/aktualizr_secondary/aktualizr_secondary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ AktualizrSecondary::ReturnCode AktualizrSecondary::getRootVerHdlr(Asn1Message& i
repo_type = Uptane::RepositoryType::Image();
} else {
LOG_WARNING << "Received Root version request with invalid repo type: " << rv->repotype;
repo_type = Uptane::RepositoryType(-1);
}

int32_t root_version = -1;
Expand All @@ -350,7 +349,6 @@ AktualizrSecondary::ReturnCode AktualizrSecondary::putRootHdlr(Asn1Message& in_m
} else if (pr->repotype == AKRepoType_image) {
repo_type = Uptane::RepositoryType::Image();
} else {
repo_type = Uptane::RepositoryType(-1);
}

const std::string json = ToString(pr->json);
Expand Down
5 changes: 3 additions & 2 deletions src/libaktualizr/primary/sotauptaneclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "crypto/crypto.h"
#include "crypto/keymanager.h"
#include "libaktualizr/campaign.h"
#include "libaktualizr/types.h"
#include "logging/logging.h"
#include "provisioner.h"
#include "uptane/exceptions.h"
Expand Down Expand Up @@ -993,13 +994,13 @@ result::UpdateStatus SotaUptaneClient::checkUpdatesOffline(const std::vector<Upt
const auto it = std::find_if(director_targets.cbegin(), director_targets.cend(), target_comp);
if (it == director_targets.cend()) {
LOG_ERROR << "No matching target in Director Targets metadata for " << target;
throw Uptane::Exception(Uptane::RepositoryType::DIRECTOR, "No matching target in Director Targets metadata");
throw Uptane::Exception(Uptane::RepositoryType::Director(), "No matching target in Director Targets metadata");
}

const auto image_target = findTargetInDelegationTree(target, true);
if (image_target == nullptr) {
LOG_ERROR << "No matching target in Image repo Targets metadata for " << target;
throw Uptane::Exception(Uptane::RepositoryType::IMAGE, "No matching target in Director Targets metadata");
throw Uptane::Exception(Uptane::RepositoryType::Director(), "No matching target in Director Targets metadata");
}
}

Expand Down
62 changes: 24 additions & 38 deletions src/libaktualizr/storage/fsstorage_read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,18 @@ bool FSStorageRead::loadTlsPkey(std::string* pkey) const { return loadTlsCommon(

bool FSStorageRead::loadRoot(std::string* data, Uptane::RepositoryType repo, Uptane::Version version) const {
boost::filesystem::path metafile;
switch (repo) {
case (Uptane::RepositoryType::Director()):
if (version.version() < 0) {
version = latest_director_root;
}
metafile =
config_.uptane_metadata_path.get(config_.path) / "director" / version.RoleFileName(Uptane::Role::Root());
break;

case (Uptane::RepositoryType::Image()):
if (version.version() < 0) {
version = latest_director_root;
}
metafile = config_.uptane_metadata_path.get(config_.path) / "repo" / version.RoleFileName(Uptane::Role::Root());
break;

default:
return false;
if (repo == Uptane::RepositoryType::Director()) {
if (version.version() < 0) {
version = latest_director_root;
}
metafile = config_.uptane_metadata_path.get(config_.path) / "director" / version.RoleFileName(Uptane::Role::Root());
} else if (repo == Uptane::RepositoryType::Image()) {
if (version.version() < 0) {
version = latest_image_root;
}
metafile = config_.uptane_metadata_path.get(config_.path) / "repo" / version.RoleFileName(Uptane::Role::Root());
} else {
return false;
}

if (version.version() < 0) {
Expand All @@ -145,17 +139,12 @@ bool FSStorageRead::loadRoot(std::string* data, Uptane::RepositoryType repo, Upt

bool FSStorageRead::loadNonRoot(std::string* data, Uptane::RepositoryType repo, const Uptane::Role& role) const {
boost::filesystem::path metafile;
switch (repo) {
case (Uptane::RepositoryType::Director()):
metafile = config_.uptane_metadata_path.get(config_.path) / "director" / Uptane::Version().RoleFileName(role);
break;

case (Uptane::RepositoryType::Image()):
metafile = config_.uptane_metadata_path.get(config_.path) / "repo" / Uptane::Version().RoleFileName(role);
break;

default:
return false;
if (repo == Uptane::RepositoryType::Director()) {
metafile = config_.uptane_metadata_path.get(config_.path) / "director" / Uptane::Version().RoleFileName(role);
} else if (repo == Uptane::RepositoryType::Image()) {
metafile = config_.uptane_metadata_path.get(config_.path) / "repo" / Uptane::Version().RoleFileName(role);
} else {
return false;
}

if (!boost::filesystem::exists(metafile)) {
Expand Down Expand Up @@ -319,15 +308,12 @@ void FSStorageRead::clearTlsCreds() {

void FSStorageRead::clearNonRootMeta(Uptane::RepositoryType repo) {
boost::filesystem::path meta_path;
switch (repo) {
case Uptane::RepositoryType::Image():
meta_path = config_.uptane_metadata_path.get(config_.path) / "repo";
break;
case Uptane::RepositoryType::Director():
meta_path = config_.uptane_metadata_path.get(config_.path) / "director";
break;
default:
return;
if (repo == Uptane::RepositoryType::Image()) {
meta_path = config_.uptane_metadata_path.get(config_.path) / "repo";
} else if (repo == Uptane::RepositoryType::Director()) {
meta_path = config_.uptane_metadata_path.get(config_.path) / "director";
} else {
return;
}

boost::filesystem::directory_iterator it{meta_path};
Expand Down
4 changes: 2 additions & 2 deletions src/libaktualizr/storage/sqlstorage_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ TEST(sqlstorage, migrate_root_works) {
std::string raw_director_root;
storage.loadRoot(&raw_director_root, Uptane::RepositoryType::Director(), Uptane::Version());
Uptane::DirectorRepository director;
EXPECT_NO_THROW(director.initRoot(Uptane::RepositoryType(Uptane::RepositoryType::DIRECTOR), raw_director_root));
EXPECT_NO_THROW(director.initRoot(Uptane::RepositoryType::Director(), raw_director_root));

std::string raw_director_targets;
storage.loadNonRoot(&raw_director_targets, Uptane::RepositoryType::Director(), Uptane::Role::Targets());
Expand All @@ -460,7 +460,7 @@ TEST(sqlstorage, migrate_root_works) {
std::string raw_image_root;
storage.loadRoot(&raw_image_root, Uptane::RepositoryType::Image(), Uptane::Version());
Uptane::ImageRepository imagerepository;
EXPECT_NO_THROW(imagerepository.initRoot(Uptane::RepositoryType(Uptane::RepositoryType::IMAGE), raw_image_root));
EXPECT_NO_THROW(imagerepository.initRoot(Uptane::RepositoryType::Image(), raw_image_root));

// Check that the roots are different and haven't been swapped
EXPECT_NE(raw_director_root, raw_image_root);
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/uptane/director_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST(Director, EmptyTargets) {
uptane_gen.run({"generate", "--path", meta_dir.PathString(), "--correlationid", "cid1"});

DirectorRepository director;
EXPECT_NO_THROW(director.initRoot(Uptane::RepositoryType(Uptane::RepositoryType::DIRECTOR),
EXPECT_NO_THROW(director.initRoot(Uptane::RepositoryType::Director(),
Utils::readFile(meta_dir.Path() / "repo/director/root.json")));

EXPECT_NO_THROW(director.verifyTargets(Utils::readFile(meta_dir.Path() / "repo/director/targets.json")));
Expand Down
16 changes: 8 additions & 8 deletions src/libaktualizr/uptane/directorrepository.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ void DirectorRepository::resetMeta() {

void DirectorRepository::checkTargetsExpired() {
if (latest_targets.isExpired(TimeStamp::Now())) {
throw Uptane::ExpiredMetadata(type.ToString(), Role::TARGETS);
throw Uptane::ExpiredMetadata(type, Role::TARGETS);
}
}

void DirectorRepository::targetsSanityCheck() {
// 5.4.4.6.6. If checking Targets metadata from the Director repository,
// verify that there are no delegations.
if (!latest_targets.delegated_role_names_.empty()) {
throw Uptane::InvalidMetadata(type.ToString(), Role::TARGETS, "Found unexpected delegation.");
throw Uptane::InvalidMetadata(type, Role::TARGETS, "Found unexpected delegation.");
}
// 5.4.4.6.7. If checking Targets metadata from the Director repository,
// check that no ECU identifier is represented more than once.
Expand All @@ -35,7 +35,7 @@ void DirectorRepository::targetsSanityCheck() {
ecu_ids.insert(ecu.first);
} else {
LOG_ERROR << "ECU " << ecu.first << " appears twice in Director's Targets";
throw Uptane::InvalidMetadata(type.ToString(), Role::TARGETS, "Found repeated ECU ID.");
throw Uptane::InvalidMetadata(type, Role::TARGETS, "Found repeated ECU ID.");
}
}
}
Expand Down Expand Up @@ -68,13 +68,13 @@ void DirectorRepository::checkMetaOffline(INvStorage& storage) {
{
std::string director_root;
if (!storage.loadLatestRoot(&director_root, RepositoryType::Director())) {
throw Uptane::SecurityException(RepositoryType::DIRECTOR, "Could not load latest root");
throw Uptane::SecurityException(RepositoryType::Director(), "Could not load latest root");
}

initRoot(RepositoryType(RepositoryType::DIRECTOR), director_root);
initRoot(RepositoryType(RepositoryType::Director()), director_root);

if (rootExpired()) {
throw Uptane::ExpiredMetadata(RepositoryType::DIRECTOR, Role::ROOT);
throw Uptane::ExpiredMetadata(RepositoryType::Director(), Role::ROOT);
}
}

Expand All @@ -83,7 +83,7 @@ void DirectorRepository::checkMetaOffline(INvStorage& storage) {
std::string director_targets;

if (!storage.loadNonRoot(&director_targets, RepositoryType::Director(), Role::Targets())) {
throw Uptane::SecurityException(RepositoryType::DIRECTOR, "Could not load Targets role");
throw Uptane::SecurityException(RepositoryType::Director(), "Could not load Targets role");
}

verifyTargets(director_targets);
Expand Down Expand Up @@ -135,7 +135,7 @@ void DirectorRepository::updateMeta(INvStorage& storage, const IMetadataFetcher&
// that case, the member variable targets is updated, but it isn't stored in
// the database, which can cause some minor confusion.
if (local_version > remote_version) {
throw Uptane::SecurityException(RepositoryType::DIRECTOR, "Rollback attempt");
throw Uptane::SecurityException(RepositoryType::Director(), "Rollback attempt");
} else if (local_version < remote_version && !usePreviousTargets()) {
storage.storeNonRoot(director_targets, RepositoryType::Director(), Role::Targets());
}
Expand Down
58 changes: 30 additions & 28 deletions src/libaktualizr/uptane/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,40 @@
#include <stdexcept>
#include <string>
#include <utility>
#include "libaktualizr/types.h"
#include "uptane/tuf.h"

namespace Uptane {

class Exception : public std::logic_error {
public:
Exception(std::string reponame, const std::string& what_arg)
: std::logic_error(what_arg.c_str()), reponame_(std::move(reponame)) {}
virtual std::string getName() const { return reponame_; };
Exception(RepositoryType repo_type, const std::string& what_arg)
: std::logic_error(what_arg.c_str()), subject_{repo_type.ToString()} {}
Exception(std::string subject, const std::string& what_arg)
: std::logic_error(what_arg.c_str()), subject_{std::move(subject)} {}
[[nodiscard]] virtual std::string getName() const { return subject_; };

protected:
std::string reponame_;
std::string subject_;
};

class MetadataFetchFailure : public Exception {
public:
MetadataFetchFailure(const std::string& reponame, const std::string& role)
: Exception(reponame, std::string("Failed to fetch role ") + role + " in " + reponame + " repository.") {}
MetadataFetchFailure(RepositoryType repo_type, const std::string& role)
: Exception(repo_type,
std::string("Failed to fetch role ") + role + " in " + repo_type.ToString() + " repository.") {}
};

class SecurityException : public Exception {
public:
SecurityException(const std::string& reponame, const std::string& what_arg) : Exception(reponame, what_arg) {}
SecurityException(RepositoryType repo_type, const std::string& what_arg) : Exception(repo_type, what_arg) {}
};

class TargetContentMismatch : public Exception {
public:
explicit TargetContentMismatch(const std::string& targetname)
: Exception(targetname, "Director Target filename matches currently installed version, but content differs.") {}
: Exception(RepositoryType::Director(), "Director Target filename " + targetname +
" matches currently installed version, but content differs.") {}
};

class TargetHashMismatch : public Exception {
Expand All @@ -48,30 +54,26 @@ class OversizedTarget : public Exception {

class IllegalThreshold : public Exception {
public:
IllegalThreshold(const std::string& reponame, const std::string& what_arg) : Exception(reponame, what_arg) {}
};

class MissingRepo : public Exception {
public:
explicit MissingRepo(const std::string& reponame) : Exception(reponame, "The " + reponame + " repo is missing.") {}
IllegalThreshold(RepositoryType repo_type, const std::string& what_arg) : Exception(repo_type, what_arg) {}
};

class UnmetThreshold : public Exception {
public:
UnmetThreshold(const std::string& reponame, const std::string& role)
: Exception(reponame, "The " + role + " metadata had an unmet threshold.") {}
UnmetThreshold(RepositoryType repo_type, const std::string& role)
: Exception(repo_type, "The " + role + " metadata had an unmet threshold.") {}
};

class ExpiredMetadata : public Exception {
public:
ExpiredMetadata(const std::string& reponame, const std::string& role)
: Exception(reponame, "The " + role + " metadata was expired.") {}
ExpiredMetadata(RepositoryType repo_type, const std::string& role)
: Exception(repo_type, "The " + role + " metadata was expired.") {}
};

class InvalidMetadata : public Exception {
public:
InvalidMetadata(const std::string& reponame, const std::string& role, const std::string& reason)
: Exception(reponame, "The " + role + " metadata failed to parse: " + reason) {}
InvalidMetadata(RepositoryType repo_type, const std::string& role, const std::string& reason)
: Exception(repo_type, "The " + role + " metadata failed to parse: " + reason) {}
explicit InvalidMetadata(const std::string& reason) : Exception("", "The metadata failed to parse: " + reason) {}
};

class TargetMismatch : public Exception {
Expand All @@ -82,13 +84,13 @@ class TargetMismatch : public Exception {

class NonUniqueSignatures : public Exception {
public:
NonUniqueSignatures(const std::string& reponame, const std::string& role)
: Exception(reponame, "The role " + role + " had non-unique signatures.") {}
NonUniqueSignatures(RepositoryType repo_type, const std::string& role)
: Exception(repo_type, "The role " + role + " had non-unique signatures.") {}
};

class BadKeyId : public Exception {
public:
explicit BadKeyId(const std::string& reponame) : Exception(reponame, "A key has an incorrect associated key ID") {}
explicit BadKeyId(RepositoryType repo_type) : Exception(repo_type, "A key has an incorrect associated key ID") {}
};

class BadEcuId : public Exception {
Expand All @@ -105,14 +107,14 @@ class BadHardwareId : public Exception {

class RootRotationError : public Exception {
public:
explicit RootRotationError(const std::string& reponame)
: Exception(reponame, "Version in Root metadata does not match its expected value.") {}
explicit RootRotationError(RepositoryType repo_type)
: Exception(repo_type, "Version in Root metadata does not match its expected value.") {}
};

class VersionMismatch : public Exception {
public:
VersionMismatch(const std::string& reponame, const std::string& role)
: Exception(reponame, "The version of role " + role + " does not match the entry in Snapshot metadata.") {}
VersionMismatch(RepositoryType repo_type, const std::string& role)
: Exception(repo_type, "The version of role " + role + " does not match the entry in Snapshot metadata.") {}
};

class DelegationHashMismatch : public Exception {
Expand All @@ -136,7 +138,7 @@ class InvalidTarget : public Exception {

class LocallyAborted : public Exception {
public:
explicit LocallyAborted(const std::string& reponame) : Exception(reponame, "Update was aborted on the client") {}
explicit LocallyAborted(RepositoryType repo_type) : Exception(repo_type, "Update was aborted on the client") {}
};

} // namespace Uptane
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/uptane/fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void Fetcher::fetchRole(std::string* result, int64_t maxsize, RepositoryType rep
throw Uptane::LocallyAborted(repo);
}
if (!response.isOk()) {
throw Uptane::MetadataFetchFailure(repo.ToString(), role.ToString());
throw Uptane::MetadataFetchFailure(repo, role.ToString());
}
*result = response.body;
}
Expand Down
Loading

0 comments on commit 753818b

Please sign in to comment.