|
| 1 | +"""Security protocol models with backward-compatible, additive checks.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from copy import deepcopy |
| 6 | + |
| 7 | +CREATE = "create" |
| 8 | +READ = "read" |
| 9 | +UPDATE = "update" |
| 10 | +DELETE = "delete" |
| 11 | +VALID_ACTIONS = [CREATE, READ, UPDATE, DELETE] |
| 12 | + |
| 13 | +USER_LIST = "user_list" |
| 14 | +CHECKS = "checks" |
| 15 | +LOGIN = "login" |
| 16 | +ALLOWED_ROLES = "allowed_roles" |
| 17 | +API_KEY = "api_key" |
| 18 | +API_KEYS = "api_keys" |
| 19 | +PASS_PHRASE = "pass_phrase" |
| 20 | +PASSWORD = "password" |
| 21 | +CODES = "codes" |
| 22 | + |
| 23 | + |
| 24 | +class ActionChecks: |
| 25 | + """Represents the checks required for one CRUD action.""" |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + login: bool = False, |
| 30 | + valid_users: list[str] | None = None, |
| 31 | + allowed_roles: list[str] | None = None, |
| 32 | + api_key: bool = False, |
| 33 | + valid_api_keys: list[str] | None = None, |
| 34 | + pass_phrase: bool = False, |
| 35 | + phrase: str = "", |
| 36 | + codes: dict[str, str] | None = None, |
| 37 | + ): |
| 38 | + if not isinstance(login, bool): |
| 39 | + raise TypeError("login must be a bool") |
| 40 | + if not isinstance(api_key, bool): |
| 41 | + raise TypeError("api_key must be a bool") |
| 42 | + if not isinstance(pass_phrase, bool): |
| 43 | + raise TypeError("pass_phrase must be a bool") |
| 44 | + if valid_users is not None and not isinstance(valid_users, list): |
| 45 | + raise TypeError("valid_users must be a list or None") |
| 46 | + if allowed_roles is not None and not isinstance(allowed_roles, list): |
| 47 | + raise TypeError("allowed_roles must be a list or None") |
| 48 | + if valid_api_keys is not None and not isinstance(valid_api_keys, list): |
| 49 | + raise TypeError("valid_api_keys must be a list or None") |
| 50 | + if codes is not None and not isinstance(codes, dict): |
| 51 | + raise TypeError("codes must be a dict or None") |
| 52 | + |
| 53 | + self.login = login |
| 54 | + self.valid_users = list(valid_users or []) |
| 55 | + self.allowed_roles = list(allowed_roles or []) |
| 56 | + self.api_key = api_key |
| 57 | + self.valid_api_keys = list(valid_api_keys or []) |
| 58 | + self.pass_phrase = pass_phrase |
| 59 | + self.phrase = phrase |
| 60 | + self.codes = deepcopy(codes) if codes else None |
| 61 | + |
| 62 | + def to_json(self) -> dict: |
| 63 | + payload = { |
| 64 | + LOGIN: self.login, |
| 65 | + API_KEY: self.api_key, |
| 66 | + PASS_PHRASE: self.pass_phrase, |
| 67 | + } |
| 68 | + if self.valid_users: |
| 69 | + payload[USER_LIST] = list(self.valid_users) |
| 70 | + if self.allowed_roles: |
| 71 | + payload[ALLOWED_ROLES] = list(self.allowed_roles) |
| 72 | + if self.valid_api_keys: |
| 73 | + payload[API_KEYS] = list(self.valid_api_keys) |
| 74 | + if self.phrase: |
| 75 | + payload[PASSWORD] = self.phrase |
| 76 | + if self.codes: |
| 77 | + payload[CODES] = deepcopy(self.codes) |
| 78 | + return payload |
| 79 | + |
| 80 | + def is_permitted(self, user_id: str = "", check_vals: dict | None = None) -> bool: |
| 81 | + check_vals = check_vals or {} |
| 82 | + if self.login and not user_id: |
| 83 | + return False |
| 84 | + if self.valid_users and user_id not in self.valid_users: |
| 85 | + return False |
| 86 | + if self.allowed_roles and check_vals.get("role") not in self.allowed_roles: |
| 87 | + return False |
| 88 | + if self.api_key and check_vals.get("api_key") not in self.valid_api_keys: |
| 89 | + return False |
| 90 | + if self.pass_phrase and check_vals.get("phrase") != self.phrase: |
| 91 | + return False |
| 92 | + if self.codes and check_vals.get("code") not in self.codes.values(): |
| 93 | + return False |
| 94 | + return True |
| 95 | + |
| 96 | + |
| 97 | +class SecProtocol: |
| 98 | + """Represents security rules for a named feature/resource.""" |
| 99 | + |
| 100 | + def __init__( |
| 101 | + self, |
| 102 | + name: str, |
| 103 | + create: ActionChecks | None = None, |
| 104 | + read: ActionChecks | None = None, |
| 105 | + update: ActionChecks | None = None, |
| 106 | + delete: ActionChecks | None = None, |
| 107 | + ): |
| 108 | + if not isinstance(name, str): |
| 109 | + raise TypeError("name must be a str") |
| 110 | + self.name = name |
| 111 | + self.create = create or ActionChecks() |
| 112 | + self.read = read or ActionChecks() |
| 113 | + self.update = update or ActionChecks() |
| 114 | + self.delete = delete or ActionChecks() |
| 115 | + |
| 116 | + def to_json(self) -> dict: |
| 117 | + return { |
| 118 | + "feature_name": self.name, |
| 119 | + CREATE: self.create.to_json(), |
| 120 | + READ: self.read.to_json(), |
| 121 | + UPDATE: self.update.to_json(), |
| 122 | + DELETE: self.delete.to_json(), |
| 123 | + } |
| 124 | + |
| 125 | + def is_permitted(self, action: str, user_id: str = "", check_vals: dict | None = None) -> bool: |
| 126 | + if action not in VALID_ACTIONS: |
| 127 | + raise ValueError(f"Invalid action: {action}") |
| 128 | + return getattr(self, action).is_permitted(user_id=user_id, check_vals=check_vals) |
| 129 | + |
| 130 | + |
| 131 | +def checks_from_legacy(action_record: dict | None) -> ActionChecks: |
| 132 | + if not action_record: |
| 133 | + return ActionChecks() |
| 134 | + checks = action_record.get(CHECKS, {}) |
| 135 | + return ActionChecks( |
| 136 | + login=checks.get(LOGIN, False), |
| 137 | + valid_users=action_record.get(USER_LIST, []), |
| 138 | + allowed_roles=checks.get(ALLOWED_ROLES, []), |
| 139 | + api_key=checks.get(API_KEY, False), |
| 140 | + valid_api_keys=checks.get(API_KEYS, []), |
| 141 | + pass_phrase=checks.get(PASS_PHRASE, False), |
| 142 | + phrase=checks.get(PASSWORD, ""), |
| 143 | + codes=checks.get(CODES), |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | +def protocol_from_legacy(feature_name: str, feature_record: dict | None) -> SecProtocol: |
| 148 | + feature_record = feature_record or {} |
| 149 | + return SecProtocol( |
| 150 | + feature_name, |
| 151 | + create=checks_from_legacy(feature_record.get(CREATE)), |
| 152 | + read=checks_from_legacy(feature_record.get(READ)), |
| 153 | + update=checks_from_legacy(feature_record.get(UPDATE)), |
| 154 | + delete=checks_from_legacy(feature_record.get(DELETE)), |
| 155 | + ) |
0 commit comments