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