Commit Graph

51 Commits

Author SHA1 Message Date
Jay Faulkner 059f9ba16e Add method for getting redacted copy of context
There are times, such as in oslo.messaging notifications, when we'd like
a context object to be stripped of any secrets. This adds a method to
RequestContext, redacted_copy(), which returns a copy of that context,
carrying over an allowlist of fields only, leaving secrets behind.

Related-bug: 2030976
Change-Id: Ie4a8eb784c505c41ec5fcd4cba091cc555146763
2023-09-08 12:11:44 -07:00
Hervé Beraud f4cbe4c296 Remove the deprecated argument tenant from RequestContext
The tenant argument of RequestContext have been deprecated long
time ago it is now removed by these changes.

Sem-Ver: api-break
Closes-Bug: 1505827
Change-Id: I2fded6f3476df1fb8c4e042ef28ed8ccb7ab0737
2021-10-29 09:16:31 +02:00
Michael Johnson 8290621b05 Fix context from_dict() for system_scope
A previous patch[1] added "system_scope" to the context for to_dict()
but the from_dict() method was not updated. This caused the
system_scope to always be None.
This patch corrects that by adding "system_scope" to the list of
values that may need to be extracted from the context dict.

[1] https://review.opendev.org/c/openstack/oslo.context/+/530509

Change-Id: Ica23d5c4183a692de3cb65a7ad72b19f47988ca6
2021-08-12 20:14:34 +00:00
Ben Nemec 1dd72d1d20 Filter out auth_token_info from logging values
auth_token_info is a common field that subclasses of RequestContext
add. It contains things like the token itself and the entire catalog,
both of which are undesirable to log. The token is a security concern
and the catalog is huge, which bloats the logs an unacceptable amount.

This change removes the auth_token_info key from the logging dict
that we return to the log formatter, which eliminates both problems.

Change-Id: If5ebaa3c1859d32cd05f51defe173fc625b21af5
Closes-Bug: 1866705
2020-03-10 17:55:16 +00:00
Lance Bragstad f65408df5c Implement domain-scope for context objects
The OpenStack identity sevice issues domain-scoped tokens and
keystonemiddlware sets a specific header when it validates a
domain-scoped token. This commit allows context objects to set
domain_id attributes for tokens scoped to a domain and also processes
the domain ID header from keystonemiddlware.

Change-Id: I620d647499abaf5a7103d82af22a190fcc0b3fae
2018-10-26 16:24:27 +00:00
Lance Bragstad 1a40b3d43b Implement system-scope
The context should carry some information that all services will need
in order to enforce scoping. System scope can be implemented here
and available for projects when they start adding scope types to
policies.

bp system-scope

Change-Id: I02fdaccfdd002d60b0b51c5d3327c783009cf35e
2018-05-16 18:03:24 +00:00
Juan Antonio Osorio Robles a8d86df940 Ouput a placeholder instead of the auth_token
As the name of the get_logging_values suggests, that function will
get entries from the context for logging purposes. For this, we
shouldn't need the auth_token since it might potentially leak
in the logs.

This filters out the auth_token by setting it as '***' by
default.

Change-Id: I2b245c1665c3587be3c476b803122788d186e5d5
2017-11-07 13:08:11 +02:00
Ben Nemec e75f4c5ad9 Make from_dict extensible
Some of our consumers define additional members on the context class
that they want included in to_dict and from_dict.  While it is
possible to do this today via overrides of those functions, the
from_dict implementation in particular is a little non-obvious.
This has led to bugs when the base class's to_dict behavior changes.

This change moves the logic around extending the keys recognized by
from_dict into from_dict itself and allows consumers to simply
provide a list of those keys by overriding a class member.

Change-Id: Ib143f8a5c129dbf6711800c4d87c8830a8aa3365
Related-Bug: 1721432
2017-10-11 21:46:02 +00:00
Jamie Lennox d78cf592e1 Rename deprecated context params
The user, tenant and parameters without _id are deprecated as properties
on the object however have never been deprecated as the argument that is
passed.

