DeWitt Gibson commited on
Commit
d5c8c48
·
1 Parent(s): 392abc7

Adding Agency Package

Browse files
src/llmguardian/agency/README.md ADDED
File without changes
src/llmguardian/agency/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # src/llmguardian/agency/__init__.py
2
+ from .permission_manager import PermissionManager
3
+ from .action_validator import ActionValidator
4
+ from .scope_limiter import ScopeLimiter
5
+ from .executor import SafeExecutor
src/llmguardian/agency/action_validator.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/llmguardian/agency/action_validator.py
2
+ from typing import Dict, List, Optional
3
+ from dataclasses import dataclass
4
+ from enum import Enum
5
+ from ..core.logger import SecurityLogger
6
+
7
+ class ActionType(Enum):
8
+ READ = "read"
9
+ WRITE = "write"
10
+ DELETE = "delete"
11
+ EXECUTE = "execute"
12
+ MODIFY = "modify"
13
+
14
+ @dataclass
15
+ class Action:
16
+ type: ActionType
17
+ resource: str
18
+ parameters: Optional[Dict] = None
19
+
20
+ class ActionValidator:
21
+ def __init__(self, security_logger: Optional[SecurityLogger] = None):
22
+ self.security_logger = security_logger
23
+ self.allowed_actions: Dict[str, List[ActionType]] = {}
24
+
25
+ def validate_action(self, action: Action, context: Dict) -> bool:
26
+ if action.resource not in self.allowed_actions:
27
+ return False
28
+
29
+ if action.type not in self.allowed_actions[action.resource]:
30
+ return False
31
+
32
+ # Validate parameters and context
33
+ return self._validate_parameters(action, context)
34
+
35
+ def _validate_parameters(self, action: Action, context: Dict) -> bool:
36
+ # Implementation of parameter validation
37
+ return True
src/llmguardian/agency/executor.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/llmguardian/agency/executor.py
2
+ from typing import Dict, Any, Optional
3
+ from dataclasses import dataclass
4
+ from ..core.logger import SecurityLogger
5
+ from .action_validator import Action, ActionValidator
6
+ from .permission_manager import PermissionManager
7
+ from .scope_limiter import ScopeLimiter
8
+
9
+ @dataclass
10
+ class ExecutionResult:
11
+ success: bool
12
+ output: Optional[Any] = None
13
+ error: Optional[str] = None
14
+
15
+ class SafeExecutor:
16
+ def __init__(self,
17
+ security_logger: Optional[SecurityLogger] = None,
18
+ permission_manager: Optional[PermissionManager] = None,
19
+ action_validator: Optional[ActionValidator] = None,
20
+ scope_limiter: Optional[ScopeLimiter] = None):
21
+ self.security_logger = security_logger
22
+ self.permission_manager = permission_manager or PermissionManager()
23
+ self.action_validator = action_validator or ActionValidator()
24
+ self.scope_limiter = scope_limiter or ScopeLimiter()
25
+
26
+ async def execute(self,
27
+ action: Action,
28
+ user_id: str,
29
+ context: Dict[str, Any]) -> ExecutionResult:
30
+ try:
31
+ # Validate permissions
32
+ if not self.permission_manager.check_permission(
33
+ user_id, action.resource, action.type
34
+ ):
35
+ return ExecutionResult(
36
+ success=False,
37
+ error="Permission denied"
38
+ )
39
+
40
+ # Validate action
41
+ if not self.action_validator.validate_action(action, context):
42
+ return ExecutionResult(
43
+ success=False,
44
+ error="Invalid action"
45
+ )
46
+
47
+ # Check scope
48
+ if not self.scope_limiter.check_scope(
49
+ user_id, action.type, action.resource
50
+ ):
51
+ return ExecutionResult(
52
+ success=False,
53
+ error="Out of scope"
54
+ )
55
+
56
+ # Execute action safely
57
+ result = await self._execute_action(action, context)
58
+ return ExecutionResult(success=True, output=result)
59
+
60
+ except Exception as e:
61
+ if self.security_logger:
62
+ self.security_logger.log_security_event(
63
+ "execution_error",
64
+ action=action.__dict__,
65
+ error=str(e)
66
+ )
67
+ return ExecutionResult(
68
+ success=False,
69
+ error=f"Execution failed: {str(e)}"
70
+ )
71
+
72
+ async def _execute_action(self,
73
+ action: Action,
74
+ context: Dict[str, Any]) -> Any:
75
+ # Implementation of safe action execution
76
+ pass
src/llmguardian/agency/permission_manager.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/llmguardian/agency/permission_manager.py
2
+ from typing import Dict, List, Optional, Set
3
+ from dataclasses import dataclass
4
+ from enum import Enum
5
+ from ..core.logger import SecurityLogger
6
+
7
+ class PermissionLevel(Enum):
8
+ NO_ACCESS = 0
9
+ READ = 1
10
+ WRITE = 2
11
+ EXECUTE = 3
12
+ ADMIN = 4
13
+
14
+ @dataclass
15
+ class Permission:
16
+ resource: str
17
+ level: PermissionLevel
18
+ conditions: Optional[Dict[str, str]] = None
19
+
20
+ class PermissionManager:
21
+ def __init__(self, security_logger: Optional[SecurityLogger] = None):
22
+ self.security_logger = security_logger
23
+ self.permissions: Dict[str, Set[Permission]] = {}
24
+
25
+ def check_permission(self, user_id: str, resource: str, level: PermissionLevel) -> bool:
26
+ if user_id not in self.permissions:
27
+ return False
28
+
29
+ for perm in self.permissions[user_id]:
30
+ if perm.resource == resource and perm.level.value >= level.value:
31
+ return True
32
+ return False
33
+
34
+ def grant_permission(self, user_id: str, permission: Permission):
35
+ if user_id not in self.permissions:
36
+ self.permissions[user_id] = set()
37
+ self.permissions[user_id].add(permission)
38
+
39
+ if self.security_logger:
40
+ self.security_logger.log_security_event(
41
+ "permission_granted",
42
+ user_id=user_id,
43
+ permission=permission.__dict__
44
+ )
45
+
46
+ def revoke_permission(self, user_id: str, resource: str):
47
+ if user_id in self.permissions:
48
+ self.permissions[user_id] = {
49
+ p for p in self.permissions[user_id]
50
+ if p.resource != resource
51
+ }
src/llmguardian/agency/scope_limiter.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/llmguardian/agency/scope_limiter.py
2
+ from typing import Dict, List, Optional, Set
3
+ from dataclasses import dataclass
4
+ from enum import Enum
5
+ from ..core.logger import SecurityLogger
6
+
7
+ class ScopeType(Enum):
8
+ DATA = "data"
9
+ FUNCTION = "function"
10
+ SYSTEM = "system"
11
+ NETWORK = "network"
12
+
13
+ @dataclass
14
+ class Scope:
15
+ type: ScopeType
16
+ resources: Set[str]
17
+ limits: Optional[Dict] = None
18
+
19
+ class ScopeLimiter:
20
+ def __init__(self, security_logger: Optional[SecurityLogger] = None):
21
+ self.security_logger = security_logger
22
+ self.scopes: Dict[str, Scope] = {}
23
+
24
+ def check_scope(self, user_id: str, scope_type: ScopeType, resource: str) -> bool:
25
+ if user_id not in self.scopes:
26
+ return False
27
+
28
+ scope = self.scopes[user_id]
29
+ return (scope.type == scope_type and
30
+ resource in scope.resources)
31
+
32
+ def add_scope(self, user_id: str, scope: Scope):
33
+ self.scopes[user_id] = scope