/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-10 03:33:46 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190310033346-be20o4gcr3sjkr91
mandos-ctl: Refactor

* mandos-ctl (ValueArgumentMixIn): Remove.
  (PropertyValueCmd): New proper subclass of PropertyCmd; replaces
                      ValueArgumentMixIn.
  (MillisecondsValueArgumentMixIn): Remove.
  (MillisecondsPropertyValueArgumentCmd): New subclass of
            PropertyValueCmd; replaces MillisecondsValueArgumentMixIn.
  (TestValueArgumentPropertyCmd): Rename to "TestPropertyValueCmd".  All users changed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
317
317
    def propname(self):
318
318
        raise NotImplementedError()
319
319
 
320
 
class ValueArgumentMixIn(object):
321
 
    """Mixin class for commands taking a value as argument"""
 
320
class PropertyValueCmd(PropertyCmd):
 
321
    """Abstract class for PropertyCmd recieving a value as argument"""
322
322
    def __init__(self, value):
323
323
        self.value_to_set = value
324
324
 
325
 
class MillisecondsValueArgumentMixIn(ValueArgumentMixIn):
326
 
    """Mixin class for commands taking a value argument as
327
 
    milliseconds."""
 
325
class MillisecondsPropertyValueArgumentCmd(PropertyValueCmd):
 
326
    """Abstract class for PropertyValueCmd taking a value argument as
 
327
a datetime.timedelta() but should store it as milliseconds."""
328
328
    @property
329
329
    def value_to_set(self):
330
330
        return self._vts
331
331
    @value_to_set.setter
332
332
    def value_to_set(self, value):
333
 
        """When setting, convert value to a datetime.timedelta"""
 
333
        """When setting, convert value from a datetime.timedelta"""
334
334
        self._vts = int(round(value.total_seconds() * 1000))
335
335
 
336
336
# Actual (non-abstract) command classes
493
493
    propname = "ApprovedByDefault"
494
494
    value_to_set = dbus.Boolean(False)
495
495
 
496
 
class SetCheckerCmd(PropertyCmd, ValueArgumentMixIn):
 
496
class SetCheckerCmd(PropertyValueCmd):
497
497
    propname = "Checker"
498
498
 
499
 
class SetHostCmd(PropertyCmd, ValueArgumentMixIn):
 
499
class SetHostCmd(PropertyValueCmd):
500
500
    propname = "Host"
501
501
 
502
 
class SetSecretCmd(PropertyCmd, ValueArgumentMixIn):
 
502
class SetSecretCmd(PropertyValueCmd):
503
503
    propname = "Secret"
504
504
    @property
505
505
    def value_to_set(self):
510
510
        self._vts = value.read()
511
511
        value.close()
512
512
 
513
 
class SetTimeoutCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
 
513
class SetTimeoutCmd(MillisecondsPropertyValueArgumentCmd):
514
514
    propname = "Timeout"
515
515
 
516
 
class SetExtendedTimeoutCmd(PropertyCmd,
517
 
                            MillisecondsValueArgumentMixIn):
 
516
class SetExtendedTimeoutCmd(MillisecondsPropertyValueArgumentCmd):
518
517
    propname = "ExtendedTimeout"
519
518
 
520
 
class SetIntervalCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
 
519
class SetIntervalCmd(MillisecondsPropertyValueArgumentCmd):
521
520
    propname = "Interval"
522
521
 
523
 
class SetApprovalDelayCmd(PropertyCmd,
524
 
                          MillisecondsValueArgumentMixIn):
 
522
class SetApprovalDelayCmd(MillisecondsPropertyValueArgumentCmd):
525
523
    propname = "ApprovalDelay"
526
524
 
527
 
class SetApprovalDurationCmd(PropertyCmd,
528
 
                             MillisecondsValueArgumentMixIn):
 
525
class SetApprovalDurationCmd(MillisecondsPropertyValueArgumentCmd):
529
526
    propname = "ApprovalDuration"
530
527
 
531
528
def add_command_line_options(parser):
1130
1127
    propname = "ApprovedByDefault"
1131
1128
    values_to_set = [dbus.Boolean(False)]
