This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Serhiy Stetskovych <patriotyk@gmail.com>
- Loading branch information
Showing
11 changed files
with
281 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "director_repo.h" | ||
|
||
void DirectorRepo::addTarget(const std::string &target_name, const Json::Value &target, const std::string &hardware_id, | ||
const std::string &ecu_serial) { | ||
const boost::filesystem::path current = path_ / "repo/director/targets.json"; | ||
const boost::filesystem::path staging = path_ / "repo/director/staging/targets.json"; | ||
|
||
Json::Value director_targets; | ||
if (boost::filesystem::exists(staging)) { | ||
director_targets = Utils::parseJSONFile(staging); | ||
} else if (boost::filesystem::exists(current)) { | ||
director_targets = Utils::parseJSONFile(current)["signed"]; | ||
} else { | ||
throw std::runtime_error(std::string("targets.json not found at ") + staging.c_str() + " or " + current.c_str() + | ||
"!"); | ||
} | ||
director_targets["targets"][target_name] = target; | ||
director_targets["targets"][target_name]["custom"]["ecuIdentifiers"][ecu_serial]["hardwareId"] = hardware_id; | ||
director_targets["version"] = (Utils::parseJSONFile(current)["signed"]["version"].asUInt()) + 1; | ||
Utils::writeFile(staging, Utils::jsonToCanonicalStr(director_targets)); | ||
} | ||
|
||
void DirectorRepo::signTargets() { | ||
const boost::filesystem::path current = path_ / "repo/director/targets.json"; | ||
const boost::filesystem::path staging = path_ / "repo/director/staging/targets.json"; | ||
Json::Value targets_unsigned; | ||
|
||
if (boost::filesystem::exists(staging)) { | ||
targets_unsigned = Utils::parseJSONFile(staging); | ||
} else if (boost::filesystem::exists(current)) { | ||
targets_unsigned = Utils::parseJSONFile(current)["signed"]; | ||
} else { | ||
throw std::runtime_error(std::string("targets.json not found at ") + staging.c_str() + " or " + current.c_str() + | ||
"!"); | ||
} | ||
|
||
Utils::writeFile(path_ / "repo/director/targets.json", | ||
Utils::jsonToCanonicalStr(signTuf(keys_[Uptane::Role::Targets()], targets_unsigned))); | ||
boost::filesystem::remove(path_ / "repo/director/staging/targets.json"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef DIRECTOR_REPO_H_ | ||
#define DIRECTOR_REPO_H_ | ||
|
||
#include "repo.h" | ||
|
||
class DirectorRepo : public Repo { | ||
public: | ||
DirectorRepo(boost::filesystem::path path, const std::string &expires, std::string correlation_id) | ||
: Repo(RepoType::Type::kDirector, std::move(path), expires, std::move(correlation_id)) {} | ||
void addTarget(const std::string &target_name, const Json::Value &target, const std::string &hardware_id, | ||
const std::string &ecu_serial); | ||
void signTargets(); | ||
}; | ||
|
||
#endif // DIRECTOR_REPO_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "image_repo.h" | ||
|
||
void ImageRepo::addImage(const boost::filesystem::path &image_path) { | ||
boost::filesystem::path repo_dir(path_ / "repo/image"); | ||
|
||
boost::filesystem::path targets_path = repo_dir / "targets"; | ||
boost::filesystem::create_directories(targets_path); | ||
if (image_path != targets_path / image_path.filename()) { | ||
boost::filesystem::copy_file(image_path, targets_path / image_path.filename(), | ||
boost::filesystem::copy_option::overwrite_if_exists); | ||
} | ||
std::string image = Utils::readFile(image_path); | ||
|
||
Json::Value targets = Utils::parseJSONFile(repo_dir / "targets.json")["signed"]; | ||
std::string target_name = image_path.filename().string(); | ||
targets["targets"][target_name]["length"] = Json::UInt64(image.size()); | ||
targets["targets"][target_name]["hashes"]["sha256"] = | ||
boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(image))); | ||
targets["targets"][target_name]["hashes"]["sha512"] = | ||
boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha512digest(image))); | ||
targets["version"] = (targets["version"].asUInt()) + 1; | ||
|
||
std::string signed_targets = Utils::jsonToCanonicalStr(signTuf(keys_[Uptane::Role::Targets()], targets)); | ||
Utils::writeFile(repo_dir / "targets.json", signed_targets); | ||
|
||
Json::Value snapshot = Utils::parseJSONFile(repo_dir / "snapshot.json")["signed"]; | ||
snapshot["version"] = (snapshot["version"].asUInt()) + 1; | ||
snapshot["meta"]["targets.json"]["hashes"]["sha256"] = | ||
boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(signed_targets))); | ||
snapshot["meta"]["targets.json"]["hashes"]["sha512"] = | ||
boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha512digest(signed_targets))); | ||
snapshot["meta"]["targets.json"]["length"] = static_cast<Json::UInt>(signed_targets.length()); | ||
snapshot["meta"]["targets.json"]["version"] = targets["version"].asUInt(); | ||
std::string signed_snapshot = Utils::jsonToCanonicalStr(signTuf(keys_[Uptane::Role::Snapshot()], snapshot)); | ||
Utils::writeFile(repo_dir / "snapshot.json", signed_snapshot); | ||
|
||
Json::Value timestamp = Utils::parseJSONFile(repo_dir / "timestamp.json")["signed"]; | ||
timestamp["version"] = (timestamp["version"].asUInt()) + 1; | ||
timestamp["meta"]["snapshot.json"]["hashes"]["sha256"] = | ||
boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(signed_snapshot))); | ||
timestamp["meta"]["snapshot.json"]["hashes"]["sha512"] = | ||
boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha512digest(signed_snapshot))); | ||
timestamp["meta"]["snapshot.json"]["length"] = static_cast<Json::UInt>(signed_snapshot.length()); | ||
timestamp["meta"]["snapshot.json"]["version"] = snapshot["version"].asUInt(); | ||
Utils::writeFile(repo_dir / "timestamp.json", | ||
Utils::jsonToCanonicalStr(signTuf(keys_[Uptane::Role::Timestamp()], timestamp))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef IMAGE_REPO_H_ | ||
#define IMAGE_REPO_H_ | ||
|
||
#include "repo.h" | ||
|
||
class ImageRepo : public Repo { | ||
public: | ||
ImageRepo(boost::filesystem::path path, const std::string &expires, std::string correlation_id) | ||
: Repo(RepoType::Type::kImage, std::move(path), expires, std::move(correlation_id)) {} | ||
void addImage(const boost::filesystem::path &image_path); | ||
}; | ||
|
||
#endif // IMAGE_REPO_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.