From 1ee0e4302b0f9f36f7d02266d0457bd32cd28a12 Mon Sep 17 00:00:00 2001 From: Tanishq Jasoria Date: Wed, 15 May 2019 15:21:44 -0400 Subject: [PATCH 1/5] Added a deregister_vehicle function --- uptane/services/inventorydb.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/uptane/services/inventorydb.py b/uptane/services/inventorydb.py index e07c207..9815927 100644 --- a/uptane/services/inventorydb.py +++ b/uptane/services/inventorydb.py @@ -353,6 +353,21 @@ def register_vehicle(vin, primary_ecu_serial=None, overwrite=True): +def deregister_vehicle(vin): + + if vin not in ecus_by_vin: + raise uptane.UnknownVehicle('The given VIN, ' + repr(vin) + ', is not ' + 'registered.') + + buff = ecus_by_vin.pop(vin) + buff = vehicle_manifests.pop(vin) + buff = primary_ecus_by_vin.pop(vin) + buff = ecu_manifests.pop(vin) + buff = ecu_public_keys.pop(vin) + + + + def check_vin_registered(vin): From 89c510c487abb8cb97cc874b75087b30181d227a Mon Sep 17 00:00:00 2001 From: Tanishq Jasoria Date: Wed, 15 May 2019 15:22:57 -0400 Subject: [PATCH 2/5] Added a remove_vehicle function --- uptane/services/director.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/uptane/services/director.py b/uptane/services/director.py index df9a259..861ee4d 100644 --- a/uptane/services/director.py +++ b/uptane/services/director.py @@ -463,6 +463,18 @@ def add_new_vehicle(self, vin, primary_ecu_serial=None): + def remove_vehicle(self, vin): + """ + For removing vehicles and all the ECU registered under that vehicle + + """ + inventory.deregister_vehicle(vin) + + self.remove_director_repo_for_vehicle(vin) + + + + def create_director_repo_for_vehicle(self, vin): """ @@ -526,6 +538,26 @@ def create_director_repo_for_vehicle(self, vin): + def remove_director_repo_for_vehicle(self, vin): + """ + Remove the repository object and corresponding keys + for a given vehicle identifier. + + """ + + uptane.formats.VIN_SCHEMA.check_match(vin) + + # Remove the VIN repository for the Director repository + vin = uptane.common.scrub_filename(vin, self.director_repos_dir) + shutil.rmtree(vin) + + # Remove the reference of the repository from the Director + buff = self.vehicle_repositories.pop(vin) + + + + + def add_target_for_ecu(self, vin, ecu_serial, target_filepath): """ Add a target to the repository for a vehicle, marked as being for a From bfb1f9c27644f0969bc04b0411a4c52c58c1e058 Mon Sep 17 00:00:00 2001 From: Tanishq Jasoria Date: Tue, 4 Jun 2019 17:28:49 -0400 Subject: [PATCH 3/5] FIX minor bugs in the implementation --- uptane/services/director.py | 5 +++-- uptane/services/inventorydb.py | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/uptane/services/director.py b/uptane/services/director.py index 861ee4d..b1dc391 100644 --- a/uptane/services/director.py +++ b/uptane/services/director.py @@ -43,6 +43,7 @@ import os import hashlib +import shutil from uptane.encoding.asn1_codec import DATATYPE_TIME_ATTESTATION from uptane.encoding.asn1_codec import DATATYPE_ECU_MANIFEST @@ -548,8 +549,8 @@ def remove_director_repo_for_vehicle(self, vin): uptane.formats.VIN_SCHEMA.check_match(vin) # Remove the VIN repository for the Director repository - vin = uptane.common.scrub_filename(vin, self.director_repos_dir) - shutil.rmtree(vin) + vin_dir = uptane.common.scrub_filename(vin, self.director_repos_dir) + shutil.rmtree(vin_dir) # Remove the reference of the repository from the Director buff = self.vehicle_repositories.pop(vin) diff --git a/uptane/services/inventorydb.py b/uptane/services/inventorydb.py index 9815927..83aaded 100644 --- a/uptane/services/inventorydb.py +++ b/uptane/services/inventorydb.py @@ -359,11 +359,14 @@ def deregister_vehicle(vin): raise uptane.UnknownVehicle('The given VIN, ' + repr(vin) + ', is not ' 'registered.') + buff = ecus_by_vin.pop(vin) buff = vehicle_manifests.pop(vin) buff = primary_ecus_by_vin.pop(vin) - buff = ecu_manifests.pop(vin) - buff = ecu_public_keys.pop(vin) + if vin in ecu_manifests: + buff = ecu_manifests.pop(vin) + if vin in ecu_public_keys: + buff = ecu_public_keys.pop(vin) From 75d06ce6b35859b5a5eb638e81276ee7596c86b5 Mon Sep 17 00:00:00 2001 From: Tanishq Jasoria Date: Tue, 4 Jun 2019 17:29:22 -0400 Subject: [PATCH 4/5] Add test for the remove vehicle function --- tests/test_director.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_director.py b/tests/test_director.py index 4ab0946..559db84 100644 --- a/tests/test_director.py +++ b/tests/test_director.py @@ -234,6 +234,35 @@ def test_03_add_new_vehicle(self): + def test_04_remove_vehicle(self): + + # Register a new vehicle, to remove it afterwards + vin = 'testcar' + TestDirector.instance.add_new_vehicle(vin) + + # The vin should be present in the inventory db. + self.assertIn(vin, TestDirector.instance.vehicle_repositories) + + # Remove the new vehicle + # This also removes the TUF repository of the vehicle containing + # the director metadata for that vehicle + TestDirector.instance.remove_vehicle(vin) + + # Check resulting contents of the Director + self.assertNotIn(vin, TestDirector.instance.vehicle_repositories) + self.assertNotIn(vin, os.walk(TestDirector.instance.director_repos_dir)) + + # Check resulting contents of the inventorydb + self.assertNotIn(vin, inventory.ecus_by_vin) + self.assertNotIn(vin, inventory.primary_ecus_by_vin) + self.assertNotIn(vin, inventory.vehicle_manifests) + self.assertNotIn(vin, inventory.ecu_manifests) + self.assertNotIn(vin, inventory.ecu_public_keys) + + + + + def test_05_register_ecu_serial(self): From c909d87e2c22dfab19e10ff0ac046465d98f7afa Mon Sep 17 00:00:00 2001 From: Tanishq Jasoria Date: Wed, 5 Jun 2019 13:23:40 -0400 Subject: [PATCH 5/5] Change CWD after adding a vehicle to the director While adding a vehicle the current working directory is changed. --- tests/test_director.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_director.py b/tests/test_director.py index 559db84..e3c7239 100644 --- a/tests/test_director.py +++ b/tests/test_director.py @@ -239,6 +239,7 @@ def test_04_remove_vehicle(self): # Register a new vehicle, to remove it afterwards vin = 'testcar' TestDirector.instance.add_new_vehicle(vin) + os.chdir(uptane.WORKING_DIR) # The vin should be present in the inventory db. self.assertIn(vin, TestDirector.instance.vehicle_repositories)