raven-shield 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ """raven-shield: Deterministic, fail-closed runtime policy gateway for LLM tool calling."""
2
+
3
+ from raven_shield.context import (
4
+ get_current_context,
5
+ reset_current_context,
6
+ security_context,
7
+ set_current_context,
8
+ )
9
+ from raven_shield.errors import (
10
+ ApprovalRequiredError,
11
+ MalformedArgumentsError,
12
+ PolicyConfigurationError,
13
+ PolicyViolationError,
14
+ RavenShieldError,
15
+ )
16
+ from raven_shield.gateway import PolicyGateway
17
+ from raven_shield.integrations import (
18
+ OpenTelemetrySecurityListener,
19
+ RavenShieldCallbackHandler,
20
+ RavenShieldMiddleware,
21
+ RavenShieldToolNode,
22
+ RavenToolInterceptor,
23
+ setup_opentelemetry,
24
+ trace_tool_execution,
25
+ wrap_tool,
26
+ )
27
+ from raven_shield.loader import (
28
+ find_default_policy,
29
+ load_policy,
30
+ load_policy_file,
31
+ parse_policy_string,
32
+ )
33
+ from raven_shield.models import (
34
+ Decision,
35
+ PolicyContext,
36
+ PolicyDocument,
37
+ SecurityContext,
38
+ ToolPolicy,
39
+ )
40
+ from raven_shield.telemetry import (
41
+ SecurityAuditEvent,
42
+ clear_telemetry_listeners,
43
+ register_telemetry_listener,
44
+ unregister_telemetry_listener,
45
+ )
46
+
47
+ __version__ = "1.0.0"
48
+
49
+ __all__ = [
50
+ "ApprovalRequiredError",
51
+ "Decision",
52
+ "MalformedArgumentsError",
53
+ "OpenTelemetrySecurityListener",
54
+ "PolicyConfigurationError",
55
+ "PolicyContext",
56
+ "PolicyDocument",
57
+ "PolicyGateway",
58
+ "PolicyViolationError",
59
+ "RavenShieldCallbackHandler",
60
+ "RavenShieldError",
61
+ "RavenShieldMiddleware",
62
+ "RavenShieldToolNode",
63
+ "RavenToolInterceptor",
64
+ "SecurityAuditEvent",
65
+ "SecurityContext",
66
+ "ToolPolicy",
67
+ "clear_telemetry_listeners",
68
+ "find_default_policy",
69
+ "get_current_context",
70
+ "load_policy",
71
+ "load_policy_file",
72
+ "parse_policy_string",
73
+ "register_telemetry_listener",
74
+ "reset_current_context",
75
+ "security_context",
76
+ "set_current_context",
77
+ "setup_opentelemetry",
78
+ "trace_tool_execution",
79
+ "unregister_telemetry_listener",
80
+ "wrap_tool",
81
+ ]
@@ -0,0 +1,57 @@
1
+ """Context management for raven-shield security tokens and session state."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Generator
6
+ from contextlib import contextmanager
7
+ from contextvars import ContextVar, Token
8
+ from typing import Any
9
+
10
+ from raven_shield.models import SecurityContext
11
+
12
+ _CURRENT_CONTEXT: ContextVar[SecurityContext | None] = ContextVar(
13
+ "raven_shield_security_context", default=None
14
+ )
15
+
16
+
17
+ def get_current_context() -> SecurityContext | None:
18
+ """Retrieve the currently active security context for this async task or thread."""
19
+ return _CURRENT_CONTEXT.get()
20
+
21
+
22
+ def set_current_context(context: SecurityContext | dict[str, Any] | None) -> Token[SecurityContext | None]:
23
+ """Set the current security context, returning a token to reset it."""
24
+ ctx = SecurityContext.from_any(context) if context is not None else None
25
+ return _CURRENT_CONTEXT.set(ctx)
26
+
27
+
28
+ def reset_current_context(token: Token[SecurityContext | None]) -> None:
29
+ """Reset the security context to its previous value via its token."""
30
+ _CURRENT_CONTEXT.reset(token)
31
+
32
+
33
+ @contextmanager
34
+ def security_context(
35
+ context: SecurityContext | dict[str, Any] | None = None,
36
+ **kwargs: Any,
37
+ ) -> Generator[SecurityContext, None, None]:
38
+ """Context manager for setting an active SecurityContext within a block.
39
+
40
+ Example:
41
+ with security_context(verified_user=True, verified_order_id=True):
42
+ my_protected_tool(order_id="ORD-1")
43
+ """
44
+ if context is not None and kwargs:
45
+ ctx_dict = context.to_dict() if hasattr(context, "to_dict") else dict(context)
46
+ ctx_dict.update(kwargs)
47
+ active_ctx = SecurityContext.from_any(ctx_dict)
48
+ elif context is not None:
49
+ active_ctx = SecurityContext.from_any(context)
50
+ else:
51
+ active_ctx = SecurityContext.from_any(kwargs)
52
+
53
+ token = set_current_context(active_ctx)
54
+ try:
55
+ yield active_ctx
56
+ finally:
57
+ reset_current_context(token)
raven_shield/errors.py ADDED
@@ -0,0 +1,39 @@
1
+ """Exception hierarchy for raven-shield runtime policy gateway."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+
8
+ class RavenShieldError(Exception):
9
+ """Base exception for all raven-shield runtime errors."""
10
+
11
+
12
+ class PolicyConfigurationError(RavenShieldError, ValueError):
13
+ """Raised when policy definitions or files are malformed."""
14
+
15
+
16
+ class PolicyViolationError(RavenShieldError, PermissionError):
17
+ """Raised when an action or tool invocation violates the active policy."""
18
+
19
+ def __init__(
20
+ self,
21
+ message: str,
22
+ tool: str,
23
+ reason: str,
24
+ decision: Any = None,
25
+ arguments: dict[str, Any] | None = None,
26
+ ) -> None:
27
+ super().__init__(message)
28
+ self.tool = tool
29
+ self.reason = reason
30
+ self.decision = decision
31
+ self.arguments = arguments or {}
32
+
33
+
34
+ class ApprovalRequiredError(PolicyViolationError):
35
+ """Raised when an operation requires explicit human-in-the-loop approval."""
36
+
37
+
38
+ class MalformedArgumentsError(PolicyViolationError):
39
+ """Raised when arguments violate basic type or boundary constraints."""