1# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
2#
3# SPDX-License-Identifier: MPL-2.0
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0.  If a copy of the MPL was not distributed with this
7# file, you can obtain one at https://mozilla.org/MPL/2.0/.
8#
9# See the COPYRIGHT file distributed with this work for additional
10# information regarding copyright ownership.
11
12import time
13
14
15########################################################################
16# Class keyevent
17########################################################################
18class keyevent:
19    """A discrete key event, e.g., Publish, Activate, Inactive, Delete,
20    etc. Stores the date of the event, and identifying information
21    about the key to which the event will occur."""
22
23    def __init__(self, what, key, when=None):
24        self.what = what
25        self.when = when or key.gettime(what)
26        self.key = key
27        self.sep = key.sep
28        self.zone = key.name
29        self.alg = key.alg
30        self.keyid = key.keyid
31
32    def __repr__(self):
33        return repr((self.when, self.what, self.keyid, self.sep, self.zone, self.alg))
34
35    def showtime(self):
36        return time.strftime("%a %b %d %H:%M:%S UTC %Y", self.when)
37
38    # update sets of active and published keys, based on
39    # the contents of this keyevent
40    def status(self, active, published, output=None):
41        def noop(*args, **kwargs):
42            pass
43
44        if not output:
45            output = noop
46
47        if not active:
48            active = set()
49        if not published:
50            published = set()
51
52        if self.what == "Activate":
53            active.add(self.keyid)
54        elif self.what == "Publish":
55            published.add(self.keyid)
56        elif self.what == "Inactive":
57            if self.keyid not in active:
58                output(
59                    "\tWARNING: %s scheduled to become inactive "
60                    "before it is active" % repr(self.key)
61                )
62            else:
63                active.remove(self.keyid)
64        elif self.what == "Delete":
65            if self.keyid in published:
66                published.remove(self.keyid)
67            else:
68                output(
69                    "WARNING: key %s is scheduled for deletion "
70                    "before it is published" % repr(self.key)
71                )
72        elif self.what == "Revoke":
73            # We don't need to worry about the logic of this one;
74            # just stop counting this key as either active or published
75            if self.keyid in published:
76                published.remove(self.keyid)
77            if self.keyid in active:
78                active.remove(self.keyid)
79
80        return active, published
81