Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / userdata / views.py @ e3ff6830

History | View | Annotate | Download (3.6 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.utils import simplejson as json
37
from django.conf import settings
38

    
39
from synnefo.ui.userdata import rest
40
from synnefo.ui.userdata.models import PublicKeyPair
41
from synnefo.ui.userdata.util import exportKey
42
from snf_django.lib.astakos import get_user
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

    
54
class PublicKeyPairResourceView(rest.UserResourceView):
55
    model = PublicKeyPair
56
    exclude_fields = ["user"]
57

    
58

    
59
class PublicKeyPairCollectionView(rest.UserCollectionView):
60
    model = PublicKeyPair
61
    exclude_fields = ["user"]
62

    
63

    
64
SSH_KEY_LENGTH = getattr(settings, 'USERDATA_SSH_KEY_LENGTH', 2048)
65

    
66

    
67
def generate_key_pair(request):
68
    """
69
    Response to generate private/public RSA key pair
70
    """
71

    
72
    get_user(request, settings.ASTAKOS_BASE_URL)
73

    
74
    if request.method != "POST":
75
        return http.HttpResponseNotAllowed(["POST"])
76

    
77
    if not SUPPORT_GENERATE_KEYS:
78
        raise Exception("Application does not support ssh keys generation")
79

    
80
    if PublicKeyPair.user_limit_exceeded(request.user):
81
        raise http.HttpResponseServerError("SSH keys limit exceeded")
82

    
83
    # generate RSA key
84
    from Crypto import Random
85
    Random.atfork()
86

    
87
    key = rsakey.RSA.generate(SSH_KEY_LENGTH)
88

    
89
    # get PEM string
90
    pem = exportKey(key, 'PEM')
91

    
92
    public_data = Message()
93
    public_data.add_string('ssh-rsa')
94
    public_data.add_mpint(key.key.e)
95
    public_data.add_mpint(key.key.n)
96

    
97
    # generate public content
98
    public = str("ssh-rsa %s" % base64.b64encode(str(public_data)))
99

    
100
    data = {'private': pem, 'public': public}
101
    return http.HttpResponse(json.dumps(data), mimetype="application/json")
102

    
103

    
104
def download_private_key(request):
105
    """
106
    Return key contents
107
    """
108
    data = request.POST.get("data")
109
    name = request.POST.get("name", "key")
110

    
111
    response = http.HttpResponse(mimetype='application/x-pem-key')
112
    response['Content-Disposition'] = 'attachment; filename=%s' % name
113
    response.write(data)
114
    return response