users: Avoid error when user's groups cannot be parsed

Add log warnings to help debug if there is a related issue.

May help #1834.

Tested:
- Run action command with valid and invalid username. Warning is printed for
  invalid username.
- Modify the output to remove '='. Warning is printed instead of exception.
- Ensure that warnings messages are output to stderr and not stdout.
- On frontpage.py change the call to get user groups and ensure that that output
  warning messages are not parsed groups.

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
[sunil: Drop module logger as root logger is at use]
[sunil: Use %s formatting for logging API]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy 2020-05-29 10:53:07 -04:00 committed by Sunil Mohan Adapa
parent b524cbe399
commit 1aaf5efb52
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2

View File

@ -5,6 +5,7 @@ Configuration helper for the LDAP user directory
"""
import argparse
import logging
import os
import re
import shutil
@ -12,6 +13,7 @@ import subprocess
import sys
import augeas
from plinth import action_utils
ACCESS_CONF = '/etc/security/access.conf'
@ -200,8 +202,8 @@ def configure_ldapscripts():
# modify a copy of the config file
shutil.copy('/etc/ldapscripts/ldapscripts.conf', LDAPSCRIPTS_CONF)
aug = augeas.Augeas(
flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD)
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns')
aug.set('/augeas/load/Shellvars/incl[last() + 1]', LDAPSCRIPTS_CONF)
aug.load()
@ -325,13 +327,20 @@ def get_user_groups(username):
output = process.stdout.decode().strip()
if output:
groups_part = output.split(' ')[2]
groups = groups_part.split('=')[1]
try:
groups = groups_part.split('=')[1]
except IndexError:
logging.warning('Could not read groups for user %s: \n%s',
username, output)
return []
group_names = [
user.strip('()') for user in re.findall(r'\(.*?\)', groups)
]
group_names.remove('users')
return group_names
logging.warning('User %s not found in LDAP', username)
return []