Skip to content

Commit 7a74fb8

Browse files
committed
fixed fipsEanbledSillyCase; and the resulting psf-black/pylint waterfall
1 parent d5c270f commit 7a74fb8

File tree

2 files changed

+57
-107
lines changed

2 files changed

+57
-107
lines changed

hubblestack/modules/network.py

+56-105
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from hubblestack.exceptions import CommandExecutionError
2323

2424
log = logging.getLogger(__name__)
25-
isFipsEnabled = True if 'usedforsecurity' in getfullargspec(hashlib.new).kwonlyargs else False
25+
IS_FIPS_ENABLED = True if "usedforsecurity" in getfullargspec(hashlib.new).kwonlyargs else False
26+
2627

2728
def __virtual__():
2829
"""
@@ -85,13 +86,9 @@ def ping(host, timeout=False, return_boolean=False):
8586
"""
8687
if timeout:
8788
if __grains__["kernel"] == "SunOS":
88-
cmd = "ping -c 4 {1} {0}".format(
89-
timeout, __utils__["network.sanitize_host"](host)
90-
)
89+
cmd = "ping -c 4 {1} {0}".format(timeout, __utils__["network.sanitize_host"](host))
9190
else:
92-
cmd = "ping -W {0} -c 4 {1}".format(
93-
timeout, __utils__["network.sanitize_host"](host)
94-
)
91+
cmd = "ping -W {0} -c 4 {1}".format(timeout, __utils__["network.sanitize_host"](host))
9592
else:
9693
cmd = "ping -c 4 {0}".format(__utils__["network.sanitize_host"](host))
9794
if return_boolean:
@@ -212,10 +209,7 @@ def _netinfo_openbsd():
212209
Get process information for network connections using fstat
213210
"""
214211
ret = {}
215-
_fstat_re = re.compile(
216-
r"internet(6)? (?:stream tcp 0x\S+ (\S+)|dgram udp (\S+))"
217-
r"(?: [<>=-]+ (\S+))?$"
218-
)
212+
_fstat_re = re.compile(r"internet(6)? (?:stream tcp 0x\S+ (\S+)|dgram udp (\S+))" r"(?: [<>=-]+ (\S+))?$")
219213
out = __mods__["cmd.run"]("fstat")
220214
for line in out.splitlines():
221215
try:
@@ -245,9 +239,9 @@ def _netinfo_openbsd():
245239
else:
246240
remote_addr = ".".join(remote_addr.rsplit(":", 1))
247241

248-
ret.setdefault(local_addr, {}).setdefault(remote_addr, {}).setdefault(
249-
proto, {}
250-
).setdefault(pid, {})["user"] = user
242+
ret.setdefault(local_addr, {}).setdefault(remote_addr, {}).setdefault(proto, {}).setdefault(pid, {})[
243+
"user"
244+
] = user
251245
ret[local_addr][remote_addr][proto][pid]["cmd"] = cmd
252246
return ret
253247

@@ -259,18 +253,16 @@ def _netinfo_freebsd_netbsd():
259253
ret = {}
260254
# NetBSD requires '-n' to disable port-to-service resolution
261255
out = __mods__["cmd.run"](
262-
"sockstat -46 {0} | tail -n+2".format(
263-
"-n" if __grains__["kernel"] == "NetBSD" else ""
264-
),
256+
"sockstat -46 {0} | tail -n+2".format("-n" if __grains__["kernel"] == "NetBSD" else ""),
265257
python_shell=True,
266258
)
267259
for line in out.splitlines():
268260
user, cmd, pid, _, proto, local_addr, remote_addr = line.split()
269261
local_addr = ".".join(local_addr.rsplit(":", 1))
270262
remote_addr = ".".join(remote_addr.rsplit(":", 1))
271-
ret.setdefault(local_addr, {}).setdefault(remote_addr, {}).setdefault(
272-
proto, {}
273-
).setdefault(pid, {})["user"] = user
263+
ret.setdefault(local_addr, {}).setdefault(remote_addr, {}).setdefault(proto, {}).setdefault(pid, {})[
264+
"user"
265+
] = user
274266
ret[local_addr][remote_addr][proto][pid]["cmd"] = cmd
275267
return ret
276268

@@ -364,9 +356,7 @@ def _netstat_bsd():
364356
try:
365357
# Master pid for this connection will be the pid whose ppid isn't
366358
# in the subset dict we created above
367-
master_pid = next(
368-
iter(x for x, y in conn_ppid.items() if y not in ptr)
369-
)
359+
master_pid = next(iter(x for x, y in conn_ppid.items() if y not in ptr))
370360
except StopIteration:
371361
continue
372362
ret[idx]["user"] = ptr[master_pid]["user"]
@@ -636,10 +626,7 @@ def _netstat_route_freebsd():
636626
out = __mods__["cmd.run"](cmd, python_shell=True)
637627
for line in out.splitlines():
638628
comps = line.split()
639-
if (
640-
__grains__["os"] == "FreeBSD"
641-
and int(__grains__.get("osmajorrelease", 0)) < 10
642-
):
629+
if __grains__["os"] == "FreeBSD" and int(__grains__.get("osmajorrelease", 0)) < 10:
643630
ret.append(
644631
{
645632
"addr_family": "inet",
@@ -939,16 +926,14 @@ def traceroute(host):
939926
# Modern traceroute for Linux, version 2.0.19, Dec 10 2012
940927
# Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin]
941928

942-
version_raw = re.findall(
943-
r".*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)", version_out
944-
)[0]
929+
version_raw = re.findall(r".*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)", version_out)[0]
945930
log.debug("traceroute_version_raw: %s", version_raw)
946931
traceroute_version = []
947-
for t in version_raw:
932+
for i in version_raw:
948933
try:
949-
traceroute_version.append(int(t))
934+
traceroute_version.append(int(i))
950935
except ValueError:
951-
traceroute_version.append(t)
936+
traceroute_version.append(i)
952937

953938
if len(traceroute_version) < 3:
954939
traceroute_version.append(0)
@@ -1272,9 +1257,7 @@ def ip_addrs(interface=None, include_loopback=False, cidr=None, type=None):
12721257
12731258
salt '*' network.ip_addrs
12741259
"""
1275-
addrs = __utils__["network.ip_addrs"](
1276-
interface=interface, include_loopback=include_loopback
1277-
)
1260+
addrs = __utils__["network.ip_addrs"](interface=interface, include_loopback=include_loopback)
12781261
if cidr:
12791262
return [i for i in addrs if __utils__["network.in_subnet"](cidr, [i])]
12801263
else:
@@ -1285,7 +1268,6 @@ def ip_addrs(interface=None, include_loopback=False, cidr=None, type=None):
12851268
return addrs
12861269

