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 SecondaryProvider::getTreehubCredentials to
6 // encapsulate them in one shared place if possible.
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 data::InstallationResult OstreeUpdateAgent::downloadTargetRev(const Uptane::Target& target,
41  const std::string& treehub_tls_creds) {
42  std::string treehub_server;
43 
44  try {
45  std::string ca;
46  std::string cert;
47  std::string pkey;
48  std::string server_url;
49  extractCredentialsArchive(treehub_tls_creds, &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();
56  std::string("Error loading Treehub credentials: ") + exc.what());
57  }
58 
59  auto result = OstreeManager::pull(sysrootPath_, treehub_server, *keyMngr_, target);
60 
61  switch (result.result_code.num_code) {
62  case data::ResultCode::Numeric::kOk: {
63  LOG_INFO << "The target commit has been successfully downloaded: " << target.sha256Hash();
64  break;
65  }
67  LOG_INFO << "The target commit is already present on the local OSTree repo: " << target.sha256Hash();
68  break;
69  }
70  default: {
71  LOG_ERROR << "Failed to download the target commit: " << target.sha256Hash() << " ( "
72  << result.result_code.toString() << " ): " << result.description;
73  }
74  }
75 
76  return result;
77 }
78 
79 data::InstallationResult OstreeUpdateAgent::install(const Uptane::Target& target) {
80  return ostreePackMan_->install(target);
81 }
82 
83 void OstreeUpdateAgent::completeInstall() { ostreePackMan_->completeInstall(); }
84 
85 data::InstallationResult OstreeUpdateAgent::applyPendingInstall(const Uptane::Target& target) {
86  return ostreePackMan_->finalizeInstall(target);
87 }
88 
89 void extractCredentialsArchive(const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
90  std::string* treehub_server) {
91  {
92  std::stringstream as(archive);
93  *ca = Utils::readFileFromArchive(as, "ca.pem");
94  }
95  {
96  std::stringstream as(archive);
97  *cert = Utils::readFileFromArchive(as, "client.pem");
98  }
99  {
100  std::stringstream as(archive);
101  *pkey = Utils::readFileFromArchive(as, "pkey.pem");
102  }
103  {
104  std::stringstream as(archive);
105  *treehub_server = Utils::readFileFromArchive(as, "server.url", true);
106  }
107 }
data::ResultCode::Numeric::kAlreadyProcessed
@ kAlreadyProcessed
Operation has already been processed.
data::InstallationResult
Definition: types.h:277
Uptane::InstalledImageInfo
Definition: types.h:306
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:179
result
Results of libaktualizr API calls.
Definition: results.h:12
Uptane::Target
Definition: types.h:379
data::ResultCode::Numeric::kDownloadFailed
@ kDownloadFailed
Package download failed.