项目作者: b4b4r07

项目描述 :
Run opa test command with GitHub Actions
高级语言: Shell
项目地址: git://github.com/b4b4r07/action-opa.git
创建时间: 2020-01-31T09:02:12Z
项目社区:https://github.com/b4b4r07/action-opa

开源协议:

下载


action-opa

Run opa test command with GitHub Actions

You can use the fixed version from: Releases

Usage

A whole example is here:

  1. name: opa
  2. on:
  3. pull_request:
  4. paths:
  5. - '.github/workflows/opa.yml' ## self-trigger
  6. - '.policy/**'
  7. jobs:
  8. opa:
  9. name: opa test
  10. runs-on: ubuntu-latest
  11. steps:
  12. - name: Checkout
  13. uses: actions/checkout@v1
  14. - name: Get changed objects
  15. uses: b4b4r07/action-changed-objects@master
  16. with:
  17. added: 'true'
  18. deleted: 'false'
  19. modified: 'true'
  20. id: objects
  21. - name: Run opa test against changed files
  22. uses: b4b4r07/action-opa@master
  23. if: steps.objects.outputs.changed
  24. with:
  25. coverage: 90%
  26. files: ${{ steps.objects.outputs.changed }}
  27. id: opa
  28. - name: Post opa command result to GitHub comment
  29. uses: b4b4r07/action-github-comment@master
  30. if: steps.opa.outputs.result
  31. with:
  32. body: |
  33. ## opa test result
  1. ${{ steps.opa.outputs.result }}
  2. ```
  3. env:
  4. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  5. LOG: trace
  1. If you want to test [opa](https://github.com/open-policy-agent/opa)/[conftest](https://github.com/instrumenta/conftest) policies against only changed files, you need to use [b4b4r07/action-changed-objects](https://github.com/b4b4r07/action-changed-objects) to get the changed files in Git commit. It defaults to compare with checkout-ed branch and origin/master branch.
  2. Besides, if you want to post the `opa test` command result on your pull requests, you need to set the step `Post opa command result to GitHub comment`. The contents of `body` is the message itself. You can configure it as Markdown. For more details, please see also [b4b4r07/action-github-comment](https://github.com/b4b4r07/action-github-comment).
  3. <img src="docs/comment.png" width="600">
  4. BTW, you want the example rego files:
  5. <details><summary><code>example.rego</code></summary>
  6. </br>
  7. ```rego
  8. package authz
  9. allow {
  10. input.path == ["users"]
  11. input.method == "POST"
  12. }
  13. allow {
  14. some profile_id
  15. input.path = ["users", profile_id]
  16. input.method == "GET"
  17. profile_id == input.user_id
  18. }

example_test.rego



rego package authz test_post_allowed { allow with input as {"path": ["users"], "method": "POST"} } test_get_anonymous_denied { not allow with input as {"path": ["users"], "method": "GET"} } test_get_user_allowed { allow with input as {"path": ["users", "bob"], "method": "GET", "user_id": "bob"} } test_get_another_user_denied { not allow with input as {"path": ["users", "bob"], "method": "GET", "user_id": "alice"} }

  1. $ docker run -v $(pwd):/tests openpolicyagent/opa test -v /tests/example.rego /tests/example_test.rego
  2. data.authz.test_post_allowed: PASS (2.3088ms)
  3. data.authz.test_get_anonymous_denied: PASS (765.8µs)
  4. data.authz.test_get_user_allowed: PASS (880.2µs)
  5. data.authz.test_get_another_user_denied: PASS (1.1077ms)
  6. --------------------------------------------------------------------------------
  7. PASS: 4/4

Customizing

inputs

The following are optional as step.with keys

Name Type Description Default
path String Path to directory where rego files are located .
coverage String Percentage of test coverage to require 80%
files String Files subject to OPA testing. List should be separated by a space

When providing a path and files at the same time, files will be attempted first, then falling back on path if the files can not be got from.

outputs

The following outputs can be accessed via ${{ steps.<step-id>.outputs }} from this action

Name Type Description
result String Outputs of opa command

environment variables

The following are as step.env keys

Name Description
n/a n/a

License

MIT