Aktualizr
C++ SOTA Client
aktualizr_secondary_metadata.cc
1 #include "aktualizr_secondary_metadata.h"
2 
3 Metadata::Metadata(Uptane::MetaBundle meta_bundle_in) : meta_bundle_(std::move(meta_bundle_in)) {
4  director_root_version_ = Uptane::Version(Uptane::extractVersionUntrusted(
5  Uptane::getMetaFromBundle(meta_bundle_, Uptane::RepositoryType::Director(), Uptane::Role::Root())));
6  image_root_version_ = Uptane::Version(Uptane::extractVersionUntrusted(
7  Uptane::getMetaFromBundle(meta_bundle_, Uptane::RepositoryType::Image(), Uptane::Role::Root())));
8 }
9 
10 void Metadata::fetchRole(std::string* result, int64_t maxsize, Uptane::RepositoryType repo, const Uptane::Role& role,
11  Uptane::Version version) const {
12  (void)maxsize;
13 
14  getRoleMetadata(result, repo, role, version);
15 }
16 
17 void Metadata::fetchLatestRole(std::string* result, int64_t maxsize, Uptane::RepositoryType repo,
18  const Uptane::Role& role) const {
19  (void)maxsize;
20  getRoleMetadata(result, repo, role, Uptane::Version());
21 }
22 
23 void Metadata::getRoleMetadata(std::string* result, const Uptane::RepositoryType& repo, const Uptane::Role& role,
24  Uptane::Version version) const {
25  if (role == Uptane::Role::Root() && version != Uptane::Version()) {
26  // If requesting a Root version beyond what we have available, fail as
27  // expected. If requesting a version before what is available, just use what
28  // is available, since root rotation isn't supported here.
29  if (repo == Uptane::RepositoryType::Director() && director_root_version_ < version) {
30  LOG_DEBUG << "Requested Director root version " << version << " but only version " << director_root_version_
31  << " is available.";
32  throw std::runtime_error("Metadata not found");
33  } else if (repo == Uptane::RepositoryType::Image() && image_root_version_ < version) {
34  LOG_DEBUG << "Requested Image repo root version " << version << " but only version " << image_root_version_
35  << " is available.";
36  throw std::runtime_error("Metadata not found");
37  }
38  }
39 
40  *result = Uptane::getMetaFromBundle(meta_bundle_, repo, role);
41 }
Uptane::Version
Metadata version numbers.
Definition: tuf.h:120
Uptane::RepositoryType
Definition: tuf.h:21
result
Results of libaktualizr API calls.
Definition: results.h:12
Uptane::Role
TUF Roles.
Definition: tuf.h:61