def test_050_match(self): line = qubespolicy.PolicyRule('$anyvm $anyvm allow') self.assertTrue(line.is_match(system_info, 'test-vm1', 'test-vm2')) line = qubespolicy.PolicyRule('$anyvm $anyvm allow') self.assertFalse(line.is_match(system_info, 'no-such-vm', 'test-vm2')) line = qubespolicy.PolicyRule('$anyvm $anyvm allow') self.assertFalse(line.is_match(system_info, 'test-vm1', 'no-such-vm'))
def test_002_init_invalid(self): rule_ask = qubespolicy.PolicyRule('$anyvm $anyvm ask') rule_allow = qubespolicy.PolicyRule('$anyvm $anyvm allow') with self.assertRaises(AssertionError): qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule_allow, 'test-vm2', None) with self.assertRaises(AssertionError): qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2', rule_allow, 'test-vm2', ['test-vm2', 'test-vm3']) with self.assertRaises(AssertionError): qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule_ask, 'test-vm2', None)
def test_030_line_invalid(self): invalid_lines = [ '@dispvm @default allow', # @dispvm can't be a source '@default @default allow', # @default can't be a source '@anyvm @default allow,target=@dispvm:@tag:tag1', # @dispvm:@tag # as override target '@anyvm @default allow,target=@tag:tag1', # @tag as override target '@anyvm @default deny,target=test-vm1', # target= used with deny '@anyvm @anyvm deny,default_target=test-vm1', # default_target= # with deny '@anyvm @anyvm deny,user=user', # user= with deny '@anyvm @anyvm invalid', # invalid action '@anyvm @anyvm allow,invalid=xx', # invalid option '@anyvm @anyvm', # missing action '@anyvm @anyvm allow,default_target=test-vm1', # default_target= # with allow '@invalid @anyvm allow', # invalid source '@anyvm @invalid deny', # invalid target '', # empty line '@anyvm @anyvm allow extra', # trailing words '@anyvm @default allow', # @default allow without target= ] for line in invalid_lines: with self.subTest(line): with self.assertRaises(qubespolicy.PolicySyntaxError): qubespolicy.PolicyRule(line, 'filename', 12)
def test_060_expand_target(self): lines = { '@anyvm @anyvm allow': [ 'test-vm1', 'test-vm2', 'test-vm3', '@dispvm:test-vm3', 'default-dvm', '@dispvm:default-dvm', 'test-invalid-dvm', 'test-no-dvm', 'test-template', 'test-standalone', '@dispvm' ], '@anyvm @dispvm allow': ['@dispvm'], '@anyvm @dispvm:default-dvm allow': ['@dispvm:default-dvm'], # no DispVM from test-vm1 allowed '@anyvm @dispvm:test-vm1 allow': [], '@anyvm @dispvm:test-vm3 allow': ['@dispvm:test-vm3'], '@anyvm @dispvm:@tag:tag1 allow': [], '@anyvm @dispvm:@tag:tag3 allow': ['@dispvm:test-vm3'], '@anyvm test-vm1 allow': ['test-vm1'], '@anyvm @type:AppVM allow': [ 'test-vm1', 'test-vm2', 'test-vm3', 'default-dvm', 'test-invalid-dvm', 'test-no-dvm' ], '@anyvm @type:TemplateVM allow': ['test-template'], '@anyvm @tag:tag1 allow': [ 'test-vm1', 'test-invalid-dvm', 'test-template', 'test-standalone', 'test-no-dvm' ], '@anyvm @tag:tag2 allow': [ 'test-vm1', 'test-vm2', 'test-invalid-dvm', 'test-template', 'test-standalone', 'test-no-dvm' ], '@anyvm @tag:no-such-tag allow': [], } for line in lines: with self.subTest(line): policy_line = qubespolicy.PolicyRule(line) self.assertCountEqual( list(policy_line.expand_target(system_info)), lines[line])
def test_011_handle_user_response(self): rule = qubespolicy.PolicyRule('@anyvm @anyvm ask') action = qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule, 'test-vm2', ['test-vm2', 'test-vm3']) with self.assertRaises(AssertionError): action.handle_user_response(True, 'test-no-dvm')
def test_012_handle_user_response(self): rule = qubespolicy.PolicyRule('$anyvm $anyvm ask') action = qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule, 'test-vm2', ['test-vm2', 'test-vm3']) with self.assertRaises(qubespolicy.AccessDenied): action.handle_user_response(False, None) self.assertEqual(action.action, qubespolicy.Action.deny)
def test_010_handle_user_response(self): rule = qubespolicy.PolicyRule('$anyvm $anyvm ask') action = qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule, 'test-vm2', ['test-vm2', 'test-vm3']) action.handle_user_response(True, 'test-vm2') self.assertEqual(action.action, qubespolicy.Action.allow) self.assertEqual(action.target, 'test-vm2')
def test_013_handle_user_response_with_default_target(self): rule = qubespolicy.PolicyRule( '@anyvm @anyvm ask,default_target=test-vm2') action = qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule, 'test-vm2', ['test-vm2', 'test-vm3']) action.handle_user_response(True, 'test-vm2') self.assertEqual(action.action, qubespolicy.Action.allow) self.assertEqual(action.target, 'test-vm2')
def test_020_execute(self, mock_subprocess, mock_qubesd_call): rule = qubespolicy.PolicyRule('$anyvm $anyvm allow') action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2', rule, 'test-vm2') action.execute('some-ident') self.assertEqual(mock_qubesd_call.mock_calls, [unittest.mock.call('test-vm2', 'admin.vm.Start')]) self.assertEqual(mock_subprocess.mock_calls, [unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'test-vm2', '-c', 'some-ident', 'DEFAULT:QUBESRPC test.service test-vm1'])])
def test_003_init_default_target(self): rule_ask = qubespolicy.PolicyRule('$anyvm $anyvm ask') action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm1', rule_ask, 'test-vm2', ['test-vm2']) self.assertIsNone(action.target) action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2', rule_ask, 'test-vm2', ['test-vm2']) self.assertEqual(action.target, 'test-vm2')
def test_020_line_simple(self): line = qubespolicy.PolicyRule('@anyvm @anyvm ask', 'filename', 12) self.assertEqual(line.filename, 'filename') self.assertEqual(line.lineno, 12) self.assertEqual(line.action, qubespolicy.Action.ask) self.assertEqual(line.source, '@anyvm') self.assertEqual(line.target, '@anyvm') self.assertEqual(line.full_action, 'ask') self.assertIsNone(line.override_target) self.assertIsNone(line.override_user) self.assertIsNone(line.default_target)
def test_021_execute_dom0(self, mock_subprocess, mock_qubesd_call): rule = qubespolicy.PolicyRule('$anyvm dom0 allow') action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'dom0', rule, 'dom0') action.execute('some-ident') self.assertEqual(mock_qubesd_call.mock_calls, []) self.assertEqual(mock_subprocess.mock_calls, [unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'dom0', '-c', 'some-ident', qubespolicy.QUBES_RPC_MULTIPLEXER_PATH + ' test.service test-vm1 dom0'])])
def test_001_init(self): rule = qubespolicy.PolicyRule('$anyvm $anyvm ask') action = qubespolicy.PolicyAction('test.service', 'test-vm1', None, rule, 'test-vm2', ['test-vm2', 'test-vm3']) self.assertEqual(action.service, 'test.service') self.assertEqual(action.source, 'test-vm1') self.assertIsNone(action.target) self.assertEqual(action.original_target, 'test-vm2') self.assertEqual(action.targets_for_ask, ['test-vm2', 'test-vm3']) self.assertEqual(action.rule, rule) self.assertEqual(action.action, qubespolicy.Action.ask)
def test_023_line_simple(self): line = qubespolicy.PolicyRule( '$anyvm $default ask,default_target=test-vm1', 'filename', 12) self.assertEqual(line.filename, 'filename') self.assertEqual(line.lineno, 12) self.assertEqual(line.action, qubespolicy.Action.ask) self.assertEqual(line.source, '$anyvm') self.assertEqual(line.target, '$default') self.assertEqual(line.full_action, 'ask,default_target=test-vm1') self.assertIsNone(line.override_target) self.assertIsNone(line.override_user) self.assertEqual(line.default_target, 'test-vm1')
def test_024_execute_startup_error(self, mock_subprocess, mock_qubesd_call): rule = qubespolicy.PolicyRule('@anyvm @anyvm allow') action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2', rule, 'test-vm2') mock_qubesd_call.side_effect = \ qubespolicy.QubesMgmtException('QubesVMError') with self.assertRaises(qubespolicy.QubesMgmtException): action.execute('some-ident') self.assertEqual(mock_qubesd_call.mock_calls, [unittest.mock.call('test-vm2', 'admin.vm.Start')]) self.assertEqual(mock_subprocess.mock_calls, [])
def test_021_execute_dom0_keyword(self, mock_subprocess, mock_qubesd_call): rule = qubespolicy.PolicyRule('@anyvm dom0 allow') action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'dom0', rule, '@adminvm') action.execute('some-ident') self.assertEqual(mock_qubesd_call.mock_calls, []) self.assertEqual(mock_subprocess.mock_calls, [ unittest.mock.call([ qubespolicy.QREXEC_CLIENT, '-d', 'dom0', '-c', 'some-ident', 'QUBESRPC test.service test-vm1 keyword adminvm' ]) ])
def test_022_line_simple(self): line = qubespolicy.PolicyRule( '@anyvm @default allow,target=@dispvm:test-vm2', 'filename', 12) self.assertEqual(line.filename, 'filename') self.assertEqual(line.lineno, 12) self.assertEqual(line.action, qubespolicy.Action.allow) self.assertEqual(line.source, '@anyvm') self.assertEqual(line.target, '@default') self.assertEqual(line.full_action, 'allow,target=@dispvm:test-vm2') self.assertEqual(line.override_target, '@dispvm:test-vm2') self.assertIsNone(line.override_user) self.assertIsNone(line.default_target)
def test_023_execute_already_running(self, mock_subprocess, mock_qubesd_call): rule = qubespolicy.PolicyRule('$anyvm $anyvm allow') action = qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2', rule, 'test-vm2') mock_qubesd_call.side_effect = \ qubespolicy.QubesMgmtException('QubesVMNotHaltedError') action.execute('some-ident') self.assertEqual(mock_qubesd_call.mock_calls, [unittest.mock.call('test-vm2', 'admin.vm.Start')]) self.assertEqual(mock_subprocess.mock_calls, [unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'test-vm2', '-c', 'some-ident', 'DEFAULT:QUBESRPC test.service test-vm1'])])
def test_021_line_simple(self): line = qubespolicy.PolicyRule( '$tag:tag1 $type:AppVM ask,target=test-vm2,user=user', 'filename', 12) self.assertEqual(line.filename, 'filename') self.assertEqual(line.lineno, 12) self.assertEqual(line.action, qubespolicy.Action.ask) self.assertEqual(line.source, '$tag:tag1') self.assertEqual(line.target, '$type:AppVM') self.assertEqual(line.full_action, 'ask,target=test-vm2,user=user') self.assertEqual(line.override_target, 'test-vm2') self.assertEqual(line.override_user, 'user') self.assertIsNone(line.default_target)
def test_022_execute_dispvm(self, mock_subprocess, mock_qubesd_call): rule = qubespolicy.PolicyRule('$anyvm $dispvm:default-dvm allow') action = qubespolicy.PolicyAction('test.service', 'test-vm1', '$dispvm:default-dvm', rule, '$dispvm:default-dvm') mock_qubesd_call.side_effect = (lambda target, call: b'dispvm-name' if call == 'admin.vm.CreateDisposable' else unittest.mock.DEFAULT) action.execute('some-ident') self.assertEqual(mock_qubesd_call.mock_calls, [unittest.mock.call('default-dvm', 'admin.vm.CreateDisposable'), unittest.mock.call('dispvm-name', 'admin.vm.Start'), unittest.mock.call('dispvm-name', 'admin.vm.Kill')]) self.assertEqual(mock_subprocess.mock_calls, [unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'dispvm-name', '-c', 'some-ident', '-W', 'DEFAULT:QUBESRPC test.service test-vm1'])])
def test_050_match(self): line = qubespolicy.PolicyRule('@anyvm @anyvm allow') self.assertTrue(line.is_match(system_info, 'test-vm1', 'test-vm2')) line = qubespolicy.PolicyRule('@anyvm @anyvm allow') self.assertFalse(line.is_match(system_info, 'no-such-vm', 'test-vm2')) line = qubespolicy.PolicyRule('@anyvm @anyvm allow') self.assertFalse(line.is_match(system_info, 'test-vm1', 'no-such-vm')) line = qubespolicy.PolicyRule('@anyvm @dispvm allow') self.assertTrue(line.is_match(system_info, 'test-vm1', '@dispvm')) line = qubespolicy.PolicyRule('@anyvm @dispvm allow') self.assertFalse( line.is_match(system_info, 'test-vm1', '@dispvm:default-dvm')) line = qubespolicy.PolicyRule('@anyvm @dispvm:default-dvm allow') self.assertTrue(line.is_match(system_info, 'test-vm1', '@dispvm')) line = qubespolicy.PolicyRule('@anyvm @dispvm:default-dvm allow') self.assertTrue( line.is_match(system_info, 'test-vm1', '@dispvm:default-dvm')) line = qubespolicy.PolicyRule('@anyvm @dispvm:@tag:tag3 allow') self.assertTrue( line.is_match(system_info, 'test-vm1', '@dispvm:test-vm3'))
def test_030_line_invalid(self): invalid_lines = [ '$dispvm $default allow', # $dispvm can't be a source '$default $default allow', # $default can't be a source '$anyvm $default deny,target=test-vm1', # target= used with deny '$anyvm $anyvm deny,default_target=test-vm1', # default_target= # with deny '$anyvm $anyvm deny,user=user', # user= with deny '$anyvm $anyvm invalid', # invalid action '$anyvm $anyvm allow,invalid=xx', # invalid option '$anyvm $anyvm', # missing action '$anyvm $anyvm allow,default_target=test-vm1', # default_target= # with allow '$invalid $anyvm allow', # invalid source '$anyvm $invalid deny', # invalid target '', # empty line '$anyvm $anyvm allow extra', # trailing words '$anyvm $default allow', # $default allow without target= ] for line in invalid_lines: with self.subTest(line): with self.assertRaises(qubespolicy.PolicySyntaxError): qubespolicy.PolicyRule(line, 'filename', 12)
def test_000_init(self): rule = qubespolicy.PolicyRule('@anyvm @anyvm deny') with self.assertRaises(qubespolicy.AccessDenied): qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2', rule, 'test-vm2')
def test_075_expand_override_target_dom0(self): line = qubespolicy.PolicyRule('@anyvm @anyvm allow,target=@adminvm') self.assertEqual( line.expand_override_target(system_info, 'test-no-dvm'), '@adminvm')
def test_073_expand_override_target_dispvm_none(self): line = qubespolicy.PolicyRule('@anyvm @anyvm allow,target=@dispvm') self.assertEqual( line.expand_override_target(system_info, 'test-no-dvm'), None)
def test_072_expand_override_target_dispvm_specific(self): line = qubespolicy.PolicyRule( '@anyvm @anyvm allow,target=@dispvm:test-vm3') self.assertEqual(line.expand_override_target(system_info, 'test-vm1'), '@dispvm:test-vm3')
def test_071_expand_override_target_dispvm(self): line = qubespolicy.PolicyRule('@anyvm @anyvm allow,target=@dispvm') self.assertEqual(line.expand_override_target(system_info, 'test-vm1'), '@dispvm:default-dvm')
def test_070_expand_override_target(self): line = qubespolicy.PolicyRule('@anyvm @anyvm allow,target=test-vm2') self.assertEqual(line.expand_override_target(system_info, 'test-vm1'), 'test-vm2')