diff --git a/.forgejo/workflows/dunechat.yml b/.forgejo/workflows/dunechat.yml index e69deb19..c474ed6a 100644 --- a/.forgejo/workflows/dunechat.yml +++ b/.forgejo/workflows/dunechat.yml @@ -29,10 +29,12 @@ jobs: run: | set -euo pipefail gradle --no-daemon -p DuneChat test jar + python3 tools/test_plugin_dunechat.py -v mkdir dunechat-artifacts cp DuneChat/build/libs/DuneChat.jar dunechat-artifacts/ + cp tools/{plugin_dunechat,install-plugin-fleet,write-plugin-manifest,plugin_menu_pack,plugin_backend_abi,plugin_fleet_rollback,plugin_pack_properties,plugin_pillager_abi}.py dunechat-artifacts/ printf '%s\n' "$(git rev-parse HEAD)" > dunechat-artifacts/SOURCE_COMMIT - (cd dunechat-artifacts && sha256sum -- *.jar SOURCE_COMMIT > SHA256SUMS) + (cd dunechat-artifacts && sha256sum -- *.jar *.py SOURCE_COMMIT > SHA256SUMS) - uses: actions/upload-artifact@v3 with: name: DuneRealms-DuneChat diff --git a/tools/install-plugin-fleet.py b/tools/install-plugin-fleet.py index 2862617a..3d845e48 100644 --- a/tools/install-plugin-fleet.py +++ b/tools/install-plugin-fleet.py @@ -75,10 +75,11 @@ def verify_dunechest_fleet_target(sid, root, unit, incoming, installed): def plan(artifacts, inventory, expected_source): mode=inventory.get('mode') - if mode not in {'gui-preserve-backends','pillager-preserve-economy','arrakis-crysknife','navigator','planet-dunechest','fleet-dunechest','arrakis-raw-spice','arrakis-jerboa','compact-menus','lobby-food','lobby-account','arrakis-dunegroups'}: + if mode not in {'gui-preserve-backends','pillager-preserve-economy','arrakis-crysknife','navigator','planet-dunechest','fleet-dunechest','arrakis-raw-spice','arrakis-jerboa','compact-menus','lobby-food','lobby-account','arrakis-dunegroups','arrakis-dunechat'}: raise ValueError('An explicit supported rollout mode is required') compact_menus=mode=='compact-menus' dunegroups=mode=='arrakis-dunegroups' + dunechat=mode=='arrakis-dunechat' lobby_food=mode=='lobby-food' lobby_account=mode=='lobby-account' jerboa=mode=='arrakis-jerboa' @@ -113,6 +114,12 @@ def plan(artifacts, inventory, expected_source): allowed_names={'JarvisTranslator','DuneGroups'} if inventory.get('menuPack') is not None or inventory.get('privatePrerequisites'): raise ValueError('DuneGroups must retain pack configuration and unrelated runtimes') + if dunechat: + from plugin_dunechat import verify_artifact + allowed_names={'DuneChat'} + if inventory.get('menuPack') is not None or inventory.get('privatePrerequisites'): + raise ValueError('DuneChat must retain pack configuration and unrelated runtimes') + verify_artifact(artifacts, expected_source) files, names = {}, set() for entry in manifest['plugins']: name = entry['filename'] @@ -179,7 +186,7 @@ def plan(artifacts, inventory, expected_source): uses_menu=not compact_menus and ('dunemenus' in wanted or bool(menu and menu.get('unified') and not (crysknife or dunechest or jerboa))) if menu and menu.get('unified') and not (crysknife or dunechest or jerboa) and 'dunerealmsgui' not in wanted: raise ValueError('Unified pack rollout must select its matching DunerealmsGUI adapter') - if not (pillager or crysknife or navigator or dunechest or raw_spice or jerboa or lobby_food or lobby_account or dunegroups) and wanted - {'dunemenus','dunerealmsgui','dunenpc','dunelobby','mcgui'}: + if not (pillager or crysknife or navigator or dunechest or raw_spice or jerboa or lobby_food or lobby_account or dunegroups or dunechat) and wanted - {'dunemenus','dunerealmsgui','dunenpc','dunelobby','mcgui'}: raise ValueError('GUI rollout must retain working Ess/JARVIS/Core/Terrain binaries') if compact_menus: expected_root = Path('/opt/dunerealms/lobby') if sid == 'lobby' else Path('/opt/dunerealms/servers') / sid @@ -195,6 +202,9 @@ def plan(artifacts, inventory, expected_source): if dunegroups and (sid != 'arrakis' or root != ARRAKIS_ROOT or unit != 'dune-arrakis.service' or wanted != {'jarvistranslator','dunegroups'} or selected_prerequisites): raise ValueError('DuneGroups canary requires the exact Arrakis service and approved plugin pair') + if dunechat and (sid != 'arrakis' or root != ARRAKIS_ROOT or unit != 'dune-arrakis.service' + or wanted != {'dunechat'} or selected_prerequisites): + raise ValueError('DuneChat canary requires the exact Arrakis service and standalone plugin') if lobby_food and (sid != 'lobby' or root != Path('/opt/dunerealms/lobby') or unit != 'dune-lobby.service' or wanted != {'jarvistranslator'} or selected_prerequisites): raise ValueError('Lobby food rollout may only replace JARVIS on Lobby') @@ -407,6 +417,22 @@ def plan(artifacts, inventory, expected_source): retained = [{'filename': path.name, 'sha256': digest(path), 'metadata': metadata(path)} for path in sorted(plugins.glob('*.jar')) if path.name not in replaced] configurations = [] + if dunechat: + from plugin_dunechat import DISCORD_CONFIG, DISCORD_JAR, enable_modern_discord + discord = existing_paths.get('discordsrv') + if (discord is None or discord.name != 'DiscordSRV.jar' or digest(discord) != DISCORD_JAR + or not {'jarvistranslator', 'dunegroups'} <= existing_paths.keys() + or 'medievalfactions' in existing_paths or 'dunechat' in existing_paths): + raise ValueError('DuneChat requires the reviewed Discord relay, retained JARVIS and DuneGroups') + config = plugins/'DiscordSRV/config.yml' + if config.is_symlink() or config.parent.is_symlink() or digest(config) != DISCORD_CONFIG: + raise ValueError('Unexpected private Arrakis DiscordSRV config revision') + updated = enable_modern_discord(config.read_bytes().decode('utf-8')).encode('utf-8') + configurations.append({'path': 'DiscordSRV/config.yml', 'transform': 'enable-modern-discord', + 'sha256': hashlib.sha256(updated).hexdigest(), + 'previousSha256': DISCORD_CONFIG, 'previousMetadata': metadata(config)}) + retained = [{'filename': path.name, 'sha256': digest(path), 'metadata': metadata(path)} + for path in sorted(plugins.glob('*.jar'))] if dunegroups: from plugin_dunegroups import CONFIG_BASELINE, VENDORS, enable_backend, transition, verify_artifact incoming = {entry['name'].casefold(): entry for entry in chosen} @@ -537,11 +563,14 @@ def apply(deployment, backup_root, control=systemctl): target.parent.mkdir(parents=True, exist_ok=True) if new_parent: set_owner(target.parent, plugins.stat(), 0o750) - if config.get('transform') == 'enable-dunegroups': - from plugin_dunegroups import enable_backend + if config.get('transform') in {'enable-dunegroups', 'enable-modern-discord'}: + if config['transform'] == 'enable-dunegroups': + from plugin_dunegroups import enable_backend as update + else: + from plugin_dunechat import enable_modern_discord as update if digest(target) != config['previousSha256']: - raise ValueError('Private JARVIS config changed after service stop') - target.write_bytes(enable_backend(target.read_bytes().decode('utf-8')).encode('utf-8')) + raise ValueError('Private configuration changed after service stop') + target.write_bytes(update(target.read_bytes().decode('utf-8')).encode('utf-8')) restore_metadata(target, config['previousMetadata']) else: target.write_bytes(config['content'].encode()) diff --git a/tools/plugin_dunechat.py b/tools/plugin_dunechat.py new file mode 100644 index 00000000..d20296b7 --- /dev/null +++ b/tools/plugin_dunechat.py @@ -0,0 +1,34 @@ +"""Verify the DuneChat artifact and prepare the Arrakis public relay.""" + +import hashlib +import re + + +DISCORD_CONFIG = 'ea5460f59766315e47e343e0a6d88deb71232df065f7bde0074ab742c8f65b4d' +DISCORD_JAR = 'ef2fa1f2eb146c7c77412b7190a7dd33f1fc91282683fe45407739554c8aefef' + + +def enable_modern_discord(config): + setting = re.compile(r'(?m)^(UseModernPaperChatEvent:[ \t]*)false([ \t]*(?:#[^\r\n]*)?)(\r?\n)') + matches = list(setting.finditer(config)) + if len(matches) != 1 or len(re.findall(r'(?m)^UseModernPaperChatEvent\s*:', config)) != 1: + raise ValueError('Expected exactly one disabled modern Discord chat setting') + if re.search(r'(?m)^RespectChatPlugins\s*:', config): + raise ValueError('Discord cancellation policy differs from the reviewed config') + return setting.sub(r'\g<1>true\g<2>\g<3>RespectChatPlugins: true\g<3>', config) + + +def verify_artifact(artifact, source): + expected = { + 'DuneChat.jar', 'SOURCE_COMMIT', 'plugin_dunechat.py', 'install-plugin-fleet.py', + 'write-plugin-manifest.py', 'plugin_menu_pack.py', 'plugin_backend_abi.py', + 'plugin_fleet_rollback.py', 'plugin_pack_properties.py', 'plugin_pillager_abi.py', + } + lines = (artifact / 'SHA256SUMS').read_text().splitlines() + sums = dict(line.split(' ', 1)[::-1] for line in lines) + if len(lines) != len(sums) or sums.keys() != expected or any( + hashlib.sha256((artifact / name).read_bytes()).hexdigest() != sha for name, sha in sums.items() + ): + raise ValueError('Unexpected DuneChat archive manifest') + if (artifact / 'SOURCE_COMMIT').read_text().strip() != source: + raise ValueError('DuneChat archive source differs from the selected commit') diff --git a/tools/plugin_fleet_rollback.py b/tools/plugin_fleet_rollback.py index 4e52a532..7734702e 100644 --- a/tools/plugin_fleet_rollback.py +++ b/tools/plugin_fleet_rollback.py @@ -56,7 +56,9 @@ def rollback_receipt(receipt_path, expected_source, apply=False, run=control, le relative=entry['path'] private_jarvis = (relative == 'JarvisTranslator/config.yml' and entry.get('transform') == 'enable-dunegroups' and server['id'] == 'arrakis' and root == Path('/opt/dunerealms/servers/arrakis')) - if relative not in ('MCGUI/config.yml','DunerealmsGUI/menu-pack.yml') and not private_jarvis: + private_discord = (relative == 'DiscordSRV/config.yml' and entry.get('transform') == 'enable-modern-discord' + and server['id'] == 'arrakis' and root == Path('/opt/dunerealms/servers/arrakis')) + if relative not in ('MCGUI/config.yml','DunerealmsGUI/menu-pack.yml') and not (private_jarvis or private_discord): raise ValueError('Unsafe rollback configuration path') current=plugins/relative if current.is_symlink() or current.parent.is_symlink():raise ValueError('Symlinked rollback configuration') diff --git a/tools/test_plugin_dunechat.py b/tools/test_plugin_dunechat.py new file mode 100644 index 00000000..d3fd3a96 --- /dev/null +++ b/tools/test_plugin_dunechat.py @@ -0,0 +1,20 @@ +import unittest + +from plugin_dunechat import enable_modern_discord + + +class DiscordConfigTest(unittest.TestCase): + def test_only_the_two_reviewed_settings_change(self): + source = 'Channels: {}\r\nUseModernPaperChatEvent: false # Paper\r\nBotToken: example\r\n' + expected = source.replace('false # Paper\r\n', 'true # Paper\r\nRespectChatPlugins: true\r\n') + self.assertEqual(enable_modern_discord(source), expected) + + def test_unexpected_settings_are_rejected(self): + for source in ('UseModernPaperChatEvent: true\n', + 'UseModernPaperChatEvent: false\nRespectChatPlugins: false\n'): + with self.subTest(source=source), self.assertRaises(ValueError): + enable_modern_discord(source) + + +if __name__ == '__main__': + unittest.main()