Skip to content

Commit ca48f81

Browse files
committed
feat: Check connection
First, the internet connection is tested. The DNS is queried, then a connection is established to the resolved IP. If resolving fails, a hard-coded IP is tried for production or staging. In case of either failure, DNS query for www.redhat.com and connection to 1.1.1.1 is tried.
1 parent 1f7879c commit ca48f81

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

insights/client/connection.py

+77-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"""
44
from __future__ import print_function
55
from __future__ import absolute_import
6+
7+
import socket
8+
69
import requests
710
import os
811
import six
@@ -365,7 +368,6 @@ def _legacy_test_urls(self, url, method):
365368
if test_req.status_code in (200, 201):
366369
return True
367370
else:
368-
logger.error(" Failed.")
369371
return False
370372
except REQUEST_FAILED_EXCEPTIONS as exc:
371373
last_ex = exc
@@ -474,6 +476,65 @@ def _dump_urls(self):
474476

475477
logger.info("")
476478

479+
def _test_dns(self, hostname):
480+
port = requests.utils.DEFAULT_PORTS["http"]
481+
482+
if self.config.verbose:
483+
logger.info(" Checking DNS for %s:%s...", hostname, port)
484+
else:
485+
logger.info(" Resolving %s...", hostname)
486+
487+
try:
488+
with socket.create_connection((hostname, port)) as conn:
489+
conn_ip, conn_port = conn.getpeername()
490+
logger.debug(" Resolved as: %s:%s", conn_ip, conn_port)
491+
except socket.gaierror as exc:
492+
logger.debug(" socket.gaierror: %s", exc)
493+
logger.error(" Can't resolve the domain name.")
494+
logger.error(" FAILED.")
495+
logger.error("")
496+
return None
497+
498+
logger.info(" SUCCESS.")
499+
logger.info("")
500+
return conn_ip
501+
502+
def _test_ip(self, ip):
503+
port = requests.utils.DEFAULT_PORTS["http"]
504+
505+
try:
506+
if self.config.verbose:
507+
logger.info(" Connecting to %s:%s...", ip, port)
508+
else:
509+
logger.info(" Connecting to %s...", ip)
510+
511+
with socket.create_connection((ip, port)) as conn:
512+
logger.info(" SUCCESS.")
513+
logger.info("")
514+
conn.close()
515+
except socket.timeout:
516+
logger.error(" Connection to %s:%s timed out.", ip, port)
517+
logger.error(" FAILED.")
518+
logger.info("")
519+
return False
520+
except socket.error:
521+
logger.error(" Can't connect to %s:%s.", ip, port)
522+
logger.error(" FAILED.")
523+
logger.info("")
524+
return False
525+
526+
return True
527+
528+
def _test_connection(self, hostname, fallback_ip=None):
529+
resolved_ip = self._test_dns(hostname)
530+
531+
for ip in filter(None, [resolved_ip, fallback_ip]):
532+
success = self._test_ip(ip)
533+
if success:
534+
return resolved_ip, ip
535+
536+
return resolved_ip, None
537+
477538
def test_connection(self, rc=0):
478539
"""
479540
Test connection to Red Hat
@@ -487,6 +548,21 @@ def test_connection(self, rc=0):
487548
logger.info("Running Connection Tests...")
488549
logger.info("")
489550

551+
base_url_hostname = urlparse(self.base_url).hostname
552+
if base_url_hostname.endswith("redhat.com"):
553+
if base_url_hostname.endswith("stage.redhat.com"):
554+
base_url_ip = constants.insights_ip_stage
555+
else:
556+
base_url_ip = constants.insights_ip_prod
557+
else:
558+
base_url_ip = None
559+
560+
resolved_base_url_ip, success_base_url_ip = self._test_connection(base_url_hostname, base_url_ip)
561+
if not resolved_base_url_ip or not success_base_url_ip:
562+
resolved_fallback_ip, success_fallback_ip = self._test_connection("www.redhat.com", constants.stable_public_ip)
563+
logger.debug("dns %s, conn %s", resolved_fallback_ip, success_fallback_ip)
564+
return False
565+
490566
for description, url, method in [
491567
("Uploading a file to Ingress", self.upload_url, "POST"),
492568
("Getting hosts from Inventory", self.inventory_url + "/hosts", "GET"),

insights/client/constants.py

+5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class InsightsConstants(object):
5050
default_payload_log = os.path.join(log_dir, app_name + '-payload.log')
5151
custom_network_log_level = 11
5252
default_sed_file = os.path.join(default_conf_dir, '.exp.sed')
53+
# base_url = 'cert-api.access.redhat.com/r/insights/platform'
5354
base_url = 'cert-api.access.redhat.com/r/insights/platform'
55+
# legacy_base_url = 'cert-api.access.redhat.com/r/insights'
5456
legacy_base_url = 'cert-api.access.redhat.com/r/insights'
5557
unregistered_files = [os.path.join(default_conf_dir, '.unregistered'),
5658
os.path.join(simple_find_replace_dir, '.unregistered')]
@@ -90,3 +92,6 @@ class InsightsConstants(object):
9092
rhsm_facts_file = os.path.join(os.sep, 'etc', 'rhsm', 'facts', 'insights-client.facts')
9193
# In MB
9294
archive_filesize_max = 100
95+
insights_ip_prod = "23.37.45.238"
96+
insights_ip_stage = "23.53.5.13"
97+
stable_public_ip = "1.1.1.1" # Public CloudFlare DNS

0 commit comments

Comments
 (0)