1# Copyright 2014 Google Inc.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software Foundation,
15# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16"""An implementation of a sshkey map for nsscache.
17
18SshkeyMap:  An implementation of NSS sshkey maps based on the Map
19class.
20
21SshkeyMapEntry:  A sshkey map entry based on the MapEntry class.
22"""
23
24__author__ = 'mimianddaniel@gmail.com'
25
26from nss_cache.maps import maps
27
28
29class SshkeyMap(maps.Map):
30    """This class represents an NSS sshkey map.
31
32    Map data is stored as a list of MapEntry objects, see the abstract
33    class Map.
34    """
35
36    def Add(self, entry):
37        """Add a new object, verify it is a SshkeyMapEntry instance.
38
39        Args:
40          entry: A SshkeyMapEntry instance.
41
42        Returns:
43          True if added successfully, False otherwise.
44
45        Raises:
46          TypeError: The argument is of the wrong type.
47        """
48        if not isinstance(entry, SshkeyMapEntry):
49            raise TypeError
50        return super(SshkeyMap, self).Add(entry)
51
52
53class SshkeyMapEntry(maps.MapEntry):
54    """This class represents NSS sshkey map entries."""
55    # Using slots saves us over 2x memory on large maps.
56    __slots__ = ('name', 'sshkey')
57    _KEY = 'name'
58    _ATTRS = ('name', 'sshkey')
59
60    def __init__(self, data=None):
61        """Construct a SshkeyMapEntry, setting reasonable defaults."""
62        self.name = None
63        self.sshkey = None
64
65        super(SshkeyMapEntry, self).__init__(data)
66        # Seed data with defaults if still empty
67        if self.sshkey is None:
68            self.sshkey = ''
69