[hub] add non-host evalPolicy API

This commit is contained in:
Yu Ming Zhu 2021-08-11 18:53:57 +00:00
parent 601cd33902
commit cdbebd963d
3 changed files with 21 additions and 8 deletions

View file

@ -10099,6 +10099,20 @@ def check_policy(name, data, default='deny', strict=False, force=False):
raise koji.ActionNotAllowed(err_str)
def eval_policy(name, data):
"""Evaluate named policy with given data and return the result
:param str name: the policy name
:param dict data: the policy data
:returns the action as a string
:raises koji.GenericError if the policy is empty or not found
"""
ruleset = context.policy.get(name)
if not ruleset:
raise koji.GenericError("no such policy: %s" % name)
return ruleset.apply(data)
def policy_data_from_task(task_id):
"""Calculate policy data from task id
@ -10653,6 +10667,8 @@ class RootExports(object):
q += """ ORDER BY id DESC LIMIT 1"""
return _singleRow(q, values, fields, strict=True)
evalPolicy = staticmethod(eval_policy)
def makeTask(self, *args, **opts):
"""Creates task manually. This is mainly for debugging, only an admin
can make arbitrary tasks. You need to supply all *args and **opts
@ -14622,10 +14638,7 @@ class HostExports(object):
"""Evaluate named policy with given data and return the result"""
host = Host()
host.verify()
ruleset = context.policy.get(name)
if not ruleset:
raise koji.GenericError("no such policy: %s" % name)
return ruleset.apply(data)
return eval_policy(name, data)
def newBuildRoot(self, repo, arch, task_id=None):
host = Host()

View file

@ -480,7 +480,7 @@ class SCM(object):
for k, v in six.iteritems(self.get_info()):
policy_data[re.sub(r'^(scm_?)?', 'scm_', k)] = v
policy_data.update(extra_data)
result = (session.host.evalPolicy('build_from_scm', policy_data) or '').split()
result = (session.evalPolicy('build_from_scm', policy_data) or '').split()
is_allowed = result and result[0].lower() in ('yes', 'true', 'allow', 'allowed')
if not is_allowed:
raise koji.BuildError(

View file

@ -259,7 +259,7 @@ class TestSCM(unittest.TestCase):
"git://maybeserver/..//badpath/project#1234",
]
session = mock.MagicMock()
session.host.evalPolicy.side_effect = FakePolicy(policy['one']).evalPolicy
session.evalPolicy.side_effect = FakePolicy(policy['one']).evalPolicy
for url in good:
scm = SCM(url)
scm.assert_allowed(session=session, by_config=False, by_policy=True)
@ -271,7 +271,7 @@ class TestSCM(unittest.TestCase):
def test_opts_by_policy(self):
session = mock.MagicMock()
session.host.evalPolicy.side_effect = FakePolicy(policy['two']).evalPolicy
session.evalPolicy.side_effect = FakePolicy(policy['two']).evalPolicy
url = "git://default/koji.git#1234"
scm = SCM(url)
@ -372,7 +372,7 @@ class TestSCM(unittest.TestCase):
'''
session = mock.MagicMock()
session.host.evalPolicy.side_effect = FakePolicy(policy['two']).evalPolicy
session.evalPolicy.side_effect = FakePolicy(policy['two']).evalPolicy
url = "git://default/koji.git#1234"
scm = SCM(url)