Skip to content

Instantly share code, notes, and snippets.

@ahsan518
Last active January 24, 2017 20:35
Show Gist options
  • Save ahsan518/3d843212c4a596f4a26a1d5c21898d08 to your computer and use it in GitHub Desktop.
Save ahsan518/3d843212c4a596f4a26a1d5c21898d08 to your computer and use it in GitHub Desktop.
(craton) ubuntu@ubuntu-xenial:~/craton/craton/common$ python t-policy-using-role-assignments.py
{
"fleet:audit": "(role:admin or (principal:%(principal)s and role_:%(role_)s and resource:%(resource)s))"
}
Traceback (most recent call last):
File "t-policy-using-role-assignments.py", line 54, in <module>
ENF.register_defaults(rules)
File "/home/ubuntu/craton/lib/python3.5/site-packages/oslo_policy/policy.py", line 761, in register_defaults
self.register_default(default)
File "/home/ubuntu/craton/lib/python3.5/site-packages/oslo_policy/policy.py", line 747, in register_default
if default.name in self.registered_rules:
AttributeError: 'str' object has no attribute 'name'
(craton) ubuntu@ubuntu-xenial:~/craton/craton/common$ cat t-policy-using-role-assignments.py
# Copyright (c) 2015 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Policy Engine for craton."""
from oslo_config import cfg
from oslo_policy import policy as common_policy
from craton import exceptions
import pdb
CONF = cfg.CONF # should just be an empty config file
ENFORCER = common_policy.Enforcer(CONF)
ENF = common_policy.Enforcer(CONF, policy_file=None)
rules = {
"fleet:audit": "role:admin or (principal:%(principal)s and role_:%(role_)s and resource:%(resource)s)"
}
these_rules = common_policy.Rules.from_dict(rules)
print(these_rules)
ENFORCER.set_rules(these_rules)
ENFORCER.check_rules()
credentials = {'roles': ['user'], 'principal': 'foo', 'role_': 'fleet:audit', 'resource': 'baz'}
target = {'principal':'foo', 'role_':'fleet:audit', 'resource':'baz'}
ENFORCER.enforce("fleet:audit", target, credentials, do_raise=True)
#Using Enforcer Authorize
ENF.check_rules()
ENF.set_rules(these_rules)
ENF.register_defaults(rules)
ENF.register_default(policy.default('fleet:audit', 'rule: fleet:audit'))
ENFORCER.authorize("fleet:audit", target, credentials, do_raise=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment