/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-07 19:15:13 UTC
  • Revision ID: teddy@recompile.se-20190307191513-xr13bq6bhfkzaiud
mandos-ctl: Refactor

* mandos-ctl (has_action): Move into main(), where is it being used.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
import json
43
43
import unittest
44
44
import logging
 
45
import io
 
46
import tempfile
45
47
 
46
48
import dbus
47
49
 
475
477
    property = "Host"
476
478
 
477
479
class SetSecretCmd(PropertyCmd, ValueArgumentMixIn):
 
480
    @property
 
481
    def value_to_set(self):
 
482
        return self._vts
 
483
    @value_to_set.setter
 
484
    def value_to_set(self, value):
 
485
        """When setting, read data from supplied file object"""
 
486
        self._vts = value.read()
 
487
        value.close()
478
488
    property = "Secret"
479
489
 
480
490
class SetTimeoutCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
495
505
                             MillisecondsValueArgumentMixIn):
496
506
    property = "ApprovalDuration"
497
507
 
498
 
def has_actions(options):
499
 
    return any((options.enable,
500
 
                options.disable,
501
 
                options.bump_timeout,
502
 
                options.start_checker,
503
 
                options.stop_checker,
504
 
                options.is_enabled,
505
 
                options.remove,
506
 
                options.checker is not None,
507
 
                options.timeout is not None,
508
 
                options.extended_timeout is not None,
509
 
                options.interval is not None,
510
 
                options.approved_by_default is not None,
511
 
                options.approval_delay is not None,
512
 
                options.approval_duration is not None,
513
 
                options.host is not None,
514
 
                options.secret is not None,
515
 
                options.approve,
516
 
                options.deny))
517
 
 
518
508
def add_command_line_options(parser):
519
509
    parser.add_argument("--version", action="version",
520
510
                        version="%(prog)s {}".format(version),
609
599
        commands.append(RemoveCmd())
610
600
 
611
601
    if options.checker is not None:
612
 
        commands.append(SetCheckerCmd())
 
602
        commands.append(SetCheckerCmd(options.checker))
613
603
 
614
604
    if options.timeout is not None:
615
605
        commands.append(SetTimeoutCmd(options.timeout))
619
609
            SetExtendedTimeoutCmd(options.extended_timeout))
620
610
 
621
611
    if options.interval is not None:
622
 
        command.append(SetIntervalCmd(options.interval))
 
612
        commands.append(SetIntervalCmd(options.interval))
623
613
 
624
614
    if options.approved_by_default is not None:
625
615
        if options.approved_by_default:
626
 
            command.append(ApproveByDefaultCmd())
 
616
            commands.append(ApproveByDefaultCmd())
627
617
        else:
628
 
            command.append(DenyByDefaultCmd())
 
618
            commands.append(DenyByDefaultCmd())
629
619
 
630
620
    if options.approval_delay is not None:
631
 
        command.append(SetApprovalDelayCmd(options.approval_delay))
 
621
        commands.append(SetApprovalDelayCmd(options.approval_delay))
632
622
 
633
623
    if options.approval_duration is not None:
