<div dir="ltr"><a href="https://www.digitalocean.com/community/questions/how-enforce-an-updated-policy-in-etc-nova-policy-json">https://www.digitalocean.com/community/questions/how-enforce-an-updated-policy-in-etc-nova-policy-json</a><br><div><br></div><div><a href="https://ask.openstack.org/en/question/96797/how-enforce-and-apply-an-updated-policy-in-etcnovapolicyjson/">https://ask.openstack.org/en/question/96797/how-enforce-and-apply-an-updated-policy-in-etcnovapolicyjson/</a><br></div><div><br></div><div><br></div><div><br></div><div><div>from keystoneclient.v2_0.client import Client</div><div>import os</div><div>import csv</div><div>import sys</div><div>import logging</div><div>import time</div><div>from nova import db</div><div>from nova import config</div><div>from nova import context</div><div>import novaclient.v1_1.client as nvclient</div><div>from keystoneauth1 import loading</div><div>from keystoneauth1 import session</div><div>#from novaclient import nvclient</div><div>import urllib3</div><div>from os import environ as env</div><div>from nova import policy</div><div><br></div><div>def get_nova_credentials_v2():</div><div>    d = {}</div><div>    d['version'] = '2'</div><div>    d['username'] = os.environ['OS_USERNAME']</div><div>    d['password'] = os.environ['OS_PASSWORD']</div><div>    d['auth_url'] = os.environ['OS_AUTH_URL']</div><div>    d['tenant_name'] = os.environ['OS_TENANT_NAME']</div><div>    d['insecure'] = 'True'</div><div>    #d['os_cacert'] = os.environ['OS_CACERT']</div><div>    return d</div><div><br></div><div># set up logging to file - see previous section for more details</div><div>logging.basicConfig(level=logging.DEBUG,</div><div>                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',</div><div>                    datefmt='%m-%d %H:%M',</div><div>                    filename='/home/martin/createprojectanduser'+time.strftime("%d-%m-%y-%H-%M-%S")+'.log',</div><div>                    filemode='w')</div><div># define a Handler which writes INFO messages or higher to the sys.stderr</div><div>console = logging.StreamHandler()</div><div>console.setLevel(logging.INFO)</div><div># set a format which is simpler for console use</div><div>formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')</div><div># tell the handler to use this format</div><div>console.setFormatter(formatter)</div><div># add the handler to the root logger</div><div>logging.getLogger('').addHandler(console)</div><div><br></div><div>credentials = get_nova_credentials_v2()</div><div>keystone_client = Client(**credentials)</div><div>tenants = keystone_client.tenants.list()</div><div>rolelist = keystone_client.roles.list()</div><div>userlist = keystone_client.users.list()</div><div>my_admin = [x for x in rolelist if <a href="http://x.name">x.name</a>=="admin"][0]</div><div>my_member = [x for x in rolelist if <a href="http://x.name">x.name</a>=="_member_"][0]</div><div>my_admin_user = [x for x in userlist if <a href="http://x.name">x.name</a>=="admin"][0]</div><div>my_tenant = [x for x in tenants if <a href="http://x.name">x.name</a>=="CoinMarkets"][0]</div><div>#nc = nvclient.Client(None, None, None, auth_url=os.environ['OS_AUTH_URL'], tenant_id=<a href="http://my_tenant.id">my_tenant.id</a>, auth_token=keystone_client.auth_token, insecure='True')</div><div>my_tenant = [x for x in tenants if <a href="http://x.name">x.name</a>=="MEDULLA"][0]</div><div><br></div><div>#loader = loading.get_plugin_loader('password')</div><div>#auth = loader.load_from_options(auth_url=os.environ['OS_AUTH_URL'],username="<a href="mailto:hello@gmail.com">hello@gmail.com</a>",password="8_hrRfsa",project_id=<a href="http://my_tenant.id">my_tenant.id</a>)</div><div>#sess = session.Session(auth=auth)</div><div>#nc = nvclient.Client("1.1", session=sess)</div><div><br></div><div>#nc = nvclient.Client(None, None, None, auth_url=os.environ['OS_AUTH_URL'], tenant_id=<a href="http://my_tenant.id">my_tenant.id</a>, auth_token=keystone_client.auth_token)</div><div>#nc = nvclient.Client("<a href="mailto:hello@gmail.com">hello@gmail.com</a>", "8_hrRfsa", <a href="http://my_tenant.id">my_tenant.id</a>, os.environ['OS_AUTH_URL'], insecure='True')</div><div>#,region_name=os.environ['OS_REGION_NAME']</div><div>#nc = nvclient.Client(auth_url=os.environ['OS_AUTH_URL'],username="<a href="mailto:hello@gmail.com">hello@gmail.com</a>",api_key="8_hrRfsa",project_id=<a href="http://my_tenant.id">my_tenant.id</a>,cacert=os.environ['OS_CACERT'])</div><div><br></div><div><br></div><div>auth_system = env.get('OS_AUTH_SYSTEM', 'keystone')</div><div>if auth_system != "keystone":</div><div>  print("here")</div><div>  auth_plugin = novaclient.auth_plugin.load_plugin(auth_system)</div><div>else:</div><div>  auth_plugin = None</div><div><br></div><div><br></div><div>#nc = nvclient.Client(auth_url=env['OS_AUTH_URL'],username=env['OS_USERNAME'],api_key=env['OS_PASSWORD'],project_id=env['OS_TENANT_NAME'],region_name=env['OS_REGION_NAME'],auth_system=auth_system,auth_plugin=auth_plugin)</div><div>#,region_name=env['OS_REGION_NAME']</div><div>#nc.authenticate()</div><div><br></div><div>nc = nvclient.Client(auth_url=os.environ['OS_AUTH_URL'],username="<a href="mailto:hello@gmail.com">hello@gmail.com</a>",api_key="8_hrRfsa",project_id="MEDULLA",auth_system=auth_system,auth_plugin=auth_plugin,cacert=os.environ['OS_CACERT'])</div><div><br></div><div><br></div><div>server_policies = [("compute_extension:quotas:update", ""),]</div><div><br></div><div>policy_engine = policy.get_rules()</div><div>policy.enforce('compute_extension:quotas:update', policy_engine, nc)</div></div><div><br></div><div><br></div><div><div><br></div><div>>>> policy_engine = policy.get_rules()</div><div>>>> policy.enforce('compute_extension:quotas:update', policy_engine, nc)</div><div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div><div>  File "/usr/lib/python2.7/dist-packages/nova/policy.py", line 92, in enforce</div><div>    init()</div><div>  File "/usr/lib/python2.7/dist-packages/nova/policy.py", line 58, in init</div><div>    _POLICY_PATH = CONF.find_file(_POLICY_PATH)</div><div>  File "/usr/lib/python2.7/dist-packages/oslo/config/cfg.py", line 1908, in find_file</div><div>    if self.config_dir:</div><div>  File "/usr/lib/python2.7/dist-packages/oslo/config/cfg.py", line 1648, in __getattr__</div><div>    raise NoSuchOptError(name)</div><div>oslo.config.cfg.NoSuchOptError: no such option: config_dir</div><div>>>></div></div><div><br></div><div><br></div></div>