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