| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
"""Context: context for security/db session.""" |
| 14 |
|
| 15 |
import collections |
| 16 |
import copy |
| 17 |
|
| 18 |
from oslo_context import context as oslo_context |
| 19 |
from oslo_db.sqlalchemy import enginefacade |
| 20 |
from oslo_log import log as logging |
| 21 |
from oslo_utils import timeutils |
| 22 |
|
| 23 |
from neutron_lib.db import api as db_api |
| 24 |
from neutron_lib.policy import _engine as policy_engine |
| 25 |
|
| 26 |
LOG = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
class ContextBase(oslo_context.RequestContext): |
| 30 |
"""Security context and request information. |
| 31 |
|
| 32 |
Represents the user taking a given action within the system. |
| 33 |
|
| 34 |
""" |
| 35 |
|
| 36 |
def __init__(self, user_id=None, project_id=None, is_admin=None, |
| 37 |
timestamp=None, project_name=None, user_name=None, |
| 38 |
is_advsvc=None, is_service_role=None, |
| 39 |
tenant_id=None, tenant_name=None, |
| 40 |
has_global_access=False, can_set_project_id=False, **kwargs): |
| 41 |
# NOTE(jamielennox): We maintain this argument order for tests that |
| 42 |
# pass arguments positionally. |
| 43 |
|
| 44 |
# Prefer project_id and project_name, as that's what's going to be |
| 45 |
# set by keystone. |
| 46 |
# NOTE(haleyb): remove fall-back and warning in E+2 release, or when |
| 47 |
# all callers have been changed to use project_*. |
| 48 |
project_id = project_id or tenant_id |
| 49 |
project_name = project_name or tenant_name |
| 50 |
if tenant_id: |
| 51 |
LOG.warning('Keyword tenant_id has been deprecated, use ' |
| 52 |
'project_id instead') |
| 53 |
if tenant_name: |
| 54 |
LOG.warning('Keyword tenant_name has been deprecated, use ' |
| 55 |
'project_name instead') |
| 56 |
kwargs.setdefault('project_id', project_id) |
| 57 |
kwargs.setdefault('project_name', project_name) |
| 58 |
self._has_global_access = has_global_access |
| 59 |
self._can_set_project_id = can_set_project_id |
| 60 |
self._is_advsvc = is_advsvc |
| 61 |
self._is_service_role = is_service_role |
| 62 |
super().__init__( |
| 63 |
is_admin=is_admin, user_id=user_id, **kwargs) |
| 64 |
|
| 65 |
self.user_name = user_name |
| 66 |
self.timestamp = timestamp or timeutils.utcnow() |
| 67 |
|
| 68 |
if self._is_advsvc is not None: |
| 69 |
LOG.warning( |
| 70 |
"Argument 'is_advsvc' is deprecated since 2026.1 release " |
| 71 |
"(neutron-lib 3.23.0) and will be removed once support for " |
| 72 |
"the old RBAC API policies will be removed from Neutron. " |
| 73 |
"Please use argument 'is_service_role' instead.") |
| 74 |
else: |
| 75 |
# TODO(slaweq): remove this once is_advsvc will be removed and |
| 76 |
# we will rely on is_service_role only |
| 77 |
self._is_advsvc = (self.is_admin or |
| 78 |
policy_engine.check_is_advsvc(self)) |
| 79 |
|
| 80 |
if self._is_service_role is None: |
| 81 |
self._is_service_role = policy_engine.check_is_service_role(self) |
| 82 |
|
| 83 |
if self.is_admin is None: |
| 84 |
self.is_admin = policy_engine.check_is_admin(self) |
| 85 |
if not self._has_global_access: |
| 86 |
self._has_global_access = policy_engine.check_has_global_access( |
| 87 |
self) |
| 88 |
if not self._can_set_project_id: |
| 89 |
self._can_set_project_id = policy_engine.check_can_set_project_id( |
| 90 |
self) |
| 91 |
|
| 92 |
@property |
| 93 |
def tenant_id(self): |
| 94 |
return self.project_id |
| 95 |
|
| 96 |
@tenant_id.setter |
| 97 |
def tenant_id(self, tenant_id): |
| 98 |
self.project_id = tenant_id |
| 99 |
|
| 100 |
@property |
| 101 |
def tenant_name(self): |
| 102 |
return self.project_name |
| 103 |
|
| 104 |
@tenant_name.setter |
| 105 |
def tenant_name(self, tenant_name): |
| 106 |
self.project_name = tenant_name |
| 107 |
|
| 108 |
@property |
| 109 |
def is_service_role(self): |
| 110 |
# TODO(slaweq): we will not need to check ContextBase._is_advsvc once |
| 111 |
# we will get rid of the old API policies |
| 112 |
return self._is_service_role or self._is_advsvc |
| 113 |
|
| 114 |
@property |
| 115 |
def is_advsvc(self): |
| 116 |
# TODO(slaweq): this property should be removed once we will get rid |
| 117 |
# of old policy rules and only new, S-RBAC rules will be available as |
| 118 |
# then we will use ContextBase.is_service_role instead |
| 119 |
LOG.warning("Method 'is_advsvc' is deprecated since 2023.2 release " |
| 120 |
"(neutron-lib 3.8.0) and will be removed once support for " |
| 121 |
"the old RBAC API policies will be removed from Neutron. " |
| 122 |
"Please use method 'is_service_role' instead.") |
| 123 |
return self.is_service_role |
| 124 |
|
| 125 |
@property |
| 126 |
def has_global_access(self): |
| 127 |
return self.is_admin or self._has_global_access |
| 128 |
|
| 129 |
@property |
| 130 |
def can_set_project_id(self): |
| 131 |
return ( |
| 132 |
self.is_admin or |
| 133 |
self.is_service_role or |
| 134 |
self._can_set_project_id |
| 135 |
) |
| 136 |
|
| 137 |
def to_dict(self): |
| 138 |
context = super().to_dict() |
| 139 |
context.update({ |
| 140 |
'user_id': self.user_id, |
| 141 |
'tenant_id': self.project_id, |
| 142 |
'project_id': self.project_id, |
| 143 |
'timestamp': str(self.timestamp), |
| 144 |
'tenant_name': self.project_name, |
| 145 |
'project_name': self.project_name, |
| 146 |
'user_name': self.user_name, |
| 147 |
'has_global_access': self.has_global_access, |
| 148 |
'can_set_project_id': self.can_set_project_id, |
| 149 |
}) |
| 150 |
return context |
| 151 |
|
| 152 |
def to_policy_values(self): |
| 153 |
values = super().to_policy_values() |
| 154 |
values['tenant_id'] = self.project_id |
| 155 |
values['is_admin'] = self.is_admin |
| 156 |
values['has_global_access'] = self.has_global_access |
| 157 |
values['can_set_project_id'] = self.can_set_project_id |
| 158 |
|
| 159 |
# NOTE(jamielennox): These are almost certainly unused and non-standard |
| 160 |
# but kept for backwards compatibility. Remove them in Pike |
| 161 |
# (oslo.context from Ocata release already issues deprecation warnings |
| 162 |
# for non-standard keys). |
| 163 |
values['user'] = self.user_id |
| 164 |
values['tenant'] = self.project_id |
| 165 |
values['domain'] = self.domain_id |
| 166 |
values['user_domain'] = self.user_domain_id |
| 167 |
values['project_domain'] = self.project_domain_id |
| 168 |
values['tenant_name'] = self.project_name |
| 169 |
values['project_name'] = self.project_name |
| 170 |
values['user_name'] = self.user_name |
| 171 |
|
| 172 |
return values |
| 173 |
|
| 174 |
@classmethod |
| 175 |
def from_dict(cls, values): |
| 176 |
cls_obj = super().from_dict(values) |
| 177 |
if values.get('timestamp'): |
| 178 |
cls_obj.timestamp = values['timestamp'] |
| 179 |
cls_obj.user_id = values.get('user_id', values.get('user')) |
| 180 |
cls_obj.tenant_id = values.get('tenant_id', values.get('project_id')) |
| 181 |
cls_obj.tenant_name = values.get('tenant_name') |
| 182 |
return cls_obj |
| 183 |
|
| 184 |
def elevated(self): |
| 185 |
"""Return a version of this context with admin flag set.""" |
| 186 |
context = copy.copy(self) |
| 187 |
context.is_admin = True |
| 188 |
|
| 189 |
context.roles = list( |
| 190 |
set(context.roles) | {'admin', 'member', 'reader'} |
| 191 |
) |
| 192 |
|
| 193 |
return context |
| 194 |
|
| 195 |
|
| 196 |
@enginefacade.transaction_context_provider |
| 197 |
class ContextBaseWithSession(ContextBase): |
| 198 |
pass |
| 199 |
|
| 200 |
|
| 201 |
_TransactionConstraint = collections.namedtuple( |
| 202 |
'_TransactionConstraint', ['if_revision_match', 'resource', 'resource_id']) |
| 203 |
|
| 204 |
|
| 205 |
class Context(ContextBaseWithSession): |
| 206 |
def __init__(self, *args, **kwargs): |
| 207 |
super().__init__(*args, **kwargs) |
| 208 |
self._session = None |
| 209 |
self._txn_constraint = None |
| 210 |
|
| 211 |
@property |
| 212 |
def session(self): |
| 213 |
# TODO(ralonsoh): "Context.session" is provided by |
| 214 |
# "enginefacade.transaction_context_provider" when a new transaction, |
| 215 |
# READER or WRITER, is created. This property is just a temporary fix |
| 216 |
# for those transactions that are not executed inside a transaction. |
| 217 |
# By manually selecting the type of transaction we can speed up the |
| 218 |
# code because by default a writer session is always created, even |
| 219 |
# within read only operations. |
| 220 |
if hasattr(super(), 'session'): |
| 221 |
self._session = super().session |
| 222 |
if self._session is None: |
| 223 |
self._session = db_api.get_writer_session() |
| 224 |
return self._session |
| 225 |
|
| 226 |
def set_transaction_constraint(self, resource, resource_id, rev_number): |
| 227 |
"""Set a revision constraint to enforce before resource_id is changed. |
| 228 |
|
| 229 |
:param resource: Collection name of resource (e.g. ports or networks) |
| 230 |
:param resource_id: The primary key ID of the individual resource that |
| 231 |
should have its revision number matched before |
| 232 |
allowing the transaction to proceed. |
| 233 |
:param rev_number: The revision_number that the resource should be at. |
| 234 |
""" |
| 235 |
|
| 236 |
self._txn_constraint = _TransactionConstraint( |
| 237 |
if_revision_match=rev_number, resource=resource, |
| 238 |
resource_id=resource_id) |
| 239 |
|
| 240 |
def clear_transaction_constraint(self): |
| 241 |
self._txn_constraint = None |
| 242 |
|
| 243 |
def get_transaction_constraint(self): |
| 244 |
return self._txn_constraint |
| 245 |
|
| 246 |
|
| 247 |
def get_admin_context(): |
| 248 |
# NOTE(slaweq): elevated() method will set is_admin=True but setting it |
| 249 |
# explicity here will avoid checking in policy rules if is_admin should be |
| 250 |
# set to True or not |
| 251 |
return Context(user_id=None, |
| 252 |
project_id=None, |
| 253 |
is_admin=True, |
| 254 |
overwrite=False).elevated() |
| 255 |
|
| 256 |
|
| 257 |
def get_admin_context_without_session(): |
| 258 |
# NOTE(slaweq): elevated() method will set is_admin=True but setting it |
| 259 |
# explicity here will avoid checking in policy rules if is_admin should be |
| 260 |
# set to True or not |
| 261 |
return ContextBase(user_id=None, project_id=None, is_admin=True).elevated() |