This should be a fairly easy transition from a context perspective so
simply use the debtcollector function to rename them in place.

Change-Id: Id6ae5a1c869b8e4279d0100bdb6dbf6790dd9b83
2017-09-21 10:54:52 +10:00
Jenkins ba3307845b Merge "Revert "Postpone deprecation warnings to Pike"" 2017-09-20 13:25:42 +00:00
ChangBo Guo(gcb) 7a91ceed4e Revert "Postpone deprecation warnings to Pike"
This reverts commit 96432cbe4d.
it helps us a lot in Ocata, need open the deprecation warnings
to make consuming projects move to fix the warning.

Change-Id: I10874c66bfd7c7f582a4fe91dcb6354f1c55fc04
2017-08-21 15:28:23 +08:00
Morgan Fainberg a144ebd6c3 Remove use of positional decorator
The positional decorator results in poorly maintainable code in
a misguided effort to emulate python3's key-word-arg only notation
and functionality. This patch removes oslo.context dependance
on the positional decorator.

Change-Id: I9b83917fedd00a8d4fdf904208a17ebdc5cda59a
2017-08-07 16:11:39 -07:00
Sean Dague ec7cdc4cc5 Add global_request_id to context
This provides the facility to have a global_request_id which is
different from the service level request_id that we've been using.

Includes basic testing.

Change-Id: I1521188ae627fa2e7d35aa2ffffbcb620c527765
2017-05-23 10:22:34 -04:00
Davanum Srinivas 96432cbe4d Postpone deprecation warnings to Pike
2.12.0 added a DeprecationWarning a bit late in
Ocata for other projects to react properly. For example
Neutron logs get filled up pretty badly. So let's
defer the deprecation warnings for one more release

Note f25543fcc7 adds
the warning(s).

Instead of a full revert, let's just key off of a
variable _log_deprecation_warnings both in context.py
and test_context.py with a FIXME reminder

Closes-Bug: #1660088

Change-Id: I45ba9a0ed628f3347c7c3b346e907e4363452dd8
2017-01-29 15:28:38 -05:00
Jamie Lennox 2eafb0eb6b Add the service token details to context
In the case of communications that include service tokens we need to add
that information to the context so that we can also enforce policy on
it. Add this information and load this information in the from_environ
method. Add these details to to_policy_values so that we can start to
enforce policy based on these attributes.

Change-Id: Id90f32795905112de804a18ddc8a69c038c829bb
2016-11-15 16:39:58 +11:00
Jamie Lennox f25543fcc7 Move existing attributes to _id suffixed attributes
There is confusion now between whether parameters refer to the name or
id. Similar to adding _name we should rename the other variables with
_id to make it more obvious.

Change-Id: I203acefae8270bd3373b006fa096bea5ef3106f3
2016-11-15 14:25:05 +11:00
Jenkins b788a184a0 Merge "Revert "Fix X-User and X-Tenant deprecated headers in from_environ"" 2016-11-10 18:36:26 +00:00
Jenkins 522b211033 Merge "Provide a way to deprecate policy values" 2016-11-09 18:33:58 +00:00
Joshua Harlow 2cd47bc2ef Revert "Fix X-User and X-Tenant deprecated headers in from_environ"
This reverts commit fd22458619.

Currently causing nova periodic jobs to fail.

Change-Id: Ifef9b232aeaded19bca3741dfdd3841006409422
2016-11-08 15:50:22 -08:00
Jamie Lennox 2394cff063 Provide a way to deprecate policy values
If we want to move all the services over to a standard policy
enforcement dict we need a way to start notifying deployers when the
policy enforcement files are using deprecated values.

Instead of returning a dictionary return an object that acts like a
dictionary but emits a DeprecationWarning whenever a deprecated policy
enforcement value is read from it.

