40
40
from nova import exception
41
41
from nova .i18n import _
42
42
from nova .objects import cell_mapping as cell_mapping_obj
43
+ from nova import policy
43
44
from nova import utils
44
45
from nova import version
45
46
from nova .volume import cinder
@@ -346,6 +347,70 @@ def _check_cinder(self):
346
347
six .text_type (ex ))
347
348
return upgradecheck .Result (upgradecheck .Code .SUCCESS )
348
349
350
+ def _check_policy (self ):
351
+ """Checks to see if policy file is overwritten with the new
352
+ defaults.
353
+ """
354
+ msg = _ ("Your policy file contains rules which examine token scope, "
355
+ "which may be due to generation with the new defaults. "
356
+ "If that is done intentionally to migrate to the new rule "
357
+ "format, then you are required to enable the flag "
358
+ "'oslo_policy.enforce_scope=True' and educate end users on "
359
+ "how to request scoped tokens from Keystone. Another easy "
360
+ "and recommended way for you to achieve the same is via two "
361
+ "flags, 'oslo_policy.enforce_scope=True' and "
362
+ "'oslo_policy.enforce_new_defaults=True' and avoid "
363
+ "overwriting the file. Please refer to this document to "
364
+ "know the complete migration steps: "
365
+ "https://docs.openstack.org/nova/latest/configuration"
366
+ "/policy-concepts.html. If you did not intend to migrate "
367
+ "to new defaults in this upgrade, then with your current "
368
+ "policy file the scope checking rule will fail. A possible "
369
+ "reason for such a policy file is that you generated it with "
370
+ "'oslopolicy-sample-generator' in json format. "
371
+ "Three ways to fix this until you are ready to migrate to "
372
+ "scoped policies: 1. Generate the policy file with "
373
+ "'oslopolicy-sample-generator' in yaml format, keep "
374
+ "the generated content commented out, and update "
375
+ "the generated policy.yaml location in "
376
+ "``oslo_policy.policy_file``. "
377
+ "2. Use a pre-existing sample config file from the Train "
378
+ "release. 3. Use an empty or non-existent file to take all "
379
+ "the defaults." )
380
+ rule = "system_admin_api"
381
+ rule_new_default = "role:admin and system_scope:all"
382
+ status = upgradecheck .Result (upgradecheck .Code .SUCCESS )
383
+ # NOTE(gmann): Initialise the policy if it not initialized.
384
+ # We need policy enforcer with all the rules loaded to check
385
+ # their value with defaults.
386
+ try :
387
+ if policy ._ENFORCER is None :
388
+ policy .init (suppress_deprecation_warnings = True )
389
+
390
+ # For safer side, recheck that the enforcer is available before
391
+ # upgrade checks. If something is wrong on oslo side and enforcer
392
+ # is still not available the return warning to avoid any false
393
+ # result.
394
+ if policy ._ENFORCER is not None :
395
+ current_rule = str (policy ._ENFORCER .rules [rule ]).strip ("()" )
396
+ if (current_rule == rule_new_default and
397
+ not CONF .oslo_policy .enforce_scope ):
398
+ status = upgradecheck .Result (upgradecheck .Code .WARNING ,
399
+ msg )
400
+ else :
401
+ status = upgradecheck .Result (
402
+ upgradecheck .Code .WARNING ,
403
+ _ ('Policy is not initialized to check the policy rules' ))
404
+ except Exception as ex :
405
+ status = upgradecheck .Result (
406
+ upgradecheck .Code .WARNING ,
407
+ _ ('Unable to perform policy checks due to error: %s' ) %
408
+ six .text_type (ex ))
409
+ # reset the policy state so that it can be initialized from fresh if
410
+ # operator changes policy file after running this upgrade checks.
411
+ policy .reset ()
412
+ return status
413
+
349
414
# The format of the check functions is to return an upgradecheck.Result
350
415
# object with the appropriate upgradecheck.Code and details set. If the
351
416
# check hits warnings or failures then those should be stored in the
@@ -362,6 +427,8 @@ def _check_cinder(self):
362
427
(_ ('Ironic Flavor Migration' ), _check_ironic_flavor_migration ),
363
428
# Added in Train
364
429
(_ ('Cinder API' ), _check_cinder ),
430
+ # Added in Ussuri
431
+ (_ ('Policy Scope-based Defaults' ), _check_policy ),
365
432
)
366
433
367
434
0 commit comments