summaryrefslogtreecommitdiff
path: root/oslo_policy/shell.py
diff options
context:
space:
mode:
authorMoisés Guimarães de Medeiros <moguimar@redhat.com>2019-01-03 13:28:22 +0100
committerMoisés Guimarães <moguimar@redhat.com>2019-01-03 12:30:36 +0000
commit48f963b29eac2f7da2b1fd68b1955c2735caa21a (patch)
tree7beb0f59401134563b1c889072344daba752d531 /oslo_policy/shell.py
parentc9ea8f7bfa417f6bd82e1e098b7e9060108ed60a (diff)
downloadoslo-policy-48f963b29eac2f7da2b1fd68b1955c2735caa21a.tar.gz
Fixes file access using with statements.
Change-Id: If29fcf542c48a931edfebb7763d80f045c2fa0d7 Signed-off-by: Moisés Guimarães de Medeiros <moguimar@redhat.com>
Diffstat (limited to 'oslo_policy/shell.py')
-rw-r--r--oslo_policy/shell.py18
1 files changed, 11 insertions, 7 deletions
diff --git a/oslo_policy/shell.py b/oslo_policy/shell.py
index b030fdf..56f5cce 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__":