Change-Id: I4b2fda188bbccfd491556cc5631e5c4a76314492
2016-09-12 13:48:34 -04:00
Jamie Lennox fd22458619 Fix X-User and X-Tenant deprecated headers in from_environ
The X-User and X-Tenant deprecated headers from auth_token middleware
are actually name values, not ID values. In most cases this doesn't
matter because these are deprecated and only used when no newer headers
are available but we should still fix it.

Closes-Bug: #1614776
Change-Id: I16e80671e853387004434e766dd6ceebe0a6b2a6
2016-08-19 09:18:57 +10:00
Jenkins 0ad0950e06 Merge "Manually specify from_dict parameters" 2016-08-17 17:36:33 +00:00
Jenkins 6467cacbb1 Merge "Emit deprecation warnings when positional args passed" 2016-08-12 21:26:44 +00:00
yanheven 10fd6fd30a Fix parameters of assertEqual are misplaced
Many assertEqual sentences don't follow assertEqual(expected, actual),
These misplaces have 2 impacts:
1, giving confusing messages when some tests failed.
2, mislead other developers, new test modules may follow these wrong pattern.

This patch fix all of them.

Change-Id: I806c893fd46242b021ce90037609ee47e015e1f3
Closes-Bug: #1604213
2016-07-23 17:00:31 +08:00
Jamie Lennox 3a118faa83 Manually specify from_dict parameters
The way from_dict is structured today means that it is virtually
impossible for a subclass to use it. The function looks only at its own
__init__ variables and then creates a class with them. If a subclass is
adding something to to_dict (they almost all do) then there is no way
for that subclass to add anything to the from_dict so that it is
correctly reconstructed.

This problem is solvable by the pattern of passing keyword args through
to the constructor and having from_dict add to these arguments if they
are not already present. This pattern is already used by from_environ.

This change also moves from an inspecting __init__ args to explicitly
specifying the arguments supported by from_dict. This will be required
to start doing debtcollector and parameter cleanup without breaking
compatibility in the subclasses where the values returned by inspect may
not be those we actually want to pass to __init__.

Change-Id: Id5d78c2dbf76741338982e8d5ba08b6ad432ec42
2016-07-13 10:37:09 +10:00
Jenkins 6a9bc57a9b Merge "Allow deprecated X-Tenant-Name in from_environ" 2016-07-11 23:00:35 +00:00
Jamie Lennox 9e6c924040 Emit deprecation warnings when positional args passed
We should try and enforce that all arguments passed to an oslo_context
are passed as keyword arguments. positional is a library maintained by
the openstack community for exactly this.

We can't simply switch over to raising an error, so by setting
enforcement to warn we will issue a deprecation warning whenever
arguments are being passed positionally.

Change-Id: I47456ac65911d4cc4a5acbacfd1d0dae8429684a
2016-07-12 08:07:08 +10:00
Jenkins 9feb9db8d6 Merge "Add is_admin_project to context" 2016-07-11 15:16:53 +00:00
Jamie Lennox 5118040676 Allow deprecated X-Tenant-Name in from_environ
Support the old and deprecated X-Tenant-Name header in the
from_environ method.

Change-Id: I0d1784ee96e38a5be256370a3e6dbf7cd1b21b05
2016-07-11 11:10:46 +10:00
Jamie Lennox 740b81784b Handle openstack.request_id in from_environ
The oslo.middleware request_id middleware that is used by most services
sets the request_id into the environment at 'openstack.request_id'. Pick
this value up into the context object if it is present in the
environment.

Change-Id: Iae3799f4c5ad799961a4a6b01d2b77fd97e72be3
2016-07-08 12:33:22 +10:00
Jamie Lennox d3af1d06b4 Add is_admin_project to context
is_admin_project is provided by keystonemiddleware and used by
oslo.policy to enforce that a project scoped token exists in the admin
project.

To make this usable we add the ability to read the X-Is-Admin-Project
header from the environment, and add it to the outputted policy values.

