Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
update_agent_ostree.cc
1 #include "update_agent_ostree.h"
2 
3 #include "package_manager/ostreemanager.h"
4 
5 // TODO: consider moving this and SotaUptaneClient::secondaryTreehubCredentials() to encapsulate them in one place that
6 // is shared between IP Secondary's component
7 static void extractCredentialsArchive(const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
8  std::string* treehub_server);
9 
10 // TODO(OTA-4939): Unify this with the check in
11 // SotaUptaneClient::getNewTargets() and make it more generic.
12 bool OstreeUpdateAgent::isTargetSupported(const Uptane::Target& target) const { return target.IsOstree(); }
13 
14 bool OstreeUpdateAgent::getInstalledImageInfo(Uptane::InstalledImageInfo& installed_image_info) const {
15  bool result = false;
16  try {
17  installed_image_info.len = 0;
18  installed_image_info.hash = ostreePackMan_->getCurrentHash();
19 
20  // TODO(OTA-4545): consider more elegant way of storing currently installed target name
21  // usage of the SQLStorage and OSTree implementions aimed for Primary is
22  // a quite overhead for Secondary
23  auto currently_installed_target = ostreePackMan_->getCurrent();
24  if (!currently_installed_target.IsValid()) {
25  // This is the policy on a target image name in case of OSTree
26  // The policy in followed and implied in meta-updater (garage-sign/push) and the backend
27  // installed_image_info.name = _targetname_prefix + "-" + installed_image_info.hash;
28  installed_image_info.name = targetname_prefix_ + "-" + installed_image_info.hash;
29  } else {
30  installed_image_info.name = currently_installed_target.filename();
31  }
32 
33  result = true;
34  } catch (const std::exception& exc) {
35  LOG_ERROR << "Failed to get the currently installed revision: " << exc.what();
36  }
37  return result;
38 }
39 
40 bool OstreeUpdateAgent::download(const Uptane::Target& target, const std::string& data) {
41  std::string treehub_server;
42  bool download_result = false;
43 
44  try {
45  std::string ca;
46  std::string cert;
47  std::string pkey;
48  std::string server_url;
49  extractCredentialsArchive(data, &ca, &cert, &pkey, &server_url);
50  keyMngr_->loadKeys(&pkey, &cert, &ca);
51  boost::trim(server_url);
52  treehub_server = server_url;
53  } catch (std::runtime_error& exc) {
54  LOG_ERROR << exc.what();
55  return false;
56  }
57 
58  auto install_res = OstreeManager::pull(sysrootPath_, treehub_server, *keyMngr_, target);
59 
60  switch (install_res.result_code.num_code) {
61  case data::ResultCode::Numeric::kOk: {
62  LOG_INFO << "The target revision has been successfully downloaded: " << target.sha256Hash();
63  download_result = true;
64  break;
65  }
67  LOG_INFO << "The target revision is already present on the local OSTree repo: " << target.sha256Hash();
68  download_result = true;
69  break;
70  }
71  default: {
72  LOG_ERROR << "Failed to download the target revision: " << target.sha256Hash() << " ( "
73  << install_res.result_code.toString() << " ): " << install_res.description;
74  }
75  }
76 
77  return download_result;
78 }
79 
80 data::ResultCode::Numeric OstreeUpdateAgent::install(const Uptane::Target& target) {
81  return (ostreePackMan_->install(target)).result_code.num_code;
82 }
83 
84 void OstreeUpdateAgent::completeInstall() { ostreePackMan_->completeInstall(); }
85 
86 data::InstallationResult OstreeUpdateAgent::applyPendingInstall(const Uptane::Target& target) {
87  return ostreePackMan_->finalizeInstall(target);
88 }
89 
90 void extractCredentialsArchive(const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
91  std::string* treehub_server) {
92  {
93  std::stringstream as(archive);
94  *ca = Utils::readFileFromArchive(as, "ca.pem");
95  }
96  {
97  std::stringstream as(archive);
98  *cert = Utils::readFileFromArchive(as, "client.pem");
99  }
100  {
101  std::stringstream as(archive);
102  *pkey = Utils::readFileFromArchive(as, "pkey.pem");
103  }
104  {
105  std::stringstream as(archive);
106  *treehub_server = Utils::readFileFromArchive(as, "server.url", true);
107  }
108 }
General data structures.
Definition: types.cc:55
bool IsOstree() const
Is this an OSTree target? OSTree targets need special treatment because the hash doesn&#39;t represent th...
Definition: tuf.cc:171
Operation has already been processed.
Results of libaktualizr API calls.
Definition: results.h:13