INNER CODE UNIT · Python
apply_rule
kejilion/sh · site_access_cli.py:99
def apply_rule(rootfd, request, *, validate=None, reload_config=None,
worker_group=nginx_worker_group, verify_read=check_worker_read, global_policy=nginx_global_config):
common = {'operation', 'configName', 'configVersion', 'scope', 'path', 'modifier'}
action = request.get('operation') if isinstance(request, dict) else None
needed = {'username', 'password', 'serverIndices'} if action == 'rule-add' else {'fileVersion', 'serverIndex'} if action == 'rule-delete' else set()
if action not in ('rule-add', 'rule-delete') or request.keys() != common | needed:
raise ConfigError('invalid rule request')
name = request['configName']
if not isinstance(name, str) or not re.fullmatch(r'[A-Za-z0-9_.-]{1,253}\.conf', name):
raise ConfigError('invalid configuration name')
validate = validate or (lambda: nginx_command('-t'))
reload_config = reload_config or (lambda: nginx_command('-s', 'reload'))
config_dir = directory_beneath(rootfd, ['conf.d'])
try:
if action == 'rule-add':
return add_rule(config_dir, name, request['configVersion'], request['serverIndices'],
request['scope'], request['path'], request['modifier'],
request['username'], request['password'], validate=validate,