Commit Graph

67 Commits

Author SHA1 Message Date
Takashi Kajinami e31a7a1af2 Add is_admin to redacted context
Heat is known to override RequestContext in a strange way and it
attempts to run policy check, which loads full request context, in case
is_admin is not given.

This ensures is_admin is copied during redactation, to avoid failures
in unit tests. is_admin is not a secret field so can appear in
notifications.

This also adds **kwargs interface to redacted_copy, so that we can
inject a few more arguments in subclasses.

Change-Id: I19f83d7b0a13f14df16ac805998d5b3751801c12
2024-02-26 18:20:35 +09:00
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
Stephen Finucane 3400cc2871 Integrate mypy
oslo.context is pretty small and therefore easy to introduce mypy for.
Minimal type hints are added, along with tox targets to validate things.
We can build on these in the future if we'd like to.

Change-Id: Ie5ab9eedfc89654ac3aa635f4a3ad24409ad23b6
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
2022-01-25 15:00:32 +01: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 0d02866365 Switch to collections.abc.MutableMapping
collections.MutableMapping has been deprecated since Python 3.3 and
is removed in Python 3.10. The functionality should be identical.

Change-Id: Ic9b2f8a89a92ac3a5fa5f49bb19f13e547d12eda
2021-01-29 15:57:59 +00:00
Moisés Guimarães de Medeiros 37dea647ef Bump bandit version
This patch bumps bandit allowed version to >=1.6.0,<1.7.0 in order to
avoid the errors detailed here https://github.com/PyCQA/bandit/pull/393

Change-Id: I2e498fd13356f571acb905384206b4c97e6c5d92
Signed-off-by: Moisés Guimarães de Medeiros <moguimar@redhat.com>
2020-07-25 11:31:15 +02: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
Juan Antonio Osorio Robles 936ce1aa54 Output 'project' key in context's to_dict function
This adds the 'project' key to the output dict, which contains the same
value as the 'tenant' key.

Change-Id: I9f7514daa162c5a9f0e7c8c24455bd1d29517cf5
2017-09-26 12:24:28 +03: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
ChangBo Guo(gcb) e86ee74a72 Fix URLs according to document migration
Change-Id: I5e452dfc33c4bdf8f344f6e28ff1fd4a5e049476
2017-07-12 22:46:06 +08:00
Van Hung Pham d0f8de0eb6 Optimize the link address
Update link address to ensure safety and preciseness

Change-Id: Id193a5cefa3220036df69f5ca48aadc7ae02890b
2017-06-13 10:20:56 +07:00
Sean Dague cce83ad5e1 Provide unified calling interface for global_id
Once we get to > 2 services chained, it's important that the caller
pass the global_request_id, not the local one. This provides a unified
property so that it's easy for callers to do the right thing.

Change-Id: I6c56187f776602e0af88a368e41a4239901bde3e
2017-05-24 07:01:33 -04: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 608114fbfe pbr.version.VersionInfo needs package name (oslo.xyz and not oslo_xyz)
We need to pass into VersionInfo what one would expect from running:
 setup.py --name

 Right now we pass in say oslo_context and pbr fails if there is no .git in
  the python source tree

  Closes-Bug: #1662266

Change-Id: Ia9da53a5d617d1790c7ed642d114b723e04bddb7
2017-02-06 12:49:22 -05: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 c4621f04ff Reformat the Context.__init__ arguments
To add service token information to the context we are going to add more
information to the base context object. To hopefully make this easier to
read seperate these to an option per line.

Change-Id: Ic523d3e1545bf5dd865f769e1651500205b9b1a9
2016-11-15 16:35:41 +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
Jenkins ae6f152447 Merge "Add oslo.context name attributes matching ids" 2016-07-08 02:23:13 +00:00
Kirill Bespalov 061741287a Add reno for releasenotes management
An initial patch to add reno and create a base directory for
release notes

Change-Id: I8a18a2f2550ff82954d28a6e897a9ce9b1f8f1ba
2016-05-20 17:58:52 +03: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