1 #include "fsstorage_read.h" 10 #include <boost/lexical_cast.hpp> 11 #include <boost/scoped_array.hpp> 12 #include "json/json.h" 14 #include "logging/logging.h" 15 #include "utilities/utils.h" 17 FSStorageRead::FSStorageRead(
const StorageConfig& config) : config_(config) {
18 boost::filesystem::path images_path = config_.uptane_metadata_path.get(config_.path) /
"repo";
19 boost::filesystem::path director_path = config_.uptane_metadata_path.get(config_.path) /
"director";
22 for (
auto repo : {Uptane::RepositoryType::Director, Uptane::RepositoryType::Images}) {
23 boost::filesystem::path& meta_dir = repo == (Uptane::RepositoryType::Director) ? director_path : images_path;
24 boost::filesystem::path meta_path = meta_dir /
Uptane::Version().RoleFileName(Uptane::Role::Root());
25 if (boost::filesystem::exists(meta_path)) {
26 std::string
data = Utils::readFile(meta_path);
28 boost::filesystem::remove(meta_path);
29 if (version.version() >= 0) {
30 Utils::writeFile(meta_dir / version.RoleFileName(Uptane::Role::Root()),
data);
36 latest_director_root = findMaxVersion(director_path, Uptane::Role::Root());
37 latest_images_root = findMaxVersion(images_path, Uptane::Role::Root());
40 bool FSStorageRead::loadPrimaryKeys(std::string* public_key, std::string* private_key) {
41 return loadPrimaryPublic(public_key) && loadPrimaryPrivate(private_key);
44 bool FSStorageRead::loadPrimaryPublic(std::string* public_key) {
45 boost::filesystem::path public_key_path = config_.uptane_public_key_path.get(config_.path);
46 if (!boost::filesystem::exists(public_key_path)) {
50 if (public_key !=
nullptr) {
51 *public_key = Utils::readFile(public_key_path.string());
56 bool FSStorageRead::loadPrimaryPrivate(std::string* private_key) {
57 boost::filesystem::path private_key_path = config_.uptane_private_key_path.get(config_.path);
58 if (!boost::filesystem::exists(private_key_path)) {
62 if (private_key !=
nullptr) {
63 *private_key = Utils::readFile(private_key_path.string());
68 bool FSStorageRead::loadTlsCreds(std::string* ca, std::string* cert, std::string* pkey) {
69 boost::filesystem::path ca_path(config_.tls_cacert_path.get(config_.path));
70 boost::filesystem::path cert_path(config_.tls_clientcert_path.get(config_.path));
71 boost::filesystem::path pkey_path(config_.tls_pkey_path.get(config_.path));
72 if (!boost::filesystem::exists(ca_path) || boost::filesystem::is_directory(ca_path) ||
73 !boost::filesystem::exists(cert_path) || boost::filesystem::is_directory(cert_path) ||
74 !boost::filesystem::exists(pkey_path) || boost::filesystem::is_directory(pkey_path)) {
78 *ca = Utils::readFile(ca_path.string());
80 if (cert !=
nullptr) {
81 *cert = Utils::readFile(cert_path.string());
83 if (pkey !=
nullptr) {
84 *pkey = Utils::readFile(pkey_path.string());
89 bool FSStorageRead::loadTlsCommon(std::string*
data,
const BasedPath& path_in) {
90 boost::filesystem::path path(path_in.get(config_.path));
91 if (!boost::filesystem::exists(path)) {
95 if (
data !=
nullptr) {
96 *
data = Utils::readFile(path.string());
102 bool FSStorageRead::loadTlsCa(std::string* ca) {
return loadTlsCommon(ca, config_.tls_cacert_path); }
104 bool FSStorageRead::loadTlsCert(std::string* cert) {
return loadTlsCommon(cert, config_.tls_clientcert_path); }
106 bool FSStorageRead::loadTlsPkey(std::string* pkey) {
return loadTlsCommon(pkey, config_.tls_pkey_path); }
109 boost::filesystem::path metafile;
111 case (Uptane::RepositoryType::Director):
112 if (version.version() < 0) {
113 version = latest_director_root;
116 config_.uptane_metadata_path.get(config_.path) /
"director" / version.RoleFileName(Uptane::Role::Root());
119 case (Uptane::RepositoryType::Images):
120 if (version.version() < 0) {
121 version = latest_director_root;
123 metafile = config_.uptane_metadata_path.get(config_.path) /
"repo" / version.RoleFileName(Uptane::Role::Root());
130 if (version.version() < 0) {
134 if (!boost::filesystem::exists(metafile)) {
138 if (
data !=
nullptr) {
139 *
data = Utils::readFile(metafile);
145 boost::filesystem::path metafile;
147 case (Uptane::RepositoryType::Director):
148 metafile = config_.uptane_metadata_path.get(config_.path) /
"director" /
Uptane::Version().RoleFileName(role);
151 case (Uptane::RepositoryType::Images):
152 metafile = config_.uptane_metadata_path.get(config_.path) /
"repo" /
Uptane::Version().RoleFileName(role);
159 if (!boost::filesystem::exists(metafile)) {
163 if (
data !=
nullptr) {
164 *
data = Utils::readFile(metafile);
169 bool FSStorageRead::loadDeviceId(std::string* device_id) {
170 if (!boost::filesystem::exists(Utils::absolutePath(config_.path,
"device_id").string())) {
174 if (device_id !=
nullptr) {
175 *device_id = Utils::readFile(Utils::absolutePath(config_.path,
"device_id").string());
180 bool FSStorageRead::loadEcuRegistered() {
181 return boost::filesystem::exists(Utils::absolutePath(config_.path,
"is_registered").string());
184 bool FSStorageRead::loadEcuSerials(EcuSerials* serials) {
189 const boost::filesystem::path serial_path = Utils::absolutePath(config_.path,
"primary_ecu_serial");
190 if (!boost::filesystem::exists(serial_path)) {
193 serial = Utils::readFile(serial_path.string());
195 const boost::filesystem::path hw_id_path = Utils::absolutePath(config_.path,
"primary_ecu_hardware_id");
196 if (!boost::filesystem::exists(hw_id_path)) {
197 hw_id = Utils::getHostname();
199 hw_id = Utils::readFile(hw_id_path.string());
202 if (serials !=
nullptr) {
207 const boost::filesystem::path sec_list_path = Utils::absolutePath(config_.path,
"secondaries_list");
208 if (!boost::filesystem::exists(sec_list_path)) {
211 std::ifstream file(sec_list_path.c_str());
212 while (std::getline(file, buf)) {
213 size_t tab = buf.find(
'\t');
214 serial = buf.substr(0, tab);
216 hw_id = buf.substr(tab + 1);
217 }
catch (
const std::out_of_range& e) {
218 if (serials !=
nullptr) {
224 if (serials !=
nullptr) {
232 bool FSStorageRead::loadMisconfiguredEcus(std::vector<MisconfiguredEcu>* ecus) {
233 if (!boost::filesystem::exists(Utils::absolutePath(config_.path,
"misconfigured_ecus"))) {
236 Json::Value content_json = Utils::parseJSONFile(Utils::absolutePath(config_.path,
"misconfigured_ecus").string());
238 for (Json::ValueIterator it = content_json.begin(); it != content_json.end(); ++it) {
241 static_cast<EcuState>((*it)[
"state"].asInt())));
246 std::string FSStorageRead::loadInstalledVersions(std::vector<Uptane::Target>* installed_versions) {
247 std::string current_hash;
248 if (!boost::filesystem::exists(Utils::absolutePath(config_.path,
"installed_versions"))) {
251 Json::Value installed_versions_json =
252 Utils::parseJSONFile(Utils::absolutePath(config_.path,
"installed_versions").string());
253 std::vector<Uptane::Target> new_versions;
254 for (Json::ValueIterator it = installed_versions_json.begin(); it != installed_versions_json.end(); ++it) {
255 if (!(*it).isObject()) {
258 t_json[
"hashes"][
"sha256"] = it.key();
260 new_versions.push_back(t);
262 if ((*it)[
"is_current"].asBool()) {
263 current_hash = (*it)[
"hashes"][
"sha256"].asString();
266 new_versions.push_back(t);
269 *installed_versions = new_versions;
275 if (!boost::filesystem::exists(Utils::absolutePath(config_.path,
"installation_result").string())) {
279 if (result !=
nullptr) {
280 *result = data::OperationResult::fromJson(
281 Utils::readFile(Utils::absolutePath(config_.path,
"installation_result").string()));
286 bool FSStorageRead::splitNameRoleVersion(
const std::string& full_name, std::string* role_name,
int* version) {
287 size_t dot_pos = full_name.find(
'.');
290 if (dot_pos == std::string::npos) {
293 std::string prefix = full_name.substr(0, dot_pos);
294 if (role_name !=
nullptr) {
295 *role_name = full_name.substr(dot_pos + 1);
299 auto v = boost::lexical_cast<
int>(prefix);
300 if (version !=
nullptr) {
303 }
catch (
const boost::bad_lexical_cast&) {
311 if (!boost::filesystem::exists(meta_directory)) {
315 boost::filesystem::directory_iterator it{meta_directory};
316 for (; it != boost::filesystem::directory_iterator(); ++it) {
317 if (!boost::filesystem::is_regular_file(it->path())) {
320 std::string name = it->path().filename().native();
322 std::string file_role;
323 if (splitNameRoleVersion(name, &file_role, &file_version)) {
324 if (file_role ==
Uptane::Version().RoleFileName(role) && file_version > version) {
325 version = file_version;
335 void FSStorageRead::clearPrimaryKeys() {
336 boost::filesystem::remove(config_.uptane_public_key_path.get(config_.path));
337 boost::filesystem::remove(config_.uptane_private_key_path.get(config_.path));
340 void FSStorageRead::clearTlsCreds() {
341 boost::filesystem::remove(config_.tls_cacert_path.get(config_.path));
342 boost::filesystem::remove(config_.tls_clientcert_path.get(config_.path));
343 boost::filesystem::remove(config_.tls_pkey_path.get(config_.path));
347 boost::filesystem::path meta_path;
349 case Uptane::RepositoryType::Images:
350 meta_path = config_.uptane_metadata_path.get(config_.path) /
"repo";
352 case Uptane::RepositoryType::Director:
353 meta_path = config_.uptane_metadata_path.get(config_.path) /
"director";
359 boost::filesystem::directory_iterator it{meta_path};
360 for (; it != boost::filesystem::directory_iterator(); ++it) {
361 for (
auto role : Uptane::Role::Roles()) {
362 if (role == Uptane::Role::Root()) {
365 std::string role_name;
366 std::string fn = it->path().filename().native();
368 (splitNameRoleVersion(fn, &role_name,
nullptr) && (role_name ==
Uptane::Version().RoleFileName(role)))) {
369 boost::filesystem::remove(it->path());
375 void FSStorageRead::clearMetadata() {
376 for (
const auto& meta_path : {config_.uptane_metadata_path.get(config_.path) /
"repo",
377 config_.uptane_metadata_path.get(config_.path) /
"director"}) {
378 if (!boost::filesystem::exists(meta_path)) {
382 boost::filesystem::directory_iterator it{meta_path};
383 for (; it != boost::filesystem::directory_iterator(); ++it) {
384 boost::filesystem::remove(it->path());
389 void FSStorageRead::clearDeviceId() { boost::filesystem::remove(Utils::absolutePath(config_.path,
"device_id")); }
391 void FSStorageRead::clearEcuRegistered() {
392 boost::filesystem::remove(Utils::absolutePath(config_.path,
"is_registered"));
395 void FSStorageRead::clearEcuSerials() {
396 boost::filesystem::remove(Utils::absolutePath(config_.path,
"primary_ecu_serial"));
397 boost::filesystem::remove(Utils::absolutePath(config_.path,
"primary_ecu_hardware_id"));
398 boost::filesystem::remove(Utils::absolutePath(config_.path,
"secondaries_list"));
401 void FSStorageRead::clearMisconfiguredEcus() {
402 boost::filesystem::remove(Utils::absolutePath(config_.path,
"misconfigured_ecus"));
405 void FSStorageRead::clearInstalledVersions() {
406 if (boost::filesystem::exists(Utils::absolutePath(config_.path,
"installed_versions"))) {
407 boost::filesystem::remove(Utils::absolutePath(config_.path,
"installed_versions"));
411 void FSStorageRead::clearInstallationResult() {
412 boost::filesystem::remove(Utils::absolutePath(config_.path,
"installation_result"));
415 void FSStorageRead::cleanUpAll() {
420 clearEcuRegistered();
421 clearMisconfiguredEcus();
422 clearInstalledVersions();
423 clearInstallationResult();
426 boost::filesystem::remove_all(config_.uptane_metadata_path.get(config_.path));
427 boost::filesystem::remove_all(config_.path /
"targets");
Metadata version numbers.
RepositoryType
This must match the repo_type table in sqlstorage.