From cf50ee52629849c969bab159bbeaa0bb3c4a2a68 Mon Sep 17 00:00:00 2001 From: Kostiantyn Bushko Date: Mon, 6 Apr 2020 17:03:08 +0300 Subject: [PATCH 1/4] Added API to direct update raw_report in the installation result Signed-off-by: Kostiantyn Bushko --- src/libaktualizr/storage/invstorage.h | 1 + src/libaktualizr/storage/sqlstorage.cc | 10 ++++++++++ src/libaktualizr/storage/sqlstorage.h | 1 + src/libaktualizr/storage/storage_common_test.cc | 3 +++ 4 files changed, 15 insertions(+) diff --git a/src/libaktualizr/storage/invstorage.h b/src/libaktualizr/storage/invstorage.h index 004cee46b6..e39af29742 100644 --- a/src/libaktualizr/storage/invstorage.h +++ b/src/libaktualizr/storage/invstorage.h @@ -139,6 +139,7 @@ class INvStorage { std::vector>* results) const = 0; virtual void storeDeviceInstallationResult(const data::InstallationResult& result, const std::string& raw_report, const std::string& correlation_id) = 0; + virtual bool storeDeviceInstallationRawReport(const std::string& raw_report) = 0; virtual bool loadDeviceInstallationResult(data::InstallationResult* result, std::string* raw_report, std::string* correlation_id) const = 0; virtual void clearInstallationResults() = 0; diff --git a/src/libaktualizr/storage/sqlstorage.cc b/src/libaktualizr/storage/sqlstorage.cc index 560bdaf6b4..5b0521160b 100644 --- a/src/libaktualizr/storage/sqlstorage.cc +++ b/src/libaktualizr/storage/sqlstorage.cc @@ -1456,6 +1456,16 @@ void SQLStorage::storeDeviceInstallationResult(const data::InstallationResult& r } } +bool SQLStorage::storeDeviceInstallationRawReport(const std::string& raw_report) { + SQLite3Guard db = dbConnection(); + auto statement = db.prepareStatement("UPDATE device_installation_result SET raw_report=?;", raw_report); + if (statement.step() != SQLITE_DONE || sqlite3_changes(db.get()) != 1) { + LOG_ERROR << "Can't set device raw report result: " << db.errmsg(); + return false; + } + return true; +} + bool SQLStorage::loadDeviceInstallationResult(data::InstallationResult* result, std::string* raw_report, std::string* correlation_id) const { SQLite3Guard db = dbConnection(); diff --git a/src/libaktualizr/storage/sqlstorage.h b/src/libaktualizr/storage/sqlstorage.h index 820f9ebcff..58e6b8772c 100644 --- a/src/libaktualizr/storage/sqlstorage.h +++ b/src/libaktualizr/storage/sqlstorage.h @@ -87,6 +87,7 @@ class SQLStorage : public SQLStorageBase, public INvStorage { std::vector>* results) const override; void storeDeviceInstallationResult(const data::InstallationResult& result, const std::string& raw_report, const std::string& correlation_id) override; + bool storeDeviceInstallationRawReport(const std::string& raw_report) override; bool loadDeviceInstallationResult(data::InstallationResult* result, std::string* raw_report, std::string* correlation_id) const override; void saveEcuReportCounter(const Uptane::EcuSerial& ecu_serial, int64_t counter) override; diff --git a/src/libaktualizr/storage/storage_common_test.cc b/src/libaktualizr/storage/storage_common_test.cc index 9e6566a989..01aa13fbe2 100644 --- a/src/libaktualizr/storage/storage_common_test.cc +++ b/src/libaktualizr/storage/storage_common_test.cc @@ -455,12 +455,15 @@ TEST(storage, load_store_installation_results) { EXPECT_EQ(dev_res.result_code.num_code, data::ResultCode::Numeric::kGeneralError); EXPECT_EQ(report, "raw"); EXPECT_EQ(correlation_id, "corrid"); + EXPECT_TRUE(storage->storeDeviceInstallationRawReport("user's raw report")); storage->clearInstallationResults(); res.clear(); EXPECT_FALSE(storage->loadEcuInstallationResults(&res)); EXPECT_EQ(res.size(), 0); EXPECT_FALSE(storage->loadDeviceInstallationResult(&dev_res, &report, &correlation_id)); + EXPECT_FALSE(storage->storeDeviceInstallationRawReport( + "This call will return a negative value since the installation report was cleaned!")); } TEST(storage, downloaded_files_info) { From 3c7607e3bdd48948a71ba46cd27299360c88dc36 Mon Sep 17 00:00:00 2001 From: Kostiantyn Bushko Date: Tue, 28 Apr 2020 15:16:16 +0300 Subject: [PATCH 2/4] an interface for replacing raw report in the device inst result Signed-off-by: Kostiantyn Bushko --- src/libaktualizr/primary/aktualizr.cc | 4 ++++ src/libaktualizr/primary/aktualizr.h | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libaktualizr/primary/aktualizr.cc b/src/libaktualizr/primary/aktualizr.cc index 17af45acf4..a1f707a7cb 100644 --- a/src/libaktualizr/primary/aktualizr.cc +++ b/src/libaktualizr/primary/aktualizr.cc @@ -156,6 +156,10 @@ std::future Aktualizr::Install(const std::vectorstoreDeviceInstallationRawReport(custom_raw_report.toStyledString()); +} + std::future Aktualizr::SendManifest(const Json::Value &custom) { std::function task([this, custom]() { return uptane_client_->putManifest(custom); }); return api_queue_.enqueue(task); diff --git a/src/libaktualizr/primary/aktualizr.h b/src/libaktualizr/primary/aktualizr.h index eb5d0e0638..bca093b09d 100644 --- a/src/libaktualizr/primary/aktualizr.h +++ b/src/libaktualizr/primary/aktualizr.h @@ -134,10 +134,22 @@ class Aktualizr { */ std::future Install(const std::vector& updates); + /** + * SetInstallationRawReport allows setting a custom raw report field in the device installation result. + * + * @note An invocation of this method will have effect only after call of Aktualizr::Install and before calling + * Aktualizr::SendManifest member function. + * @param custom_raw_report is an arbitrary json object which is intended to replace a default value in the device + * installation report. + * @return true if the custom raw report was successfully applied to the device installation result. + * If there is no installation report in the storage the function will always return false. + */ + bool SetInstallationRawReport(const Json::Value& custom_raw_report); + /** * Send installation report to the backend. * - * Note that the device manifest is also sent as a part of CheckUpdates and + * @note The device manifest is also sent as a part of CheckUpdates and * SendDeviceData calls, as well as after a reboot if it was initiated * by Aktualizr as a part of an installation process. * All these manifests will not include the custom data provided in this call. From ada82fbfcd223237a7c82ed52d39d509d3711478 Mon Sep 17 00:00:00 2001 From: Kostiantyn Bushko Date: Wed, 27 May 2020 05:06:02 +0300 Subject: [PATCH 3/4] SetInstallationRawReport takes string as an argument Signed-off-by: Kostiantyn Bushko --- src/libaktualizr/primary/aktualizr.cc | 4 ++-- src/libaktualizr/primary/aktualizr.h | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libaktualizr/primary/aktualizr.cc b/src/libaktualizr/primary/aktualizr.cc index a1f707a7cb..1855ee6270 100644 --- a/src/libaktualizr/primary/aktualizr.cc +++ b/src/libaktualizr/primary/aktualizr.cc @@ -156,8 +156,8 @@ std::future Aktualizr::Install(const std::vectorstoreDeviceInstallationRawReport(custom_raw_report.toStyledString()); +bool Aktualizr::SetInstallationRawReport(const std::string &custom_raw_report) { + return storage_->storeDeviceInstallationRawReport(custom_raw_report); } std::future Aktualizr::SendManifest(const Json::Value &custom) { diff --git a/src/libaktualizr/primary/aktualizr.h b/src/libaktualizr/primary/aktualizr.h index bca093b09d..0098f47eaa 100644 --- a/src/libaktualizr/primary/aktualizr.h +++ b/src/libaktualizr/primary/aktualizr.h @@ -139,12 +139,11 @@ class Aktualizr { * * @note An invocation of this method will have effect only after call of Aktualizr::Install and before calling * Aktualizr::SendManifest member function. - * @param custom_raw_report is an arbitrary json object which is intended to replace a default value in the device - * installation report. + * @param custom_raw_report is intended to replace a default value in the device installation report. * @return true if the custom raw report was successfully applied to the device installation result. * If there is no installation report in the storage the function will always return false. */ - bool SetInstallationRawReport(const Json::Value& custom_raw_report); + bool SetInstallationRawReport(const std::string& custom_raw_report); /** * Send installation report to the backend. From 6fe3f350206fa94bbecf5689263aaf5e48e9a79d Mon Sep 17 00:00:00 2001 From: Kostiantyn Bushko Date: Wed, 27 May 2020 05:08:49 +0300 Subject: [PATCH 4/4] aktualizr: test sending raw report Signed-off-by: Kostiantyn Bushko --- src/libaktualizr/primary/aktualizr_test.cc | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libaktualizr/primary/aktualizr_test.cc b/src/libaktualizr/primary/aktualizr_test.cc index 0a4e631944..29f7500a80 100644 --- a/src/libaktualizr/primary/aktualizr_test.cc +++ b/src/libaktualizr/primary/aktualizr_test.cc @@ -2028,6 +2028,29 @@ TEST(Aktualizr, ManifestCustom) { } } +TEST(Aktualizr, CustomInstallationRawReport) { + TemporaryDirectory temp_dir; + auto http = std::make_shared(temp_dir.Path(), "hasupdate", fake_meta_dir); + + Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); + auto storage = INvStorage::newStorage(conf.storage); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + + aktualizr.Initialize(); + result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); + result::Download download_result = aktualizr.Download(update_result.updates).get(); + result::Install install_result = aktualizr.Install(download_result.updates).get(); + + auto custom_raw_report = "Installation's custom raw report!"; + EXPECT_TRUE(aktualizr.SetInstallationRawReport(custom_raw_report)); + aktualizr.SendManifest().get(); + EXPECT_EQ(http->last_manifest["signed"]["installation_report"]["report"]["raw_report"], custom_raw_report); + + // After sending manifest, an installation report will be removed from the DB, + // so Aktualzr::SetInstallationRawReport must return a negative value. + EXPECT_FALSE(aktualizr.SetInstallationRawReport(custom_raw_report)); +} + class CountUpdateCheckEvents { public: CountUpdateCheckEvents() = default;