Revision fd00a867

/dev/null
1
[
2
    {
3
        "model": "db.SynnefoUser",
4
        "pk": 1,
5
        "fields": {
6
            "name": "testdbuser",
7
            "realname" :"test db user",
8
            "uniq" :"test@synnefo.gr",
9
            "credit": 10,
10
            "auth_token": "46e427d657b20defe352804f0eb6f8a2",
11
            "auth_token_created": "2011-04-07 09:17:14",
12
            "auth_token_expires": "2015-04-07 09:17:14",
13
            "type": "STUDENT",
14
            "created": "2011-02-06"
15
   	    }
16
    }
17
]
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
from django.conf import settings
31
from django.http import HttpResponse, HttpResponseRedirect
32
from django.utils.cache import patch_vary_headers
33
from synnefo.db.models import SynnefoUser
34
from synnefo.aai.shibboleth import Tokens, register_shibboleth_user
35
import time
36

  
37
DONT_CHECK = getattr(settings, "AAI_SKIP_AUTH_URLS", ['/api', '/invitations/login'])
38

  
39
class SynnefoAuthMiddleware(object):
40

  
41
    def process_request(self, request):
42

  
43
        for path in DONT_CHECK:
44
            if request.path.startswith(path):
45
                return
46

  
47
        # Special case for testing purposes, delivers the cookie for the
48
        # test user on first access
49
        if settings.BYPASS_AUTHENTICATION and \
50
           request.GET.get('test') is not None:
51
            try:
52
                u = SynnefoUser.objects.get(
53
                    auth_token=settings.BYPASS_AUTHENTICATION_SECRET_TOKEN)
54
            except SynnefoUser.DoesNotExist:
55
                raise Exception("No user found with token matching "
56
                                "BYPASS_AUTHENTICATION_SECRET_TOKEN.")
57
            return self._redirect_shib_auth_user(user = u)
58

  
59
        token = None
60

  
61
        # Try to find token in a cookie
62
        token = request.COOKIES.get('X-Auth-Token', None)
63

  
64
        # Try to find token in request header
65
        if not token:
66
            token = request.META.get('HTTP_X_AUTH_TOKEN', None)
67

  
68
        if token:
69
            # token was found, retrieve user from backing store
70
            try:
71
                user = SynnefoUser.objects.get(auth_token=token)
72

  
73
            except SynnefoUser.DoesNotExist:
74
                return HttpResponseRedirect(settings.LOGIN_URL)
75
            # check user's auth token validity
76
            if (time.time() -
77
                time.mktime(user.auth_token_expires.timetuple())) > 0:
78
                # the user's token has expired, prompt to re-login
79
                return HttpResponseRedirect(settings.LOGIN_URL)
80

  
81
            request.user = user
82
            return
83

  
84
        # token was not found but user authenticated by Shibboleth
85
        if Tokens.SHIB_EPPN in request.META and \
86
           Tokens.SHIB_SESSION_ID in request.META:
87
            try:
88
                user = SynnefoUser.objects.get(uniq=request.META[Tokens.SHIB_EPPN])
89
                return self._redirect_shib_auth_user(user)
90
            except SynnefoUser.DoesNotExist:
91
                if register_shibboleth_user(request.META):
92
                    user = SynnefoUser.objects.get(uniq=request.META[Tokens.SHIB_EPPN])
93
                    return self._redirect_shib_auth_user(user)
94
                else:
95
                    return HttpResponseRedirect(settings.LOGIN_URL)
96

  
97
        if settings.TEST and 'TEST-AAI' in request.META:
98
            return HttpResponseRedirect(settings.LOGIN_URL)
99

  
100
        if request.path.endswith(settings.LOGIN_URL):
101
            # avoid redirect loops
102
            return
103
        else:
104
            # no authentication info found in headers, redirect back
105
            return HttpResponseRedirect(settings.LOGIN_URL)
106

  
107
    def process_response(self, request, response):
108
        # Tell proxies and other interested parties that the request varies
109
        # based on X-Auth-Token, to avoid caching of results
110
        patch_vary_headers(response, ('X-Auth-Token',))
111
        return response
112

  
113
    def _redirect_shib_auth_user(self, user):
114
        expire_fmt = user.auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
115

  
116
        response = HttpResponse()
117
        response.set_cookie('X-Auth-Token', value=user.auth_token,
118
                            expires=expire_fmt, path='/')
119
        response['X-Auth-Token'] = user.auth_token
