Aktualizr
C++ SOTA Client
director_repo.cc
1 #include "director_repo.h"
2 
3 void DirectorRepo::addTarget(const std::string &target_name, const Json::Value &target, const std::string &hardware_id,
4  const std::string &ecu_serial, const std::string &url, const std::string &expires) {
5  const boost::filesystem::path current = path_ / DirectorRepo::dir / "targets.json";
6  const boost::filesystem::path staging = path_ / DirectorRepo::dir / "staging/targets.json";
7 
8  Json::Value director_targets;
9  if (boost::filesystem::exists(staging)) {
10  director_targets = Utils::parseJSONFile(staging);
11  } else if (boost::filesystem::exists(current)) {
12  director_targets = Utils::parseJSONFile(current)["signed"];
13  } else {
14  throw std::runtime_error(std::string("targets.json not found at ") + staging.c_str() + " or " + current.c_str() +
15  "!");
16  }
17  if (!expires.empty()) {
18  director_targets["expires"] = expires;
19  }
20  director_targets["targets"][target_name] = target;
21  director_targets["targets"][target_name]["custom"].removeMember("hardwareIds");
22  director_targets["targets"][target_name]["custom"]["ecuIdentifiers"][ecu_serial]["hardwareId"] = hardware_id;
23  if (!url.empty()) {
24  director_targets["targets"][target_name]["custom"]["uri"] = url;
25  } else {
26  director_targets["targets"][target_name]["custom"].removeMember("uri");
27  }
28  director_targets["version"] = (Utils::parseJSONFile(current)["signed"]["version"].asUInt()) + 1;
29  Utils::writeFile(staging, Utils::jsonToCanonicalStr(director_targets));
30  updateRepo();
31 }
32 
33 void DirectorRepo::revokeTargets(const std::vector<std::string> &targets_to_remove) {
34  auto targets_path = path_ / DirectorRepo::dir / "targets.json";
35  auto targets_unsigned = Utils::parseJSONFile(targets_path)["signed"];
36 
37  Json::Value new_targets;
38  for (auto it = targets_unsigned["targets"].begin(); it != targets_unsigned["targets"].end(); ++it) {
39  if (std::find(targets_to_remove.begin(), targets_to_remove.end(), it.key().asString()) == targets_to_remove.end()) {
40  new_targets[it.key().asString()] = *it;
41  }
42  }
43  targets_unsigned["targets"] = new_targets;
44  targets_unsigned["version"] = (targets_unsigned["version"].asUInt()) + 1;
45  Utils::writeFile(path_ / DirectorRepo::dir / "targets.json",
46  Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Targets(), targets_unsigned)));
47  updateRepo();
48 }
49 
50 void DirectorRepo::signTargets() {
51  const boost::filesystem::path current = path_ / DirectorRepo::dir / "targets.json";
52  const boost::filesystem::path staging = path_ / DirectorRepo::dir / "staging/targets.json";
53  Json::Value targets_unsigned;
54 
55  if (boost::filesystem::exists(staging)) {
56  targets_unsigned = Utils::parseJSONFile(staging);
57  } else if (boost::filesystem::exists(current)) {
58  targets_unsigned = Utils::parseJSONFile(current)["signed"];
59  } else {
60  throw std::runtime_error(std::string("targets.json not found at ") + staging.c_str() + " or " + current.c_str() +
61  "!");
62  }
63 
64  Utils::writeFile(path_ / DirectorRepo::dir / "targets.json",
65  Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Targets(), targets_unsigned)));
66  boost::filesystem::remove(path_ / DirectorRepo::dir / "staging/targets.json");
67  updateRepo();
68 }
69 
70 void DirectorRepo::emptyTargets() {
71  const boost::filesystem::path current = path_ / DirectorRepo::dir / "targets.json";
72  const boost::filesystem::path staging = path_ / DirectorRepo::dir / "staging/targets.json";
73 
74  Json::Value targets_current = Utils::parseJSONFile(current);
75  Json::Value targets_unsigned;
76  targets_unsigned = Utils::parseJSONFile(staging);
77  targets_unsigned["_type"] = "Targets";
78  targets_unsigned["expires"] = expiration_time_;
79  targets_unsigned["version"] = (targets_current["signed"]["version"].asUInt()) + 1;
80  targets_unsigned["targets"] = Json::objectValue;
81  if (repo_type_ == Uptane::RepositoryType::Director() && !correlation_id_.empty()) {
82  targets_unsigned["custom"]["correlationId"] = correlation_id_;
83  }
84  Utils::writeFile(staging, Utils::jsonToCanonicalStr(targets_unsigned));
85 }
86 
87 void DirectorRepo::oldTargets() {
88  const boost::filesystem::path current = path_ / DirectorRepo::dir / "targets.json";
89  const boost::filesystem::path staging = path_ / DirectorRepo::dir / "staging/targets.json";
90 
91  if (!boost::filesystem::exists(current)) {
92  throw std::runtime_error(std::string("targets.json not found at ") + current.c_str() + "!");
93  }
94  Json::Value targets_current = Utils::parseJSONFile(current);
95  Json::Value targets_unsigned = targets_current["signed"];
96  Utils::writeFile(staging, Utils::jsonToCanonicalStr(targets_unsigned));
97 }