一组实用程序和类,用于使用基于开放策略代理的工具,包括Gatekeeper和Conftest
policykit的Python项目详细描述
保险箱
一组用于使用基于Open Policy Agent的工具的实用程序和类,包括Gatekeeper和{a4}。在
安装
可以使用pip
或类似工具从PyPI安装策略包:
pip install policykit
命令行接口
该模块提供了一个名为pk
的CLI工具,用于使用某些功能。在
您也可以通过Docker使用该工具:
docker run --rm -it -v $(pwd):/app garethr/policykit build
Python
此模块当前包含几个类,第一个类用于在Gatekeeper中使用ConstraintTemplates
。在
frompolicykitimportConstraintTemplatewithopen(path_to_rego_source_file,"r")asrego:ct=ConstraintTemplate(name,rego.read())print(ct.yaml())
{{cd4}使得与Python类的交互变得容易。
请注意,这要求路径上有conftest
可执行文件。在
>>>frompolicykitimportConftest>>>cli=Conftest("policy")>>>result=cli.test("deployment.yaml")>>>resultConftestRun(code=1,results=[ConftestResult(filename='/Users/garethr/Documents/conftest/examples/kubernetes/deployment.yaml',Warnings=[],Failures=['hello-kubernetes must include Kubernetes recommended labels: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels ','Containers must not run as root in Deployment hello-kubernetes','Deployment hello-kubernetes must provide app/release labels for pod selectors'],Successes=[])]>>>result.successFalse
传递给json_input
的字典被解析为JSON,然后作为stdin发送到confest
可执行文件。在
frompolicykitimportConftestresult=Conftest("policy").test(json_input={"foo":"bar"})print(result)
行动
在GitHub操作中也可以很容易地使用policykit,使用以下操作。这个例子也展示了
将生成的文件提交回Git存储库。根据需要更新<>
中的值。在
on:pushname:Gatekeeperjobs:build:runs-on:ubuntu-lateststeps:-uses:actions/checkout@master-name:Generate ConstraintTemplates for Gatekeeperuses:garethr/policykit/action@masterwith:args:<directory-of-rego-source-files>-name:Commit to repositoryenv:GITHUB_TOKEN:${{ secrets.github_token }}COMMIT_MSG:|Generated new ConstraintTemplates from Rego sourceskip-checks: truerun:|# Hard-code user configgit config user.email "<your-email-address>"git config user.name "<your-username>"git config --get-regexp "user\.(name|email)"# Update origin with tokengit remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git# Checkout the branch so we can push back to itgit checkout mastergit add .# Only commit and push if we have changesgit diff --quiet && git diff --staged --quiet || (git commit -m "${COMMIT_MSG}"; git push origin master
注释
对于任何尝试使用此模块的人来说,有一些注意事项。在
- Loading libraries with ^{
} 目前只在Gatekeeper HEAD中受支持,但在下一个版本中应该支持。在 - 此模块不支持参数化ConstraintTemplates
- 项目
标签: