From dc8a2bd290ba2b318d3372ac338e1626ff7fbf39 Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:13:35 +0100 Subject: [PATCH] Replace default voice wildcard with player permissions --- .../workflows/default-voice-permissions.yml | 33 +++++ tools/set-default-voice-permissions.py | 113 ++++++++++++++++++ tools/test_default_voice_permissions.py | 50 ++++++++ 3 files changed, 196 insertions(+) create mode 100644 .forgejo/workflows/default-voice-permissions.yml create mode 100644 tools/set-default-voice-permissions.py create mode 100644 tools/test_default_voice_permissions.py diff --git a/.forgejo/workflows/default-voice-permissions.yml b/.forgejo/workflows/default-voice-permissions.yml new file mode 100644 index 00000000..4c2ad551 --- /dev/null +++ b/.forgejo/workflows/default-voice-permissions.yml @@ -0,0 +1,33 @@ +name: Default Voice Permissions Operations Build +on: + workflow_dispatch: + push: + branches: ['ops/default-player-voice-permissions-*'] + +jobs: + default-voice-permissions: + runs-on: dune-ci + steps: + - name: Check out exact revision + shell: bash + env: + REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + git init . + git -c http.extraheader="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$REPO_TOKEN" | base64 -w0)" fetch --depth=1 https://www.dunerealms.com/git/unimatrix/plugins.git "$GITHUB_SHA" + git checkout --detach FETCH_HEAD + - name: Validate and package the scoped LuckPerms operation + shell: bash + run: | + set -euo pipefail + python3 tools/test_default_voice_permissions.py -v + mkdir default-voice-artifacts + cp tools/{set-default-voice-permissions,allow-giedi-nether-pearl}.py default-voice-artifacts/ + printf '%s\n' "$(git rev-parse HEAD)" > default-voice-artifacts/SOURCE_COMMIT + (cd default-voice-artifacts && sha256sum -- *.py SOURCE_COMMIT > SHA256SUMS) + - uses: actions/upload-artifact@v3 + with: + name: DuneRealms-Default-Voice-Permissions + path: default-voice-artifacts/ + if-no-files-found: error diff --git a/tools/set-default-voice-permissions.py b/tools/set-default-voice-permissions.py new file mode 100644 index 00000000..cbfb3cf5 --- /dev/null +++ b/tools/set-default-voice-permissions.py @@ -0,0 +1,113 @@ +"""Replace the default LuckPerms voice wildcard with the three player actions.""" + +import argparse +import importlib.util +import json +from pathlib import Path +import sys +import time + + +ROOT = Path('/opt/dunerealms/servers/giedi_prime') +UNIT = 'dune-giedi_prime.service' +CONTROLLER = Path('/opt/dunerealms/server-control') +PINS = { + 'LuckPerms-Bukkit.jar': 'af18d92f0b8d85b73835546e3376630dea62417291ed8571930e6875436a64be', + 'voicechat-bukkit-2.6.25.jar': 'f39f2e840873aa08e0ccec733fdc824ed4c3b9282e027fd6f18a79006eebad20', +} +PLAY = ('voicechat.listen', 'voicechat.speak', 'voicechat.groups') +WILDCARD = 'voicechat.*' +TOUCHED = {*PLAY, WILDCARD} + +spec = importlib.util.spec_from_file_location('dune_native_console', Path(__file__).with_name('allow-giedi-nether-pearl.py')) +console = importlib.util.module_from_spec(spec) +spec.loader.exec_module(console) + + +def global_grant(name): + return ('default', name, 1, 'global', 'global', 0, '{}') + + +def default_voice(rows): + return {row for row in rows if row[0] == 'default' and row[1] in TOUCHED} + + +def fixed_plan(rows): + scope = default_voice(rows) + allowed = {global_grant(name) for name in TOUCHED} + if scope - allowed: + raise ValueError('Default voice permissions have an unreviewed value or context') + needed = tuple(name for name in PLAY if global_grant(name) not in scope) + wildcard = global_grant(WILDCARD) in scope + if needed and not wildcard: + raise ValueError('Expected the live default wildcard before projecting the precise replacement') + # Refuse to treat default as unprivileged if an independent admin assignment appears. + if any(row[0] == 'default' and row[1] in {'*', 'voicechat.admin'} and row[2] == 1 for row in rows): + raise ValueError('A separate default admin grant requires review') + return needed, wildcard + + +def wait_exact(read, previous, expected, *, attempts=40, pause=time.sleep): + for _ in range(attempts): + observed = read() + if observed == expected: + return + if observed != previous: + raise RuntimeError('Unexpected permission drift. Check native console before continuing.') + pause(.25) + raise RuntimeError('LuckPerms persistence was not confirmed. Check native console before continuing.') + + +def update(read, issue, apply): + before = read() + needed, wildcard = fixed_plan(before) + result = {'group': 'default', 'ordinaryNodes': PLAY, 'missingNodes': needed, 'wildcardPresent': wildcard} + if not apply: + return {**result, 'status': 'planned'} + current = set(before) + for node in needed: + previous = current.copy() + issue(f'luckperms group default permission set {node} true') + current.add(global_grant(node)) + wait_exact(read, previous, current) + if wildcard: + if read() != current or not {global_grant(node) for node in PLAY} <= current: + raise RuntimeError('Concrete voice permissions must be persistent before removing the wildcard') + previous = current.copy() + issue('luckperms group default permission unset voicechat.*') + current.remove(global_grant(WILDCARD)) + wait_exact(read, previous, current) + return {**result, 'status': 'verified', 'allOtherGroupRowsVerified': len(before - default_voice(before))} + + +def read_groups(): + # Existing server-control database connection. This query is read-only. + sys.path.insert(0, str(CONTROLLER)) + from agent import permissions_database + sql = 'SELECT name,permission,value,server,world,expiry,contexts FROM luckperms_group_permissions' + with permissions_database() as connection, connection.cursor() as query: + query.execute(sql) + return {tuple(row[key] for key in ('name', 'permission', 'value', 'server', 'world', 'expiry', 'contexts')) + for row in query.fetchall()} + + +def run(apply): + if console.service('is-active', UNIT) != 'active' or console.service('show', UNIT, '--property=WorkingDirectory', '--value') != str(ROOT): + raise ValueError('Expected the active Giedi Prime LuckPerms console') + if any(console.sha(ROOT / 'plugins' / name) != pin for name, pin in PINS.items()): + raise ValueError('Installed LuckPerms or VoiceChat differs from the reviewed build') + return update(read_groups, lambda line: console.send(ROOT / 'console.fifo', line), apply) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--apply', action='store_true') + args = parser.parse_args() + if args.apply: + import fcntl + with Path('/run/lock/dunerealms-default-voice-permissions.lock').open('a') as lock: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + result = run(True) + else: + result = run(False) + print(json.dumps(result, indent=2)) diff --git a/tools/test_default_voice_permissions.py b/tools/test_default_voice_permissions.py new file mode 100644 index 00000000..b22b6488 --- /dev/null +++ b/tools/test_default_voice_permissions.py @@ -0,0 +1,50 @@ +import importlib.util +from pathlib import Path +import unittest + + +spec = importlib.util.spec_from_file_location('voice', Path(__file__).with_name('set-default-voice-permissions.py')) +voice = importlib.util.module_from_spec(spec) +spec.loader.exec_module(voice) + + +class DefaultVoiceTests(unittest.TestCase): + def test_group_only_change_and_safe_restart(self): + admin = ('staff', 'voicechat.admin', 1, 'global', 'global', 0, '{}') + parent = voice.global_grant('group.dr_access_user') + rows = {admin, parent, voice.global_grant(voice.WILDCARD), voice.global_grant(voice.PLAY[0])} + commands = [] + + def issue(command): + commands.append(command) + fields = command.split() + if fields[4] == 'set': + rows.add(voice.global_grant(fields[5])) + else: + self.assertTrue({voice.global_grant(node) for node in voice.PLAY} <= rows) + rows.remove(voice.global_grant(fields[5])) + + self.assertEqual(voice.update(lambda: rows.copy(), issue, False)['status'], 'planned') + self.assertEqual(commands, []) + self.assertEqual(voice.update(lambda: rows.copy(), issue, True)['status'], 'verified') + self.assertEqual(rows, {admin, parent, *(voice.global_grant(node) for node in voice.PLAY)}) + self.assertEqual(commands, [f'luckperms group default permission set {node} true' for node in voice.PLAY[1:]] + + ['luckperms group default permission unset voicechat.*']) + voice.update(lambda: rows.copy(), lambda _: self.fail('unexpected replay command'), True) + + def test_context_override_is_never_rewritten(self): + rows = {voice.global_grant(voice.WILDCARD), ('default', voice.PLAY[0], 0, 'global', 'nether', 0, '{}')} + with self.assertRaises(ValueError): + voice.update(lambda: rows.copy(), lambda _: self.fail('unexpected command'), True) + + def test_readback_waits_for_command_and_rejects_unrelated_writes(self): + previous = {voice.global_grant(voice.WILDCARD)} + expected = set() + snapshots = iter([previous, previous, expected]) + voice.wait_exact(lambda: next(snapshots), previous, expected, pause=lambda _: None) + with self.assertRaises(RuntimeError): + voice.wait_exact(lambda: {voice.global_grant('unexpected')}, previous, expected, pause=lambda _: None) + + +if __name__ == '__main__': + unittest.main()