1 #include "directorrepository.h"
5 void DirectorRepository::resetMeta() {
8 latest_targets = Targets();
11 void DirectorRepository::checkTargetsExpired() {
12 if (latest_targets.isExpired(TimeStamp::Now())) {
17 void DirectorRepository::targetsSanityCheck() {
20 if (!latest_targets.delegated_role_names_.empty()) {
25 std::set<Uptane::EcuSerial> ecu_ids;
26 for (
const auto& target : targets.targets) {
27 for (
const auto& ecu : target.ecus()) {
28 if (ecu_ids.find(ecu.first) == ecu_ids.end()) {
29 ecu_ids.insert(ecu.first);
31 LOG_ERROR <<
"ECU " << ecu.first <<
" appears twice in Director's Targets";
38 bool DirectorRepository::usePreviousTargets()
const {
41 return !targets.targets.empty() && latest_targets.targets.empty();
44 void DirectorRepository::verifyTargets(
const std::string& targets_raw) {
47 latest_targets = Targets(RepositoryType::Director(), Role::Targets(), Utils::parseJSON(targets_raw),
48 std::make_shared<MetaWithKeys>(root));
49 if (!usePreviousTargets()) {
50 targets = latest_targets;
53 LOG_ERROR <<
"Signature verification for Director Targets metadata failed";
58 void DirectorRepository::checkMetaOffline(
INvStorage& storage) {
62 std::string director_root;
63 if (!storage.loadLatestRoot(&director_root, RepositoryType::Director())) {
67 initRoot(RepositoryType(RepositoryType::DIRECTOR), director_root);
76 std::string director_targets;
78 if (!storage.loadNonRoot(&director_targets, RepositoryType::Director(), Role::Targets())) {
82 verifyTargets(director_targets);
84 checkTargetsExpired();
90 void DirectorRepository::updateMeta(
INvStorage& storage,
const IMetadataFetcher& fetcher) {
97 updateRoot(storage, fetcher, RepositoryType::Director());
105 std::string director_targets;
107 fetcher.fetchLatestRole(&director_targets, kMaxDirectorTargetsSize, RepositoryType::Director(), Role::Targets());
108 int remote_version = extractVersionUntrusted(director_targets);
111 std::string director_targets_stored;
112 if (storage.loadNonRoot(&director_targets_stored, RepositoryType::Director(), Role::Targets())) {
113 local_version = extractVersionUntrusted(director_targets_stored);
115 verifyTargets(director_targets_stored);
116 }
catch (
const std::exception& e) {
117 LOG_WARNING <<
"Unable to verify stored Director Targets metadata.";
123 verifyTargets(director_targets);
128 if (local_version > remote_version) {
130 }
else if (local_version < remote_version && !usePreviousTargets()) {
131 storage.storeNonRoot(director_targets, RepositoryType::Director(), Role::Targets());
134 checkTargetsExpired();
136 targetsSanityCheck();
140 void DirectorRepository::dropTargets(
INvStorage& storage) {
142 storage.clearNonRootMeta(RepositoryType::Director());
145 LOG_ERROR <<
"Failed to reset Director Targets metadata: " << ex.what();
149 bool DirectorRepository::matchTargetsWithImageTargets(
const Uptane::Targets& image_targets)
const {
156 const auto& image_target_array = image_targets.targets;
157 const auto& director_target_array = targets.targets;
159 for (
const auto& director_target : director_target_array) {
160 auto found_it = std::find_if(
161 image_target_array.begin(), image_target_array.end(),
162 [&director_target](
const Target& image_target) { return director_target.MatchTarget(image_target); });
164 if (found_it == image_target_array.end()) {