1 #include "director_repo.h"
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) {
5 const boost::filesystem::path current = path_ / DirectorRepo::dir /
"targets.json";
6 const boost::filesystem::path staging = path_ / DirectorRepo::dir /
"staging/targets.json";
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"];
14 throw std::runtime_error(std::string(
"targets.json not found at ") + staging.c_str() +
" or " + current.c_str() +
17 director_targets[
"targets"][target_name] = target;
18 director_targets[
"targets"][target_name][
"custom"].removeMember(
"hardwareIds");
19 director_targets[
"targets"][target_name][
"custom"][
"ecuIdentifiers"][ecu_serial][
"hardwareId"] = hardware_id;
21 director_targets[
"targets"][target_name][
"custom"][
"uri"] = url;
23 director_targets[
"targets"][target_name][
"custom"].removeMember(
"uri");
25 director_targets[
"version"] = (Utils::parseJSONFile(current)[
"signed"][
"version"].asUInt()) + 1;
26 Utils::writeFile(staging, Utils::jsonToCanonicalStr(director_targets));
30 void DirectorRepo::revokeTargets(
const std::vector<std::string> &targets_to_remove) {
31 auto targets_path = path_ / DirectorRepo::dir /
"targets.json";
32 auto targets_unsigned = Utils::parseJSONFile(targets_path)[
"signed"];
34 Json::Value new_targets;
35 for (
auto it = targets_unsigned[
"targets"].begin(); it != targets_unsigned[
"targets"].end(); ++it) {
36 if (std::find(targets_to_remove.begin(), targets_to_remove.end(), it.key().asString()) == targets_to_remove.end()) {
37 new_targets[it.key().asString()] = *it;
40 targets_unsigned[
"targets"] = new_targets;
41 targets_unsigned[
"version"] = (targets_unsigned[
"version"].asUInt()) + 1;
42 Utils::writeFile(path_ / DirectorRepo::dir /
"targets.json",
43 Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Targets(), targets_unsigned)));
47 void DirectorRepo::signTargets() {
48 const boost::filesystem::path current = path_ / DirectorRepo::dir /
"targets.json";
49 const boost::filesystem::path staging = path_ / DirectorRepo::dir /
"staging/targets.json";
50 Json::Value targets_unsigned;
52 if (boost::filesystem::exists(staging)) {
53 targets_unsigned = Utils::parseJSONFile(staging);
54 }
else if (boost::filesystem::exists(current)) {
55 targets_unsigned = Utils::parseJSONFile(current)[
"signed"];
57 throw std::runtime_error(std::string(
"targets.json not found at ") + staging.c_str() +
" or " + current.c_str() +
61 Utils::writeFile(path_ / DirectorRepo::dir /
"targets.json",
62 Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Targets(), targets_unsigned)));
63 boost::filesystem::remove(path_ / DirectorRepo::dir /
"staging/targets.json");
67 void DirectorRepo::emptyTargets() {
68 const boost::filesystem::path current = path_ / DirectorRepo::dir /
"targets.json";
69 const boost::filesystem::path staging = path_ / DirectorRepo::dir /
"staging/targets.json";
71 Json::Value targets_current = Utils::parseJSONFile(current);
72 Json::Value targets_unsigned;
73 targets_unsigned = Utils::parseJSONFile(staging);
74 targets_unsigned[
"_type"] =
"Targets";
75 targets_unsigned[
"expires"] = expiration_time_;
76 targets_unsigned[
"version"] = (targets_current[
"signed"][
"version"].asUInt()) + 1;
77 targets_unsigned[
"targets"] = Json::objectValue;
78 if (repo_type_ == Uptane::RepositoryType::Director() && correlation_id_ !=
"") {
79 targets_unsigned[
"custom"][
"correlationId"] = correlation_id_;
81 Utils::writeFile(staging, Utils::jsonToCanonicalStr(targets_unsigned));
84 void DirectorRepo::oldTargets() {
85 const boost::filesystem::path current = path_ / DirectorRepo::dir /
"targets.json";
86 const boost::filesystem::path staging = path_ / DirectorRepo::dir /
"staging/targets.json";
88 if (!boost::filesystem::exists(current)) {
89 throw std::runtime_error(std::string(
"targets.json not found at ") + current.c_str() +
"!");
91 Json::Value targets_current = Utils::parseJSONFile(current);
92 Json::Value targets_unsigned = targets_current[
"signed"];
93 Utils::writeFile(staging, Utils::jsonToCanonicalStr(targets_unsigned));