2020-02-19 08:49:38 +00:00
|
|
|
"""passbook expression policy evaluator"""
|
2020-10-20 10:05:56 +00:00
|
|
|
from ipaddress import ip_address, ip_network
|
2020-06-05 10:00:27 +00:00
|
|
|
from typing import List
|
2020-02-19 08:49:38 +00:00
|
|
|
|
2020-06-01 13:25:38 +00:00
|
|
|
from django.http import HttpRequest
|
2020-02-19 08:49:38 +00:00
|
|
|
from structlog import get_logger
|
|
|
|
|
2020-05-08 14:10:27 +00:00
|
|
|
from passbook.flows.planner import PLAN_CONTEXT_SSO
|
2020-06-05 10:00:27 +00:00
|
|
|
from passbook.lib.expression.evaluator import BaseEvaluator
|
2020-02-22 18:26:16 +00:00
|
|
|
from passbook.lib.utils.http import get_client_ip
|
2020-02-20 12:52:05 +00:00
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
2020-02-19 08:49:38 +00:00
|
|
|
|
2020-02-19 15:19:02 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
2020-02-19 08:49:38 +00:00
|
|
|
|
2020-06-05 10:00:27 +00:00
|
|
|
class PolicyEvaluator(BaseEvaluator):
|
|
|
|
"""Validate and evaluate python-based expressions"""
|
2020-02-19 08:49:38 +00:00
|
|
|
|
2020-06-01 13:25:38 +00:00
|
|
|
_messages: List[str]
|
|
|
|
|
2020-06-05 10:00:27 +00:00
|
|
|
def __init__(self, policy_name: str):
|
|
|
|
super().__init__()
|
2020-06-01 13:25:38 +00:00
|
|
|
self._messages = []
|
2020-06-05 10:00:27 +00:00
|
|
|
self._context["pb_message"] = self.expr_func_message
|
2020-10-20 10:05:56 +00:00
|
|
|
self._context["ip_address"] = ip_address
|
|
|
|
self._context["ip_network"] = ip_network
|
2020-06-19 16:19:20 +00:00
|
|
|
self._filename = policy_name or "PolicyEvaluator"
|
2020-06-01 13:25:38 +00:00
|
|
|
|
2020-06-05 10:00:27 +00:00
|
|
|
def expr_func_message(self, message: str):
|
2020-06-01 13:25:38 +00:00
|
|
|
"""Wrapper to append to messages list, which is returned with PolicyResult"""
|
|
|
|
self._messages.append(message)
|
|
|
|
|
|
|
|
def set_policy_request(self, request: PolicyRequest):
|
|
|
|
"""Update context based on policy request (if http request is given, update that too)"""
|
2020-02-19 09:22:28 +00:00
|
|
|
# update docs/policies/expression/index.md
|
2020-06-01 13:25:38 +00:00
|
|
|
self._context["pb_is_sso_flow"] = request.context.get(PLAN_CONTEXT_SSO, False)
|
2020-02-23 14:54:26 +00:00
|
|
|
if request.http_request:
|
2020-06-01 13:25:38 +00:00
|
|
|
self.set_http_request(request.http_request)
|
2020-06-05 10:00:27 +00:00
|
|
|
self._context["request"] = request
|
2020-09-26 11:58:32 +00:00
|
|
|
self._context["context"] = request.context
|
2020-06-01 13:25:38 +00:00
|
|
|
|
|
|
|
def set_http_request(self, request: HttpRequest):
|
|
|
|
"""Update context based on http request"""
|
|
|
|
# update docs/policies/expression/index.md
|
2020-07-12 14:36:49 +00:00
|
|
|
self._context["pb_client_ip"] = ip_address(
|
|
|
|
get_client_ip(request) or "255.255.255.255"
|
|
|
|
)
|
2020-06-01 13:25:38 +00:00
|
|
|
self._context["request"] = request
|
|
|
|
|
|
|
|
def evaluate(self, expression_source: str) -> PolicyResult:
|
|
|
|
"""Parse and evaluate expression. Policy is expected to return a truthy object.
|
|
|
|
Messages can be added using 'do pb_message()'."""
|
2020-02-19 08:49:38 +00:00
|
|
|
try:
|
2020-06-05 10:00:27 +00:00
|
|
|
result = super().evaluate(expression_source)
|
|
|
|
except (ValueError, SyntaxError) as exc:
|
2020-02-19 08:49:38 +00:00
|
|
|
return PolicyResult(False, str(exc))
|
2020-06-01 13:25:38 +00:00
|
|
|
except Exception as exc: # pylint: disable=broad-except
|
|
|
|
LOGGER.warning("Expression error", exc=exc)
|
|
|
|
return PolicyResult(False, str(exc))
|
|
|
|
else:
|
|
|
|
policy_result = PolicyResult(False)
|
|
|
|
policy_result.messages = tuple(self._messages)
|
2020-06-05 10:00:27 +00:00
|
|
|
if result is None:
|
2020-02-19 15:19:02 +00:00
|
|
|
LOGGER.warning(
|
2020-06-05 10:00:27 +00:00
|
|
|
"Expression policy returned None",
|
2020-02-19 15:19:02 +00:00
|
|
|
src=expression_source,
|
2020-06-01 13:25:38 +00:00
|
|
|
req=self._context,
|
2020-02-19 15:19:02 +00:00
|
|
|
)
|
2020-06-01 13:25:38 +00:00
|
|
|
policy_result.passing = False
|
2020-02-19 08:49:38 +00:00
|
|
|
if result:
|
2020-06-01 13:25:38 +00:00
|
|
|
policy_result.passing = bool(result)
|
|
|
|
return policy_result
|