Initialize global config object in cli tools

Currently, passing --config-file to a tool like oslopolicy-list-redundant
is ineffective because the projects pass an empty cli arg list to the
conf object when they initialize it. By registering our cli args on the
global conf object, the projects can safely parse cli args in their
call to the conf object so things like --config-file won't be ignored.
This didn't work before because oslo.policy recognizes cli args like
--namespace that aren't recognized by the consuming projects.

This will require followup changes in each project to stop passing an
empty cli arg list to the conf object initialization.  In the meantime,
everything should continue to work as it did before.

Change-Id: Iacd257fc6c351582de45476768e3fd1775317d3c
Closes-Bug: 1849518
This commit is contained in:
Ben Nemec 2019-10-23 15:36:42 +00:00
parent 0f7e144d01
commit 686aa238f9
2 changed files with 18 additions and 11 deletions

View File

@ -334,9 +334,11 @@ def on_load_failure_callback(*args, **kwargs):
raise
def generate_sample(args=None):
def generate_sample(args=None, conf=None):
logging.basicConfig(level=logging.WARN)
conf = cfg.ConfigOpts()
# Allow the caller to pass in a local conf object for unit testing
if conf is None:
conf = cfg.CONF
conf.register_cli_opts(GENERATOR_OPTS + RULE_OPTS)
conf.register_opts(GENERATOR_OPTS + RULE_OPTS)
conf(args)
@ -345,7 +347,7 @@ def generate_sample(args=None):
def generate_policy(args=None):
logging.basicConfig(level=logging.WARN)
conf = cfg.ConfigOpts()
conf = cfg.CONF
conf.register_cli_opts(GENERATOR_OPTS + ENFORCER_OPTS)
conf.register_opts(GENERATOR_OPTS + ENFORCER_OPTS)
conf(args)
@ -367,9 +369,11 @@ def _upgrade_policies(policies, default_policies):
'new_name': rule_default.name})
def upgrade_policy(args=None):
def upgrade_policy(args=None, conf=None):
logging.basicConfig(level=logging.WARN)
conf = cfg.ConfigOpts()
# Allow the caller to pass in a local conf object for unit testing
if conf is None:
conf = cfg.CONF
conf.register_cli_opts(GENERATOR_OPTS + RULE_OPTS + UPGRADE_OPTS)
conf.register_opts(GENERATOR_OPTS + RULE_OPTS + UPGRADE_OPTS)
conf(args)
@ -396,7 +400,7 @@ def upgrade_policy(args=None):
def list_redundant(args=None):
logging.basicConfig(level=logging.WARN)
conf = cfg.ConfigOpts()
conf = cfg.CONF
conf.register_cli_opts(ENFORCER_OPTS)
conf.register_opts(ENFORCER_OPTS)
conf(args)

View File

@ -500,8 +500,9 @@ class GeneratorRaiseErrorTestCase(testtools.TestCase):
def test_generator_call_with_no_arguments_raises_error(self):
testargs = ['oslopolicy-sample-generator']
with mock.patch('sys.argv', testargs):
local_conf = cfg.ConfigOpts()
self.assertRaises(cfg.RequiredOptError, generator.generate_sample,
[])
[], local_conf)
class GeneratePolicyTestCase(base.PolicyBaseTestCase):
@ -655,6 +656,8 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase):
plugin=None,
obj=[self.new_policy])
self.extensions.append(ext)
# Just used for cli opt parsing
self.local_conf = cfg.ConfigOpts()
def test_upgrade_policy_json_file(self):
test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
@ -669,7 +672,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase):
self.get_config_file_fullname('new_policy.json'),
'--format', 'json']
with mock.patch('sys.argv', testargs):
generator.upgrade_policy()
generator.upgrade_policy(conf=self.local_conf)
new_file = self.get_config_file_fullname('new_policy.json')
new_policy = jsonutils.loads(open(new_file, 'r').read())
self.assertIsNotNone(new_policy.get('new_policy_name'))
@ -688,7 +691,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase):
self.get_config_file_fullname('new_policy.yaml'),
'--format', 'yaml']
with mock.patch('sys.argv', testargs):
generator.upgrade_policy()
generator.upgrade_policy(conf=self.local_conf)
new_file = self.get_config_file_fullname('new_policy.yaml')
new_policy = yaml.safe_load(open(new_file, 'r'))
self.assertIsNotNone(new_policy.get('new_policy_name'))
@ -706,7 +709,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase):
'--namespace', 'test_upgrade',
'--format', 'json']
with mock.patch('sys.argv', testargs):
generator.upgrade_policy()
generator.upgrade_policy(conf=self.local_conf)
expected = '''{
"new_policy_name": "rule:admin"
}'''
@ -724,7 +727,7 @@ class UpgradePolicyTestCase(base.PolicyBaseTestCase):
'--namespace', 'test_upgrade',
'--format', 'yaml']
with mock.patch('sys.argv', testargs):
generator.upgrade_policy()
generator.upgrade_policy(conf=self.local_conf)
expected = '''new_policy_name: rule:admin
'''
self.assertEqual(expected, stdout.getvalue())