From 48f963b29eac2f7da2b1fd68b1955c2735caa21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es=20de=20Medeiros?= Date: Thu, 3 Jan 2019 13:28:22 +0100 Subject: [PATCH] Fixes file access using with statements. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If29fcf542c48a931edfebb7763d80f045c2fa0d7 Signed-off-by: Moisés Guimarães de Medeiros --- oslo_policy/shell.py | 18 +++++++++++------- oslo_policy/tests/test_shell.py | 18 +++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/oslo_policy/shell.py b/oslo_policy/shell.py index b030fdfe..56f5cce2 100644 --- a/oslo_policy/shell.py +++ b/oslo_policy/shell.py @@ -53,12 +53,17 @@ def flatten(d, parent_key=''): def tool(policy_file, access_file, apply_rule, is_admin=False, target_file=None): - access = access_file.read() + with open(access_file, "rb", 0) as a: + access = a.read() + access_data = jsonutils.loads(access)['token'] access_data['roles'] = [role['name'] for role in access_data['roles']] access_data['project_id'] = access_data['project']['id'] access_data['is_admin'] = is_admin - policy_data = policy_file.read() + + with open(policy_file, "rb", 0) as p: + policy_data = p.read() + rules = policy.Rules.load(policy_data, "default") class Object(object): @@ -67,7 +72,9 @@ def tool(policy_file, access_file, apply_rule, is_admin=False, o.rules = rules if target_file: - target = target_file.read() + with open(target_file, "rb", 0) as t: + target = t.read() + target_data = flatten(jsonutils.loads(target)) else: target_data = {"project_id": access_data['project_id']} @@ -112,12 +119,9 @@ def main(): conf() - policy = open(conf.policy, "rb", 0) - access = open(conf.access, "rb", 0) - target = open(conf.target, "rb", 0) if conf.target else None is_admin = conf.is_admin.lower() == "true" - tool(policy, access, conf.rule, is_admin, target) + tool(conf.policy, conf.access, conf.rule, is_admin, conf.target) if __name__ == "__main__": diff --git a/oslo_policy/tests/test_shell.py b/oslo_policy/tests/test_shell.py index 3a915ffe..09a07c71 100644 --- a/oslo_policy/tests/test_shell.py +++ b/oslo_policy/tests/test_shell.py @@ -46,8 +46,8 @@ class CheckerTestCase(base.PolicyBaseTestCase): @mock.patch("oslo_policy._checks.TrueCheck.__call__") def test_pass_rule_parameters(self, call_mock): - policy_file = open(self.get_config_file_fullname('policy.yaml'), 'r') - access_file = open(self.get_config_file_fullname('access.json'), 'r') + policy_file = self.get_config_file_fullname('policy.yaml') + access_file = self.get_config_file_fullname('access.json') apply_rule = None is_admin = False stdout = self._capture_stdout() @@ -74,8 +74,8 @@ class CheckerTestCase(base.PolicyBaseTestCase): def test_pass_rule_parameters_sorted(self): self.create_config_file("policy.yaml", self.SAMPLE_POLICY_UNSORTED) - policy_file = open(self.get_config_file_fullname('policy.yaml'), 'r') - access_file = open(self.get_config_file_fullname('access.json'), 'r') + policy_file = self.get_config_file_fullname('policy.yaml') + access_file = self.get_config_file_fullname('access.json') apply_rule = None is_admin = False stdout = self._capture_stdout() @@ -114,9 +114,9 @@ passed: sampleservice:sample_rule2 "target.json", jsonutils.dumps(sample_target)) - policy_file = open(self.get_config_file_fullname('policy.yaml'), 'r') - access_file = open(self.get_config_file_fullname('access.json'), 'r') - target_file = open(self.get_config_file_fullname('target.json'), 'r') + policy_file = self.get_config_file_fullname('policy.yaml') + access_file = self.get_config_file_fullname('access.json') + target_file = self.get_config_file_fullname('target.json') stdout = self._capture_stdout() shell.tool(policy_file, access_file, apply_rule, is_admin, @@ -131,8 +131,8 @@ passed: sampleservice:sample_rule2 def test_all_nonadmin(self): - policy_file = open(self.get_config_file_fullname('policy.yaml'), 'r') - access_file = open(self.get_config_file_fullname('access.json'), 'r') + policy_file = self.get_config_file_fullname('policy.yaml') + access_file = self.get_config_file_fullname('access.json') apply_rule = None is_admin = False stdout = self._capture_stdout()