summaryrefslogtreecommitdiff
path: root/oslo_policy
diff options
context:
space:
mode:
authorBrant Knudson <bknudson@us.ibm.com>2016-02-15 13:35:53 -0600
committerBrant Knudson <bknudson@us.ibm.com>2016-02-22 13:17:36 -0600
commitea1268bcd38775554026db5adffc33fbde2bd1cf (patch)
treefe8344d4f08419a08c1d77cde9332350f3f8a340 /oslo_policy
parent83d209e9ed1a1f7f70cd818e6142f97515f0e178 (diff)
downloadoslo-policy-ea1268bcd38775554026db5adffc33fbde2bd1cf.tar.gz
Deprecate load_json() in favor of load()
The Rules.load_json function now supports loading YAML and is not limited to JSON. As such, the name is changed to load(). The old name remains but is deprecated. bp policy-yaml Change-Id: I8c71d1a566779f1641106a996ebbc9551e2f05e3
Diffstat (limited to 'oslo_policy')
-rw-r--r--oslo_policy/policy.py26
-rw-r--r--oslo_policy/shell.py2
-rw-r--r--oslo_policy/tests/test_fixtures.py2
-rw-r--r--oslo_policy/tests/test_policy.py21
4 files changed, 38 insertions, 13 deletions
diff --git a/oslo_policy/policy.py b/oslo_policy/policy.py
index 7ccd2fc..95cc6f1 100644
--- a/oslo_policy/policy.py
+++ b/oslo_policy/policy.py
@@ -217,6 +217,7 @@ desired rule name.
import logging
import os
+import warnings
from oslo_config import cfg
from oslo_serialization import jsonutils
@@ -299,8 +300,12 @@ class Rules(dict):
"""A store for rules. Handles the default_rule setting directly."""
@classmethod
- def load_json(cls, data, default_rule=None):
- """Allow loading of YAML/JSON rule data."""
+ def load(cls, data, default_rule=None):
+ """Allow loading of YAML/JSON rule data.
+
+ .. versionadded:: 1.5.0
+
+ """
try:
parsed = yaml.safe_load(data)
@@ -315,6 +320,21 @@ class Rules(dict):
return cls(rules, default_rule)
@classmethod
+ def load_json(cls, data, default_rule=None):
+ """Allow loading of YAML/JSON rule data.
+
+ .. warning::
+ This method is deprecated as of the 1.5.0 release in favor of
+ :meth:`load` and may be removed in the 2.0 release.
+
+ """
+ warnings.warn(
+ 'The load_json() method is deprecated as of the 1.5.0 release in '
+ 'favor of load() and may be removed in the 2.0 release.',
+ DeprecationWarning)
+ return cls.load(data, default_rule)
+
+ @classmethod
def from_dict(cls, rules_dict, default_rule=None):
"""Allow loading of rule data from a dictionary."""
@@ -494,7 +514,7 @@ class Enforcer(object):
reloaded, data = _cache_handler.read_cached_file(
self._file_cache, path, force_reload=force_reload)
if reloaded or not self.rules:
- rules = Rules.load_json(data, self.default_rule)
+ rules = Rules.load(data, self.default_rule)
self.set_rules(rules, overwrite=overwrite, use_conf=True)
self._loaded_files.append(path)
LOG.debug('Reloaded policy file: %(path)s', {'path': path})
diff --git a/oslo_policy/shell.py b/oslo_policy/shell.py
index 32f5d96..0b14a75 100644
--- a/oslo_policy/shell.py
+++ b/oslo_policy/shell.py
@@ -40,7 +40,7 @@ def tool(policy_file, access_file, apply_rule, is_admin=False):
access_data['project_id'] = access_data['project']['id']
access_data['is_admin'] = is_admin
policy_data = policy_file.read()
- rules = policy.Rules.load_json(policy_data, "default")
+ rules = policy.Rules.load(policy_data, "default")
class Object(object):
pass
diff --git a/oslo_policy/tests/test_fixtures.py b/oslo_policy/tests/test_fixtures.py
index 074c2da..a6e115a 100644
--- a/oslo_policy/tests/test_fixtures.py
+++ b/oslo_policy/tests/test_fixtures.py
@@ -30,7 +30,7 @@ class FixtureTestCase(test_base.PolicyBaseTestCase):
rules_json = {
action: "http:" + self.getUniqueString()
}
- rules = oslo_policy.Rules.load_json(json.dumps(rules_json))
+ rules = oslo_policy.Rules.load(json.dumps(rules_json))
self.enforcer.set_rules(rules)
return self.enforcer.enforce(rule=action,
target={},
diff --git a/oslo_policy/tests/test_policy.py b/oslo_policy/tests/test_policy.py
index 84206fe..43f4951 100644
--- a/oslo_policy/tests/test_policy.py
+++ b/oslo_policy/tests/test_policy.py
@@ -87,7 +87,7 @@ class RulesTestCase(test_base.BaseTestCase):
"admin_or_owner": [["role:admin"], ["project_id:%(project_id)s"]],
"default": []
})
- rules = policy.Rules.load_json(exemplar, 'default')
+ rules = policy.Rules.load(exemplar, 'default')
self.assertEqual('default', rules.default_rule)
self.assertEqual(dict(
@@ -103,12 +103,12 @@ class RulesTestCase(test_base.BaseTestCase):
"admin_or_owner": [["role:admin"], ["project_id:%(project_id)s"]],
"default": [
}"""
- self.assertRaises(ValueError, policy.Rules.load_json, exemplar,
+ self.assertRaises(ValueError, policy.Rules.load, exemplar,
'default')
@mock.patch.object(_parser, 'parse_rule', lambda x: x)
def test_load_yaml(self):
- # Test that simplified YAML can be used with load_json.
+ # Test that simplified YAML can be used with load().
# Show that YAML allows useful comments.
exemplar = """
# Define a custom rule.
@@ -116,7 +116,7 @@ admin_or_owner: role:admin or project_id:%(project_id)s
# The default rule is used when there's no action defined.
default: []
"""
- rules = policy.Rules.load_json(exemplar, 'default')
+ rules = policy.Rules.load(exemplar, 'default')
self.assertEqual('default', rules.default_rule)
self.assertEqual(dict(
@@ -126,7 +126,7 @@ default: []
@mock.patch.object(_parser, 'parse_rule', lambda x: x)
def test_load_yaml_invalid_exc(self):
- # When the JSON isn't valid, ValueError is raised on load_json.
+ # When the JSON isn't valid, ValueError is raised on load().
# Note the trailing , in the exemplar is invalid JSON.
exemplar = """{
# Define a custom rule.
@@ -134,7 +134,7 @@ admin_or_owner: role:admin or project_id:%(project_id)s
# The default rule is used when there's no action defined.
default: [
}"""
- self.assertRaises(ValueError, policy.Rules.load_json, exemplar,
+ self.assertRaises(ValueError, policy.Rules.load, exemplar,
'default')
@mock.patch.object(_parser, 'parse_rule', lambda x: x)
@@ -165,6 +165,11 @@ default: [
self.assertEqual(exemplar, str(rules))
+ def test_load_json_deprecated(self):
+ with self.assertWarnsRegex(DeprecationWarning,
+ r'load_json\(\).*load\(\)'):
+ policy.Rules.load_json(jsonutils.dumps({'default': ''}, 'default'))
+
class EnforcerTest(base.PolicyBaseTestCase):
@@ -311,7 +316,7 @@ class EnforcerTest(base.PolicyBaseTestCase):
"deny_stack_user": "not role:stack_user",
"cloudwatch:PutMetricData": ""
})
- rules = policy.Rules.load_json(rules_json)
+ rules = policy.Rules.load(rules_json)
self.enforcer.set_rules(rules)
action = 'cloudwatch:PutMetricData'
creds = {'roles': ''}
@@ -322,7 +327,7 @@ class EnforcerTest(base.PolicyBaseTestCase):
"deny_stack_user": "not role:stack_user",
"cloudwatch:PutMetricData": ""
})
- rules = policy.Rules.load_json(rules_json)
+ rules = policy.Rules.load(rules_json)
default_rule = _checks.TrueCheck()
enforcer = policy.Enforcer(self.conf, default_rule=default_rule)
enforcer.set_rules(rules)