root / snf-cyclades-app / synnefo / helpdesk / views.py @ ba8ff608
History | View | Annotate | Download (8.4 kB)
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 |
|
34 |
import re |
35 |
import logging |
36 |
|
37 |
from django.shortcuts import redirect |
38 |
from django.views.generic.simple import direct_to_template |
39 |
from django.conf import settings |
40 |
from django.core.exceptions import PermissionDenied |
41 |
from django.http import Http404, HttpResponseRedirect |
42 |
from django.core.urlresolvers import reverse |
43 |
|
44 |
from urllib import unquote |
45 |
|
46 |
from synnefo.lib.astakos import get_user |
47 |
from synnefo.db.models import VirtualMachine, NetworkInterface, Network |
48 |
from synnefo.lib import astakos |
49 |
|
50 |
logger = logging.getLogger(__name__) |
51 |
|
52 |
IP_SEARCH_REGEX = re.compile('([0-9]+)(?:\.[0-9]+){3}')
|
53 |
UUID_SEARCH_REGEX = re.compile('([0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12})')
|
54 |
VM_SEARCH_REGEX = re.compile('vm(-){0,}(?P<vmid>[0-9]+)')
|
55 |
|
56 |
|
57 |
|
58 |
def get_token_from_cookie(request, cookiename): |
59 |
"""
|
60 |
Extract token from the cookie name provided. Cookie should be in the same
|
61 |
form as astakos service sets its cookie contents::
|
62 |
|
63 |
<user_uniq>|<user_token>
|
64 |
"""
|
65 |
try:
|
66 |
cookie_content = unquote(request.COOKIES.get(cookiename, None))
|
67 |
return cookie_content.split("|")[1] |
68 |
except AttributeError: |
69 |
pass
|
70 |
|
71 |
return None |
72 |
|
73 |
|
74 |
AUTH_COOKIE_NAME = getattr(settings, 'HELPDESK_AUTH_COOKIE_NAME', |
75 |
getattr(settings, 'UI_AUTH_COOKIE_NAME', |
76 |
'_pithos2_a'))
|
77 |
PERMITTED_GROUPS = getattr(settings, 'HELPDESK_PERMITTED_GROUPS', ['helpdesk']) |
78 |
SHOW_DELETED_VMS = getattr(settings, 'HELPDESK_SHOW_DELETED_VMS', False) |
79 |
|
80 |
# guess cyclades setting too
|
81 |
USER_CATALOG_URL = getattr(settings, 'CYCLADES_USER_CATALOG_URL', None) |
82 |
USER_CATALOG_URL = getattr(settings, 'HELPDESK_USER_CATALOG_URL', |
83 |
USER_CATALOG_URL) |
84 |
|
85 |
|
86 |
def token_check(func): |
87 |
"""
|
88 |
Mimic csrf security check using user auth token.
|
89 |
"""
|
90 |
def wrapper(request, *args, **kwargs): |
91 |
if not hasattr(request, 'user'): |
92 |
raise PermissionDenied
|
93 |
|
94 |
token = request.POST.get('token', None) |
95 |
if token and token != request.user.get('auth_token', None): |
96 |
return func(request, *args, **kwargs)
|
97 |
|
98 |
raise PermissionDenied
|
99 |
|
100 |
return wrapper
|
101 |
|
102 |
|
103 |
def helpdesk_user_required(func, permitted_groups=PERMITTED_GROUPS): |
104 |
"""
|
105 |
Django view wrapper that checks if identified request user has helpdesk
|
106 |
permissions (exists in helpdesk group)
|
107 |
"""
|
108 |
def wrapper(request, *args, **kwargs): |
109 |
HELPDESK_ENABLED = getattr(settings, 'HELPDESK_ENABLED', True) |
110 |
if not HELPDESK_ENABLED: |
111 |
raise Http404
|
112 |
|
113 |
token = get_token_from_cookie(request, AUTH_COOKIE_NAME) |
114 |
get_user(request, settings.ASTAKOS_URL, fallback_token=token) |
115 |
if hasattr(request, 'user') and request.user: |
116 |
groups = request.user.get('groups', [])
|
117 |
|
118 |
if not groups: |
119 |
raise PermissionDenied
|
120 |
|
121 |
has_perm = False
|
122 |
for g in groups: |
123 |
if g in permitted_groups: |
124 |
has_perm = True
|
125 |
|
126 |
if not has_perm: |
127 |
raise PermissionDenied
|
128 |
else:
|
129 |
raise PermissionDenied
|
130 |
|
131 |
logging.debug("User %s accessed helpdesk view" % (request.user_uniq))
|
132 |
return func(request, *args, **kwargs)
|
133 |
|
134 |
return wrapper
|
135 |
|
136 |
|
137 |
@helpdesk_user_required
|
138 |
def index(request): |
139 |
"""
|
140 |
Helpdesk index view.
|
141 |
"""
|
142 |
|
143 |
# if form submitted redirect to details
|
144 |
account = request.GET.get('account', None) |
145 |
if account:
|
146 |
return redirect('synnefo.helpdesk.views.account', |
147 |
search_query=account) |
148 |
|
149 |
# show index template
|
150 |
return direct_to_template(request, "helpdesk/index.html") |
151 |
|
152 |
|
153 |
@helpdesk_user_required
|
154 |
def account(request, search_query): |
155 |
"""
|
156 |
Account details view.
|
157 |
"""
|
158 |
|
159 |
show_deleted = bool(int(request.GET.get('deleted', SHOW_DELETED_VMS))) |
160 |
|
161 |
account_exists = True
|
162 |
vms = [] |
163 |
networks = [] |
164 |
is_ip = IP_SEARCH_REGEX.match(search_query) |
165 |
is_uuid = UUID_SEARCH_REGEX.match(search_query) |
166 |
is_vm = VM_SEARCH_REGEX.match(search_query) |
167 |
account_name = search_query |
168 |
auth_token = request.user.get('auth_token')
|
169 |
|
170 |
if is_ip:
|
171 |
try:
|
172 |
nic = NetworkInterface.objects.get(ipv4=search_query) |
173 |
search_query = nic.machine.userid |
174 |
is_uuid = True
|
175 |
except NetworkInterface.DoesNotExist:
|
176 |
account_exists = False
|
177 |
account = None
|
178 |
|
179 |
if is_vm:
|
180 |
vmid = is_vm.groupdict().get('vmid')
|
181 |
try:
|
182 |
vm = VirtualMachine.objects.get(pk=int(vmid))
|
183 |
search_query = vm.userid |
184 |
is_uuid = True
|
185 |
except VirtualMachine.DoesNotExist:
|
186 |
account_exists = False
|
187 |
account = None
|
188 |
search_query = vmid |
189 |
|
190 |
if is_uuid:
|
191 |
account = search_query |
192 |
account_name = astakos.get_displayname(auth_token, account, |
193 |
USER_CATALOG_URL) |
194 |
|
195 |
if account_exists and not is_uuid: |
196 |
account_name = search_query |
197 |
account = astakos.get_user_uuid(auth_token, account_name, |
198 |
USER_CATALOG_URL) |
199 |
|
200 |
if not account: |
201 |
account_exists = False
|
202 |
|
203 |
filter_extra = {} |
204 |
if not show_deleted: |
205 |
filter_extra['deleted'] = False |
206 |
|
207 |
# all user vms
|
208 |
vms = VirtualMachine.objects.filter(userid=account, |
209 |
**filter_extra).order_by('deleted')
|
210 |
# return all user private and public networks
|
211 |
public_networks = Network.objects.filter(public=True,
|
212 |
nics__machine__userid=account, |
213 |
**filter_extra |
214 |
).order_by('state').distinct()
|
215 |
private_networks = Network.objects.filter(userid=account, |
216 |
**filter_extra).order_by('state')
|
217 |
networks = list(public_networks) + list(private_networks) |
218 |
|
219 |
if vms.count() == 0 and private_networks.count() == 0: |
220 |
account_exists = False
|
221 |
|
222 |
user_context = { |
223 |
'account_exists': account_exists,
|
224 |
'is_ip': is_ip,
|
225 |
'is_vm': is_vm,
|
226 |
'is_uuid': is_uuid,
|
227 |
'account': account,
|
228 |
'search_query': search_query,
|
229 |
'vms': vms,
|
230 |
'show_deleted': show_deleted,
|
231 |
'account_name': account_name,
|
232 |
'csrf_token': request.user['auth_token'], |
233 |
'networks': networks,
|
234 |
'UI_MEDIA_URL': settings.UI_MEDIA_URL
|
235 |
} |
236 |
|
237 |
return direct_to_template(request, "helpdesk/account.html", |
238 |
extra_context=user_context) |
239 |
|
240 |
|
241 |
@helpdesk_user_required
|
242 |
@token_check
|
243 |
def suspend_vm(request, vm_id): |
244 |
vm = VirtualMachine.objects.get(pk=vm_id) |
245 |
vm.suspended = True
|
246 |
vm.save() |
247 |
account = vm.userid |
248 |
return HttpResponseRedirect(reverse('helpdesk-details', args=(account,))) |
249 |
|
250 |
|
251 |
@helpdesk_user_required
|
252 |
@token_check
|
253 |
def suspend_vm_release(request, vm_id): |
254 |
vm = VirtualMachine.objects.get(pk=vm_id) |
255 |
vm.suspended = False
|
256 |
vm.save() |
257 |
account = vm.userid |
258 |
return HttpResponseRedirect(reverse('helpdesk-details', args=(account,))) |