Note the value is added to keystonemiddleware in the depend review
however it must work even with older auth_token middlewares so is fine
to merge prior to a middleware release.

Closes-Bug: #1577996
Depends-On: Ic680e6eaa683926914cf4b2152ec3bb67c6601ff
Change-Id: Ie48fedb8092e33e9645a37ea3fe44b88d34ad3b8
2016-07-08 12:30:25 +10:00
Ronald Bradford f0de0c6291 Add oslo.context name attributes matching ids
Provide in the oslo.context base class _name attributes to match
the already defined id attributes (that are not yet called _id).

These attributes are made available for oslo.log to use in context
aware logging.

This will replace any projects that subclass RequestContext and
subsequently add these attributes to to_dict(). e.g.
http://git.openstack.org/cgit/openstack/nova/tree/nova/context.py#n176

See as discussed on ML at
http://lists.openstack.org/pipermail/openstack-dev/2016-April/091453.html

Change-Id: I1fbb613ded8dbcb99b2f43a4bc43f9046097c0ce
2016-05-19 17:07:53 -04:00
Jamie Lennox 0511e11287 Strip roles in from_environ
If somehow whitespace gets into the roles coming from headers we should
remove it and only save the actual role name. This is not possible
coming from auth_token middleware but is tested by some services and is
simple to support here.

Change-Id: I11ac3959d8f8b233c8785671d7a59263a4dc36df
2016-05-09 11:23:23 +10:00
Jamie Lennox e1925637b4 Allow deprecated headers in from_environ
from_environ only supported the current environ variables from
auth_token middleware as these have been in use for a long time. It
turns out particularly in testing that services may still use the
deprecated headers so we should still support those in oslo.context.

Change-Id: Ia7faf7b657525c11f728ac753422efe87f6ac34f
2016-05-09 11:23:23 +10:00
ChangBo Guo(gcb) c63a359094 Ensure to_dict() supports unicode
When we compose a string for user_identity, we should
ensure that we don't break for non-ascii strings in
user name or tenant name or domain names.

Change-Id: Id457ebe415f78c435fc8ece810f0a1b20964dabf
Closes-Bug: #1507937
2016-02-27 22:28:48 +08:00
Jamie Lennox 187d574063 Standardize an oslo.policy credentials dictionary
Currently all the services pass there own dictionary to the oslo.policy
enforcement engine. This means that there is no standard policy language
between services.

Create a dictionary with the standard credential items that may be
required in policy enforcement. This method will need to be overriden
from individual services for backwards compatibility for some time.

Change-Id: I7bc31764e79eb61f602c7b8601149ac75bee9f9f
Closes-Bug: #1537653
2016-02-24 17:01:33 +00:00
Jenkins 410d14ff05 Merge "Revert "Add common oslo.log format parameters"" 2016-02-24 02:24:32 +00:00
Ronald Bradford 217a4edc2e Revert "Add common oslo.log format parameters"
This reverts commit 01aaeae060.
This causes test failures with oslo.log change
Ica445ad5dfe9bd27dfcf1b1bcb6401a0bc9496a5 when passing these attributes
to a log message and a context object. The result of setting these in
context causes log.record values to be overridden with blank values.
Catering for this condition causes subsequent mismatches for test
conditions mixing context and optional passing additional attributes
to log.

Change-Id: I5450e105dc914f822a2b4c03b759a682d8b4a3e7
2016-02-23 18:53:54 +00:00
Jamie Lennox f383bd2973 Add roles to context
Roles are an important part of the user context. They typically don't
need to be serialized for RPC but they are needed for policy. Include
roles onto the context object and make sure it is loaded from the
auth_token middleware environment.

Related-Bug: #1537653
Change-Id: Ia575ba803a0fb70f39146bd75d381ed19414bd23
2016-02-19 21:17:36 +00:00
Ronald Bradford 0327388695 Agnostic approach to construct context from_dict
Replace the existing argument specified values in from_dict() method
to be argument agnostic for any future arguments. Based from version
found in Solum.

