Aktualizr
C++ SOTA Client
secondary_provider.cc
1 #include "libaktualizr/secondary_provider.h"
2 #include "logging/logging.h"
3 #include "storage/invstorage.h"
4 #include "uptane/tuf.h"
5 
6 bool SecondaryProvider::getMetadata(Uptane::MetaBundle* meta_bundle, const Uptane::Target& target) const {
7  std::string root;
8  std::string timestamp;
9  std::string snapshot;
10  std::string targets;
11 
12  if (!getDirectorMetadata(&root, &targets)) {
13  return false;
14  }
15  meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Director(), Uptane::Role::Root()), root);
16  meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Director(), Uptane::Role::Targets()), targets);
17 
18  if (!getImageRepoMetadata(&root, &timestamp, &snapshot, &targets)) {
19  return false;
20  }
21  meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Root()), root);
22  meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Timestamp()), timestamp);
23  meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Snapshot()), snapshot);
24  meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Targets()), targets);
25 
26  // TODO: Support delegations for Secondaries. This is the purpose of providing
27  // the desired Target.
28  (void)target;
29 
30  return true;
31 }
32 
33 bool SecondaryProvider::getDirectorMetadata(std::string* root, std::string* targets) const {
34  if (!storage_->loadLatestRoot(root, Uptane::RepositoryType::Director())) {
35  LOG_ERROR << "No Director Root metadata to send";
36  return false;
37  }
38  if (!storage_->loadNonRoot(targets, Uptane::RepositoryType::Director(), Uptane::Role::Targets())) {
39  LOG_ERROR << "No Director Targets metadata to send";
40  return false;
41  }
42  return true;
43 }
44 
45 bool SecondaryProvider::getImageRepoMetadata(std::string* root, std::string* timestamp, std::string* snapshot,
46  std::string* targets) const {
47  if (!storage_->loadLatestRoot(root, Uptane::RepositoryType::Image())) {
48  LOG_ERROR << "No Image repo Root metadata to send";
49  return false;
50  }
51  if (!storage_->loadNonRoot(timestamp, Uptane::RepositoryType::Image(), Uptane::Role::Timestamp())) {
52  LOG_ERROR << "No Image repo Timestamp metadata to send";
53  return false;
54  }
55  if (!storage_->loadNonRoot(snapshot, Uptane::RepositoryType::Image(), Uptane::Role::Snapshot())) {
56  LOG_ERROR << "No Image repo Snapshot metadata to send";
57  return false;
58  }
59  if (!storage_->loadNonRoot(targets, Uptane::RepositoryType::Image(), Uptane::Role::Targets())) {
60  LOG_ERROR << "No Image repo Targets metadata to send";
61  return false;
62  }
63  return true;
64 }
65 
66 std::string SecondaryProvider::getTreehubCredentials() const {
67  if (config_.tls.pkey_source != CryptoSource::kFile || config_.tls.cert_source != CryptoSource::kFile ||
68  config_.tls.ca_source != CryptoSource::kFile) {
69  LOG_ERROR << "Cannot send OSTree update to a Secondary when not using file as credential sources";
70  return "";
71  }
72  std::string ca;
73  std::string cert;
74  std::string pkey;
75  if (!storage_->loadTlsCreds(&ca, &cert, &pkey)) {
76  LOG_ERROR << "Could not load TLS credentials from storage";
77  return "";
78  }
79 
80  const std::string treehub_url = config_.pacman.ostree_server;
81  std::map<std::string, std::string> archive_map = {
82  {"ca.pem", ca}, {"client.pem", cert}, {"pkey.pem", pkey}, {"server.url", treehub_url}};
83 
84  try {
85  std::stringstream as;
86  Utils::writeArchive(archive_map, as);
87 
88  return as.str();
89  } catch (std::runtime_error& exc) {
90  LOG_ERROR << "Could not create credentials archive: " << exc.what();
91  return "";
92  }
93 }
94 
95 std::ifstream SecondaryProvider::getTargetFileHandle(const Uptane::Target& target) const {
96  return package_manager_->openTargetFile(target);
97 }
Uptane::Target
Definition: types.h:379