~smoser/ubuntu/trusty/maas/lp-1172566

« back to all changes in this revision

Viewing changes to src/maasserver/models/sshkey.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2014-04-03 13:45:02 UTC
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: package-import@ubuntu.com-20140403134502-8a6wvuqwyuekufh0
Tags: upstream-1.5+bzr2227
ImportĀ upstreamĀ versionĀ 1.5+bzr2227

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    ]
18
18
 
19
19
 
20
 
import binascii
21
20
from cgi import escape
22
21
 
23
22
from django.contrib.auth.models import User
28
27
    TextField,
29
28
    )
30
29
from django.utils.safestring import mark_safe
31
 
from maasserver import DefaultMeta
 
30
from maasserver import (
 
31
    DefaultMeta,
 
32
    logger,
 
33
    )
32
34
from maasserver.models.cleansave import CleanSave
33
35
from maasserver.models.timestampedmodel import TimestampedModel
34
 
from twisted.conch.ssh.keys import (
35
 
    BadKeyError,
36
 
    Key,
37
 
    )
 
36
from twisted.conch.ssh.keys import Key
38
37
 
39
38
 
40
39
class SSHKeyManager(Manager):
49
48
    """Validate that the given value contains a valid SSH public key."""
50
49
    try:
51
50
        key = Key.fromString(value)
 
51
    except Exception:
 
52
        # twisted.conch.ssh.keys.Key.fromString raises all sorts of exceptions.
 
53
        # Here, we catch them all and return a ValidationError since this
 
54
        # method only aims at validating keys and not return the exact cause of
 
55
        # the failure.
 
56
        logger.exception("Invalid SSH public key")
 
57
        raise ValidationError("Invalid SSH public key.")
 
58
    else:
52
59
        if not key.isPublic():
53
60
            raise ValidationError(
54
61
                "Invalid SSH public key (this key is a private key).")
55
 
    except (BadKeyError, binascii.Error, UnicodeEncodeError):
56
 
        raise ValidationError("Invalid SSH public key.")
57
62
 
58
63
 
59
64
HELLIPSIS = '…'