Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / userdata / views.py @ 0dae1b9f

History | View | Annotate | Download (3.6 kB)

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