def run(target, config=None, interactive=False): """Run discovery and return results.""" if config is None: config = {} found = {} resources = {} errors = {} results = { 'target': target, 'created': utils.get_time_string(), 'found': found, 'resources': resources, } if utils.is_valid_ip_address(target): ip_address = target else: hostname = dns.parse_target_hostname(target) found['hostname'] = hostname ip_address = six.text_type(dns.resolve_hostname(hostname)) # TODO(sam): Use ipaddress.ip_address.is_global # .is_private # .is_unspecified # .is_multicast # To determine address "type" if not ipaddress.ip_address(ip_address).is_loopback: try: domain_info = dns.domain_info(hostname) resource_type = 'OS::DNS::Domain' identifier = '%s:%s' % (resource_type, hostname) resources[identifier] = { 'type': resource_type, 'key': identifier, } found['domain-key'] = identifier resources[identifier]['data'] = domain_info if 'registered' in domain_info: found['registered-domain'] = domain_info['registered'] except shared.WhoisException as exc: results['domain'] = str(exc) found['ip-address'] = ip_address host, host_errors = discover_host(ip_address, config, interactive=interactive) if host_errors: errors.update(host_errors) key = host.get('key') or ip_address resources[key] = host found['host-key'] = key results['updated'] = utils.get_time_string() return results, errors
def run(target, config=None, interactive=False): """Run discovery and return results.""" if config is None: config = {} found = {} resources = {} errors = {} results = { 'target': target, 'created': utils.get_time_string(), 'found': found, 'resources': resources, } if utils.is_valid_ip_address(target): ip_address = target else: hostname = dns.parse_target_hostname(target) found['hostname'] = hostname ip_address = six.text_type(dns.resolve_hostname(hostname)) #TODO(sam): Use ipaddress.ip_address.is_global # " .is_private # " .is_unspecified # " .is_multicast # To determine address "type" if not ipaddress.ip_address(ip_address).is_loopback: try: domain_info = dns.domain_info(hostname) resource_type = 'OS::DNS::Domain' identifier = '%s:%s' % (resource_type, hostname) resources[identifier] = { 'type': resource_type, 'key': identifier, } found['domain-key'] = identifier resources[identifier]['data'] = domain_info if 'registered' in domain_info: found['registered-domain'] = domain_info['registered'] except shared.WhoisException as exc: results['domain'] = str(exc) found['ip-address'] = ip_address host, host_errors = discover_host(ip_address, config, interactive=interactive) if host_errors: errors.update(host_errors) key = host.get('key') or ip_address resources[key] = host found['host-key'] = key results['updated'] = utils.get_time_string() return results, errors
def domain_info(domain): """Get as much information as possible for a given domain name.""" domain = get_registered_domain(domain) result = pythonwhois.get_whois(domain) registrar = [] if 'registrar' in result and len(result['registrar']) > 0: registrar = result['registrar'][0] nameservers = result.get('nameservers', []) days_until_expires = None expires = None if 'expiration_date' in result: if (isinstance(result['expiration_date'], list) and len(result['expiration_date']) > 0): expires = result['expiration_date'][0] if isinstance(expires, datetime.datetime): days_until_expires = (expires - datetime.datetime.now()).days expires = utils.get_time_string(time_obj=expires) else: days_until_expires = (utils.parse_time_string(expires) - datetime.datetime.now()).days return { 'name': domain, 'whois': result['raw'], 'registrar': registrar, 'nameservers': nameservers, 'days_until_expires': days_until_expires, 'expiration_date': expires, }
def test_get_formatted_time_string_datetime_tz(self): result = utils.get_time_string( time_obj=datetime.datetime(1970, 2, 1, 1, 2, 3, 0, SomeTZ())) self.assertEqual(result, "1970-02-01 01:47:03 +0000")
def test_get_formatted_time_string_time_struct(self): result = utils.get_time_string(time_obj=time.gmtime(0)) self.assertEqual(result, "1970-01-01 00:00:00 +0000")
def test_get_formatted_time_string(self): some_time = time.gmtime(0) with mock.patch.object(utils.time, 'gmtime') as mock_gmt: mock_gmt.return_value = some_time result = utils.get_time_string() self.assertEqual(result, "1970-01-01 00:00:00 +0000")