From 4cc14178ef68d9f49e53fc7a71b053509a763592 Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Fri, 24 Mar 2017 16:00:28 +0800 Subject: [PATCH] Simplify message of exception PolicyNotAuthorized The message is a string, and target and credentials are dicts. Not only do they make the message very ugly and hard for a human to parse, but they are giving information that is typically unnecessary and won't be understood by most users. Closes-Bug: #1673859 Change-Id: Iabd7d6ebb0fda1c631bb219846643a065fa4fb31 --- oslo_policy/policy.py | 4 +--- oslo_policy/tests/test_policy.py | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/oslo_policy/policy.py b/oslo_policy/policy.py index 211d6c9b..6480767f 100644 --- a/oslo_policy/policy.py +++ b/oslo_policy/policy.py @@ -296,9 +296,7 @@ class PolicyNotAuthorized(Exception): """Default exception raised for policy enforcement failure.""" def __init__(self, rule, target, creds): - msg = (_('%(target)s is disallowed by policy rule %(rule)s ' - 'with %(creds)s ') % - {'rule': rule, 'target': target, 'creds': creds}) + msg = _("%(rule)s is disallowed by policy") % {'rule': rule} super(PolicyNotAuthorized, self).__init__(msg) diff --git a/oslo_policy/tests/test_policy.py b/oslo_policy/tests/test_policy.py index 744b2d9d..c0506df2 100644 --- a/oslo_policy/tests/test_policy.py +++ b/oslo_policy/tests/test_policy.py @@ -724,8 +724,10 @@ class CheckFunctionTestCase(base.PolicyBaseTestCase): # raised. self.enforcer.set_rules(dict(default=_checks.FalseCheck())) - self.assertRaises(policy.PolicyNotAuthorized, self.enforcer.enforce, - 'rule', 'target', 'creds', True) + self.assertRaisesRegex(policy.PolicyNotAuthorized, + " is disallowed by policy", + self.enforcer.enforce, + 'rule', 'target', 'creds', True) def test_check_raise_custom_exception(self): self.enforcer.set_rules(dict(default=_checks.FalseCheck()))