#13 stock policy
Merged by nkinder. Opened by admiyo.
stock  into  master

Download 13.patch

Add stock policy from the services and show the difference in applying the policy whatn used with the different access data tokens

You can use this to extract some policies as well

#!/usr/bin/env python
import argparse
import importlib
import pkgutil
# Create an argparser for the service name.
parser = argparse.ArgumentParser(
    description="Generate a commented yaml file from the policy in code rules"
    " extracted from a given OpenStack Service")
# Create argument for the service name, this should be the name of the
# python module to import. It will import <service_name>.policies to get
# the python files where the policies are defined
parser.add_argument("service", type=str,
                    help="The name of the openstack services's python module"
                    " to import for policy extraction")
# Parse user supplied arguments
args = parser.parse_args()
# Import the <service_name>.policies module, which should contain
# submodules that contain the actual policy logic
try:
    policies = importlib.import_module(args.service + ".policies")
except Exception as e:
    policies = importlib.import_module(args.service + ".policy")
# Policy_files should be the list of all the python submodules defined
# inside the policy module. These contain the rule logic that is enforced
# by the service
policy_files = [submod.name for submod in pkgutil.walk_packages(
    path=policies.__path__)]
# "Base", if it's defined, will contain the base rule logic that other policy
# rules use for enforcement, for easy of use, move this to the front
if any("base" in value for value in policy_files):
    policy_files.insert(0, policy_files.pop(policy_files.index("base")))
# Iterate through the list of policy files, extract and print the actual rules
for f in policy_files:
    # Get the actual policy file from the policies module with getattr
    policy_file = getattr(policies, f)
    # Print the name of the module this was defined in
    print("### Policy Rules defined in {}".format(policy_file.__name__))
    # Newline for formatting
    print("")
    # The list containing the policy objects should be either
    # "<name>_policies" or "rules", try both, if neither exist, this is
    # probably not what we want, so continue on to next file
    try:
        # Attempt to get <policy_file_name>_policies via getattr
        policy_objects = getattr(policy_file, "{}_policies".format(f))
    except Exception as e:
        try:
            # Failing that, as it will in "Base" files, get the field "rules",
            # which should contain the role:rule mappings
            policy_objects = getattr(policy_file, "rules")
        except Exception as e2:
            continue
    # Now that we have our policy objects, iterate through them and print the
    # useful fields
    for policy in policy_objects:
        # Description is one of the most important, if it exists, print it,
        # commented
        if policy.description is not None:
            # Since the description can be a multi-line string, split it into
            # several commented lines
            desc = policy.description.splitlines()
            for line in desc:
                # Print commented either way
                print("# {}".format(line))
        # Get the operations this policy may have associated with it, this will
        # be in the form of stuff like GET and a url like /servers/{id}
        try:
            # If there's an operations field in the policy, it'll be a list,
            # for each element, print the URLs and verbs it is associated with
            for operation in policy.operations:
                # Print in the format VERB /url/path/
                print("# {} {}".format(operation["method"], operation["path"]))
        except Exception as e:
            # If we don't have anything here, just print a blank line
            print("#")
        # Finally print the policy rule and its default check_str, this will
        # turn into something of the form:
        # "rule_name": "rule:context_is_admin"
        # or something similar
        print('# "{}": "{}"'.format(policy.name, policy.check_str))
        # Finally print a newline to separate the block
        print("")

Worked for some like aodh

1 new commit added

  • Update path to use centos for Denver lab

rebased onto cbc3364ba839f19f9e3cbea1987f5ca02c648534

Pull-Request has been merged by nkinder

Metadata