120
        response['Location'] = settings.APP_INSTALL_URL
121
        response.status_code = 302
122
        return response
123

  
124

  
125
def add_url_exception(url):
126
    if not url in DONT_CHECK:
127
        DONT_CHECK.append(url)
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25

  
26
#
27
# The views and conclusions contained in the software and documentation are
28
# those of the authors and should not be interpreted as representing official
29
# policies, either expressed or implied, of GRNET S.A.
30

  
31
# Business Logic for working with sibbolleth users
32

  
33
from synnefo.logic import users
34

  
35
class Tokens:
36
    # these are mapped by the Shibboleth SP software
37
    SHIB_EPPN = "eppn" # eduPersonPrincipalName
38
    SHIB_NAME = "Shib-InetOrgPerson-givenName"
39
    SHIB_SURNAME = "Shib-Person-surname"
40
    SHIB_CN = "Shib-Person-commonName"
41
    SHIB_DISPLAYNAME = "Shib-InetOrgPerson-displayName"
42
    SHIB_EP_AFFILIATION = "Shib-EP-Affiliation"
43
    SHIB_SESSION_ID = "Shib-Session-ID"
44

  
45

  
46
class NoUniqueToken(BaseException):
47
    def __init__(self, msg):
48
        self.msg = msg
49

  
50

  
51
class NoRealName(BaseException):
52
    def __init__(self, msg):
53
        self.msg = msg
54

  
55

  
56
def register_shibboleth_user(tokens):
57
    """Registers a Shibboleth user using the input hash as a source for data."""
58

  
59
    if Tokens.SHIB_DISPLAYNAME in tokens:
60
        realname = tokens[Tokens.SHIB_DISPLAYNAME]
61
    elif Tokens.SHIB_CN in tokens:
62
        realname = tokens[Tokens.SHIB_CN]
63
    elif Tokens.SHIB_NAME in tokens and Tokens.SHIB_SURNAME in tokens:
64
        realname = tokens[Tokens.SHIB_NAME] + ' ' + tokens[Tokens.SHIB_SURNAME]
65
    else:
66
        raise NoRealName("Authentication does not return the user's name")
67

  
68
    try:
69
        affiliation = tokens[Tokens.SHIB_EP_AFFILIATION]
70
    except KeyError:
71
        affiliation = 'member'
72

  
73
    try:
74
        eppn = tokens[Tokens.SHIB_EPPN]
75
    except KeyError:
76
        raise NoUniqueToken("Authentication does not return a unique token")
77

  
78
    if affiliation == 'student':
79
        users.register_student(realname, '' , eppn)
80
    else:
81
        # this includes faculty but also staff, alumni, member, other, ...
82
        users.register_professor(realname, '' , eppn)
83

  
84
    return True
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Provides automated tests for aai module. The tests
31

  
32
from django.test import TestCase
33
from django.test.client import Client
34
from django.conf import settings
35

  
36
from synnefo.db.models import SynnefoUser
37

  
38
from datetime import datetime, timedelta
39

  
40
from synnefo.aai.shibboleth import Tokens
41

  
42

  
43
class AaiTestCase(TestCase):
44
    fixtures = ['users', 'api_test_data', 'auth_test_data']
45
    apibase = '/api/v1.1'
46

  
47
    def setUp(self):
48
        self.client = Client()
49

  
50
    def test_shibboleth_correct_request(self):
51
        """test request that should succeed and register a user
52
        """
53
        response = self.client.get('/index.html', {},
54
                                   **{Tokens.SHIB_NAME: 'Jimmy',
55
                                      Tokens.SHIB_EPPN: 'jh@gmail.com',
56
                                      Tokens.SHIB_CN: 'Jimmy Hendrix',
57
                                      Tokens.SHIB_SESSION_ID: '123321',
58
                                      'TEST-AAI' : 'true'})
59
        user = None
60
        try:
61
            user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
62
        except SynnefoUser.DoesNotExist:
63
            self.assertNotEqual(user, None)
64
        self.assertNotEqual(user, None)
65
        self.assertEquals(response.status_code, 302)
66
        self.assertEquals(response['Location'], settings.APP_INSTALL_URL)
67
        self.assertTrue('X-Auth-Token' in response)
68
        self.assertEquals(response['X-Auth-Token'], user.auth_token)
69
        #self.assertNotEquals(response.cookies['X-Auth-Token'].find(user.auth_token), -1)
