@@ -1896,22 +1896,26 @@ def add_paths(self, paths, child_rolename):
1896
1896
def add_target (self , filepath , custom = None ):
1897
1897
"""
1898
1898
<Purpose>
1899
- Add a filepath (must be located in the repository's targets directory) to
1900
- the Targets object.
1899
+ Add a filepath (must be relative to the repository's targets directory)
1900
+ to the Targets object.
1901
1901
1902
- This method does not actually create 'filepath' on the file system.
1903
- 'filepath' must already exist on the file system. If 'filepath'
1904
- has already been added, it will be replaced with any new file
1905
- or 'custom' information.
1902
+ This method does not access the file system. 'filepath' must already
1903
+ exist on the file system.
1904
+
1905
+ If 'filepath' does not exist the file will still be added to 'roleinfo'.
1906
+ Only later calls to write() and writeall() will fail.
1907
+
1908
+ If 'filepath' has already been added, it will be replaced with any new
1909
+ file or 'custom' information.
1906
1910
1907
1911
>>>
1908
1912
>>>
1909
1913
>>>
1910
1914
1911
1915
<Arguments>
1912
1916
filepath:
1913
- The path of the target file. It must exist in the repository's targets
1914
- directory.
1917
+ The path of the target file. It must be relative to the repository's
1918
+ targets directory.
1915
1919
1916
1920
custom:
1917
1921
An optional object providing additional information about the file.
@@ -1920,8 +1924,8 @@ def add_target(self, filepath, custom=None):
1920
1924
securesystemslib.exceptions.FormatError, if 'filepath' is improperly
1921
1925
formatted.
1922
1926
1923
- securesystemslib .exceptions.Error , if 'filepath' is not located in the
1924
- repository's targets directory.
1927
+ tuf .exceptions.InvalidNameError , if 'filepath' is not relative (starts
1928
+ with a directory separator) .
1925
1929
1926
1930
<Side Effects>
1927
1931
Adds 'filepath' to this role's list of targets. This role's
@@ -1935,47 +1939,38 @@ def add_target(self, filepath, custom=None):
1935
1939
# Ensure the arguments have the appropriate number of objects and object
1936
1940
# types, and that all dict keys are properly named. Raise
1937
1941
# 'securesystemslib.exceptions.FormatError' if there is a mismatch.
1938
- securesystemslib .formats .PATH_SCHEMA .check_match (filepath )
1942
+ tuf .formats .RELPATH_SCHEMA .check_match (filepath )
1939
1943
1940
1944
if custom is None :
1941
1945
custom = {}
1942
-
1943
1946
else :
1944
1947
tuf .formats .CUSTOM_SCHEMA .check_match (custom )
1945
1948
1946
- filepath = os .path .join (self ._targets_directory , filepath )
1947
-
1948
1949
# Add 'filepath' (i.e., relative to the targets directory) to the role's
1949
1950
# list of targets. 'filepath' will not be verified as an allowed path
1950
1951
# according to some delegating role. Not verifying 'filepath' here allows
1951
1952
# freedom to add targets and parent restrictions in any order, minimize the
1952
1953
# number of times these checks are performed, and allow any role to
1953
- # delegate trust of packages to this Targes role.
1954
- if os .path .isfile (filepath ):
1955
-
1956
- # Update the role's 'tuf.roledb.py' entry and avoid duplicates. Make
1957
- # sure to exclude the path separator when calculating the length of the
1958
- # targets directory.
1959
- targets_directory_length = len (self ._targets_directory ) + 1
1960
- roleinfo = tuf .roledb .get_roleinfo (self ._rolename , self ._repository_name )
1961
- relative_path = filepath [targets_directory_length :].replace ('\\ ' , '/' )
1954
+ # delegate trust of packages to this Targets role.
1962
1955
1963
- if relative_path not in roleinfo ['paths' ]:
1964
- logger .debug ('Adding new target: ' + repr (relative_path ))
1965
- roleinfo ['paths' ].update ({relative_path : custom })
1966
-
1967
- else :
1968
- logger .debug ('Replacing target: ' + repr (relative_path ))
1969
- roleinfo ['paths' ].update ({relative_path : custom })
1956
+ # Check if the target path is relative and normalize it. File's existence
1957
+ # on the file system is not verified. If the file does not exist relative
1958
+ # to the targets directory, later calls to write() will fail.
1959
+ relative_path = self ._check_relpath (filepath )
1970
1960
1961
+ # Update the role's 'tuf.roledb.py' entry and avoid duplicates.
1962
+ roleinfo = tuf .roledb .get_roleinfo (self ._rolename , self ._repository_name )
1971
1963
1972
- tuf .roledb .update_roleinfo (self ._rolename , roleinfo ,
1973
- repository_name = self ._repository_name )
1964
+ if relative_path not in roleinfo ['paths' ]:
1965
+ logger .debug ('Adding new target: ' + repr (relative_path ))
1966
+ roleinfo ['paths' ].update ({relative_path : custom })
1974
1967
1975
1968
else :
1976
- raise securesystemslib .exceptions .Error (repr (filepath ) + ' is not'
1977
- ' a valid file in the repository\' s targets'
1978
- ' directory: ' + repr (self ._targets_directory ))
1969
+ logger .debug ('Replacing target: ' + repr (relative_path ))
1970
+ roleinfo ['paths' ].update ({relative_path : custom })
1971
+
1972
+ tuf .roledb .update_roleinfo (self ._rolename , roleinfo ,
1973
+ repository_name = self ._repository_name )
1979
1974
1980
1975
1981
1976
@@ -1999,9 +1994,8 @@ def add_targets(self, list_of_targets):
1999
1994
securesystemslib.exceptions.FormatError, if the arguments are improperly
2000
1995
formatted.
2001
1996
2002
- securesystemslib.exceptions.Error, if any of the paths listed in
2003
- 'list_of_targets' is not located in the repository's targets directory or
2004
- is invalid.
1997
+ tuf.exceptions.InvalidNameError, if any target in 'list_of_targets'
1998
+ is not relative (starts with a directory separator).
2005
1999
2006
2000
<Side Effects>
2007
2001
This Targets' roleinfo is updated with the paths in 'list_of_targets'.
@@ -2017,7 +2011,6 @@ def add_targets(self, list_of_targets):
2017
2011
tuf .formats .RELPATHS_SCHEMA .check_match (list_of_targets )
2018
2012
2019
2013
# Update the tuf.roledb entry.
2020
- targets_directory_length = len (self ._targets_directory )
2021
2014
relative_list_of_targets = []
2022
2015
2023
2016
# Ensure the paths in 'list_of_targets' are valid and are located in the
@@ -2027,15 +2020,7 @@ def add_targets(self, list_of_targets):
2027
2020
# freedom to add targets and parent restrictions in any order, and minimize
2028
2021
# the number of times these checks are performed.
2029
2022
for target in list_of_targets :
2030
- filepath = os .path .join (self ._targets_directory , target )
2031
-
2032
- if os .path .isfile (filepath ):
2033
- relative_list_of_targets .append (
2034
- filepath [targets_directory_length + 1 :].replace ('\\ ' , '/' ))
2035
-
2036
- else :
2037
- raise securesystemslib .exceptions .Error (repr (filepath ) + ' is not'
2038
- ' a valid file.' )
2023
+ relative_list_of_targets .append (self ._check_relpath (target ))
2039
2024
2040
2025
# Update this Targets 'tuf.roledb.py' entry.
2041
2026
roleinfo = tuf .roledb .get_roleinfo (self ._rolename , self ._repository_name )
@@ -2223,12 +2208,11 @@ def delegate(self, rolename, public_keys, paths, threshold=1,
2223
2208
securesystemslib.exceptions.FormatError, if any of the arguments are
2224
2209
improperly formatted.
2225
2210
2226
- securesystemslib.exceptions.Error, if the delegated role already exists
2227
- or if any target in 'list_of_targets' is an invalid path (i.e., not
2228
- located in the repository's targets directory).
2211
+ securesystemslib.exceptions.Error, if the delegated role already exists.
2229
2212
2230
- tuf.exceptions.InvalidNameError, if any path in 'paths' is not a
2231
- relative path (starts with a directory separator).
2213
+ tuf.exceptions.InvalidNameError, if any path in 'paths' or any
2214
+ target in 'list_of_targets' is not relative (starts with a directory
2215
+ separator).
2232
2216
2233
2217
<Side Effects>
2234
2218
A new Target object is created for 'rolename' that is accessible to the
@@ -2274,16 +2258,15 @@ def delegate(self, rolename, public_keys, paths, threshold=1,
2274
2258
# Ensure the paths of 'list_of_targets' are located in the repository's
2275
2259
# targets directory.
2276
2260
relative_targetpaths = {}
2277
- targets_directory_length = len (self ._targets_directory )
2278
2261
2279
2262
if list_of_targets :
2280
2263
for target in list_of_targets :
2281
- target = os . path . join ( self . _targets_directory , target )
2282
- if not os . path . isfile ( target ):
2283
- logger . warning ( repr ( target ) + ' does not exist in the'
2284
- ' repository \' s targets directory: ' + repr ( self . _targets_directory ))
2285
-
2286
- relative_targetpaths .update ({target [ targets_directory_length :] : {}})
2264
+ # Check if the target path is relative and normalize it. File's
2265
+ # existence on the file system is not verified. If the file does not
2266
+ # exist relative to the targets directory, later calls to write()
2267
+ # will fail.
2268
+ rel_targetpath = self . _check_relpath ( target )
2269
+ relative_targetpaths .update ({rel_targetpath : {}})
2287
2270
2288
2271
# A list of relative and verified paths or glob patterns to be added to the
2289
2272
# child role's entry in the parent's delegations field.
@@ -2468,9 +2451,12 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
2468
2451
formatted.
2469
2452
2470
2453
securesystemslib.exceptions.Error, if 'number_of_bins' is not a power of
2471
- 2, or one of the targets in 'list_of_targets' is not located in the
2454
+ 2, or one of the targets in 'list_of_targets' is not relative to the
2472
2455
repository's targets directory.
2473
2456
2457
+ tuf.exceptions.InvalidNameError, if any target in 'list_of_targets'
2458
+ is not relative (starts with a directory separator).
2459
+
2474
2460
<Side Effects>
2475
2461
Delegates multiple target roles from the current parent role.
2476
2462
@@ -2518,17 +2504,12 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
2518
2504
for bin_index in six .moves .xrange (total_hash_prefixes ):
2519
2505
target_paths_in_bin [bin_index ] = []
2520
2506
2521
- # Assign every path to its bin. Log a warning if the target path does not
2522
- # exist in the repository's targets directory.
2507
+ # Assign every path to its bin.
2523
2508
for target_path in list_of_targets :
2524
- if not os .path .isfile (os .path .join (self ._targets_directory , target_path )):
2525
- logger .warning ('A path in "list of'
2526
- ' targets" is not located in the repository\' s targets'
2527
- ' directory: ' + repr (target_path ))
2528
-
2529
- else :
2530
- logger .debug (repr (target_path ) + ' is located in the repository\' s'
2531
- ' targets directory.' )
2509
+ # Check if the target path is relative and normalize it. File's existence
2510
+ # on the file system is not verified. If the file does not exist relative
2511
+ # to the targets directory, later calls to write() will fail.
2512
+ target_path = self ._check_relpath (target_path )
2532
2513
2533
2514
# Determine the hash prefix of 'target_path' by computing the digest of
2534
2515
# its path relative to the targets directory. Example:
@@ -2736,11 +2717,10 @@ def _locate_and_update_target_in_bin(self, target_filepath, method_name):
2736
2717
raise securesystemslib .exceptions .Error (self .rolename + ' has not'
2737
2718
' delegated to hashed bins.' )
2738
2719
2739
- # Log warning if 'target_filepath' is not located in the repository's
2740
- # targets directory.
2741
- if not os .path .isfile (os .path .join (self ._targets_directory , target_filepath )):
2742
- logger .warning (repr (target_filepath ) + ' is not located in the'
2743
- ' repository\' s targets directory: ' + repr (self ._targets_directory ))
2720
+ # Check if the target path is relative and normalize it. File's existence
2721
+ # on the file system is not verified. If the file does not exist relative
2722
+ # to the targets directory, later calls to write() will fail.
2723
+ target_filepath = self ._check_relpath (target_filepath )
2744
2724
2745
2725
# Determine the hash prefix of 'target_path' by computing the digest of
2746
2726
# its path relative to the targets directory. Example:
0 commit comments