diff --git a/barbican/common/utils.py b/barbican/common/utils.py index 00b425b56..92e29ab82 100644 --- a/barbican/common/utils.py +++ b/barbican/common/utils.py @@ -18,6 +18,7 @@ Common utilities for Barbican. """ import time +import uuid from oslo.config import cfg @@ -157,3 +158,7 @@ class TimeKeeper(object): self.logger.debug(" Final time/elapsed:" "{0:.3f}/{1:.0f}".format(time_current, total_elapsed * 1000.)) + + +def generate_uuid(): + return str(uuid.uuid4()) diff --git a/barbican/context.py b/barbican/context.py index 8dce8e582..04a7f2302 100644 --- a/barbican/context.py +++ b/barbican/context.py @@ -15,9 +15,9 @@ # License for the specific language governing permissions and limitations # under the License. +from barbican.common import utils from barbican.openstack.common import policy from barbican.openstack.common import local -from barbican.openstack.common import uuidutils class RequestContext(object): @@ -39,7 +39,7 @@ class RequestContext(object): # (mkbhanda) possibly domain could be owner # brings us to the key scope question self.owner_is_tenant = owner_is_tenant - self.request_id = uuidutils.generate_uuid() + self.request_id = utils.generate_uuid() self.service_catalog = service_catalog self.policy_enforcer = policy_enforcer or policy.Enforcer() self.is_admin = is_admin diff --git a/barbican/model/models.py b/barbican/model/models.py index 086610b5c..de5fc8196 100644 --- a/barbican/model/models.py +++ b/barbican/model/models.py @@ -23,7 +23,6 @@ from sqlalchemy import orm from barbican.common import utils from barbican.openstack.common import timeutils -from barbican.openstack.common import uuidutils LOG = utils.getLogger(__name__) @@ -55,7 +54,7 @@ class ModelBase(object): "created_at", "updated_at", "deleted_at", "deleted"]) id = sa.Column(sa.String(36), primary_key=True, - default=uuidutils.generate_uuid) + default=utils.generate_uuid) created_at = sa.Column(sa.DateTime, default=timeutils.utcnow, nullable=False) diff --git a/barbican/openstack/common/context.py b/barbican/openstack/common/context.py index 74c6bc0c4..09019ee38 100644 --- a/barbican/openstack/common/context.py +++ b/barbican/openstack/common/context.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2011 OpenStack Foundation. # All Rights Reserved. # @@ -23,12 +21,11 @@ context or provide additional information in their specific WSGI pipeline. """ import itertools - -from barbican.openstack.common import uuidutils +import uuid def generate_request_id(): - return 'req-%s' % uuidutils.generate_uuid() + return 'req-%s' % str(uuid.uuid4()) class RequestContext(object): @@ -39,26 +36,46 @@ class RequestContext(object): accesses the system, as well as additional request information. """ - def __init__(self, auth_token=None, user=None, tenant=None, is_admin=False, - read_only=False, show_deleted=False, request_id=None): + user_idt_format = '{user} {tenant} {domain} {user_domain} {p_domain}' + + def __init__(self, auth_token=None, user=None, tenant=None, domain=None, + user_domain=None, project_domain=None, is_admin=False, + read_only=False, show_deleted=False, request_id=None, + instance_uuid=None): self.auth_token = auth_token self.user = user self.tenant = tenant + self.domain = domain + self.user_domain = user_domain + self.project_domain = project_domain self.is_admin = is_admin self.read_only = read_only self.show_deleted = show_deleted + self.instance_uuid = instance_uuid if not request_id: request_id = generate_request_id() self.request_id = request_id def to_dict(self): + user_idt = ( + self.user_idt_format.format(user=self.user or '-', + tenant=self.tenant or '-', + domain=self.domain or '-', + user_domain=self.user_domain or '-', + p_domain=self.project_domain or '-')) + return {'user': self.user, 'tenant': self.tenant, + 'domain': self.domain, + 'user_domain': self.user_domain, + 'project_domain': self.project_domain, 'is_admin': self.is_admin, 'read_only': self.read_only, 'show_deleted': self.show_deleted, 'auth_token': self.auth_token, - 'request_id': self.request_id} + 'request_id': self.request_id, + 'instance_uuid': self.instance_uuid, + 'user_identity': user_idt} def get_admin_context(show_deleted=False): @@ -81,3 +98,14 @@ def get_context_from_function_and_args(function, args, kwargs): return arg return None + + +def is_user_context(context): + """Indicates if the request context is a normal user.""" + if not context: + return False + if context.is_admin: + return False + if not context.user_id or not context.project_id: + return False + return True diff --git a/barbican/openstack/common/uuidutils.py b/barbican/openstack/common/uuidutils.py deleted file mode 100644 index 7608acb94..000000000 --- a/barbican/openstack/common/uuidutils.py +++ /dev/null @@ -1,39 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2012 Intel Corporation. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -""" -UUID related utilities and helper functions. -""" - -import uuid - - -def generate_uuid(): - return str(uuid.uuid4()) - - -def is_uuid_like(val): - """Returns validation of a value as a UUID. - - For our purposes, a UUID is a canonical form string: - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa - - """ - try: - return str(uuid.UUID(val)) == val - except (TypeError, ValueError, AttributeError): - return False diff --git a/openstack-common.conf b/openstack-common.conf index 1c8e4bb97..9702d05e6 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=gettextutils,jsonutils,log,local,timeutils,uuidutils,importutils,policy +modules=gettextutils,jsonutils,log,local,timeutils,importutils,policy # The base module to hold the copy of openstack.common base=barbican