~lazr-developers/lazr.sshserver/trunk

« back to all changes in this revision

Viewing changes to src/lazr/sshserver/events.py

  • Committer: Jürgen Gmach
  • Date: 2021-10-31 16:38:55 UTC
  • Revision ID: juergen.gmach@canonical.com-20211031163855-b2brmahmbih8ho37
Moved to git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010-2015 Canonical Ltd.  This software is licensed under the
2
 
# GNU Lesser General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Events generated by the SSH server."""
5
 
 
6
 
from __future__ import absolute_import, print_function
7
 
 
8
 
__metaclass__ = type
9
 
__all__ = [
10
 
    'AuthenticationFailed',
11
 
    'AvatarEvent',
12
 
    'ILoggingEvent',
13
 
    'LoggingEvent',
14
 
    'ServerStarting',
15
 
    'ServerStopped',
16
 
    'SFTPClosed',
17
 
    'SFTPStarted',
18
 
    'UserConnected',
19
 
    'UserDisconnected',
20
 
    'UserLoggedIn',
21
 
    'UserLoggedOut',
22
 
    ]
23
 
 
24
 
import logging
25
 
 
26
 
from zope.interface import (
27
 
    Attribute,
28
 
    implementer,
29
 
    Interface,
30
 
    )
31
 
 
32
 
 
33
 
class ILoggingEvent(Interface):
34
 
    """An event is a logging event if it has a message and a severity level.
35
 
 
36
 
    Events that provide this interface will be logged in the SSH server access
37
 
    log.
38
 
    """
39
 
 
40
 
    level = Attribute("The level to log the event at.")
41
 
    message = Attribute("The message to log.")
42
 
 
43
 
 
44
 
@implementer(ILoggingEvent)
45
 
class LoggingEvent:
46
 
    """An event that can be logged to a Python logger.
47
 
 
48
 
    :ivar level: The level to log itself as. This should be defined as a
49
 
        class variable in subclasses.
50
 
    :ivar template: The format string of the message to log. This should be
51
 
        defined as a class variable in subclasses.
52
 
    """
53
 
 
54
 
    def __init__(self, level=None, template=None, **data):
55
 
        """Construct a logging event.
56
 
 
57
 
        :param level: The level to log the event as. If specified, overrides
58
 
            the 'level' class variable.
59
 
        :param template: The format string of the message to log. If
60
 
            specified, overrides the 'template' class variable.
61
 
        :param **data: Information to be logged. Entries will be substituted
62
 
            into the template and stored as attributes.
63
 
        """
64
 
        if level is not None:
65
 
            self._level = level
66
 
        if template is not None:
67
 
            self.template = template
68
 
        self._data = data
69
 
 
70
 
    @property
71
 
    def level(self):
72
 
        """See `ILoggingEvent`."""
73
 
        return self._level
74
 
 
75
 
    @property
76
 
    def message(self):
77
 
        """See `ILoggingEvent`."""
78
 
        return self.template % self._data
79
 
 
80
 
 
81
 
class ServerStarting(LoggingEvent):
82
 
 
83
 
    level = logging.INFO
84
 
    template = '---- Server started ----'
85
 
 
86
 
 
87
 
class ServerStopped(LoggingEvent):
88
 
 
89
 
    level = logging.INFO
90
 
    template = '---- Server stopped ----'
91
 
 
92
 
 
93
 
class UserConnected(LoggingEvent):
94
 
 
95
 
    level = logging.INFO
96
 
    template = '[%(session_id)s] %(address)s connected.'
97
 
 
98
 
    def __init__(self, transport, address):
99
 
        LoggingEvent.__init__(
100
 
            self, session_id=id(transport), address=address)
101
 
 
102
 
 
103
 
class AuthenticationFailed(LoggingEvent):
104
 
 
105
 
    level = logging.INFO
106
 
    template = '[%(session_id)s] failed to authenticate.'
107
 
 
108
 
    def __init__(self, transport):
109
 
        LoggingEvent.__init__(self, session_id=id(transport))
110
 
 
111
 
 
112
 
class UserDisconnected(LoggingEvent):
113
 
 
114
 
    level = logging.INFO
115
 
    template = '[%(session_id)s] disconnected.'
116
 
 
117
 
    def __init__(self, transport):
118
 
        LoggingEvent.__init__(self, session_id=id(transport))
119
 
 
120
 
 
121
 
class AvatarEvent(LoggingEvent):
122
 
    """Base avatar event."""
123
 
 
124
 
    level = logging.INFO
125
 
 
126
 
    def __init__(self, avatar):
127
 
        self.avatar = avatar
128
 
        LoggingEvent.__init__(
129
 
            self, session_id=id(avatar.transport), username=avatar.username)
130
 
 
131
 
 
132
 
class UserLoggedIn(AvatarEvent):
133
 
 
134
 
    template = '[%(session_id)s] %(username)s logged in.'
135
 
 
136
 
 
137
 
class UserLoggedOut(AvatarEvent):
138
 
 
139
 
    template = '[%(session_id)s] %(username)s disconnected.'
140
 
 
141
 
 
142
 
class SFTPStarted(AvatarEvent):
143
 
 
144
 
    template = '[%(session_id)s] %(username)s started SFTP session.'
145
 
 
146
 
 
147
 
class SFTPClosed(AvatarEvent):
148
 
 
149
 
    template = '[%(session_id)s] %(username)s closed SFTP session.'