12871270

1288-
12891271
def ip_addrs6(interface=None, include_loopback=False, cidr=None):
12901272
"""
12911273
Returns a list of IPv6 addresses assigned to the host. ::1 is ignored,
@@ -1304,9 +1286,7 @@ def ip_addrs6(interface=None, include_loopback=False, cidr=None):
13041286
13051287
salt '*' network.ip_addrs6
13061288
"""
1307-
addrs = __utils__["network.ip_addrs6"](
1308-
interface=interface, include_loopback=include_loopback
1309-
)
1289+
addrs = __utils__["network.ip_addrs6"](interface=interface, include_loopback=include_loopback)
13101290
if cidr:
13111291
return [i for i in addrs if __utils__["network.in_subnet"](cidr, [i])]
13121292
return addrs
@@ -1365,15 +1345,9 @@ def mod_hostname(hostname):
13651345
if hostname is None:
13661346
return False
13671347

1368-
hostname_cmd = __utils__["path.which"]("hostnamectl") or __utils__["path.which"](
1369-
"hostname"
1370-
)
1348+
hostname_cmd = __utils__["path.which"]("hostnamectl") or __utils__["path.which"]("hostname")
13711349
if __utils__["platform.is_sunos"]():
1372-
uname_cmd = (
1373-
"/usr/bin/uname"
1374-
if __utils__["platform.is_smartos"]()
1375-
else __utils__["path.which"]("uname")
1376-
)
1350+
uname_cmd = "/usr/bin/uname" if __utils__["platform.is_smartos"]() else __utils__["path.which"]("uname")
13771351
check_hostname_cmd = __utils__["path.which"]("check-hostname")
13781352

13791353
# Grab the old hostname so we know which hostname to change and then
@@ -1398,12 +1372,16 @@ def mod_hostname(hostname):
13981372

13991373
if hostname_cmd.endswith("hostnamectl"):
14001374
result = __mods__["cmd.run_all"](
1401-
"{0} set-hostname {1}".format(hostname_cmd, hostname,)
1375+
"{0} set-hostname {1}".format(
1376+
hostname_cmd,
1377+
hostname,
1378+
)
14021379
)
14031380
if result["retcode"] != 0:
14041381
log.debug(
14051382
"{0} was unable to set hostname. Error: {1}".format(
1406-
hostname_cmd, result["stderr"],
1383+
hostname_cmd,
1384+
result["stderr"],
14071385
)
14081386
)
14091387
return False
@@ -1435,9 +1413,7 @@ def mod_hostname(hostname):
14351413
# new hostname
14361414
if __grains__["os_family"] == "RedHat":
14371415
with __utils__["files.fopen"]("/etc/sysconfig/network", "r") as fp_:
1438-
network_c = [
1439-
__utils__["stringutils.to_unicode"](_l) for _l in fp_.readlines()
1440-
]
1416+
network_c = [__utils__["stringutils.to_unicode"](_l) for _l in fp_.readlines()]
14411417

