Skip to content

Commit

Permalink
parser_util.py: Update perm_handler with more error checking
Browse files Browse the repository at this point in the history
Update perm_handler() to have stricter error checking by using regex's
for both the perm string format and the perm octal-in-a-string format.
  • Loading branch information
morrone authored and tom95858 committed Dec 19, 2024
1 parent 18112af commit 5120368
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions ldms/python/ldmsd/parser_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import errno
import json
import subprocess
import re
import socket
import time
import itertools as it
Expand Down Expand Up @@ -173,32 +174,36 @@ def perm_handler(perm_str):
return perm_str
if type(perm_str) is not str:
raise TypeError(f'Error: YAML "perms" value must be a string')
nperm = '0'
if len(perm_str.split('-')) > 1:
if len(perm_str) != 9:
raise ValueError(f'Error: Parsing YAML permisson string "{perm_str}"\n'
f'When using unix-like vernacular to configure permissions you must account for read/write in user/group/global\n'
f'Allowed format: (r|-)(w|-)-(r|-)(w|-)-(r|-)(w|-)-')
i = 0
x = 0
for ch in perm_str:
if i == 2:
nperm += str(x)
x = 0
i = 0
continue
if ch == 'r':
x += 4
if ch == 'w' or ch == '+':
x += 2
i += 1
perms = 0
m = perm_handler.string_pattern.fullmatch(perm_str)
if m:
if m.group(1):
perms += 0o400
if m.group(2):
perms += 0o200
if m.group(3):
perms += 0o040
if m.group(4):
perms += 0o020
if m.group(5):
perms += 0o004
if m.group(6):
perms += 0o002
else:
try:
z = int(perm_str)
return perm_str
except:
raise ValueError(f'Error: permission {perm_str} is not a valid value')
return nperm
m = perm_handler.octal_pattern.fullmatch(perm_str)
if m:
try:
perms = int(perm_str, base=8)
except:
raise ValueError(f'Error: permission string \"{perm_str}\" is not a valid octal')
else:
raise ValueError(f'Error: YAML permisson string "{perm_str}"\n'
f'Must represent either a valid octal number, or a use unix-like vernacular\n'
f'Allowed format: (r|-)(w|-)-(r|-)(w|-)-(r|-)(w|-)-')

return '0'+oct(perms)[2:]
perm_handler.string_pattern = re.compile('(?:(r)|-)(?:(w)|-)-(?:(r)|-)(?:(w)|-)-(?:(r)|-)(?:(w)|-)-')
perm_handler.octal_pattern = re.compile('(0|0o)?[0-7]{1,3}')

class YamlCfg(object):
def build_daemons(self, config):
Expand Down

0 comments on commit 5120368

Please sign in to comment.