-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change regex to filesystem policy matcher, first step of #871
- Loading branch information
Showing
19 changed files
with
292 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import os | ||
from typing import List, Union | ||
|
||
|
||
class ACCESS_MODE: | ||
NONE = 0 | ||
EXACT = 1 | ||
RECURSIVE = 2 | ||
|
||
|
||
class Dir: | ||
def __init__(self): | ||
self.access_mode = ACCESS_MODE.NONE | ||
self.map = {} | ||
|
||
|
||
class File: | ||
pass | ||
|
||
|
||
class ExactFile: | ||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
|
||
class ExactDir: | ||
access_mode = ACCESS_MODE.EXACT | ||
|
||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
|
||
class RecursiveDir: | ||
access_mode = ACCESS_MODE.RECURSIVE | ||
|
||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
|
||
Rule = Union[ExactFile, ExactDir, RecursiveDir] | ||
|
||
|
||
class FilesystemPolicy: | ||
def __init__(self, rules: List[Rule]): | ||
self.root = Dir() | ||
for rule in rules: | ||
self._add_rule(rule) | ||
|
||
def _add_rule(self, rule: Rule): | ||
self._assert_rule_type(rule) | ||
if rule.path == "/": | ||
return self._finalize_root_rule(rule) | ||
|
||
path = rule.path | ||
assert os.path.isabs(path), "Rule must specify an absolute path to rule" | ||
*directory_path, final_component = path.split("/")[1:] | ||
|
||
node = self.root | ||
for component in directory_path: | ||
assert component != "", "Must not have empty components in rule to add" | ||
new_node = node.map.setdefault(component, Dir()) | ||
assert isinstance(new_node, Dir), "Cannot descend into non-directory" | ||
node = new_node | ||
|
||
self._finalize_rule(node, final_component, rule) | ||
|
||
def _assert_rule_type(self, rule: Rule): | ||
if os.path.exists(rule.path): | ||
is_dir = os.path.isdir(rule.path) | ||
if isinstance(rule, ExactFile): | ||
assert not is_dir, f"Can't apply file rule to directory {rule.path}" | ||
else: | ||
assert is_dir, f"Can't apply directory rule to non-directory {rule.path}" | ||
|
||
def _finalize_root_rule(self, rule: Rule): | ||
assert not isinstance(rule, ExactFile), "Root is not a file" | ||
self._finalize_directory_rule(self.root, rule) | ||
|
||
def _finalize_rule(self, node: Dir, final_component: str, rule: Rule): | ||
assert final_component != "", "Must not have trailing slashes in rule path" | ||
if isinstance(rule, ExactFile): | ||
new_node = node.map.setdefault(final_component, File()) | ||
assert isinstance(new_node, File), "Can't add ExactFile: Dir rule exists" | ||
else: | ||
new_node = node.map.setdefault(final_component, Dir()) | ||
assert isinstance(new_node, Dir), "Can't add rule: File rule exists" | ||
self._finalize_directory_rule(new_node, rule) | ||
|
||
def _finalize_directory_rule(self, node: Dir, rule: Union[ExactDir, RecursiveDir]): | ||
node.access_mode = max(node.access_mode, rule.access_mode) # Allow the more permissive rule | ||
|
||
# `path` should be a canonical path. The output of `realpath` is appropriate | ||
def check(self, path: str) -> bool: | ||
if path == "/": | ||
return self._check_final_node(self.root) | ||
|
||
assert os.path.isabs(path), "Must pass an absolute path to check" | ||
|
||
node = self.root | ||
for component in path.split("/")[1:]: | ||
assert component != "", "Must not have empty components in path to check" | ||
if isinstance(node, File): | ||
return False | ||
elif node.access_mode == ACCESS_MODE.RECURSIVE: | ||
return True | ||
else: | ||
node = node.map.get(component) | ||
if node is None: | ||
return False | ||
|
||
return self._check_final_node(node) | ||
|
||
def _check_final_node(self, node: Union[Dir, File]) -> bool: | ||
return isinstance(node, File) or node.access_mode != ACCESS_MODE.NONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.