70

  
71
    def test_shibboleth_no_uniq_request(self):
72
        """test a request with no unique field
73
        """
74
        response = self.client.get('/index.html', {},
75
                               **{Tokens.SHIB_NAME: 'Jimmy',
76
                                  Tokens.SHIB_CN: 'Jimmy Hendrix',
77
                                  'TEST-AAI': 'true'})
78
        self._test_redirect(response)
79

  
80
    def test_shibboleth_expired_token(self):
81
        """ test request from expired token
82
        """
83
        user = SynnefoUser.objects.get(uniq="test@synnefo.gr")
84
        self.assertNotEqual(user.auth_token_expires, None)
85
        user.auth_token_expires = datetime.now()
86
        user.save()
87
        response = self.client.get('/index.html', {},
88
                               **{'X-Auth-Token': user.auth_token,
89
                                  'TEST-AAI': 'true'})
90
        self._test_redirect(response)
91

  
92
    def test_shibboleth_redirect(self):
93
        """ test redirect to Sibboleth page
94
        """
95
        response = self.client.get('/index.html', {}, **{'TEST-AAI': 'true'})
96
        self._test_redirect(response)
97

  
98
    def test_shibboleth_auth(self):
99
        """ test authentication with X-Auth-Token
100
        """
101
        user = SynnefoUser.objects.get(uniq="test@synnefo.gr")
102
        response = self.client.get('/index.html', {},
103
                               **{'X-Auth-Token': user.auth_token,
104
                                  'TEST-AAI': 'true'})
105
        self.assertTrue(response.status_code, 200)
106
        self.assertTrue('Vary' in response)
107
        self.assertTrue('X-Auth-Token' in response['Vary'])
108

  
109
    def test_auth_cookie(self):
110
        user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
111
        self.client.cookies['X-Auth-Token'] = user.auth_token
112
        response = self.client.get('/', {},
113
                                   **{'X-Auth-Token': user.auth_token,
114
                                      'TEST-AAI' : 'true'})
115
        self.assertTrue(response.status_code, 200)
116
        self.assertTrue('Vary' in response)
117
        self.assertTrue('X-Auth-Token' in response['Vary'])
118

  
119
    def _test_redirect(self, response):
120
        self.assertEquals(response.status_code, 302)
121
        self.assertTrue('Location' in response)
122
        self.assertTrue(response['Location'].startswith(settings.LOGIN_URL))
123

  
/dev/null
1
# -*- coding: utf-8 -*-
2
from synnefo.util.entry_points import extend_list_from_entry_point
3
#
4
# AAI configuration
5
#####################
6

  
7
# Unauthenticated HTTP requests to the UI get redirected to this URL
8
LOGIN_URL = "/login"
9

  
10
# Set the expiration time of newly created auth tokens
11
# to be this many hours after their creation time.
12
AUTH_TOKEN_DURATION = 30 * 24
13

  
14
# Enable receiving a temporary auth token (using the ?test URL parameter) that
15
# bypasses the authentication mechanism.
16
#
17
# Make sure there is an actual user in the db whose token matches
18
# BYPASS_AUTHENTICATION_SECRET_TOKEN.
19
#
20
# WARNING, ACHTUNG, README, etc: DO NOT ENABLE THIS ON DEPLOYED VERSIONS!
21
#
22
BYPASS_AUTHENTICATION = False
23
BYPASS_AUTHENTICATION_SECRET_TOKEN = '5e41595e9e884543fa048e07c1094d74'
24

  
25
# Urls that bypass Shibboleth authentication
26
AAI_SKIP_AUTH_URLS = ['/api', '/plankton', '/invitations/login']
27
AAI_SKIP_AUTH_URLS = extend_list_from_entry_point(AAI_SKIP_AUTH_URLS, \
28
        'synnefo', 'web_skip_urls')