All current boolean values match instantiation defaults.

Change-Id: I17c76881aa662de7ff245b3f6bfca309896ddf24
2016-02-17 21:55:52 +00:00
Ronald Bradford 01aaeae060 Add common oslo.log format parameters
Add to get_logging_values() attributes that used in format string parameters
and that can cause an exception when the are not defined.
This is being refactored from the oslo.log formatter functionality.

Change-Id: I7108a51bea01db6ccd9b6b24afc7b8d92b8eeda7
Implements: blueprint app-agnostic-logging-parameters
2016-02-09 22:07:57 +00:00
Ronald Bradford 22ad2c2c59 Define method for oslo.log context parameters
This initial review defines a new method that will be used to define
and extend context attributes specifically useful in logging.
This enables other uses of the RequestContext to use the existing to_dict()
method.
This placholder is also needed for cross project dependencies with oslo.log.

Change-Id: I963a6db4aef74f7348e75a642e2e195cedacecaa
Implements: blueprint app-agnostic-logging-parameters
2016-01-29 17:56:53 +00:00
Ronald Bradford 800208d9d3 Add additional unit tests
These tests are to cover existing class functionality before
modifications of app agnostic parameters and changes in Oslo Log.

Change-Id: I0b041746865e4da5d2c73ce6093f96bd48760e24
2016-01-25 18:02:32 +00:00
Jamie Lennox 1f3719dfcf Provide a helper to load a context from environment
The intention here is to provide better integration between what
oslo.context expects and what keystonemiddleware provides. As auth_token
middleware changes each service has to react independently to the new
options and the way they build contexts.

By providing a helper here we can improve the consistency across
services to standardize the options and provide a common way to react to
auth_token middleware improvements.

Change-Id: Ic590b3ee359e9a9c27eef3d11c0738066f475881
2016-01-11 10:59:19 +11:00
Davanum Srinivas (dims) 04e40fa8d4 Revert "Add properties for id attributes"
This reverts commit 78644789ce.

Change-Id: I92257b7bade0e2e5c4e1b387b437d929afc1a4db
Related-Bug: #1532427
2016-01-09 12:04:52 +00:00
Jamie Lennox 78644789ce Add properties for id attributes
The user, tenant and similar values are ambiguous. Most contexts that
subclass oslo.context already provide some version of this and use the
_id attributes anyway.

Ideally we would deprecate these values in future but for now just
provide the alias.

Change-Id: Ia59bf69eaa83be1904abe835bfee68b785c92c54
2016-01-07 08:20:14 +11:00
Ronald Bradford 41598d7188 Remove reference to undefined attributes
is_user_context refers to a user_id and project_id attribute in
the context object which are not defined in this base class.

Change-Id: I2ac6d4a7d31d026e6fedcba80c4a760dcac747e8
Closes-Bug: 1508217
2015-10-20 17:59:34 -04:00
Doug Hellmann fc2d39034b Add ClearRequestContext fixture
Add a fixture class for use in tests to provide a way to flush the
cached RequestContext without exposing how that is stored.

bp/graduate-oslo-context

Change-Id: I27dd50a61d364e1698823629e2676c10ced50af3
2014-11-19 11:58:56 -05:00
Doug Hellmann 9152a0d38b Cache the current context for the thread
Use a threading.local instance to store the current RequestContext, with
an option to not overwrite an existing context.

bp/graduate-oslo-context

Change-Id: I000cb13392ee21258dc2a91683294dc9ff2aeb8f
2014-11-19 11:23:35 -05:00
Doug Hellmann cac31d9c6f Change instance_uuid to resource_uuid
Use an application-agnostic term for the resource the context is
attached to.

bp/app-agnostic-logging-parameters

Change-Id: I548120770d0f11a2af56f07d0902433a8c7c98fe
2014-11-19 11:04:52 -05:00