root / snf-app / synnefo / ui / userdata / views.py @ 483c9197
History | View | Annotate | Download (3.5 kB)
1 |
#
|
---|---|
2 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
3 |
#
|
4 |
# Redistribution and use in source and binary forms, with or
|
5 |
# without modification, are permitted provided that the following
|
6 |
# conditions are met:
|
7 |
#
|
8 |
# 1. Redistributions of source code must retain the above
|
9 |
# copyright notice, this list of conditions and the following
|
10 |
# disclaimer.
|
11 |
#
|
12 |
# 2. Redistributions in binary form must reproduce the above
|
13 |
# copyright notice, this list of conditions and the following
|
14 |
# disclaimer in the documentation and/or other materials
|
15 |
# provided with the distribution.
|
16 |
#
|
17 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
# POSSIBILITY OF SUCH DAMAGE.
|
29 |
#
|
30 |
# The views and conclusions contained in the software and
|
31 |
# documentation are those of the authors and should not be
|
32 |
# interpreted as representing official policies, either expressed
|
33 |
# or implied, of GRNET S.A.
|
34 |
|
35 |
from django import http |
36 |
from django.template import RequestContext, loader |
37 |
from django.utils import simplejson as json |
38 |
from django.conf import settings |
39 |
|
40 |
from synnefo.ui.userdata import rest |
41 |
from synnefo.ui.userdata.models import PublicKeyPair |
42 |
from synnefo.ui.userdata.util import exportKey |
43 |
|
44 |
SUPPORT_GENERATE_KEYS = True
|
45 |
try:
|
46 |
from paramiko import rsakey |
47 |
from paramiko.message import Message |
48 |
except ImportError, e: |
49 |
SUPPORT_GENERATE_KEYS = False
|
50 |
|
51 |
import base64 |
52 |
|
53 |
class PublicKeyPairResourceView(rest.UserResourceView): |
54 |
model = PublicKeyPair |
55 |
exclude_fields = ["user"]
|
56 |
|
57 |
class PublicKeyPairCollectionView(rest.UserCollectionView): |
58 |
model = PublicKeyPair |
59 |
exclude_fields = ["user"]
|
60 |
|
61 |
SSH_KEY_LENGTH = getattr(settings, 'USERDATA_SSH_KEY_LENGTH', 2048) |
62 |
def generate_key_pair(request): |
63 |
"""
|
64 |
Response to generate private/public RSA key pair
|
65 |
"""
|
66 |
|
67 |
if request.method != "POST": |
68 |
return http.HttpResponseNotAllowed(["POST"]) |
69 |
|
70 |
if not SUPPORT_GENERATE_KEYS: |
71 |
raise Exception("Application does not support ssh keys generation") |
72 |
|
73 |
if PublicKeyPair.user_limit_exceeded(request.user):
|
74 |
raise http.HttpResponseServerError("SSH keys limit exceeded"); |
75 |
|
76 |
|
77 |
# generate RSA key
|
78 |
from Crypto import Random |
79 |
Random.atfork() |
80 |
|
81 |
key = rsakey.RSA.generate(SSH_KEY_LENGTH); |
82 |
|
83 |
# get PEM string
|
84 |
pem = exportKey(key, 'PEM')
|
85 |
|
86 |
public_data = Message() |
87 |
public_data.add_string('ssh-rsa')
|
88 |
public_data.add_mpint(key.key.e) |
89 |
public_data.add_mpint(key.key.n) |
90 |
|
91 |
# generate public content
|
92 |
public = str("ssh-rsa %s" % base64.b64encode(str(public_data))) |
93 |
|
94 |
data = {'private': pem, 'public': public} |
95 |
return http.HttpResponse(json.dumps(data), mimetype="application/json") |
96 |
|
97 |
def download_private_key(request): |
98 |
"""
|
99 |
Return key contents
|
100 |
"""
|
101 |
data = request.POST.get("data")
|
102 |
name = request.POST.get("name", "key") |
103 |
|
104 |
response = http.HttpResponse(mimetype='application/x-pem-key')
|
105 |
response['Content-Disposition'] = 'attachment; filename=%s.pem' % name |
106 |
response.write(data) |
107 |
return response
|
108 |
|