/dev/null
1
# -*- coding: utf-8 -*-
2
#
3
# Helpdesk application
4
#
5

  
6
# Duration for temporary auth tokens, created for impersonating a registered
7
# user by helpdesk staff.
8
HELPDESK_TOKEN_DURATION_MIN = 30
9

  
10
# IP addresses of the machines allowed to connect as help desk
11
HELPDESK_ALLOWED_IPS = ("127.0.0.1",)
12

  
/dev/null
1
# -*- coding: utf-8 -*-
2
#
3
# Invitations settings
4
##################################
5

  
6

  
7
# Max number of invitations allowed per level
8
INVITATIONS_PER_LEVEL = {
9
   #Level  #Max Invitations
10
    0   :   10000,
11
    1   :   3,
12
    2   :   2,
13
    3   :   1,
14
    4   :   0
15
}
16

  
17
# Key to encrypt X-Auth-Token with when sending invitations
18
INVITATION_ENCR_KEY = '8d342f6e7a0366c632978a80257019af'
19

  
20
# Days for which an invitation is active
21
INVITATION_VALID_DAYS = 180
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Bill Allocator - Administration script
31
#
32
# Executed monthly to issue a detailed bill for each user
33

  
34
def calculate_bills():
35
    return
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Charger - Administration script
31
#
32
# Executed hourly to charge vm usage for each user
33

  
34
from synnefo.db.models import VirtualMachine
35

  
36
from logic import credits
37

  
38
def periodically_charge():
39
    """Scan all virtual machines and charge each user"""
40
    active_vms = VirtualMachine.objects.filter(deleted=False)
41
    
42
    if not len(active_vms):
43
        print "No virtual machines found"
44
        return
45
    
46
    for vm in active_vms:
47
        # Running and Stopped is charged, else the cost is zero
48
        credits.charge(vm)
49

  
50
# vim: set ts=4 sts=4 sw=4 et ai :
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Credit Allocator - Administration script
31
#
32
# Execute once to increase user credits according to their monthly rate
33

  
34
from datetime import datetime
35

  
36
from synnefo.db.models import SynnefoUser
37
from synnefo.util.log import getLogger
38

  
39

  
40
log = getLogger('synnefo.db')
41

  
42

  
43
# main entry point
44
def allocate_credit():
45
    # Select the users that their monthly
46
    user_list = SynnefoUser.objects.all()
47
    
48
    if len(user_list) == 0:
49
        log.warning('No users found')
50
    else:
51
        log.info('Found %d user(s)', len(user_list))
52

  
53
    for user in user_list:
54
        user.allocate_credits()
55
        log.info('Adding %d credits to %s. Total: %d', user.monthly_rate,
56
                    user.name, user.credit)
57
        user.save()
