6f828384ac2f9a9fb1cadd18a1c2f667cd7f4beb
[plcapi.git] / PLC / Keys.py
1 import re
2
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
9 from PLC.NovaTable import NovaObject, NovaTable
10
11 class Key(NovaObject):
12     """
13     Representation of a row in the keys table. To use, instantiate with a
14     dict of values. Update as you would a dict. Commit to the database
15     with sync().
16     """
17
18     tablename = 'keys'
19     join_tables = ['person_key', 'peer_key']
20     fields = {
21         'id': Parameter(str, "Key identifier", primary_key=True),
22         #'key_type': Parameter(str, "Key type"),
23         'public_key': Parameter(str, "Key string", max = 4096),
24         'name': Parameter(str, "Key name",)
25         }
26
27     def sync(self, insert = False, validate = True):
28         NovaObject.sync(self, insert, validate)
29         if insert == True or 'id' not in self:
30             self.object = self.api.client_shell.nova.keypairs.create(self.id,
31                                                                      self.key)
32         else:
33             self.object = self.api.client_shell.nova.keypairs.update(self.id, dict(self))
34
35     def delete(self):
36         assert 'id' in self
37         self.api.client_shell.nova.keypairs.delete(self.id)
38
39     def validate_public_key(self, key):
40         # Key must not be blacklisted
41         pass
42         return key
43
44     def validate_name(self, name):
45         keys = Keys(self.api, name)
46         if keys:
47             raise PLCInvalidArgument, "Key name alredy in use"
48
49     def validate(self):
50         # Basic validation
51         NovaObject.validate(self)
52
53         assert 'public_key' in self
54         key = self['public_key']
55
56         if self['key_type'] == 'ssh':
57             # Accept only SSH version 2 keys without options. From
58             # sshd(8):
59             #
60             # Each protocol version 2 public key consists of: options,
61             # keytype, base64 encoded key, comment.  The options field
62             # is optional...The comment field is not used for anything
63             # (but may be convenient for the user to identify the
64             # key). For protocol version 2 the keytype is ``ssh-dss''
65             # or ``ssh-rsa''.
66
67             good_ssh_key = r'^.*(?:ssh-dss|ssh-rsa)[ ]+[A-Za-z0-9+/=]+(?: .*)?$'
68             if not re.match(good_ssh_key, key, re.IGNORECASE):
69                 raise PLCInvalidArgument, "Invalid SSH version 2 public key"
70
71     def blacklist(self, commit = True):
72         """
73         Permanently blacklist key (and all other identical keys),
74         preventing it from ever being added again. Because this could
75         affect multiple keys associated with multiple accounts, it
76         should be admin only.
77         """
78
79         assert 'key_id' in self
80         assert 'key' in self
81
82         # Get all matching keys
83         rows = self.api.db.selectall("SELECT key_id FROM keys WHERE key = %(key)s",
84                                      self)
85         key_ids = [row['key_id'] for row in rows]
86         assert key_ids
87         assert self['key_id'] in key_ids
88
89         # Keep the keys in the table
90         self.api.db.do("UPDATE keys SET is_blacklisted = True" \
91                        " WHERE key_id IN (%s)" % ", ".join(map(str, key_ids)))
92
93         # But disassociate them from all join tables
94         for table in self.join_tables:
95             self.api.db.do("DELETE FROM %s WHERE key_id IN (%s)" % \
96                            (table, ", ".join(map(str, key_ids))))
97
98         if commit:
99             self.api.db.commit()
100
101 class Keys(NovaTable):
102     """
103     Representation of row(s) from the keys table in the
104     database.
105     """
106
107     def __init__(self, api, key_filter = None, columns = None):
108         self.api = api
109         keysManager = self.api.client_shell.nova.keypairs
110         keyPairs = []
111
112         if key_filter is not None:
113             if isinstance(key_filter, (list, tuple, set, int, long)):
114                 keyPairs = filter(lambda kp: kp.uuid in key_filter,
115                                   keysManager.findall())
116             elif isinstance(key_filter, dict):
117                 keyPairs = keysManager.findall(**key_filter)
118             elif isinstnace(key_filter, StringTypes):
119                 keyPairs = keyManagers.findall(uuid = key_filter)
120             else:
121                 raise PLCInvalidArgument, "Wrong key filter %r"%key_filter
122
123         self.extend(keyPairs)
124