def main(): '''Utility to check access to a URL by the current user and optionally to return the data retrieved from the URL. ''' parser = OptionParser(usage="%prog [options] url") parser.add_option("-k", "--private-key", dest="key_file", metavar="FILE", default=None, help="Private key file.") parser.add_option("-c", "--certificate", dest="cert_file", metavar="FILE", default=os.path.expanduser("~/.esg/credentials.pem"), help="Certificate file.") parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Print debug information.") parser.add_option("-f", "--fetch", dest="output_file", metavar="FILE", default=None, help="Output file.") (options, args) = parser.parse_args() if len(args) != 1: parser.error("Incorrect number of arguments") url = args[0] # If a private key file is not specified, the key is assumed to be stored in the certificate file. config = Configuration(options.key_file if options.key_file and os.path.exists(options.key_file) else None, options.cert_file if options.cert_file and os.path.exists(options.cert_file) else None, options.debug) if options.output_file: (return_code, return_message, success) = httpget.fetch_from_url_to_file(url, config, options.output_file) else: (return_code, return_message, success) = httpget.check_url(url, config) print return_code, return_message
def check_dataset(base_url, dataset_path, services, config): for service in services: print service.name, service.service_type extensions = [''] if service.extensions is None else service.extensions for extension in extensions: url = base_url + service.base_url + dataset_path + extension (rc, msg, success) = httpget.check_url(url, config) print(" %d %s" % (rc, url))
def check_dataset(base_url, dataset_path, services, config): for service in services: print service.name, service.service_type extensions = [""] if service.extensions is None else service.extensions for extension in extensions: url = base_url + service.base_url + dataset_path + extension (rc, msg, success) = httpget.check_url(url, config) print (" %d %s" % (rc, url))
def check_single_access(base_url, service, access, config, results): """Checks access to a THREDDS dataset with specified access details for a single service, i.e., for a a service type that is not Compound. @param base_url - base URL of the catalog @param service_map - map of service names to service configurations read from the catalog @param access - dataset access details @param config - configuration @param results - accumulated results of check """ if service.service_type not in config.services_to_test: results.add_untested() results.add_untested_service_type(service.service_type) if not config.quiet: print("### Service of type %s not tested." % service.service_type) return access_allowed_count = 0 access_denied_count = 0 inconsistent = False extensions = [''] if service.extensions is None else service.extensions url = url_util.make_url( base_url, service.base_url) + access.url_path + service.suffix for extension in extensions: extended_url = url + extension (rc, msg, success) = httpget.check_url(extended_url, config) if not config.quiet: print(" %d [%s] %s" % (rc, msg, extended_url)) if service.is_public(extension): if not success: inconsistent = True if not config.quiet: print("### Expected access allowed.") elif service.is_forbidden(extension): if success: inconsistent = True if not config.quiet: print("### Expected access denied.") else: if success: access_allowed_count += 1 else: access_denied_count += 1 if access_allowed_count + access_denied_count > 0: if access_denied_count == 0: results.add_access_allowed() elif access_allowed_count == 0: results.add_access_denied() else: inconsistent = True if not config.quiet: print( "### Expected accesses either all allowed or all denied (excluding public and forbidden)." ) if inconsistent: results.add_inconsistent()
def check_single_access(base_url, service, access, config, results): """Checks access to a THREDDS dataset with specified access details for a single service, i.e., for a a service type that is not Compound. @param base_url - base URL of the catalog @param service_map - map of service names to service configurations read from the catalog @param access - dataset access details @param config - configuration @param results - accumulated results of check """ if service.service_type not in config.services_to_test: results.add_untested() results.add_untested_service_type(service.service_type) if not config.quiet: print("### Service of type %s not tested." % service.service_type) return access_allowed_count = 0 access_denied_count = 0 inconsistent = False extensions = [''] if service.extensions is None else service.extensions url = url_util.make_url(base_url, service.base_url) + access.url_path + service.suffix for extension in extensions: extended_url = url + extension (rc, msg, success) = httpget.check_url(extended_url, config) if not config.quiet: print(" %d [%s] %s" % (rc, msg, extended_url)) if service.is_public(extension): if not success: inconsistent = True if not config.quiet: print("### Expected access allowed.") elif service.is_forbidden(extension): if success: inconsistent = True if not config.quiet: print("### Expected access denied.") else: if success: access_allowed_count += 1 else: access_denied_count += 1 if access_allowed_count + access_denied_count > 0: if access_denied_count == 0: results.add_access_allowed() elif access_allowed_count == 0: results.add_access_denied() else: inconsistent = True if not config.quiet: print("### Expected accesses either all allowed or all denied (excluding public and forbidden).") if inconsistent: results.add_inconsistent()
def main(): '''Utility to check access to a URL by the current user and optionally to return the data retrieved from the URL. ''' parser = OptionParser(usage="%prog [options] url") parser.add_option("-k", "--private-key", dest="key_file", metavar="FILE", default=None, help="Private key file.") parser.add_option("-c", "--certificate", dest="cert_file", metavar="FILE", default=os.path.expanduser("~/.esg/credentials.pem"), help="Certificate file.") parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Print debug information.") parser.add_option("-f", "--fetch", dest="output_file", metavar="FILE", default=None, help="Output file.") (options, args) = parser.parse_args() if len(args) != 1: parser.error("Incorrect number of arguments") url = args[0] # If a private key file is not specified, the key is assumed to be stored in the certificate file. config = Configuration( options.key_file if options.key_file and os.path.exists(options.key_file) else None, options.cert_file if options.cert_file and os.path.exists(options.cert_file) else None, options.debug) if options.output_file: (return_code, return_message, success) = httpget.fetch_from_url_to_file(url, config, options.output_file) else: (return_code, return_message, success) = httpget.check_url(url, config) print return_code, return_message
def check_files(file_list, config): """Checks access to a list of files. @param file_list list of files to check @param config - configuration @return accumulated results of check """ results = Results() for file_url in file_list: (rc, msg, success) = httpget.check_url(file_url, config) if not config.quiet: print(" %d [%s] %s" % (rc, msg, file_url)) if success: results.add_access_allowed() else: results.add_access_denied() return results