/dev/null
1
[
2
    {
3
        "model": "db.Disk",
4
        "pk": 1,
5
        "fields": {
6
            "name": "My_Music",
7
            "created": "2011-02-10 00:00:00",
8
            "updated": "2011-02-10 00:00:00",
9
            "size" : "20",
10
            "vm" : "1001",
11
            "owner" : "1"
12
        }
13
    }, 
14
    {
15
        "model": "db.Disk",
16
        "pk": 2,
17
        "fields": {
18
            "name": "My_Videos",
19
            "created": "2011-02-10 00:00:00",
20
            "updated": "2011-02-10 00:00:00",
21
            "size" : "300",
22
            "vm" : "1001",
23
            "owner" : "1"
24
        }
25
    } 
26
] 
/dev/null
1
[
2
    {
3
        "model": "db.Image",
4
        "pk": 1,
5
        "fields": {
6
            "name": "Debian Base",
7
            "owner": 1,
8
            "created": "2011-02-06 00:00:00",
9
            "updated": "2011-06-29 13:14:00",
10
            "state": "ACTIVE",
11
            "backend_id": "debian_base-6.0.2.1-1",
12
            "format": "dump",
13
            "public": 1
14
        }
15
    },
16
    {
17
        "model": "db.ImageMetadata",
18
        "pk": 1,
19
        "fields": {
20
            "meta_key": "OS",
21
            "meta_value": "debian",
22
            "image": 1
23
        }
24
    },
25
    {
26
        "model": "db.ImageMetadata",
27
        "pk": 2,
28
        "fields": {
29
            "meta_key": "description",
30
            "meta_value": "Debian Squeeze Base System",
31
            "image": 1
32
        }
33
    },
34
    {
35
        "model": "db.ImageMetadata",
36
        "pk": 3,
37
        "fields": {
38
            "meta_key": "size",
39
            "meta_value": "473",
40
            "image": 1
41
        }
42
    },
43
    {
44
        "model": "db.ImageMetadata",
45
        "pk": 4,
46
        "fields": {
47
            "meta_key": "kernel",
48
            "meta_value": "2.6.32",
49
            "image": 1
50
        }
51
    },
52
    {
53
        "model": "db.ImageMetadata",
54
        "pk": 5,
55
        "fields": {
56
            "meta_key": "GUI",
57
            "meta_value": "No GUI",
58
            "image": 1
59
        }
60
    },
61
    {
62
        "model": "db.Image",
63
        "pk": 2,
64
        "fields": {
65
            "name": "Debian Desktop",
66
            "owner": 1,
67
            "created": "2011-06-29 19:33:00",
68
            "updated": "2011-02-06 19:33:00",
69
            "state": "ACTIVE",
70
            "backend_id": "debian_desktop-6.0.2.1-2",
71
            "format": "dump",
72
            "public": 1
73
        }
74
    },
75
    {
76
        "model": "db.ImageMetadata",
77
        "pk": 6,
78
        "fields": {
79
            "meta_key": "OS",
80
            "meta_value": "debian",
81
            "image": 2
82
        }
83
    },
84
    {
85
        "model": "db.ImageMetadata",
86
        "pk": 7,
87
        "fields": {
88
            "meta_key": "description",
89
            "meta_value": "Debian Squeeze Desktop",
90
            "image": 2
91
        }
92
    },
93
    {
94
        "model": "db.ImageMetadata",
95
        "pk": 8,
96
        "fields": {
97
            "meta_key": "size",
98
            "meta_value": "3261",
99
            "image": 2
100
        }
101
    },
102
    {
103
        "model": "db.ImageMetadata",
104
        "pk": 9,
105
        "fields": {
106
            "meta_key": "kernel",
107
            "meta_value": "2.6.32",
108
            "image": 2
109
        }
110
    },
111
    {
112
        "model": "db.ImageMetadata",
113
        "pk": 10,
114
        "fields": {
115
            "meta_key": "GUI",
116
            "meta_value": "GNOME 2.30",
117
            "image": 2
118
        }
119
    },
120
    {
121
        "model": "db.Image",
122
        "pk": 3,
123
        "fields": {
124
            "name": "Ubuntu",
125
            "owner": 1,
126
            "created": "2011-06-30 16:45:00",
127
            "updated": "2011-06-30 16:45:00",
128
            "state": "ACTIVE",
129
            "backend_id": "ubuntu_desktop-11.04-2",
130
            "format": "dump",
131
            "public": 1
132
        }
133
    },
134
    {
135
        "model": "db.ImageMetadata",
136
        "pk": 11,
137
        "fields": {
138
            "meta_key": "OS",
139
            "meta_value": "ubuntu",
140
            "image": 3
141
        }
142
    },
143
    {
144
        "model": "db.ImageMetadata",
145
        "pk": 12,
146
        "fields": {
147
            "meta_key": "description",
148
            "meta_value": "Ubuntu 11.04",
149
            "image": 3
150
        }
151
    },
152
    {
153
        "model": "db.ImageMetadata",
154
        "pk": 13,
155
        "fields": {
156
            "meta_key": "size",
157
            "meta_value": "2279",
158
            "image": 3
159
        }
160
    },
161
    {
162
        "model": "db.ImageMetadata",
163
        "pk": 14,
164
        "fields": {
165
            "meta_key": "kernel",
166
            "meta_value": "2.6.38",
167
            "image": 3
168
        }
169
    },
170
    {
171
        "model": "db.ImageMetadata",
172
        "pk": 15,
173
        "fields": {
174
            "meta_key": "GUI",
175
            "meta_value": "Unity/GNOME 2.32.1",
176
            "image": 3
177
        }
178
    },
179
    {
180
        "model": "db.Image",
181
        "pk": 4,
182
        "fields": {
183
            "name": "Kubuntu",
184
            "owner": 1,
185
            "created": "2011-06-30 16:45:00",
186
            "updated": "2011-06-30 16:45:00",
187
            "state": "ACTIVE",
188
            "backend_id": "kubuntu_desktop-11.04-2",
189
            "format": "dump",
190
            "public": 1
191
        }
192
    },
193
    {
194
        "model": "db.ImageMetadata",
195
        "pk": 16,
196
        "fields": {
197
            "meta_key": "OS",
198
            "meta_value": "kubuntu",
199
            "image": 4
200
        }
201
    },
202
    {
203
        "model": "db.ImageMetadata",
204
        "pk": 17,
205
        "fields": {
206
            "meta_key": "description",
207
            "meta_value": "Kubuntu 11.04",
208
            "image": 4
209
        }
210
    },
211
    {
212
        "model": "db.ImageMetadata",
213
        "pk": 18,
214
        "fields": {
215
            "meta_key": "size",
216
            "meta_value": "2270",
217
            "image": 4
218
        }
219
    },
220
    {
221
        "model": "db.ImageMetadata",
222
        "pk": 19,
223
        "fields": {
224
            "meta_key": "kernel",
225
            "meta_value": "2.6.38",
226
            "image": 4
227
        }
228
    },
229
    {
230
        "model": "db.ImageMetadata",
231
        "pk": 20,
232
        "fields": {
233
            "meta_key": "GUI",
234
            "meta_value": "KDE 4.6.2",
235
            "image": 4
236
        }
237
    },
238
    {
239
        "model": "db.Image",
240
        "pk": 5,
241
        "fields": {
242
            "name": "Fedora Desktop",
243
            "owner": 1,
244
            "created": "2011-06-30 18:54:00",
245
            "updated": "2011-06-30 18:54:00",
246
            "state": "ACTIVE",
247
            "backend_id": "fedora-15-4",
248
            "format": "dump",
249
            "public": 1
250
        }
251
    },
252
    {
253
        "model": "db.ImageMetadata",
254
        "pk": 21,
255
        "fields": {
256
            "meta_key": "OS",
257
            "meta_value": "fedora",
258
            "image": 5
259
        }
260
    },
261
    {
262
        "model": "db.ImageMetadata",
263
        "pk": 22,
264
        "fields": {
265
            "meta_key": "description",
266
            "meta_value": "Fedora 15 Desktop Edition",
267
            "image": 5
268
        }
269
    },
270
    {
271
        "model": "db.ImageMetadata",
272
        "pk": 23,
273
        "fields": {
274
            "meta_key": "size",
275
            "meta_value": "2460",
276
            "image": 5
277
        }
278
    },
279
    {
280
        "model": "db.ImageMetadata",
281
        "pk": 24,
282
        "fields": {
283
            "meta_key": "kernel",
284
            "meta_value": "2.6.38.6",
285
            "image": 5
286
        }
287
    },
288
    {
289
        "model": "db.ImageMetadata",
290
        "pk": 25,
291
        "fields": {
292
            "meta_key": "GUI",
293
            "meta_value": "GNOME 3",
294
            "image": 5
295
        }
296
    },
297
   {
298
        "model": "db.Image",
299
        "pk": 6,
300
        "fields": {
301
            "name": "CentOS",
302
            "owner": 1,
303
            "created": "2011-06-09 00:00:00",
304
            "updated": "2011-06-09 00:00:00",
305
            "state": "ACTIVE",
306
            "backend_id": "centos-5.6-1",
307
            "format": "dump",
308
            "public": 1
309
        }
310
    },
311
   {
312
        "model": "db.ImageMetadata",
313
        "pk": 26,
314
        "fields": {
315
            "meta_key": "OS",
316
            "meta_value": "centos",
317
            "image": 6 
318
        }
319
    },
320
    {
321
        "model": "db.ImageMetadata",
322
        "pk": 27,
323
        "fields": {
324
            "meta_key": "description",
325
            "meta_value": "CentOS 5.6",
326
            "image": 6
327
        }
328
    },
329
    {
330
        "model": "db.ImageMetadata",
331
        "pk": 28,
332
        "fields": {
333
            "meta_key": "kernel",
334
            "meta_value": "2.6.18",
335
            "image": 6
336
        }
337
    },
338
    {
339
        "model": "db.ImageMetadata",
340
        "pk": 29,
341
        "fields": {
342
            "meta_key": "size",
343
            "meta_value": "990",
344
            "image": 6
345
        }
346
    },
347
    {
348
        "model": "db.ImageMetadata",
349
        "pk": 30,
350
        "fields": {
351
            "meta_key": "GUI",
352
            "meta_value": "No GUI",
353
            "image": 6
354
        }
355
    },
356
   {
357
        "model": "db.Image",
358
        "pk": 7,
359
        "fields": {
360
            "name": "Windows",
361
            "owner": 1,
362
            "created": "2011-06-09 00:00:00",
363
            "updated": "2011-06-09 00:00:00",
364
            "state": "ACTIVE",
365
            "backend_id": "windows-2008R2-2",
366
            "format": "ntfsclone",
367
            "public": 1
368
        }
369
    },
370
   {
371
        "model": "db.ImageMetadata",
372
        "pk": 31,
373
        "fields": {
374
            "meta_key": "OS",
375
            "meta_value": "windows",
376
            "image": 7 
377
        }
378
    },
379
    {
380
        "model": "db.ImageMetadata",
381
        "pk": 32,
382
        "fields": {
383
            "meta_key": "description",
384
            "meta_value": "Windows 2008 R2, Aero Desktop Experience",
385
            "image": 7
386
        }
387
    },
388
    {
389
        "model": "db.ImageMetadata",
390
        "pk": 33,
391
        "fields": {
392
            "meta_key": "kernel",
393
            "meta_value": "Windows NT Kernel",
394
            "image": 7
395
        }
396
    },
397
    {
398
        "model": "db.ImageMetadata",
399
        "pk": 34,
400
        "fields": {
401
            "meta_key": "size",
402
            "meta_value": "11000",
403
            "image": 7
404
        }
405
    },
406
    {
407
        "model": "db.ImageMetadata",
408
        "pk": 35,
409
        "fields": {
410
            "meta_key": "GUI",
411
            "meta_value": "Windows, Aero Theme",
412
            "image": 7
413
        }
414
    }
415
]
416

  
/dev/null
1
[
2
    {
3
        "model": "db.SynnefoUser",
4
        "pk": 1,
5
        "fields": {
6
            "name": "testdbuser",
7
            "realname" :"test db user",
8
            "uniq" :"test@synnefo.gr",
9
            "credit": 10,
10
            "auth_token": "5e41595e9e884543fa048e07c1094d74",
11
            "auth_token_created": "2011-05-10",
12
            "auth_token_expires": "2015-05-10",
13
            "type": "ADMIN",
14
            "created": "2011-05-10",
15
            "max_invitations" : 1000
16
   	    }
17
    },
18
    {
19
        "model": "db.SynnefoUser",
20
        "pk": 2,
21
        "fields": {
22
            "name": "helpdesk",
23
            "realname" :"HelpDesk user",
24
            "uniq" :"helpdesk@synnefo.grnet.gr",
25
            "credit": 10,
26
            "auth_token": "cd7c3c159507515ac495c54bee3ff8d1",
27
            "auth_token_created": "2011-05-10",
28
            "auth_token_expires": "2015-05-10",
29
            "type": "HELPDESK",
30
            "created": "2011-05-10",
31
            "max_invitations" : 10
32
   	    }
33
    },
34
    {
35
        "model" : "db.Invitations",
36
        "pk" : 1,
37
        "fields": {
38
            "source": 1,
39
            "target": 1,
40
            "accepted": "True",
41
            "level" : -1,
42
            "created": "2011-06-01",
43
            "updated": "2011-06-01"
44
        }
45
    }
46
] 
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# bill Calculator Command - Management Script
31

  
32
from django.core.management.base import NoArgsCommand
33

  
34
from synnefo.db import bill_calculator
35

  
36
class Command(NoArgsCommand):
37
    help = ''
