-
Notifications
You must be signed in to change notification settings - Fork 78
Acl #1628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Acl #1628
Changes from all commits
e71e203
dcc6031
5924a0c
fc9ec32
5b9d2da
2199eb8
7b7ae54
aa8a8c7
150cc9c
7fc04f7
3e8a749
04b35e5
7ebef5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,5 +3,3 @@ | |
|
|
||
| cd ui | ||
| npx lint-staged | ||
| ruff check | ||
| ruff format --check | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import logging | ||
|
|
||
| from flask import abort | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TRN: | ||
|
pirlgon marked this conversation as resolved.
|
||
| """ | ||
| TRN represent a temboard role/ressource name. | ||
| It's a kind of path separated by `:`. | ||
|
|
||
| Alice user: | ||
| trn:temboard:core:user:alice | ||
|
|
||
| pg001.bridoulou.fr instance from prod environment: | ||
| trn:temboard:core:instance:prod/pg001.bridoulou.fr:5432 | ||
| """ | ||
|
|
||
| def __init__(self, scope, type, name): | ||
| self.scope = scope | ||
| self.type = type | ||
| self.name = name | ||
|
|
||
| def __eq__(self, value): | ||
| return str(self) == str(value) | ||
|
|
||
| def __hash__(self): | ||
| return hash(str(self)) | ||
|
|
||
| @classmethod | ||
| def parse(cls, trn): | ||
| elems = str.split(trn, ":") | ||
| if len(elems) < 5: | ||
| raise Exception("Malformed TRN") | ||
| return cls(elems[2], elems[3], elems[4]) | ||
|
|
||
| def __str__(self): | ||
| return f"trn:temboard:{self.scope}:{self.type}:{self.name}" | ||
|
|
||
| @property | ||
| def parent(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would IMO be a good candidate for Then it would be called like this: |
||
| parent = TRN(self.scope, self.type, self.name) | ||
|
|
||
| if self.name != "*": | ||
| parent.name = "*" | ||
| if "/" in self.name: | ||
| names = str.split(self.name, "/") | ||
| parent.name = "/".join(names[:-1]) | ||
| return parent | ||
| if self.type != "*": | ||
| parent.type = "*" | ||
| return parent | ||
| parent.scope = "*" | ||
| return parent | ||
|
|
||
| @property | ||
| def parents(self): | ||
| trns = [] | ||
| trn = self | ||
| while str(trn) != "trn:temboard:*:*:*": | ||
| if trn not in trns: | ||
| trns.append(trn) | ||
| trn = trn.parent | ||
| trns.append(trn) | ||
| trns.append("*") | ||
| return trns | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of returning a list of strings, why not returning a list of TRN instances? |
||
|
|
||
|
|
||
| class ACLResult: | ||
| def __init__(self, role, action, resource, decision="allowed", statements=None): | ||
| self.role = role | ||
| self.action = action | ||
| self.resource = resource | ||
| self.decision = decision | ||
| self.statements = statements or [] | ||
|
|
||
| def raise_for_decision(self): | ||
| log_prefix = "Access <%s %s on %s> " | ||
| log_args = (self.role, self.action, self.resource or "*") | ||
|
|
||
| if self.decision == "allowed": | ||
| logger.debug( | ||
| log_prefix + "allowed by %s", | ||
| *log_args, | ||
| ", ".join(repr(s) for s in self.statements), | ||
| ) | ||
| return True | ||
| else: | ||
| if self.decision == "implicitDeny": | ||
| logger.debug(log_prefix + "implicitly denied.", *log_args) | ||
| else: | ||
| logger.debug( | ||
| log_prefix + "denied by %s", | ||
| *log_args, | ||
| ", ".join(repr(s) for s in self.statements if s.deny), | ||
| ) | ||
| raise abort(403) | ||
|
|
||
|
|
||
| def expand_actions(action): | ||
| """Returns the list of pattern relevant for this action.""" | ||
| actions = ["*"] | ||
| if action != "*": | ||
| method, _, endpoint = action.partition(":") | ||
| if method != "*": | ||
| actions.append("*:" + endpoint) | ||
| elif endpoint != "*": | ||
| actions.append(method + ":*") | ||
| actions.append(action) | ||
| return actions | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.