Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 319 replace target #403

Merged
merged 2 commits into from
Nov 10, 2016
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
8 changes: 8 additions & 0 deletions tests/test_repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,14 @@ def test_add_target(self):
self.assertEqual(self.targets_object.target_files['/file2.txt'],
custom_file_permissions)

# Attempt to replace target that has already been added.
octal_file_permissions2 = oct(os.stat(target_filepath).st_mode)[4:]
custom_file_permissions2 = {'file_permissions': octal_file_permissions}
self.targets_object.add_target(target2_filepath, custom_file_permissions2)
self.assertEqual(self.targets_object.target_files['/file2.txt'],
custom_file_permissions2)


# Test improperly formatted arguments.
self.assertRaises(tuf.ssl_commons.exceptions.FormatError, self.targets_object.add_target, 3)
self.assertRaises(tuf.ssl_commons.exceptions.FormatError, self.targets_object.add_target, 3,
Expand Down
45 changes: 29 additions & 16 deletions tuf/repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,38 +1869,41 @@ def add_target(self, filepath, custom=None):
Targets object.

This method does not actually create 'filepath' on the file system.
'filepath' must already exist on the file system.
'filepath' must already exist on the file system. If 'filepath'
has already been added, it will be replaced with any new file
or 'custom' information.

>>>
>>>
>>>

<Arguments>
filepath:
The path of the target file. It must be located in the repository's
targets directory.
The path of the target file. It must exist in the repository's targets
directory.

custom:
An optional object providing additional information about the file.

<Exceptions>
tuf.ssl_commons.exceptions.FormatError, if 'filepath' is improperly formatted.
tuf.ssl_commons.exceptions.FormatError, if 'filepath' is improperly
formatted.

tuf.ssl_commons.exceptions.Error, if 'filepath' is not found under the repository's targets
directory.
tuf.ssl_commons.exceptions.Error, if 'filepath' is not found under the
repository's targets directory.

<Side Effects>
Adds 'filepath' to this role's list of targets. This role's
'tuf.roledb.py' is also updated.
'tuf.roledb.py' entry is also updated.

<Returns>
None.
"""

# Does 'filepath' have the correct format?
# Ensure the arguments have the appropriate number of objects and object
# types, and that all dict keys are properly named.
# Raise 'tuf.ssl_commons.exceptions.FormatError' if there is a mismatch.
# types, and that all dict keys are properly named. Raise
# 'tuf.ssl_commons.exceptions.FormatError' if there is a mismatch.
tuf.ssl_crypto.formats.PATH_SCHEMA.check_match(filepath)
if custom is None:
custom = {}
Expand All @@ -1912,26 +1915,36 @@ def add_target(self, filepath, custom=None):

# Ensure 'filepath' is found under the repository's targets directory.
if not filepath.startswith(self._targets_directory):
raise tuf.ssl_commons.exceptions.Error(repr(filepath) + ' is not under the Repository\'s'
' targets directory: ' + repr(self._targets_directory))
raise tuf.ssl_commons.exceptions.Error(repr(filepath) + ' does not exist'
' under the repository\'s targets directory:'
' ' + repr(self._targets_directory))

# Add 'filepath' (i.e., relative to the targets directory) to the role's
# list of targets. 'filepath' will be verified as an allowed path according
# to this Targets parent role when write() is called. Not verifying
# 'filepath' here allows freedom to add targets and parent restrictions
# in any order, and minimize the number of times these checks are performed.
# list of targets. 'filepath' will not be verified as an allowed path
# according to some delegating role. Not verifying 'filepath' here allows
# freedom to add targets and parent restrictions in any order, minimize the
# number of times these checks are performed, and allow any role to
# delegate trust of packages to this Targes role.
if os.path.isfile(filepath):

# Update the role's 'tuf.roledb.py' entry and avoid duplicates.
targets_directory_length = len(self._targets_directory)
roleinfo = tuf.roledb.get_roleinfo(self._rolename)
relative_path = filepath[targets_directory_length:]

if relative_path not in roleinfo['paths']:
logger.debug('Adding new target: ' + repr(relative_path))
roleinfo['paths'].update({relative_path: custom})

else:
logger.debug('Replacing target: ' + repr(relative_path))
roleinfo['paths'].update({relative_path: custom})

tuf.roledb.update_roleinfo(self._rolename, roleinfo)

else:
raise tuf.ssl_commons.exceptions.Error(repr(filepath) + ' is not a valid file.')
raise tuf.ssl_commons.exceptions.Error(repr(filepath) + ' is not'
' a valid file.')



Expand Down