1132
1129
 
1133
 
class TestValueArgumentPropertyCmd(TestPropertyCmd):
1134
 
    """Abstract class for tests of PropertyCmd classes using the
1135
 
ValueArgumentMixIn"""
 
1130
class TestPropertyValueCmd(TestPropertyCmd):
 
1131
    """Abstract class for tests of PropertyValueCmd classes"""
1136
1132
    def runTest(self):
1137
 
        if type(self) is TestValueArgumentPropertyCmd:
 
1133
        if type(self) is TestPropertyValueCmd:
1138
1134
            return
1139
 
        return super(TestValueArgumentPropertyCmd, self).runTest()
 
1135
        return super(TestPropertyValueCmd, self).runTest()
1140
1136
    def run_command(self, value, clients):
1141
1137
        self.command(value).run(clients, self.bus)
1142
1138
 
1143
 
class TestSetCheckerCmd(TestValueArgumentPropertyCmd):
 
1139
class TestSetCheckerCmd(TestPropertyValueCmd):
1144
1140
    command = SetCheckerCmd
1145
1141
    propname = "Checker"
1146
1142
    values_to_set = ["", ":", "fping -q -- %s"]
1147
1143
 
1148
 
class TestSetHostCmd(TestValueArgumentPropertyCmd):
 
1144
class TestSetHostCmd(TestPropertyValueCmd):
1149
1145
    command = SetHostCmd
1150
1146
    propname = "Host"
1151
1147
    values_to_set = ["192.0.2.3", "foo.example.org"]
1152
1148
 
1153
 
class TestSetSecretCmd(TestValueArgumentPropertyCmd):
 
1149
class TestSetSecretCmd(TestPropertyValueCmd):
1154
1150
    command = SetSecretCmd
1155
1151
    propname = "Secret"
1156
1152
    values_to_set = [io.BytesIO(b""),
1157
1153
                     io.BytesIO(b"secret\0xyzzy\nbar")]
1158
1154
    values_to_get = [b"", b"secret\0xyzzy\nbar"]
1159
1155
 
1160
 
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
 
1156
class TestSetTimeoutCmd(TestPropertyValueCmd):
1161
1157
    command = SetTimeoutCmd
1162
1158
    propname = "Timeout"
1163
1159
    values_to_set = [datetime.timedelta(),
1167
1163
                     datetime.timedelta(weeks=52)]
1168
1164
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1169
1165
 
1170
 
class TestSetExtendedTimeoutCmd(TestValueArgumentPropertyCmd):
 
1166
class TestSetExtendedTimeoutCmd(TestPropertyValueCmd):
1171
1167
    command = SetExtendedTimeoutCmd
1172
1168
    propname = "ExtendedTimeout"
1173
1169
    values_to_set = [datetime.timedelta(),
1177
1173
                     datetime.timedelta(weeks=52)]
1178
1174
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1179
1175
 
1180
 
class TestSetIntervalCmd(TestValueArgumentPropertyCmd):
 
1176
class TestSetIntervalCmd(TestPropertyValueCmd):
1181
1177
    command = SetIntervalCmd
1182
1178
    propname = "Interval"
1183
1179
    values_to_set = [datetime.timedelta(),
1187
1183
                     datetime.timedelta(weeks=52)]
1188
1184
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1189
1185
 
1190
 
class TestSetApprovalDelayCmd(TestValueArgumentPropertyCmd):
 
1186
class TestSetApprovalDelayCmd(TestPropertyValueCmd):
1191
1187
    command = SetApprovalDelayCmd
1192
1188
    propname = "ApprovalDelay"
1193
1189
    values_to_set = [datetime.timedelta(),
1197
1193
                     datetime.timedelta(weeks=52)]
1198
1194
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1199
1195
 
1200
 
class TestSetApprovalDurationCmd(TestValueArgumentPropertyCmd):
 
1196
class TestSetApprovalDurationCmd(TestPropertyValueCmd):
1201
1197
    command = SetApprovalDurationCmd
1202
1198
    propname = "ApprovalDuration"
1203
1199
    values_to_set = [datetime.timedelta(),