Skip to content

Commit de8d4f3

Browse files
authored
Fix user fact comments wtih spaces parsing by refactoring fact (#597)
* Fix user fact comments wtih spaces parsing by refactoring fact * Fixed flake8 issues with whitespaces
1 parent 9f123b2 commit de8d4f3

File tree

2 files changed

+24
-41
lines changed

2 files changed

+24
-41
lines changed

pyinfra/facts/server.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -428,55 +428,38 @@ class Users(FactBase):
428428

429429
command = '''
430430
for i in `cat /etc/passwd | cut -d: -f1`; do
431-
ID=`id $i`;
432-
META=`cat /etc/passwd | grep ^$i: | cut -d: -f5-7`;
433-
echo "$ID $META";
431+
ENTRY=`grep ^$i: /etc/passwd`;
432+
echo "$ENTRY|`id -gn $i`|`id -Gn $i`";
434433
done
435434
'''.strip()
436435

437436
default = dict
438437

439-
regex = r'^uid=([0-9]+)\(([a-zA-Z0-9_\.\-]+)\) gid=([0-9]+)\(([a-zA-Z0-9_\.\-]+)\) groups=([a-zA-Z0-9_\.\-,\(\)\s]+) (.*)$' # noqa
440-
group_regex = r'^[0-9]+\(([a-zA-Z0-9_\.\-]+)\)$'
441-
442438
def process(self, output):
443439
users = {}
440+
444441
for line in output:
445-
matches = re.match(self.regex, line)
442+
entry, group, user_groups = line.split('|')
446443

447-
if matches:
444+
if entry:
448445
# Parse out the comment/home/shell
449-
comment_home_shell = matches.group(6).split(':')
450-
comment = comment_home_shell[0] or None
451-
home = comment_home_shell[1] or None
452-
shell = comment_home_shell[2] or None
446+
entries = entry.split(':')
453447

454-
# Main user group, uid & gid
455-
uid = int(matches.group(1))
456-
gid = int(matches.group(3))
457-
group = matches.group(4)
458-
459-
# Parse the groups
448+
# Parse groups
460449
groups = []
461-
for group_matches in matches.group(5).split(','):
462-
name = re.match(self.group_regex, group_matches.strip())
463-
if name:
464-
name = name.group(1)
465-
else:
466-
continue # pragma: no cover
467-
450+
for group_name in user_groups.split(' '):
468451
# We only want secondary groups here
469-
if name != group:
470-
groups.append(name)
452+
if group_name and group_name != group:
453+
groups.append(group_name)
471454

472-
users[matches.group(2)] = {
455+
users[entries[0]] = {
456+
'home': entries[5] or None,
457+
'comment': entries[4] or None,
458+
'shell': entries[6] or None,
473459
'group': group,
474460
'groups': groups,
475-
'comment': comment,
476-
'home': home,
477-
'shell': shell,
478-
'uid': uid,
479-
'gid': gid,
461+
'uid': int(entries[2]),
462+
'gid': int(entries[3]),
480463
}
481464

482465
return users

tests/facts/server.Users/mixed.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"output": [
3-
"uid=0(root) gid=0(wheel) groups=0(wheel), 2(kmem), 3(sys), 4(tty), 5(operator), 20(staff), 31(guest) root:/root:/bin/ks",
4-
"uid=1003(test.testy) gid=1003(test.testy) groups=1003(test.testy) Testy,,,:/home/test.testy:/bin/ksh",
5-
"uid=1004(_tesTy.test) gid=1004(_tesTy.test) groups=1004(_tesTy.test),1001(pyinfra) :/home/_tesTy.test:/bin/ksh",
6-
"uid=1002(noshell) gid=1002(noshell) groups=1002(noshell) noshell:/home/noshell:",
7-
"uid=1002(nohome) gid=1002(nohome) groups=1002(nohome) nohome::/bin/bash"
3+
"root:x:0:0:root:/root:/bin/ks|wheel|wheel kmem sys tty operator staff guest",
4+
"_tesTy.test:x:1004:1004::/home/_tesTy.test:/bin/ksh|_tesTy.test|_tesTy.test pyinfra",
5+
"test.testy:x:1003:1003:Testy,,,:/home/test.testy:/bin/ksh|test.testy|",
6+
"noshell:x:1002:1002:noshell comment with spaces:/home/noshell:|noshell|",
7+
"nohome:x:1002:1002:nohome comment::/bin/bash|nohome|"
88
],
9-
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ID=`id $i`;\n META=`cat /etc/passwd | grep ^$i: | cut -d: -f5-7`;\n echo \"$ID $META\";\n done",
9+
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ENTRY=`grep ^$i: /etc/passwd`;\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`\";\n done",
1010
"fact": {
1111
"root": {
1212
"home": "/root",
@@ -46,7 +46,7 @@
4646
},
4747
"noshell": {
4848
"home": "/home/noshell",
49-
"comment": "noshell",
49+
"comment": "noshell comment with spaces",
5050
"shell": null,
5151
"group": "noshell",
5252
"groups": [],
@@ -55,7 +55,7 @@
5555
},
5656
"nohome": {
5757
"home": null,
58-
"comment": "nohome",
58+
"comment": "nohome comment",
5959
"shell": "/bin/bash",
6060
"group": "nohome",
6161
"groups": [],

0 commit comments

Comments
 (0)