From fbaa55fbe8447745a20c99a68d62790f5dd5a0f7 Mon Sep 17 00:00:00 2001
From: Petr Vobornik <pvoborni@redhat.com>
Date: Thu, 3 Aug 2017 15:48:33 +0200
Subject: [PATCH] control logging of host_port_open from caller
host_port_open copied logging behavior of ipa-replica-conncheck utility
which doesn't make it much reusable.
Now log level can be controlled from caller so other callers might use
other logging level without host_port_open guessing what was the
intention.
https://pagure.io/freeipa/issue/7083
Reviewed-By: Tomas Krizek <tkrizek@redhat.com>
---
install/tools/ipa-replica-conncheck | 9 ++++++++-
ipapython/ipautil.py | 17 ++++++-----------
2 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/install/tools/ipa-replica-conncheck b/install/tools/ipa-replica-conncheck
index 528242268f9992e903781b76a379039d533853c0..f10b7e3d2f94540dba3956bf460c4b9f38da90da 100755
--- a/install/tools/ipa-replica-conncheck
+++ b/install/tools/ipa-replica-conncheck
@@ -46,6 +46,8 @@ import distutils.spawn
from ipaplatform.paths import paths
import gssapi
from cryptography.hazmat.primitives import serialization
+import logging
+
CONNECT_TIMEOUT = 5
RESPONDER = None
@@ -379,11 +381,16 @@ class PortResponder(threading.Thread):
def port_check(host, port_list):
ports_failed = []
ports_udp_warning = [] # conncheck could not verify that port is open
+ log_level = {
+ SOCK_DGRAM: logging.WARNING,
+ SOCK_STREAM: logging.ERROR
+ }
for port in port_list:
try:
port_open = ipautil.host_port_open(
host, port.port, port.port_type,
- socket_timeout=CONNECT_TIMEOUT, log_errors=True)
+ socket_timeout=CONNECT_TIMEOUT, log_errors=True,
+ log_level=log_level[port.port_type])
except socket.gaierror:
raise RuntimeError("Port check failed! Unable to resolve host name '%s'" % host)
if port_open:
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index 5a6bf5a27d5a6e25c51fbaa6e2b1167652e2735d..e1e6e32b15559486caecb070627db82e14a57bdf 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -42,6 +42,7 @@ from contextlib import contextmanager
import locale
import collections
from subprocess import CalledProcessError
+import logging
from dns import resolver, reversename
from dns.exception import DNSException
@@ -948,7 +949,8 @@ def user_input(prompt, default = None, allow_empty = True):
def host_port_open(host, port, socket_type=socket.SOCK_STREAM,
- socket_timeout=None, log_errors=False):
+ socket_timeout=None, log_errors=False,
+ log_level=logging.DEBUG):
"""
host: either hostname or IP address;
if hostname is provided, port MUST be open on ALL resolved IPs
@@ -970,23 +972,16 @@ def host_port_open(host, port, socket_type=socket.SOCK_STREAM,
s.connect(sa)
if socket_type == socket.SOCK_DGRAM:
- s.send('')
+ s.send(b'')
s.recv(512)
except socket.error:
port_open = False
-
if log_errors:
- msg = ('Failed to connect to port %(port)d %(proto)s on '
+ msg = ('Failed to connect to port %(port)s %(proto)s on '
'%(addr)s' % dict(port=port,
proto=PROTOCOL_NAMES[socket_type],
addr=sa[0]))
-
- # Do not log udp failures as errors (to be consistent with
- # the rest of the code that checks for open ports)
- if socket_type == socket.SOCK_DGRAM:
- root_logger.warning(msg)
- else:
- root_logger.error(msg)
+ root_logger.log(log_level, msg)
finally:
if s is not None:
s.close()
--
2.13.5