38
    
39
    def handle_noargs(self, **options):
40
        bill_calculator.calculate_bills()
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Charge Users Command - Management Script
31

  
32
from django.core.management.base import NoArgsCommand
33

  
34
from synnefo.db import charger
35

  
36
class Command(NoArgsCommand):
37
    help = 'Charge the users for VM usage'
38
    
39
    def handle_noargs(self, **options):
40
        charger.periodically_charge()
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Credit Allocator Command - Management Script
31

  
32
from django.core.management.base import NoArgsCommand
33

  
34
from synnefo.db import credit_allocator
35

  
36
class Command(NoArgsCommand):
37
    help = 'Add credits to users according to their monthly rate'
38
    
39
    def handle_noargs(self, **options):
40
        credit_allocator.allocate_credit()
/dev/null
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
# Run DB Control Command - Management Script
31

  
32
from django.core.management.base import NoArgsCommand
33

  
34
from synnefo.db import db_controller
35

  
36
class Command(NoArgsCommand):
37
    help = ''
38
    
39
    def handle_noargs(self, **options):
40
        db_controller.main()
/dev/null
1
# vim: set fileencoding=utf-8 :
2
# Copyright 2011 GRNET S.A. All rights reserved.
3
#
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions
6
# are met:
7
#
8
#   1. Redistributions of source code must retain the above copyright
9
#      notice, this list of conditions and the following disclaimer.
10
#
11
#  2. Redistributions in binary form must reproduce the above copyright
12
#     notice, this list of conditions and the following disclaimer in the
13
#     documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# SUCH DAMAGE.
26
#
27
# The views and conclusions contained in the software and documentation are
28
# those of the authors and should not be interpreted as representing official
29
# policies, either expressed or implied, of GRNET S.A.
30
import json
31
import time 
32

  
33
from django.views.decorators.csrf import csrf_protect
34
from django.template.loader import render_to_string
35
from django.template.context import RequestContext
36
from django.http import HttpResponse, HttpResponseBadRequest
37
from synnefo.db.models import SynnefoUser, Invitations
38
from synnefo.api.common import method_not_allowed
39
from synnefo.logic import users
40

  
41
@csrf_protect
42
def index(request):
43

  
44
    if request.method == 'GET':
