"""Focused local bootstrap guards. No SSH, service or RMM job is executed.""" import copy from datetime import datetime, timezone, timedelta import hashlib from pathlib import Path import tempfile import unittest from unittest.mock import patch import uuid import system_helper_bootstrap as helper class BootstrapGuards(unittest.TestCase): def test_passive_public_policy_uses_exact_roles_and_empty_effect_allowlist(self): import json binding=dict(tenant_id=str(uuid.uuid4()),asset_id=helper.ASSET_ID,agent_id=helper.AGENT_ID,enrollment_generation=1,assignment_version=1,public_key_hex='11'*32) raw,pin,sid,policy=helper.configurations(binding,'22'*32) settings=json.loads(raw) self.assertEqual(settings['allowed_services'],[]) self.assertIs(settings['enable_effects'],False) self.assertIs(settings['allow_reboot'],False) self.assertEqual(settings['agent']['user_sid'],'S-1-5-19') self.assertEqual(settings['helper']['user_sid'],'S-1-5-18') self.assertEqual(settings['helper']['service_sid'],sid) self.assertEqual(hashlib.sha256(raw).hexdigest(),policy) self.assertEqual(json.loads(pin)['policy_sha256'],policy) # MS-LSAT documented ALG vector confirms ordering and byte endianness. self.assertEqual(helper.service_sid('ALG'),'S-1-5-80-2387347252-3645287876-2469496166-3824418187-3586569773') def test_lost_install_reply_cannot_replay_mutation(self): with tempfile.TemporaryDirectory() as temporary: state=Path(temporary);ident=str(uuid.uuid4());folder=state/ident;folder.mkdir(mode=0o700) index=helper.store(state,'index.json',{'fixture':True}) helper.store(folder,'intent.json',{'source_file_sha256':helper.sha(helper.__file__),'payload':{'id':ident,'source_sha256':hashlib.sha256(helper.SCRIPT.encode()).hexdigest()},'current_runtime':{'path':str(index),'sha256':helper.sha(index)}}) helper.store(folder,'prepared.json',{'prepared':True}) with patch.object(helper,'STATE',state),patch.object(helper.cb,'powershell',side_effect=RuntimeError('simulated lost SSH reply')) as rpc: with self.assertRaisesRegex(RuntimeError,'simulated lost'): helper.run(ident,'install') with self.assertRaises(FileExistsError): helper.run(ident,'install') self.assertEqual(rpc.call_count,1) self.assertTrue((folder/'install-attempt.json').exists()) def test_admin_or_stale_or_ready_receipt_is_not_positive_pipe_acceptance(self): payload={'helper_service_sid':helper.service_sid('RmmDiscoverySystemHelper'),'policy_sha256':'33'*32} probe=dict(schema_version=1,source='actual_local_service_runtime',agent_process_id=100,agent_service='RmmDiscoveryWindows',agent_id=helper.AGENT_ID,asset_id=helper.ASSET_ID,authenticated_pipe_exchange=True,client_service_pid_verified_by_helper=True,verified_helper_user_sid='S-1-5-18',verified_helper_service_sid=payload['helper_service_sid'],helper_binary_sha256=helper.HELPER_SHA,policy_sha256=payload['policy_sha256'],helper_clock_qualified=False,helper_ready=False,system_capability_advertised=False,no_jobs=True,observed_at=datetime.now(timezone.utc).isoformat(),challenge=str(uuid.uuid4()),ledger_file_id={'volume_serial':1,'file_id':[1]*16},executor_lock_file_id={'volume_serial':1,'file_id':[2]*16}) result=dict(native_system_service_running=True,owner_sid='S-1-5-18',enable_effects=False,allow_reboot=False,allowed_services=[],ledger_exists=True,executor_lock_exists=True,agent={'process_id':100},agent_probe=probe) helper.verify_peer(result,payload) for field,value in [('source','administrator_ssh_probe'),('agent_process_id',999),('helper_ready',True),('helper_clock_qualified',True),('policy_sha256','44'*32),('observed_at',(datetime.now(timezone.utc)-timedelta(seconds=100)).isoformat())]: bad=copy.deepcopy(result);bad['agent_probe'][field]=value with self.assertRaises(Exception,msg=field):helper.verify_peer(bad,payload) if __name__ == '__main__': unittest.main()