3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Filter import Filter
6 from PLC.Debug import profile
7 from PLC.Table import Row, Table
8 from PLC.KeyTypes import KeyType, KeyTypes
12 Representation of a row in the keys table. To use, instantiate with a
13 dict of values. Update as you would a dict. Commit to the database
18 primary_key = 'key_id'
19 join_tables = ['person_key', 'peer_key']
21 'key_id': Parameter(int, "Key identifier"),
22 'key_type': Parameter(str, "Key type"),
23 'key': Parameter(str, "Key value", max = 4096),
24 'person_id': Parameter(int, "User to which this key belongs", nullok = True),
25 'peer_id': Parameter(int, "Peer to which this key belongs", nullok = True),
26 'peer_key_id': Parameter(int, "Foreign key identifier at peer", nullok = True),
29 def validate_key_type(self, key_type):
30 key_types = [row['key_type'] for row in KeyTypes(self.api)]
31 if key_type not in key_types:
32 raise PLCInvalidArgument, "Invalid key type"
35 def validate_key(self, key):
36 # Key must not be blacklisted
37 rows = self.api.db.selectall("SELECT 1 from keys" \
38 " WHERE key = %(key)s" \
39 " AND is_blacklisted IS True",
42 raise PLCInvalidArgument, "Key is blacklisted and cannot be used"
53 if self['key_type'] == 'ssh':
54 # Accept only SSH version 2 keys without options. From
57 # Each protocol version 2 public key consists of: options,
58 # keytype, base64 encoded key, comment. The options field
59 # is optional...The comment field is not used for anything
60 # (but may be convenient for the user to identify the
61 # key). For protocol version 2 the keytype is ``ssh-dss''
64 good_ssh_key = r'^.*(?:ssh-dss|ssh-rsa)[ ]+[A-Za-z0-9+/=]+(?: .*)?$'
65 if not re.match(good_ssh_key, key, re.IGNORECASE):
66 raise PLCInvalidArgument, "Invalid SSH version 2 public key"
68 def blacklist(self, commit = True):
70 Permanently blacklist key (and all other identical keys),
71 preventing it from ever being added again. Because this could
72 affect multiple keys associated with multiple accounts, it
76 assert 'key_id' in self
79 # Get all matching keys
80 rows = self.api.db.selectall("SELECT key_id FROM keys WHERE key = %(key)s",
82 key_ids = [row['key_id'] for row in rows]
84 assert self['key_id'] in key_ids
86 # Keep the keys in the table
87 self.api.db.do("UPDATE keys SET is_blacklisted = True" \
88 " WHERE key_id IN (%s)" % ", ".join(map(str, key_ids)))
90 # But disassociate them from all join tables
91 for table in self.join_tables:
92 self.api.db.do("DELETE FROM %s WHERE key_id IN (%s)" % \
93 (table, ", ".join(map(str, key_ids))))
100 Representation of row(s) from the keys table in the
104 def __init__(self, api, key_filter = None, columns = None):
105 Table.__init__(self, api, Key, columns)
107 sql = "SELECT %s FROM view_keys WHERE is_blacklisted IS False" % \
108 ", ".join(self.columns)
110 if key_filter is not None:
111 if isinstance(key_filter, (list, tuple, set, int, long)):
112 key_filter = Filter(Key.fields, {'key_id': key_filter})
113 elif isinstance(key_filter, dict):
114 key_filter = Filter(Key.fields, key_filter)
116 raise PLCInvalidArgument, "Wrong key filter %r"%key_filter
117 sql += " AND (%s) %s" % key_filter.sql(api)