Statistics
| Branch: | Tag: | Revision:

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

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.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
from synnefo.lib.astakos import get_user
44

    
45
SUPPORT_GENERATE_KEYS = True
46
try:
47
    from paramiko import rsakey
48
    from paramiko.message import Message
49
except ImportError, e:
50
    SUPPORT_GENERATE_KEYS = False
51

    
52
import base64
53

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

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

    
62
SSH_KEY_LENGTH = getattr(settings, 'USERDATA_SSH_KEY_LENGTH', 2048)
63
def generate_key_pair(request):
64
    """
65
    Response to generate private/public RSA key pair
66
    """
67

    
68
    get_user(request, settings.ASTAKOS_URL)
69

    
70
    if request.method != "POST":
71
        return http.HttpResponseNotAllowed(["POST"])
72

    
73
    if not SUPPORT_GENERATE_KEYS:
74
        raise Exception("Application does not support ssh keys generation")
75

    
76
    if PublicKeyPair.user_limit_exceeded(request.user):
77
        raise http.HttpResponseServerError("SSH keys limit exceeded");
78

    
79

    
80
    # generate RSA key
81
    from Crypto import Random
82
    Random.atfork()
83

    
84
    key = rsakey.RSA.generate(SSH_KEY_LENGTH);
85

    
86
    # get PEM string
87
    pem = exportKey(key, 'PEM')
88

    
89
    public_data = Message()
90
    public_data.add_string('ssh-rsa')
91
    public_data.add_mpint(key.key.e)
92
    public_data.add_mpint(key.key.n)
93

    
94
    # generate public content
95
    public = str("ssh-rsa %s" % base64.b64encode(str(public_data)))
96

    
97
    data = {'private': pem, 'public': public}
98
    return http.HttpResponse(json.dumps(data), mimetype="application/json")
99

    
100
def download_private_key(request):
101
    """
102
    Return key contents
103
    """
104
    data = request.POST.get("data")
105
    name = request.POST.get("name", "key")
106

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