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  // TODO: why are qe loading this credentials at all ?
46  _keyMngr->loadKeys(&pkey, &cert, &ca);
47  boost::trim(server_url);
48  treehub_server = server_url;
49  } catch (std::runtime_error& exc) {
50  LOG_ERROR << exc.what();
51  return false;
52  }
53 
54  auto install_res = OstreeManager::pull(_sysrootPath, treehub_server, *_keyMngr, target);
55 
56  switch (install_res.result_code.num_code) {
57  case data::ResultCode::Numeric::kOk: {
58  LOG_INFO << "The target revision has been successfully downloaded: " << target.sha256Hash();
59  download_result = true;
60  break;
61  }
63  LOG_INFO << "The target revision is already present on the local ostree repo: " << target.sha256Hash();
64  download_result = true;
65  break;
66  }
67  default: {
68  LOG_ERROR << "Failed to download the target revision: " << target.sha256Hash() << " ( "
69  << install_res.result_code.toString() << " ): " << install_res.description;
70  }
71  }
72 
73  return download_result;
74 }
75 
76 data::ResultCode::Numeric OstreeUpdateAgent::install(const Uptane::Target& target) {
77  return (_ostreePackMan->install(target)).result_code.num_code;
78 }
79 
80 void OstreeUpdateAgent::completeInstall() { _ostreePackMan->completeInstall(); }
81 
82 data::InstallationResult OstreeUpdateAgent::applyPendingInstall(const Uptane::Target& target) {
83  return _ostreePackMan->finalizeInstall(target);
84 }
85 
86 void extractCredentialsArchive(const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
87  std::string* treehub_server) {
88  {
89  std::stringstream as(archive);
90  *ca = Utils::readFileFromArchive(as, "ca.pem");
91  }
92  {
93  std::stringstream as(archive);
94  *cert = Utils::readFileFromArchive(as, "client.pem");
95  }
96  {
97  std::stringstream as(archive);
98  *pkey = Utils::readFileFromArchive(as, "pkey.pem");
99  }
100  {
101  std::stringstream as(archive);
102  *treehub_server = Utils::readFileFromArchive(as, "server.url", true);
103  }
104 }
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:132
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:220
result
Results of libaktualizr API calls.
Definition: results.h:13
Uptane::Target
Definition: tuf.h:238
data::ResultCode::Numeric
Numeric
Definition: types.h:128