Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Added API to direct update raw_report in the installation result #1628

Merged
merged 4 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libaktualizr/primary/aktualizr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ std::future<result::Install> Aktualizr::Install(const std::vector<Uptane::Target
return api_queue_.enqueue(task);
}

bool Aktualizr::SetInstallationRawReport(const std::string &custom_raw_report) {
return storage_->storeDeviceInstallationRawReport(custom_raw_report);
}

std::future<bool> Aktualizr::SendManifest(const Json::Value &custom) {
std::function<bool()> task([this, custom]() { return uptane_client_->putManifest(custom); });
return api_queue_.enqueue(task);
Expand Down
13 changes: 12 additions & 1 deletion src/libaktualizr/primary/aktualizr.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,21 @@ class Aktualizr {
*/
std::future<result::Install> Install(const std::vector<Uptane::Target>& 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 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 std::string& 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.
Expand Down
23 changes: 23 additions & 0 deletions src/libaktualizr/primary/aktualizr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,29 @@ TEST(Aktualizr, ManifestCustom) {
}
}

TEST(Aktualizr, CustomInstallationRawReport) {
TemporaryDirectory temp_dir;
auto http = std::make_shared<HttpFake>(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;
Expand Down
1 change: 1 addition & 0 deletions src/libaktualizr/storage/invstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class INvStorage {
std::vector<std::pair<Uptane::EcuSerial, data::InstallationResult>>* 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;
Expand Down
10 changes: 10 additions & 0 deletions src/libaktualizr/storage/sqlstorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("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();
Expand Down
1 change: 1 addition & 0 deletions src/libaktualizr/storage/sqlstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SQLStorage : public SQLStorageBase, public INvStorage {
std::vector<std::pair<Uptane::EcuSerial, data::InstallationResult>>* 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;
Expand Down
3 changes: 3 additions & 0 deletions src/libaktualizr/storage/storage_common_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down