|
| 1 | +from collections.abc import Iterable |
| 2 | + |
| 3 | +# When this changes, please update the frontend as well here frontend/app/lib/types/policies.ts |
| 4 | +DISTRIBOX_ADMIN_POLICY = "distribox:admin" |
| 5 | + |
| 6 | +POLICIES: list[dict[str, str]] = [ |
| 7 | + { |
| 8 | + "policy": DISTRIBOX_ADMIN_POLICY, |
| 9 | + "description": "This policy gives full access to the Distribox dashboard application.", |
| 10 | + }, |
| 11 | + { |
| 12 | + "policy": "auth:me:get", |
| 13 | + "description": "Allows a user to fetch their own authenticated profile.", |
| 14 | + }, |
| 15 | + { |
| 16 | + "policy": "auth:changePassword", |
| 17 | + "description": "Allows a user to change their own password.", |
| 18 | + }, |
| 19 | + { |
| 20 | + "policy": "host:get", |
| 21 | + "description": "Allows the user to fetch the host resources.", |
| 22 | + }, |
| 23 | + { |
| 24 | + "policy": "images:get", |
| 25 | + "description": "Allows the user to fetch images metadata from the registry.", |
| 26 | + }, |
| 27 | + { |
| 28 | + "policy": "policies:get", |
| 29 | + "description": "Allows the user to fetch policies.", |
| 30 | + }, |
| 31 | + { |
| 32 | + "policy": "users:get", |
| 33 | + "description": "Allows the user to fetch users.", |
| 34 | + }, |
| 35 | + { |
| 36 | + "policy": "users:create", |
| 37 | + "description": "Allows the user to create users.", |
| 38 | + }, |
| 39 | + { |
| 40 | + "policy": "users:updatePolicies", |
| 41 | + "description": "Allows the user to update user policies.", |
| 42 | + }, |
| 43 | + { |
| 44 | + "policy": "users:delete", |
| 45 | + "description": "Allows the user to delete users.", |
| 46 | + }, |
| 47 | + { |
| 48 | + "policy": "users:getPassword", |
| 49 | + "description": "Allows the user to fetch user passwords.", |
| 50 | + }, |
| 51 | + { |
| 52 | + "policy": "vms:get", |
| 53 | + "description": "Allows the user to list virtual machines.", |
| 54 | + }, |
| 55 | + { |
| 56 | + "policy": "vms:getById", |
| 57 | + "description": "Allows the user to fetch a virtual machine by id.", |
| 58 | + }, |
| 59 | + { |
| 60 | + "policy": "vms:create", |
| 61 | + "description": "Allows the user to create virtual machines.", |
| 62 | + }, |
| 63 | + { |
| 64 | + "policy": "vms:start", |
| 65 | + "description": "Allows the user to start virtual machines.", |
| 66 | + }, |
| 67 | + { |
| 68 | + "policy": "vms:stop", |
| 69 | + "description": "Allows the user to stop virtual machines.", |
| 70 | + }, |
| 71 | + { |
| 72 | + "policy": "vms:credentials:create", |
| 73 | + "description": "Allows the user to create virtual machine credentials.", |
| 74 | + }, |
| 75 | + { |
| 76 | + "policy": "vms:delete", |
| 77 | + "description": "Allows the user to remove virtual machines.", |
| 78 | + }, |
| 79 | + { |
| 80 | + "policy": "vms:credentials:revoke", |
| 81 | + "description": "Allows the user to revoke virtual machine credentials.", |
| 82 | + }, |
| 83 | + { |
| 84 | + "policy": "vms:credentials:list", |
| 85 | + "description": "Allows the user to list virtual machine credentials.", |
| 86 | + }, |
| 87 | + { |
| 88 | + "policy": "vms:credentials:getById", |
| 89 | + "description": "Allows the user to fetch a virtual machine credential by id.", |
| 90 | + }, |
| 91 | +] |
| 92 | + |
| 93 | +VALID_POLICIES = {entry["policy"] for entry in POLICIES} |
| 94 | +POLICY_BY_NAME = {entry["policy"]: entry for entry in POLICIES} |
| 95 | + |
| 96 | + |
| 97 | +def normalize_policies(policies: Iterable[str]) -> list[str]: |
| 98 | + """Deduplicate while preserving first-seen order.""" |
| 99 | + normalized: list[str] = [] |
| 100 | + seen: set[str] = set() |
| 101 | + |
| 102 | + for policy in policies: |
| 103 | + if policy not in seen: |
| 104 | + normalized.append(policy) |
| 105 | + seen.add(policy) |
| 106 | + |
| 107 | + return normalized |
| 108 | + |
| 109 | + |
| 110 | +def invalid_policies(policies: Iterable[str]) -> list[str]: |
| 111 | + return [policy for policy in policies if policy not in VALID_POLICIES] |
| 112 | + |
| 113 | + |
| 114 | +def expand_policies(policies: Iterable[str]) -> list[dict[str, str]]: |
| 115 | + expanded: list[dict[str, str]] = [] |
| 116 | + for policy in policies: |
| 117 | + policy_entry = POLICY_BY_NAME.get(policy) |
| 118 | + if policy_entry is not None: |
| 119 | + expanded.append(policy_entry) |
| 120 | + else: |
| 121 | + expanded.append( |
| 122 | + { |
| 123 | + "policy": policy, |
| 124 | + "description": "Custom policy", |
| 125 | + } |
| 126 | + ) |
| 127 | + return expanded |
0 commit comments