14421418
with __utils__["files.fopen"]("/etc/sysconfig/network", "w") as fh_:
14431419
for net in network_c:
@@ -1446,9 +1422,7 @@ def mod_hostname(hostname):
14461422
quote_type = __utils__["stringutils.is_quoted"](old_hostname)
14471423
fh_.write(
14481424
__utils__["stringutils.to_str"](
1449-
"HOSTNAME={1}{0}{1}\n".format(
1450-
__utils__["stringutils.dequote"](hostname), quote_type
1451-
)
1425+
"HOSTNAME={1}{0}{1}\n".format(__utils__["stringutils.dequote"](hostname), quote_type)
14521426
)
14531427
)
14541428
else:
@@ -1459,13 +1433,9 @@ def mod_hostname(hostname):
14591433
if __grains__["lsb_distrib_id"] == "nilrt":
14601434
str_hostname = __utils__["stringutils.to_str"](hostname)
14611435
nirtcfg_cmd = "/usr/local/natinst/bin/nirtcfg"
1462-
nirtcfg_cmd += " --set section=SystemSettings,token='Host_Name',value='{0}'".format(
1463-
str_hostname
1464-
)
1436+
nirtcfg_cmd += " --set section=SystemSettings,token='Host_Name',value='{0}'".format(str_hostname)
14651437
if __mods__["cmd.run_all"](nirtcfg_cmd)["retcode"] != 0:
1466-
raise CommandExecutionError(
1467-
"Couldn't set hostname to: {0}\n".format(str_hostname)
1468-
)
1438+
raise CommandExecutionError("Couldn't set hostname to: {0}\n".format(str_hostname))
14691439
elif __grains__["os_family"] == "OpenBSD":
14701440
with __utils__["files.fopen"]("/etc/myname", "w") as fh_:
14711441
fh_.write(__utils__["stringutils.to_str"](hostname + "\n"))
@@ -1475,11 +1445,7 @@ def mod_hostname(hostname):
14751445
with __utils__["files.fopen"]("/etc/nodename", "w") as fh_:
14761446
fh_.write(__utils__["stringutils.to_str"](hostname.split(".")[0] + "\n"))
14771447
with __utils__["files.fopen"]("/etc/defaultdomain", "w") as fh_:
1478-
fh_.write(
1479-
__utils__["stringutils.to_str"](
1480-
".".join(hostname.split(".")[1:]) + "\n"
1481-
)
1482-
)
1448+
fh_.write(__utils__["stringutils.to_str"](".".join(hostname.split(".")[1:]) + "\n"))
14831449

14841450
return True
14851451

@@ -1520,9 +1486,7 @@ def connect(host, port=None, **kwargs):
15201486
timeout = kwargs.get("timeout", 5)
15211487
family = kwargs.get("family", None)
15221488

1523-
if hubblestack.utils.validate.net.ipv4_addr(host) or hubblestack.utils.validate.net.ipv6_addr(
1524-
host
1525-
):
1489+
if hubblestack.utils.validate.net.ipv4_addr(host) or hubblestack.utils.validate.net.ipv6_addr(host):
15261490
address = host
15271491
else:
15281492
address = "{0}".format(__utils__["network.sanitize_host"](host))
@@ -1544,14 +1508,10 @@ def connect(host, port=None, **kwargs):
15441508
else:
15451509
__family = 0
15461510

1547-
(family, socktype, _proto, garbage, _address) = socket.getaddrinfo(
1548-
address, port, __family, 0, __proto
1549-
)[0]
1511+
(family, socktype, _proto, garbage, _address) = socket.getaddrinfo(address, port, __family, 0, __proto)[0]
15501512
except socket.gaierror:
15511513
ret["result"] = False
1552-
ret["comment"] = "Unable to resolve host {0} on {1} port {2}".format(
1553-
host, proto, port
1554-
)
1514+
ret["comment"] = "Unable to resolve host {0} on {1} port {2}".format(host, proto, port)
15551515
return ret
15561516

15571517
try:
@@ -1561,7 +1521,7 @@ def connect(host, port=None, **kwargs):
15611521
if proto == "udp":
15621522
# Generate a random string of a
15631523
# decent size to test UDP connection
1564-
if isFipsEnabled:
1524+
if IS_FIPS_ENABLED:
15651525
md5h = hashlib.md5(usedforsecurity=False)
15661526
else:
15671527
md5h = hashlib.md5()
@@ -1575,15 +1535,11 @@ def connect(host, port=None, **kwargs):
15751535
skt.shutdown(2)
15761536
except Exception as exc: # pylint: disable=broad-except
15771537
ret["result"] = False
1578-
ret["comment"] = "Unable to connect to {0} ({1}) on {2} port {3}".format(
1579-
host, _address[0], proto, port
1580-
)
1538+
ret["comment"] = "Unable to connect to {0} ({1}) on {2} port {3}".format(host, _address[0], proto, port)
15811539
return ret
15821540

