X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FKeys.py;h=1103f7cf352c7628d6c9a29e6262be08fe795a7c;hb=1d3540bd2ece27d91a2ec5843628c5fa38a25024;hp=ab8d7a60d1b8281ccce7a77ed0e4e50052200bf3;hpb=fa5d899360159b86660bf76ef0fce8f4109760cb;p=plcapi.git diff --git a/PLC/Keys.py b/PLC/Keys.py index ab8d7a6..1103f7c 100644 --- a/PLC/Keys.py +++ b/PLC/Keys.py @@ -1,4 +1,3 @@ -# $Id$ import re from PLC.Faults import * @@ -10,8 +9,8 @@ from PLC.KeyTypes import KeyType, KeyTypes class Key(Row): """ - Representation of a row in the keys table. To use, instantiate with a - dict of values. Update as you would a dict. Commit to the database + Representation of a row in the keys table. To use, instantiate with a + dict of values. Update as you would a dict. Commit to the database with sync(). """ @@ -30,19 +29,19 @@ class Key(Row): def validate_key_type(self, key_type): key_types = [row['key_type'] for row in KeyTypes(self.api)] if key_type not in key_types: - raise PLCInvalidArgument, "Invalid key type" - return key_type + raise PLCInvalidArgument("Invalid key type") + return key_type def validate_key(self, key): - # Key must not be blacklisted - rows = self.api.db.selectall("SELECT 1 from keys" \ - " WHERE key = %(key)s" \ + # Key must not be blacklisted + rows = self.api.db.selectall("SELECT 1 from keys" \ + " WHERE key = %(key)s" \ " AND is_blacklisted IS True", locals()) - if rows: - raise PLCInvalidArgument, "Key is blacklisted and cannot be used" + if rows: + raise PLCInvalidArgument("Key is blacklisted and cannot be used") - return key + return key def validate(self): # Basic validation @@ -64,17 +63,17 @@ class Key(Row): good_ssh_key = r'^.*(?:ssh-dss|ssh-rsa)[ ]+[A-Za-z0-9+/=]+(?: .*)?$' if not re.match(good_ssh_key, key, re.IGNORECASE): - raise PLCInvalidArgument, "Invalid SSH version 2 public key" + raise PLCInvalidArgument("Invalid SSH version 2 public key") def blacklist(self, commit = True): """ - Permanently blacklist key (and all other identical keys), - preventing it from ever being added again. Because this could - affect multiple keys associated with multiple accounts, it - should be admin only. - """ + Permanently blacklist key (and all other identical keys), + preventing it from ever being added again. Because this could + affect multiple keys associated with multiple accounts, it + should be admin only. + """ - assert 'key_id' in self + assert 'key_id' in self assert 'key' in self # Get all matching keys @@ -88,7 +87,7 @@ class Key(Row): self.api.db.do("UPDATE keys SET is_blacklisted = True" \ " WHERE key_id IN (%s)" % ", ".join(map(str, key_ids))) - # But disassociate them from all join tables + # But disassociate them from all join tables for table in self.join_tables: self.api.db.do("DELETE FROM %s WHERE key_id IN (%s)" % \ (table, ", ".join(map(str, key_ids)))) @@ -104,15 +103,17 @@ class Keys(Table): def __init__(self, api, key_filter = None, columns = None): Table.__init__(self, api, Key, columns) - - sql = "SELECT %s FROM view_keys WHERE is_blacklisted IS False" % \ + + sql = "SELECT %s FROM view_keys WHERE is_blacklisted IS False" % \ ", ".join(self.columns) if key_filter is not None: - if isinstance(key_filter, (list, tuple, set)): + if isinstance(key_filter, (list, tuple, set, int)): key_filter = Filter(Key.fields, {'key_id': key_filter}) elif isinstance(key_filter, dict): key_filter = Filter(Key.fields, key_filter) + else: + raise PLCInvalidArgument("Wrong key filter %r"%key_filter) sql += " AND (%s) %s" % key_filter.sql(api) - self.selectall(sql) + self.selectall(sql)