def __read_configuration(configuration_handler, filename="tuf.interposition.json", parent_repository_directory=None, parent_ssl_certificates_directory=None): """ 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) configurations = tuf_interpositions.get("configurations", {}) if len(configurations) == 0: raise InvalidConfiguration( NO_CONFIGURATIONS.format(filename=filename)) else: for network_location, configuration in configurations.iteritems( ): try: configuration_parser = ConfigurationParser( network_location, configuration, parent_repository_directory= parent_repository_directory, parent_ssl_certificates_directory= parent_ssl_certificates_directory) configuration = configuration_parser.parse() configuration_handler(configuration) parsed_configurations[ configuration.hostname] = configuration except: Logger.exception( INVALID_TUF_CONFIGURATION.format( network_location=network_location)) raise except: Logger.exception( INVALID_TUF_INTERPOSITION_JSON.format(filename=filename)) raise else: return parsed_configurations
def __read_configuration(configuration_handler, filename="tuf.interposition.json", parent_repository_directory=None, parent_ssl_certificates_directory=None): """ 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) configurations = tuf_interpositions.get("configurations", {}) if len(configurations) == 0: raise InvalidConfiguration(NO_CONFIGURATIONS.format(filename=filename)) else: for network_location, configuration in configurations.iteritems(): try: configuration_parser = ConfigurationParser(network_location, configuration, parent_repository_directory=parent_repository_directory, parent_ssl_certificates_directory=parent_ssl_certificates_directory) configuration = configuration_parser.parse() configuration_handler(configuration) parsed_configurations[configuration.hostname] = configuration except: Logger.exception(INVALID_TUF_CONFIGURATION.format(network_location=network_location)) raise except: Logger.exception(INVALID_TUF_INTERPOSITION_JSON.format(filename=filename)) raise else: return parsed_configurations
def __read_configuration(configuration_handler, filename="tuf.interposition.json", 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.""" 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}!" try: with open(filename) as tuf_interposition_json: tuf_interpositions = json.load(tuf_interposition_json) configurations = tuf_interpositions.get("configurations", {}) if len(configurations) == 0: raise InvalidConfiguration(NO_CONFIGURATIONS.format(filename=filename)) else: for network_location, configuration in configurations.iteritems(): try: configuration_parser = ConfigurationParser(network_location, configuration, parent_repository_directory=parent_repository_directory, parent_ssl_certificates_directory=parent_ssl_certificates_directory) configuration = configuration_parser.parse() configuration_handler(configuration) except: Logger.exception(INVALID_TUF_CONFIGURATION.format(network_location=network_location)) raise except: Logger.exception(INVALID_TUF_INTERPOSITION_JSON.format(filename=filename)) raise
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 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 need to specify a different parent_repository_directory for other network locations, simply call this method again with different parameters. Ditto for the optional parent_ssl_certificates_directory parameter. Example of a TUF interposition configuration JSON object: { "configurations": { "seattle.cs.washington.edu": { "repository_directory": "client/", "repository_mirrors" : { "mirror1": { "url_prefix": "http://seattle-tuf.cs.washington.edu", "metadata_path": "metadata", "targets_path": "targets", "confined_target_dirs": [ "" ] } }, ("target_paths": [ { ".*/(simple/\\w+)/$": "{0}/index.html" }, { ".*/(packages/.+)$": "{0}" } ], "ssl_certificates": "cacert.pem") } } } "target_paths" is optional: If you do not tell TUF to selectively match paths with regular expressions, TUF will work over any path under the given network location. However, if you do specify it, you are then telling TUF how to transform a specified path into another one, and TUF will *not* recognize any unspecified path for the given network location. Unless any "url_prefix" begins with "https://", "ssl_certificates" is optional; it must specify certificates bundled as PEM (RFC 1422). """ 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}!" try: with open(filename) as tuf_interposition_json: tuf_interpositions = json.load(tuf_interposition_json) configurations = tuf_interpositions.get("configurations", {}) if len(configurations) == 0: raise InvalidConfiguration(NO_CONFIGURATIONS.format(filename=filename)) else: for network_location, configuration in configurations.iteritems(): try: configuration_parser = ConfigurationParser(network_location, configuration, parent_repository_directory=parent_repository_directory, parent_ssl_certificates_directory=parent_ssl_certificates_directory) configuration = configuration_parser.parse() __updater_controller.add(configuration) except: Logger.exception(INVALID_TUF_CONFIGURATION.format(network_location=network_location)) raise except: Logger.exception(INVALID_TUF_INTERPOSITION_JSON.format(filename=filename)) raise