45
        data = render_to_string('helpdesk.html',
46
                                {'users': get_users(request)},
47
                                context_instance=RequestContext(request))
48
        return HttpResponse(data)
49
    else:
50
        method_not_allowed(request)
51

  
52
def get_users(request):
53
    #XXX: The following filter should change when the invitations app is removed
54
    invitations = Invitations.objects.filter(accepted = False)
55
    ids = map(lambda x: x.target.id, invitations)
56
    users = SynnefoUser.objects.exclude(id__in = ids)\
57
                               .exclude(type__exact = "HELPDESK")\
58
                               .order_by('realname')
59
    result = []
60

  
61
    for user in users:
62
        resultentry = {}
63

  
64
        resultentry['id'] = user.id
65
        resultentry['name'] = user.realname
66

  
67
        result.append(resultentry)
68

  
69
    return result
70

  
71
def get_tmp_token(request):
72

  
73
    try:
74
        user_id = request.GET['user_id']
75
    except KeyError:
76
        return HttpResponseBadRequest()
77

  
78
    user = SynnefoUser.objects.get(id = user_id)
79

  
80
    if user is None:
81
        return HttpResponseBadRequest()
82

  
83
    if  user.tmp_auth_token_expires is None or \
84
        time.time() - time.mktime(user.tmp_auth_token_expires.timetuple()) > 0:
