1 #include "aktualizr_secondary.h"
3 #include "crypto/keymanager.h"
4 #include "logging/logging.h"
5 #include "update_agent.h"
6 #include "uptane/manifest.h"
7 #include "utilities/utils.h"
13 std::shared_ptr<KeyManager> key_mngr, std::shared_ptr<UpdateAgent> update_agent)
14 : config_(std::move(config)),
15 storage_(std::move(storage)),
16 keys_(std::move(key_mngr)),
17 update_agent_(std::move(update_agent)) {
18 if (!uptaneInitialize()) {
19 LOG_ERROR <<
"Failed to initialize";
23 manifest_issuer_ = std::make_shared<Uptane::ManifestIssuer>(keys_, ecu_serial_);
25 if (hasPendingUpdate()) {
28 std::vector<Uptane::Target> installed_versions;
29 boost::optional<Uptane::Target> pending_target;
30 storage_->loadInstalledVersions(ecu_serial_.ToString(),
nullptr, &pending_target);
33 LOG_INFO <<
"Pending update found; attempting to apply it. Target hash: " << pending_target_.sha256Hash();
35 if (!!pending_target) {
36 install_res = update_agent_->applyPendingInstall(*pending_target);
38 if (install_res.result_code != data::ResultCode::Numeric::kNeedCompletion) {
39 storage_->saveEcuInstallationResult(ecu_serial_, install_res);
40 if (install_res.success) {
41 LOG_INFO <<
"Pending update has been successfully applied: " << pending_target_.sha256Hash();
42 storage_->saveInstalledVersion(ecu_serial_.ToString(), *pending_target, InstalledVersionUpdateMode::kCurrent);
44 LOG_ERROR <<
"Application of the pending update has failed: (" << install_res.result_code.toString() <<
")"
45 << install_res.description;
46 storage_->saveInstalledVersion(ecu_serial_.ToString(), *pending_target, InstalledVersionUpdateMode::kNone);
47 director_repo_.dropTargets(*storage_);
50 LOG_INFO <<
"Pending update hasn't been applied because a reboot hasn't been detected";
60 PublicKey AktualizrSecondary::getPublicKey()
const {
return keys_->UptanePublicKey(); }
65 if (update_agent_->getInstalledImageInfo(installed_image_info)) {
66 manifest = manifest_issuer_->assembleAndSignManifest(installed_image_info);
72 int32_t AktualizrSecondary::getRootVersion(
bool director)
const {
73 std::string root_meta;
74 if (!storage_->loadLatestRoot(&root_meta,
75 (director) ? Uptane::RepositoryType::Director() : Uptane::RepositoryType::Image())) {
76 LOG_ERROR <<
"Could not load root metadata";
80 return Uptane::extractVersionUntrusted(root_meta);
83 bool AktualizrSecondary::putRoot(
const std::string& root,
bool director) {
86 LOG_ERROR <<
"putRootResp is not implemented yet";
90 bool AktualizrSecondary::putMetadata(
const Metadata& metadata) {
return doFullVerification(metadata); }
92 bool AktualizrSecondary::sendFirmware(
const std::string& firmware) {
94 if (!pending_target_.IsValid()) {
95 LOG_ERROR <<
"Aborting image download/receiving; no valid target found.";
99 if (!update_agent_->download(pending_target_, firmware)) {
100 LOG_ERROR <<
"Failed to pull/store an update data";
101 pending_target_ = Uptane::Target::Unknown();
110 if (!pending_target_.IsValid()) {
111 LOG_ERROR <<
"Aborting target image installation; no valid target found.";
115 if (pending_target_.filename() != target_name) {
116 LOG_ERROR <<
"name of the target to install and a name of the pending target do not match";
120 auto install_result = update_agent_->install(pending_target_);
122 switch (install_result) {
123 case data::ResultCode::Numeric::kOk: {
124 storage_->saveInstalledVersion(ecu_serial_.ToString(), pending_target_, InstalledVersionUpdateMode::kCurrent);
125 pending_target_ = Uptane::Target::Unknown();
126 LOG_INFO <<
"The target has been successfully installed: " << target_name;
129 case data::ResultCode::Numeric::kNeedCompletion: {
130 storage_->saveInstalledVersion(ecu_serial_.ToString(), pending_target_, InstalledVersionUpdateMode::kPending);
131 LOG_INFO <<
"The target has been successfully installed, but a reboot is required to be applied: " << target_name;
134 default: { LOG_INFO <<
"Failed to install the target: " << target_name; }
137 return install_result;
140 bool AktualizrSecondary::doFullVerification(
const Metadata& metadata) {
164 if (!director_repo_.updateMeta(*storage_, metadata)) {
165 LOG_ERROR <<
"Failed to update director metadata: " << director_repo_.getLastException().what();
176 if (!image_repo_.updateMeta(*storage_, metadata)) {
177 LOG_ERROR <<
"Failed to update image metadata: " << image_repo_.getLastException().what();
182 if (!director_repo_.matchTargetsWithImageTargets(*(image_repo_.getTargets()))) {
183 LOG_ERROR <<
"Targets metadata from the Director and Image repositories DOES NOT match ";
187 auto targetsForThisEcu = director_repo_.getTargets(getSerial(), getHwId());
189 if (targetsForThisEcu.size() != 1) {
190 LOG_ERROR <<
"Invalid number of targets (should be 1): " << targetsForThisEcu.size();
194 if (!update_agent_->isTargetSupported(targetsForThisEcu[0])) {
195 LOG_ERROR <<
"The given target type is not supported: " << targetsForThisEcu[0].type();
199 pending_target_ = targetsForThisEcu[0];
204 bool AktualizrSecondary::uptaneInitialize() {
205 if (keys_->generateUptaneKeyPair().size() == 0) {
206 LOG_ERROR <<
"Failed to generate uptane key pair";
211 EcuSerials ecu_serials;
213 if (storage_->loadEcuSerials(&ecu_serials)) {
214 ecu_serial_ = ecu_serials[0].first;
215 hardware_id_ = ecu_serials[0].second;
220 std::string ecu_serial_local = config_.uptane.ecu_serial;
221 if (ecu_serial_local.empty()) {
222 ecu_serial_local = keys_->UptanePublicKey().KeyId();
225 std::string ecu_hardware_id = config_.uptane.ecu_hardware_id;
226 if (ecu_hardware_id.empty()) {
227 ecu_hardware_id = Utils::getHostname();
228 if (ecu_hardware_id ==
"") {
234 storage_->storeEcuSerials(ecu_serials);
235 ecu_serial_ = ecu_serials[0].first;
236 hardware_id_ = ecu_serials[0].second;