~ubuntu-branches/ubuntu/vivid/ceilometer/vivid-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page, Corey Bryant, James Page
  • Date: 2015-02-19 14:59:07 UTC
  • mfrom: (1.2.3)
  • Revision ID: package-import@ubuntu.com-20150219145907-9jojybdsl64zcn14
Tags: 2015.1~b2-0ubuntu1
[ Corey Bryant ]
* New upstream release.
  - d/control: Align requirements with upstream.
  - d/p/skip-test.patch: Rebased.

[ James Page ]
* d/rules,d/p/skip-gabbi.patch: Skip tests that rely on python-gabbi until
  packaging and MIR is complete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 OpenStack Foundation.
2
 
# All Rights Reserved.
3
 
#
4
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
 
#    not use this file except in compliance with the License. You may obtain
6
 
#    a copy of the License at
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#    Unless required by applicable law or agreed to in writing, software
11
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 
#    License for the specific language governing permissions and limitations
14
 
#    under the License.
15
 
 
16
 
"""
17
 
Simple class that stores security context information in the web request.
18
 
 
19
 
Projects should subclass this class if they wish to enhance the request
20
 
context or provide additional information in their specific WSGI pipeline.
21
 
"""
22
 
 
23
 
import itertools
24
 
import uuid
25
 
 
26
 
 
27
 
def generate_request_id():
28
 
    return b'req-' + str(uuid.uuid4()).encode('ascii')
29
 
 
30
 
 
31
 
class RequestContext(object):
32
 
 
33
 
    """Helper class to represent useful information about a request context.
34
 
 
35
 
    Stores information about the security context under which the user
36
 
    accesses the system, as well as additional request information.
37
 
    """
38
 
 
39
 
    user_idt_format = '{user} {tenant} {domain} {user_domain} {p_domain}'
40
 
 
41
 
    def __init__(self, auth_token=None, user=None, tenant=None, domain=None,
42
 
                 user_domain=None, project_domain=None, is_admin=False,
43
 
                 read_only=False, show_deleted=False, request_id=None,
44
 
                 instance_uuid=None):
45
 
        self.auth_token = auth_token
46
 
        self.user = user
47
 
        self.tenant = tenant
48
 
        self.domain = domain
49
 
        self.user_domain = user_domain
50
 
        self.project_domain = project_domain
51
 
        self.is_admin = is_admin
52
 
        self.read_only = read_only
53
 
        self.show_deleted = show_deleted
54
 
        self.instance_uuid = instance_uuid
55
 
        if not request_id:
56
 
            request_id = generate_request_id()
57
 
        self.request_id = request_id
58
 
 
59
 
    def to_dict(self):
60
 
        user_idt = (
61
 
            self.user_idt_format.format(user=self.user or '-',
62
 
                                        tenant=self.tenant or '-',
63
 
                                        domain=self.domain or '-',
64
 
                                        user_domain=self.user_domain or '-',
65
 
                                        p_domain=self.project_domain or '-'))
66
 
 
67
 
        return {'user': self.user,
68
 
                'tenant': self.tenant,
69
 
                'domain': self.domain,
70
 
                'user_domain': self.user_domain,
71
 
                'project_domain': self.project_domain,
72
 
                'is_admin': self.is_admin,
73
 
                'read_only': self.read_only,
74
 
                'show_deleted': self.show_deleted,
75
 
                'auth_token': self.auth_token,
76
 
                'request_id': self.request_id,
77
 
                'instance_uuid': self.instance_uuid,
78
 
                'user_identity': user_idt}
79
 
 
80
 
    @classmethod
81
 
    def from_dict(cls, ctx):
82
 
        return cls(
83
 
            auth_token=ctx.get("auth_token"),
84
 
            user=ctx.get("user"),
85
 
            tenant=ctx.get("tenant"),
86
 
            domain=ctx.get("domain"),
87
 
            user_domain=ctx.get("user_domain"),
88
 
            project_domain=ctx.get("project_domain"),
89
 
            is_admin=ctx.get("is_admin", False),
90
 
            read_only=ctx.get("read_only", False),
91
 
            show_deleted=ctx.get("show_deleted", False),
92
 
            request_id=ctx.get("request_id"),
93
 
            instance_uuid=ctx.get("instance_uuid"))
94
 
 
95
 
 
96
 
def get_admin_context(show_deleted=False):
97
 
    context = RequestContext(None,
98
 
                             tenant=None,
99
 
                             is_admin=True,
100
 
                             show_deleted=show_deleted)
101
 
    return context
102
 
 
103
 
 
104
 
def get_context_from_function_and_args(function, args, kwargs):
105
 
    """Find an arg of type RequestContext and return it.
106
 
 
107
 
       This is useful in a couple of decorators where we don't
108
 
       know much about the function we're wrapping.
109
 
    """
110
 
 
111
 
    for arg in itertools.chain(kwargs.values(), args):
112
 
        if isinstance(arg, RequestContext):
113
 
            return arg
114
 
 
115
 
    return None
116
 
 
117
 
 
118
 
def is_user_context(context):
119
 
    """Indicates if the request context is a normal user."""
120
 
    if not context or context.is_admin:
121
 
        return False
122
 
    return context.user_id and context.project_id