15831541
ret["result"] = True
1584-
ret["comment"] = "Successfully connected to {0} ({1}) on {2} port {3}".format(
1585-
host, _address[0], proto, port
1586-
)
1542+
ret["comment"] = "Successfully connected to {0} ({1}) on {2} port {3}".format(host, _address[0], proto, port)
15871543
return ret
15881544

15891545

@@ -1694,9 +1650,7 @@ def _mod_bufsize_linux(iface, *args, **kwargs):
16941650
if not kwargs:
16951651
return ret
16961652
if args:
1697-
ret["comment"] = "Unknown arguments: " + " ".join(
1698-
[str(item) for item in args]
1699-
)
1653+
ret["comment"] = "Unknown arguments: " + " ".join([str(item) for item in args])
17001654
return ret
17011655
eargs = ""
17021656
for kw in ["rx", "tx", "rx-mini", "rx-jumbo"]:
@@ -1800,13 +1754,17 @@ def default_route(family=None):
18001754
if __grains__["kernel"] == "Linux":
18011755
default_route["inet"] = ["0.0.0.0", "default"]
18021756
default_route["inet6"] = ["::/0", "default"]
1803-
elif __grains__["os"] in [
1804-
"FreeBSD",
1805-
"NetBSD",
1806-
"OpenBSD",
1807-
"MacOS",
1808-
"Darwin",
1809-
] or __grains__["kernel"] in ("SunOS", "AIX"):
1757+
elif (
1758+
__grains__["os"]
1759+
in [
1760+
"FreeBSD",
1761+
"NetBSD",
1762+
"OpenBSD",
1763+
"MacOS",
1764+
"Darwin",
1765+
]
1766+
or __grains__["kernel"] in ("SunOS", "AIX")
1767+
):
18101768
default_route["inet"] = ["default"]
18111769
default_route["inet6"] = ["default"]
18121770
else:
@@ -1820,10 +1778,7 @@ def default_route(family=None):
18201778
continue
18211779
ret.append(route)
18221780
else:
1823-
if (
1824-
route["destination"] in default_route["inet"]
1825-
or route["destination"] in default_route["inet6"]
1826-
):
1781+
if route["destination"] in default_route["inet"] or route["destination"] in default_route["inet6"]:
18271782
ret.append(route)
18281783

18291784
return ret
@@ -2015,9 +1970,7 @@ def ip_networks(interface=None, include_loopback=False, verbose=False):
20151970
salt '*' network.list_networks interface=docker0,enp*
20161971
salt '*' network.list_networks interface=eth*
20171972
"""
2018-
return __utils__["network.ip_networks"](
2019-
interface=interface, include_loopback=include_loopback, verbose=verbose
2020-
)
1973+
return __utils__["network.ip_networks"](interface=interface, include_loopback=include_loopback, verbose=verbose)
20211974

20221975

20231976
def ip_networks6(interface=None, include_loopback=False, verbose=False):
@@ -2040,9 +1993,7 @@ def ip_networks6(interface=None, include_loopback=False, verbose=False):
20401993
salt '*' network.list_networks6 interface=docker0,enp*
20411994
salt '*' network.list_networks6 interface=eth*
20421995
"""
2043-
return __utils__["network.ip_networks6"](
2044-
interface=interface, include_loopback=include_loopback, verbose=verbose
2045-
)
1996+
return __utils__["network.ip_networks6"](interface=interface, include_loopback=include_loopback, verbose=verbose)
20461997

20471998

20481999
def fqdns():
@@ -2054,8 +2005,8 @@ def fqdns():
20542005
# fqdns
20552006

20562007
# Possible value for h_errno defined in netdb.h
2057-
HOST_NOT_FOUND = 1
2058-
NO_DATA = 4
2008+
HOST_NOT_FOUND = 1 ## pylint: disable=invalid-name
2009+
NO_DATA = 4 ## pylint: disable=invalid-name
20592010

20602011
fqdns = set()
20612012

pylintrc

+1-2
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,7 @@ function-naming-style=snake_case
641641
#function-rgx=
642642

643643
# Good variable names which should always be accepted, separated by a comma
644-
good-names=i, f, j, k, e, v, x, dt, fh, ofh, ifh, ex, Run, _, log
645-
644+
good-names=a, i, f, j, k, m, e, v, x, kw, ip, dt, fh, ofh, ifh, ex, Run, _, log
646645

647646
# Include a hint for the correct naming format with invalid-name
648647
include-naming-hint=no

0 commit comments

Comments
 (0)