A set of utilities and classes for working with Open Policy Agent based tools, including Gatekeeper and Conftest
Project description
Policy Kit
A set of utilities and classes for working with Open Policy Agent based tools, including Gatekeeper and Conftest.
Installation
Policy Kit can be installed from PyPI using pip
or similar tools:
pip install policykit
CLI
The module provides a CLI tool called pk
for using some of the functionality.
$ pk build *.rego
[SecurityControls] Generating a ConstraintTemplate from "SecurityControls.rego"
[SecurityControls] Searching "lib" for additional rego files
[SecurityControls] Adding library from "lib/kubernetes.rego"
[SecurityControls] Saving to "SecurityControls.yaml"
You can also use the tool via Docker:
docker run --rm -it -v $(pwd):/app garethr/policykit build
Python
This module currently contains several classes, the first for working with ConstraintTemplates
in Gatekeeper.
from policykit import ConstraintTemplate
with open(path_to_rego_source_file, "r") as rego:
ct = ConstraintTemplate(name, rego.read())
print(ct.yaml())
The Conftest
class makes interacting with Conftest from Python easy.
Note that this requires the conftest
executable to be available on the path.
>>> from policykit import Conftest
>>> cli = Conftest("policy")
>>> result = cli.test("deployment.yaml")
>>> result
ConftestRun(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.success
False
Passing in a dictionary to json_input
is parsed as JSON then sent as stdin to the confest
executable.
from policykit import Conftest
result = Conftest("policy").test(json_input={"foo": "bar"})
print(result)
Action
Policy Kit can also be easily used in GitHub Actions, using the following Action. This example also demonstrates
committing the generated files back into the Git repository. Update the the values in <>
as required.
on: push
name: Gatekeeper
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Generate ConstraintTemplates for Gatekeeper
uses: garethr/policykit/action@master
with:
args: <directory-of-rego-source-files>
- name: Commit to repository
env:
GITHUB_TOKEN: ${{ secrets.github_token }}
COMMIT_MSG: |
Generated new ConstraintTemplates from Rego source
skip-checks: true
run: |
# Hard-code user config
git config user.email "<your-email-address>"
git config user.name "<your-username>"
git config --get-regexp "user\.(name|email)"
# Update origin with token
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
# Checkout the branch so we can push back to it
git checkout master
git add .
# Only commit and push if we have changes
git diff --quiet && git diff --staged --quiet || (git commit -m "${COMMIT_MSG}"; git push origin master
Notes
A few caveats for anyone trying to use this module.
- Loading libraries with
lib
is only supported in Gatekeeper HEAD today but should be in the next release. - This module does not support parameterized ConstraintTemplates
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file policykit-0.4.0.tar.gz
.
File metadata
- Download URL: policykit-0.4.0.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.7.4 Darwin/19.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1a6e1bc00b00efd82ac61a6808ec0de3b7b9cf0169277047d960e52edb5b53f |
|
MD5 | 125e4a691d1b1bd1f36e27df66a99fd3 |
|
BLAKE2b-256 | ff5bc88a34e7b66f77a792b616fc4f92b542f60f14c39d3b6fcad0a4b562f5bc |
File details
Details for the file policykit-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: policykit-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.7.4 Darwin/19.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfc04f90cc5cb6fcac9fbb6c845d5c73439535a2c492c14856aff3a3e8cf3709 |
|
MD5 | 04df8d6229fa498fdfd1ee875395915a |
|
BLAKE2b-256 | 022409f64a4c7a26da318708d5426869d26c89cc133420be8e6510494d469b2c |