85
        users.create_tmp_token(user)
86

  
87
    token = dict()
88
    token['token'] = user.tmp_auth_token
89
    token['expires'] = int(time.mktime(user.tmp_auth_token_expires.timetuple()))
90

  
91
    response = HttpResponse(json.dumps(token))
92

  
93
    expire_fmt = user.tmp_auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
94
    response.set_cookie('X-Auth-Tmp-Token', value=user.tmp_auth_token,
95
                            expires = expire_fmt,
96
                            path='/')
97
    return response
/dev/null
1
# vim: set fileencoding=utf-8 :
2
# Copyright 2011 GRNET S.A. All rights reserved.
3
#
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions
6
# are met:
7
#
8
#   1. Redistributions of source code must retain the above copyright
9
#      notice, this list of conditions and the following disclaimer.
10
#
11
#  2. Redistributions in binary form must reproduce the above copyright
12
#     notice, this list of conditions and the following disclaimer in the
13
#     documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# SUCH DAMAGE.
26
#
27
# The views and conclusions contained in the software and documentation are
28
# those of the authors and should not be interpreted as representing official
29
# policies, either expressed or implied, of GRNET S.A.
30

  
31
from synnefo.aai import middleware
32
from synnefo.db.models import SynnefoUser
33
from django.conf import settings
34
from django.http import HttpResponse
35
import time
36

  
37
class HelpdeskMiddleware(object):
38

  
39
    auth_tmp_token = "X-Auth-Tmp-Token"
40
    install_path  = "/helpdesk"
41

  
42
    def __init__(self):
43
       middleware.add_url_exception(self.install_path)
44

  
45
    def process_request(self, request):
46

  
47
        if not request.path.startswith('/helpdesk'):
48
            if not 'X-Auth-Tmp-Token' in request.COOKIES:
49
                return 
50

  
51
        # Check the request's IP address
52
        allowed = settings.HELPDESK_ALLOWED_IPS
53
        if not check_ip(request.META['REMOTE_ADDR'], allowed):
54
            try:
55
                proxy_ip = request.META['HTTP_X_FORWARDED_FOR']
56
            except Exception:
57
                return HttpResponse(status=403,
58
                                    content="IP Address not allowed")
59
            if not check_ip(proxy_ip, allowed):
60
                return HttpResponse(status=403,
61
                                    content="IP Address not allowed")
62

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff