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