Revision 605d23bf snf-cyclades-app/synnefo/helpdesk/views.py
b/snf-cyclades-app/synnefo/helpdesk/views.py | ||
---|---|---|
1 |
# Copyright 2012 GRNET S.A. All rights reserved. |
|
2 |
# |
|
3 |
# Redistribution and use in source and binary forms, with or |
|
4 |
# without modification, are permitted provided that the following |
|
5 |
# conditions are met: |
|
6 |
# |
|
7 |
# 1. Redistributions of source code must retain the above |
|
8 |
# copyright notice, this list of conditions and the following |
|
9 |
# disclaimer. |
|
10 |
# |
|
11 |
# 2. Redistributions in binary form must reproduce the above |
|
12 |
# copyright notice, this list of conditions and the following |
|
13 |
# disclaimer in the documentation and/or other materials |
|
14 |
# provided with the distribution. |
|
15 |
# |
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
27 |
# POSSIBILITY OF SUCH DAMAGE. |
|
28 |
# |
|
29 |
# The views and conclusions contained in the software and |
|
30 |
# documentation are those of the authors and should not be |
|
31 |
# interpreted as representing official policies, either expressed |
|
32 |
# or implied, of GRNET S.A. |
|
33 |
|
|
1 | 34 |
import re |
35 |
import logging |
|
2 | 36 |
|
3 | 37 |
from itertools import chain |
4 | 38 |
|
... | ... | |
8 | 42 |
from django.conf import settings |
9 | 43 |
from django.core.exceptions import PermissionDenied |
10 | 44 |
from django.db.models import Q |
11 |
from django.http import Http404, HttpResponse |
|
45 |
from django.http import Http404, HttpResponse, HttpResponseRedirect
|
|
12 | 46 |
from django.utils import simplejson as json |
47 |
from django.core.urlresolvers import reverse |
|
48 |
|
|
13 | 49 |
from urllib import unquote |
14 | 50 |
|
15 | 51 |
from synnefo.lib.astakos import get_user |
16 | 52 |
from synnefo.db.models import * |
17 | 53 |
|
54 |
logger = logging.getLogger(__name__) |
|
55 |
|
|
18 | 56 |
IP_SEARCH_REGEX = re.compile('([0-9]+)(?:\.[0-9]+){3}') |
19 | 57 |
|
20 | 58 |
def get_token_from_cookie(request, cookiename): |
... | ... | |
33 | 71 |
return None |
34 | 72 |
|
35 | 73 |
|
36 |
# TODO: here we mix ui setting with helpdesk settings |
|
37 |
# if sometime in the future helpdesk gets splitted from the |
|
38 |
# cyclades api code this should change and helpdesk should provide |
|
39 |
# its own setting HELPDESK_AUTH_COOKIE_NAME. |
|
40 |
AUTH_COOKIE = getattr(settings, 'UI_AUTH_COOKIE_NAME', getattr(settings, |
|
41 |
'HELPDESK_AUTH_COOKIE_NAME', '_pithos2_a')) |
|
74 |
AUTH_COOKIE_NAME = getattr(settings, 'HELPDESK_AUTH_COOKIE_NAME', getattr(settings, |
|
75 |
'UI_AUTH_COOKIE_NAME', '_pithos2_a')) |
|
76 |
PERMITTED_GROUPS = getattr(settings, 'HELPDESK_PERMITTED_GROUPS', |
|
77 |
['helpdesk']) |
|
78 |
SHOW_DELETED_VMS = getattr(settings, 'HELPDESK_SHOW_DELETED_VMS', False) |
|
79 |
|
|
80 |
|
|
81 |
def token_check(func): |
|
82 |
""" |
|
83 |
Mimic csrf security check using user auth token. |
|
84 |
""" |
|
85 |
def wrapper(request, *args, **kwargs): |
|
86 |
if not hasattr(request, 'user'): |
|
87 |
raise PermissionDenied |
|
88 |
|
|
89 |
token = request.POST.get('token', None) |
|
90 |
if token and token != request.user.get('auth_token', None): |
|
91 |
return func(request, *args, **kwargs) |
|
92 |
|
|
93 |
raise PermissionDenied |
|
94 |
|
|
95 |
return wrapper |
|
42 | 96 |
|
43 | 97 |
|
44 |
def helpdesk_user_required(func, groups=['helpdesk']):
|
|
98 |
def helpdesk_user_required(func, permitted_groups=PERMITTED_GROUPS):
|
|
45 | 99 |
""" |
46 | 100 |
Django view wrapper that checks if identified request user has helpdesk |
47 | 101 |
permissions (exists in helpdesk group) |
... | ... | |
51 | 105 |
if not HELPDESK_ENABLED: |
52 | 106 |
raise Http404 |
53 | 107 |
|
54 |
token = get_token_from_cookie(request, AUTH_COOKIE) |
|
108 |
token = get_token_from_cookie(request, AUTH_COOKIE_NAME)
|
|
55 | 109 |
get_user(request, settings.ASTAKOS_URL, fallback_token=token) |
56 | 110 |
if hasattr(request, 'user') and request.user: |
57 | 111 |
groups = request.user.get('groups', []) |
... | ... | |
59 | 113 |
if not groups: |
60 | 114 |
raise PermissionDenied |
61 | 115 |
|
116 |
has_perm = False |
|
62 | 117 |
for g in groups: |
63 |
if not g in groups: |
|
64 |
raise PermissionDenied |
|
118 |
if g in permitted_groups: |
|
119 |
has_perm = True |
|
120 |
|
|
121 |
if not has_perm: |
|
122 |
raise PermissionDenied |
|
65 | 123 |
else: |
66 | 124 |
raise PermissionDenied |
67 | 125 |
|
126 |
logging.debug("User %s accessed helpdesk view" % (request.user_uniq)) |
|
68 | 127 |
return func(request, *args, **kwargs) |
69 | 128 |
|
70 | 129 |
return wrapper |
... | ... | |
91 | 150 |
Account details view. |
92 | 151 |
""" |
93 | 152 |
|
153 |
show_deleted = bool(int(request.GET.get('deleted', SHOW_DELETED_VMS))) |
|
154 |
|
|
94 | 155 |
account_exists = True |
95 | 156 |
vms = [] |
96 | 157 |
networks = [] |
... | ... | |
104 | 165 |
except NetworkInterface.DoesNotExist: |
105 | 166 |
account_exists = False |
106 | 167 |
else: |
107 |
# all user vms |
|
108 |
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted') |
|
168 |
filter_extra = {} |
|
169 |
if not show_deleted: |
|
170 |
filter_extra['deleted'] = False |
|
109 | 171 |
|
172 |
# all user vms |
|
173 |
vms = VirtualMachine.objects.filter(userid=account, |
|
174 |
**filter_extra).order_by('deleted') |
|
110 | 175 |
# return all user private and public networks |
111 |
public_networks = Network.objects.filter(public=True).order_by('state') |
|
112 |
private_networks = Network.objects.filter(userid=account).order_by('state') |
|
176 |
public_networks = Network.objects.filter(public=True, |
|
177 |
**filter_extra).order_by('state') |
|
178 |
private_networks = Network.objects.filter(userid=account, |
|
179 |
**filter_extra).order_by('state') |
|
113 | 180 |
networks = list(public_networks) + list(private_networks) |
114 | 181 |
|
115 | 182 |
if vms.count() == 0 and private_networks.count() == 0: |
... | ... | |
120 | 187 |
'is_ip': is_ip, |
121 | 188 |
'account': account, |
122 | 189 |
'vms': vms, |
190 |
'csrf_token': request.user['auth_token'], |
|
123 | 191 |
'networks': networks, |
124 | 192 |
'UI_MEDIA_URL': settings.UI_MEDIA_URL |
125 | 193 |
} |
... | ... | |
129 | 197 |
|
130 | 198 |
|
131 | 199 |
@helpdesk_user_required |
200 |
@token_check |
|
201 |
def suspend_vm(request, vm_id): |
|
202 |
vm = VirtualMachine.objects.get(pk=vm_id) |
|
203 |
vm.suspended = True |
|
204 |
vm.save() |
|
205 |
account = vm.userid |
|
206 |
return HttpResponseRedirect(reverse('helpdesk-details', args=(account,))) |
|
207 |
|
|
208 |
|
|
209 |
@helpdesk_user_required |
|
210 |
@token_check |
|
211 |
def suspend_vm_release(request, vm_id): |
|
212 |
vm = VirtualMachine.objects.get(pk=vm_id) |
|
213 |
vm.suspended = False |
|
214 |
vm.save() |
|
215 |
account = vm.userid |
|
216 |
return HttpResponseRedirect(reverse('helpdesk-details', args=(account,))) |
|
217 |
|
|
218 |
|
|
219 |
@helpdesk_user_required |
|
132 | 220 |
def user_list(request): |
133 | 221 |
""" |
134 | 222 |
Return a json list of users based on the prefix provided. Prefix |
Also available in: Unified diff