~ubuntu-branches/ubuntu/trusty/heat/trusty-security

« back to all changes in this revision

Viewing changes to heat/openstack/common/context.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Yolanda Robla, Chuck Short
  • Date: 2013-07-22 16:22:29 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130722162229-zzvfu40id94ii0hc
Tags: 2013.2~b2-0ubuntu1
[ Yolanda Robla ]
* debian/tests: added autopkg tests

[ Chuck Short ]
* New upstream release
* debian/control:
  - Add python-pbr to build-depends.
  - Add python-d2to to build-depends.
  - Dropped python-argparse.
  - Add python-six to build-depends.
  - Dropped python-sendfile.
  - Dropped python-nose.
  - Added testrepository.
  - Added python-testtools.
* debian/rules: Run testrepository instead of nosetets.
* debian/patches/removes-lxml-version-limitation-from-pip-requires.patch: Dropped
  no longer needed.
* debian/patches/fix-package-version-detection-when-building-doc.patch: Dropped
  no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 OpenStack Foundation.
 
4
# All Rights Reserved.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
"""
 
19
Simple class that stores security context information in the web request.
 
20
 
 
21
Projects should subclass this class if they wish to enhance the request
 
22
context or provide additional information in their specific WSGI pipeline.
 
23
"""
 
24
 
 
25
import itertools
 
26
 
 
27
from heat.openstack.common import uuidutils
 
28
 
 
29
 
 
30
def generate_request_id():
 
31
    return 'req-%s' % uuidutils.generate_uuid()
 
32
 
 
33
 
 
34
class RequestContext(object):
 
35
 
 
36
    """Helper class to represent useful information about a request context.
 
37
 
 
38
    Stores information about the security context under which the user
 
39
    accesses the system, as well as additional request information.
 
40
    """
 
41
 
 
42
    def __init__(self, auth_token=None, user=None, tenant=None, is_admin=False,
 
43
                 read_only=False, show_deleted=False, request_id=None):
 
44
        self.auth_token = auth_token
 
45
        self.user = user
 
46
        self.tenant = tenant
 
47
        self.is_admin = is_admin
 
48
        self.read_only = read_only
 
49
        self.show_deleted = show_deleted
 
50
        if not request_id:
 
51
            request_id = generate_request_id()
 
52
        self.request_id = request_id
 
53
 
 
54
    def to_dict(self):
 
55
        return {'user': self.user,
 
56
                'tenant': self.tenant,
 
57
                'is_admin': self.is_admin,
 
58
                'read_only': self.read_only,
 
59
                'show_deleted': self.show_deleted,
 
60
                'auth_token': self.auth_token,
 
61
                'request_id': self.request_id}
 
62
 
 
63
 
 
64
def get_admin_context(show_deleted="no"):
 
65
    context = RequestContext(None,
 
66
                             tenant=None,
 
67
                             is_admin=True,
 
68
                             show_deleted=show_deleted)
 
69
    return context
 
70
 
 
71
 
 
72
def get_context_from_function_and_args(function, args, kwargs):
 
73
    """Find an arg of type RequestContext and return it.
 
74
 
 
75
       This is useful in a couple of decorators where we don't
 
76
       know much about the function we're wrapping.
 
77
    """
 
78
 
 
79
    for arg in itertools.chain(kwargs.values(), args):
 
80
        if isinstance(arg, RequestContext):
 
81
            return arg
 
82
 
 
83
    return None