634
 
        command.append(
 
624
        commands.append(
635
625
            SetApprovalDurationCmd(options.approval_duration))
636
626
 
637
627
    if options.host is not None:
638
 
        command.append(SetHostCmd(options.host))
 
628
        commands.append(SetHostCmd(options.host))
639
629
 
640
630
    if options.secret is not None:
641
 
        command.append(SetSecretCmd(options.secret))
 
631
        commands.append(SetSecretCmd(options.secret))
642
632
 
643
633
    if options.approve:
644
634
        commands.append(ApproveCmd())
661
651
 
662
652
    options = parser.parse_args()
663
653
 
 
654
    def has_actions(options):
 
655
        return any((options.enable,
 
656
                    options.disable,
 
657
                    options.bump_timeout,
 
658
                    options.start_checker,
 
659
                    options.stop_checker,
 
660
                    options.is_enabled,
 
661
                    options.remove,
 
662
                    options.checker is not None,
 
663
                    options.timeout is not None,
 
664
                    options.extended_timeout is not None,
 
665
                    options.interval is not None,
 
666
                    options.approved_by_default is not None,
 
667
                    options.approval_delay is not None,
 
668
                    options.approval_duration is not None,
 
669
                    options.host is not None,
 
670
                    options.secret is not None,
 
671
                    options.approve,
 
672
                    options.deny))
 
673
 
664
674
    if has_actions(options) and not (options.client or options.all):
665
675
        parser.error("Options require clients names or --all.")
666
676
    if options.verbose and has_actions(options):
1083
1093
class TestSetSecretCmd(TestValueArgumentPropertyCmd):
1084
1094
    command = SetSecretCmd
1085
1095
    property = "Secret"
1086
 
    values_to_set = [b"", b"secret"]
 
1096
    values_to_set = [open("/dev/null", "rb"),
 
1097
                     io.BytesIO(b"secret\0xyzzy\nbar")]
 
1098
    values_to_get = [b"", b"secret\0xyzzy\nbar"]
1087
1099
 
1088
1100
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
1089
1101
    command = SetTimeoutCmd
1115
1127
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1116
1128
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
1117
1129
 
1118
 
class TestOptions(unittest.TestCase):
 
1130
class Test_command_from_options(unittest.TestCase):
1119
1131
    def setUp(self):
1120
1132
        self.parser = argparse.ArgumentParser()
1121
1133
        add_command_line_options(self.parser)
1122
 
    def commands_from_args(self, args):
1123
 
        self.options = self.parser.parse_args(args)
1124
 
        return commands_from_options(self.options)
1125
 
    def test_default_is_show_table(self):
1126
 
        commands = self.commands_from_args([])
1127
 
        self.assertEqual(len(commands), 1)
1128
 
        command = commands[0]
1129
 
        self.assertIsInstance(command, PrintTableCmd)
1130
 
        self.assertEqual(command.verbose, False)
1131
 
    def test_show_table_verbose(self):
1132
 
        commands = self.commands_from_args(["--verbose"])
1133
 
        self.assertEqual(len(commands), 1)
1134
 
        command = commands[0]
1135
 
        self.assertIsInstance(command, PrintTableCmd)
1136
 
        self.assertEqual(command.verbose, True)
 
1134
    def assert_command_from_args(self, args, command_cls, **cmd_attrs):
 
1135
        """Assert that parsing ARGS should result in an instance of
 
1136
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
 
1137
        options = self.parser.parse_args(args)
 
1138
        commands = commands_from_options(options)
 
1139
        self.assertEqual(len(commands), 1)
 
1140
        command = commands[0]
 
1141
        self.assertIsInstance(command, command_cls)
 
1142
        for key, value in cmd_attrs.items():
 
1143
            self.assertEqual(getattr(command, key), value)
 
1144
    def test_print_table(self):
 
1145
        self.assert_command_from_args([], PrintTableCmd,
 
1146
                                      verbose=False)
 
1147
 
 
1148
    def test_print_table_verbose(self):
 
1149
        self.assert_command_from_args(["--verbose"], PrintTableCmd,
 
1150
                                      verbose=True)
 
1151
 
1137
1152
    def test_enable(self):
1138
 
        commands = self.commands_from_args(["--enable", "foo"])
1139
 
        self.assertEqual(len(commands), 1)
1140
 
        command = commands[0]
1141
 
        self.assertIsInstance(command, EnableCmd)
1142
 
        self.assertEqual(self.options.client, ["foo"])
 
1153
        self.assert_command_from_args(["--enable", "foo"], EnableCmd)
 
1154
 
1143
1155
    def test_disable(self):
1144
 
        commands = self.commands_from_args(["--disable", "foo"])
1145
 
        self.assertEqual(len(commands), 1)
1146
 
        command = commands[0]
1147
 
        self.assertIsInstance(command, DisableCmd)
1148
 
        self.assertEqual(self.options.client, ["foo"])
 
1156
        self.assert_command_from_args(["--disable", "foo"],
 
1157
                                      DisableCmd)
 
1158
 
 
1159
    def test_bump_timeout(self):
 
1160
        self.assert_command_from_args(["--bump-timeout", "foo"],
 
1161
                                      BumpTimeoutCmd)
 
1162
 
 
1163
    def test_start_checker(self):
 
1164
        self.assert_command_from_args(["--start-checker", "foo"],
 
1165
                                      StartCheckerCmd)
 
1166
 
 
1167
    def test_stop_checker(self):
 
1168
        self.assert_command_from_args(["--stop-checker", "foo"],
 
1169
                                      StopCheckerCmd)
 
1170
 
 
1171
    def test_remove(self):
 
1172
        self.assert_command_from_args(["--remove", "foo"],
 
1173
                                      RemoveCmd)
 
1174
 
 
1175
    def test_checker(self):
 
1176
        self.assert_command_from_args(["--checker", ":", "foo"],
 
1177
                                      SetCheckerCmd, value_to_set=":")
 
1178
 
 
1179
    def test_checker_empty(self):
 
1180
        self.assert_command_from_args(["--checker", "", "foo"],
 
1181
                                      SetCheckerCmd, value_to_set="")
 
1182
 
 
1183
    def test_timeout(self):
 
1184
        self.assert_command_from_args(["--timeout", "PT5M", "foo"],
 
1185
                                      SetTimeoutCmd,
 
1186
                                      value_to_set=300000)
 
1187
 
 
1188
    def test_extended_timeout(self):
 
1189
        self.assert_command_from_args(["--extended-timeout", "PT15M",
 
1190
                                       "foo"],
 
1191
                                      SetExtendedTimeoutCmd,
 
1192
                                      value_to_set=900000)
 
1193
 
 
1194
    def test_interval(self):
 
1195
        self.assert_command_from_args(["--interval", "PT2M", "foo"],
 
1196
                                      SetIntervalCmd,
 
1197
                                      value_to_set=120000)
 
1198
 
 
1199
    def test_approve_by_default(self):
 
1200
        self.assert_command_from_args(["--approve-by-default", "foo"],
 
1201
                                      ApproveByDefaultCmd)
 
1202
 
 
1203
    def test_deny_by_default(self):
 
1204
        self.assert_command_from_args(["--deny-by-default", "foo"],
 
1205
                                      DenyByDefaultCmd)
 
1206
 
 
1207
    def test_approval_delay(self):
 
1208
        self.assert_command_from_args(["--approval-delay", "PT30S",
 
1209
                                       "foo"], SetApprovalDelayCmd,
 
1210
                                      value_to_set=30000)
 
1211
 
 
1212
    def test_approval_duration(self):
 
1213
        self.assert_command_from_args(["--approval-duration", "PT1S",
 
1214
                                       "foo"], SetApprovalDurationCmd,
 
1215
                                      value_to_set=1000)
 
1216
 
 
1217
    def test_host(self):
 
1218
        self.assert_command_from_args(["--host", "foo.example.org",
 
1219
                                       "foo"], SetHostCmd,
 
1220
                                      value_to_set="foo.example.org")
 
1221
 
 
1222
    def test_secret_devnull(self):
 
1223
        self.assert_command_from_args(["--secret", os.path.devnull,
 
1224
                                       "foo"], SetSecretCmd,
 
1225
                                      value_to_set=b"")
 
1226
 
 
1227
    def test_secret_tempfile(self):
 
1228
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
 
1229
            value = b"secret\0xyzzy\nbar"
 
1230
            f.write(value)
 
1231
            f.seek(0)
 
1232
            self.assert_command_from_args(["--secret", f.name,
 
1233
                                           "foo"], SetSecretCmd,
 
1234
                                          value_to_set=value)
 
1235
 
 
1236
    def test_approve(self):
 
1237
        self.assert_command_from_args(["--approve", "foo"],
 
1238
                                      ApproveCmd)
 
1239
 
 
1240
    def test_deny(self):
 
1241
        self.assert_command_from_args(["--deny", "foo"], DenyCmd)
 
1242
 
 
1243
    def test_dump_json(self):
 
1244
        self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
 
1245
 
 
1246
    def test_is_enabled(self):
 
1247
        self.assert_command_from_args(["--is-enabled", "foo"],
 
1248
                                      IsEnabledCmd)
1149
1249
 
1150
1250
 
1151
1251