Commit 08abb49d authored by Elena Grandi's avatar Elena Grandi

flake8

parent f1abb36d
......@@ -21,6 +21,7 @@ except ImportError:
# creating Futures has changed in python 3.5 with the availability of
# the method loop.create_future()
def create_future(loop):
f = getattr(loop, 'create_future', None)
if f is not None:
......@@ -32,6 +33,7 @@ def create_future(loop):
# Starting from python 3.7 the preferred way to create new Tasks is to
# use asyncio.create_task()
def create_task(coro):
try:
return asyncio.create_task(coro)
......
......@@ -57,8 +57,8 @@ class ArpwatchDataSource(MachineDataSourceMixin, DataSource):
async def parse_file(self):
with open(self.fname) as fp:
for l in fp:
parts = l.split()
for line in fp:
parts = line.split()
ts = int(parts[2])
mac = parts[0]
ip = parts[1]
......@@ -69,5 +69,5 @@ class ArpwatchDataSource(MachineDataSourceMixin, DataSource):
else:
logging.warning(
"Found unparseable arpwatch line: %s",
l
line
)
......@@ -11,6 +11,7 @@ class LDAP(db.DB):
"""
Lightweight asynchronous interface to LDAP
"""
def __init__(self, config):
self.uri = config.ldap_uri
self.user_search_base = config.ldap_user_search_base
......@@ -37,29 +38,45 @@ class LDAP(db.DB):
log.info("ldap_group_search_base not found in configuration")
return False
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, cls._sync_try_connect, config.ldap_uri)
return await loop.run_in_executor(
None, cls._sync_try_connect, config.ldap_uri
)
def _sync_authenticate(self, bind_dn, password):
try:
with ldap3.Connection(self.uri, auto_bind=True, user=bind_dn, password=password) as conn:
conn.search(bind_dn, "(objectclass=*)", attributes=ldap3.ALL_ATTRIBUTES)
with ldap3.Connection(
self.uri, auto_bind=True, user=bind_dn, password=password
) as conn:
conn.search(
bind_dn, "(objectclass=*)", attributes=ldap3.ALL_ATTRIBUTES
)
res = db.User(
name=conn.entries[0].uid.value,
id=conn.entries[0].uidNumber.value,
display_name=conn.entries[0].cn.value
name=conn.entries[0].uid.value,
id=conn.entries[0].uidNumber.value,
display_name=conn.entries[0].cn.value,
)
# res.entry = conn.entries[0]
primary_gid = conn.entries[0].gidNumber.value
conn.search(self.group_search_base, "(gidNumber={})".format(primary_gid),
attributes="cn")
conn.search(
self.group_search_base,
"(gidNumber={})".format(primary_gid),
attributes="cn",
)
for e in conn.entries:
res.groups.append(db.Group(name=e.cn.value, id=primary_gid))
res.groups.append(
db.Group(name=e.cn.value, id=primary_gid)
)
conn.search(self.group_search_base, "(memberUid={})".format(res.name),
attributes=["cn", "gidNumber"])
conn.search(
self.group_search_base,
"(memberUid={})".format(res.name),
attributes=["cn", "gidNumber"],
)
for e in conn.entries:
res.groups.append(db.Group(name=e.cn.value, id=e.gidNumber.value))
res.groups.append(
db.Group(name=e.cn.value, id=e.gidNumber.value)
)
return res
except ldap3.core.exceptions.LDAPBindError as e:
......@@ -68,4 +85,6 @@ class LDAP(db.DB):
async def authenticate(self, user: str, password: str) -> db.User:
loop = asyncio.get_running_loop()
bind_dn = "uid={},{}".format(user, self.user_search_base)
return await loop.run_in_executor(None, self._sync_authenticate, bind_dn, password)
return await loop.run_in_executor(
None, self._sync_authenticate, bind_dn, password
)
......@@ -16,6 +16,7 @@ class Local(db.DB):
"""
Lightweight asynchronous interface to the local Unix user/group database
"""
def __init__(self, config):
pass
......@@ -50,9 +51,13 @@ class Local(db.DB):
try:
cryptedpasswd = spwd.getspnam(user)[1]
except KeyError:
raise db.AuthenticationError("User does not exist in shadow database")
raise db.AuthenticationError(
"User does not exist in shadow database"
)
except PermissionError:
raise db.AuthenticationError("Shadow password database is not accessible")
raise db.AuthenticationError(
"Shadow password database is not accessible"
)
if cryptedpasswd in ["NP", "!", "", None]:
raise db.AuthenticationError("user has no password set")
......@@ -61,7 +66,9 @@ class Local(db.DB):
if cryptedpasswd == "!!":
raise db.AuthenticationError("password has expired")
if not compare_digest(crypt.crypt(password, cryptedpasswd), cryptedpasswd):
if not compare_digest(
crypt.crypt(password, cryptedpasswd), cryptedpasswd
):
raise db.AuthenticationError("invalid password")
display_name = userinfo.pw_gecos.split(",")[0]
......@@ -69,14 +76,23 @@ class Local(db.DB):
try:
ginfo = grp.getgrgid(userinfo.pw_gid)
except KeyError:
raise db.AuthenticationError("primary group ID does not exist in group database")
raise db.AuthenticationError(
"primary group ID does not exist in group database"
)
groups.append(db.Group(name=ginfo.gr_name, id=userinfo.pw_gid))
for ginfo in grp.getgrall():
if user in ginfo.gr_mem:
groups.append(db.Group(name=ginfo.gr_name, id=ginfo.gr_gid))
return db.User(name=user, id=userinfo.pw_uid, display_name=display_name, groups=groups)
return db.User(
name=user,
id=userinfo.pw_uid,
display_name=display_name,
groups=groups,
)
async def authenticate(self, user: str, password: str) -> db.User:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, self._sync_authenticate, user, password)
return await loop.run_in_executor(
None, self._sync_authenticate, user, password
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment