mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Return interposition configurations.
This commit is contained in:
parent
7936324dff
commit
3ec3b8bc05
3 changed files with 51 additions and 17 deletions
|
|
@ -172,14 +172,21 @@ def __read_configuration(configuration_handler,
|
|||
parent_repository_directory=None,
|
||||
parent_ssl_certificates_directory=None):
|
||||
"""
|
||||
A generic function to read a TUF interposition configuration off the disk,
|
||||
and handle it. configuration_handler must be a function which accepts a
|
||||
tuf.interposition.Configuration instance."""
|
||||
A generic function to read TUF interposition configurations off a file, and
|
||||
then handle those configurations with a given function. configuration_handler
|
||||
must be a function which accepts a tuf.interposition.Configuration
|
||||
instance.
|
||||
|
||||
Returns the parsed configurations as a dictionary of configurations indexed
|
||||
by hostnames."""
|
||||
|
||||
INVALID_TUF_CONFIGURATION = "Invalid configuration for {network_location}!"
|
||||
INVALID_TUF_INTERPOSITION_JSON = "Invalid configuration in {filename}!"
|
||||
NO_CONFIGURATIONS = "No configurations found in configuration in {filename}!"
|
||||
|
||||
# Configurations indexed by hostnames.
|
||||
parsed_configurations = {}
|
||||
|
||||
try:
|
||||
with open(filename) as tuf_interposition_json:
|
||||
tuf_interpositions = json.load(tuf_interposition_json)
|
||||
|
|
@ -197,6 +204,7 @@ def __read_configuration(configuration_handler,
|
|||
|
||||
configuration = configuration_parser.parse()
|
||||
configuration_handler(configuration)
|
||||
parsed_configurations[configuration.hostname] = configuration
|
||||
|
||||
except:
|
||||
Logger.exception(INVALID_TUF_CONFIGURATION.format(network_location=network_location))
|
||||
|
|
@ -206,6 +214,10 @@ def __read_configuration(configuration_handler,
|
|||
Logger.exception(INVALID_TUF_INTERPOSITION_JSON.format(filename=filename))
|
||||
raise
|
||||
|
||||
else:
|
||||
return parsed_configurations
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -218,8 +230,7 @@ def configure(filename="tuf.interposition.json",
|
|||
parent_repository_directory=None,
|
||||
parent_ssl_certificates_directory=None):
|
||||
|
||||
"""
|
||||
The optional parent_repository_directory parameter is used to specify the
|
||||
"""The optional parent_repository_directory parameter is used to specify the
|
||||
containing parent directory of the "repository_directory" specified in a
|
||||
configuration for *all* network locations, because sometimes the absolute
|
||||
location of the "repository_directory" is only known at runtime. If you
|
||||
|
|
@ -259,20 +270,26 @@ def configure(filename="tuf.interposition.json",
|
|||
|
||||
Unless any "url_prefix" begins with "https://", "ssl_certificates" is
|
||||
optional; it must specify certificates bundled as PEM (RFC 1422).
|
||||
"""
|
||||
|
||||
__read_configuration(__updater_controller.add, filename=filename,
|
||||
parent_repository_directory=parent_repository_directory,
|
||||
parent_ssl_certificates_directory=parent_ssl_certificates_directory)
|
||||
Returns the parsed configurations as a dictionary of configurations indexed
|
||||
by hostnames."""
|
||||
|
||||
configurations = \
|
||||
__read_configuration(__updater_controller.add, filename=filename,
|
||||
parent_repository_directory=parent_repository_directory,
|
||||
parent_ssl_certificates_directory=parent_ssl_certificates_directory)
|
||||
|
||||
return configurations
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def deconfigure(filename="tuf.interposition.json"):
|
||||
"""Remove TUF interposition for a previously read configuration."""
|
||||
def deconfigure(configurations):
|
||||
"""Remove TUF interposition for previously read configurations."""
|
||||
|
||||
__read_configuration(__updater_controller.remove, filename=filename)
|
||||
for configuration in configurations.itervalues():
|
||||
__updater_controller.remove(configuration)
|
||||
|
||||
|
||||
|
||||
|
|
@ -328,3 +345,8 @@ def wrapper(self, *args, **kwargs):
|
|||
|
||||
# Build and monkey patch public copies of the urllib and urllib2 modules.
|
||||
__monkey_patch()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -338,3 +338,8 @@ def remove(self, configuration):
|
|||
self.__repository_mirror_hostnames.difference_update(repository_mirror_hostnames)
|
||||
|
||||
Logger.info(UPDATER_REMOVED_MESSAGE.format(configuration=configuration))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -154,6 +154,8 @@ def disable_logging():
|
|||
|
||||
PASSWD = 'test'
|
||||
version = 1
|
||||
# Where we keep TUF configurations, if any, between every iteration.
|
||||
tuf_configurations = None
|
||||
|
||||
|
||||
def init_repo(tuf=False, port=None):
|
||||
|
|
@ -196,6 +198,8 @@ def init_repo(tuf=False, port=None):
|
|||
|
||||
|
||||
def cleanup(root_repo, server_process=None):
|
||||
global tuf_configurations
|
||||
|
||||
if server_process is not None:
|
||||
if server_process.returncode is None:
|
||||
server_process.kill()
|
||||
|
|
@ -206,9 +210,9 @@ def cleanup(root_repo, server_process=None):
|
|||
keystore.clear_keystore()
|
||||
|
||||
# Deconfigure interposition.
|
||||
interpose_json = os.path.join(root_repo, 'tuf.interposition.json')
|
||||
if os.path.exists(interpose_json):
|
||||
tuf.interposition.deconfigure(filename=interpose_json)
|
||||
if tuf_configurations is not None:
|
||||
tuf.interposition.deconfigure(tuf_configurations)
|
||||
tuf_configurations = None
|
||||
|
||||
# Removing repository directory.
|
||||
try:
|
||||
|
|
@ -365,7 +369,9 @@ def create_interposition_config(root_repo, url):
|
|||
(urllib_tuf replaces urllib module)
|
||||
urllib_tuf.urlretrieve(url, filename)
|
||||
|
||||
"""
|
||||
"""
|
||||
|
||||
global tuf_configurations
|
||||
|
||||
tuf_repo = os.path.join(root_repo, 'tuf_repo')
|
||||
tuf_client = os.path.join(root_repo, 'tuf_client')
|
||||
|
|
@ -396,7 +402,8 @@ def create_interposition_config(root_repo, url):
|
|||
with open(interpose_json, 'wb') as fileobj:
|
||||
tuf.util.json.dump(interposition_dict, fileobj)
|
||||
|
||||
tuf.interposition.configure(filename=interpose_json)
|
||||
assert tuf_configurations is None
|
||||
tuf_configurations = tuf.interposition.configure(filename=interpose_json)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue