Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional violation_format to rego rules #1728

Merged
merged 1 commit into from
Nov 27, 2023

Conversation

jhrozek
Copy link
Contributor

@jhrozek jhrozek commented Nov 24, 2023

Adds an optional parameter for the rego evaluator that allows specifying
that if the constraints mode is used, then the constraints message
should be a valid JSON object with a key and a value so that it decodes
into map[string]any.

This is done by passing an outputFormat into the rule, the usage can be
seen in unit tests.

The default is still "text" to keep backwards compatibility.

If the evaluator asks for JSON, but back comes just a string, we can
assume that the policy doesn't support JSON output, so we marshall the
string ourvelves into { "msg": $response }.

The main use-case is rules that print a list of items violating a
policy, those can then be summarized using jq like this:

./bin/minder profile_status list --provider=github -i actions-github-profile -d -ojson 2>/dev/null | jq '.ruleEvaluationStatus | map(select(.ruleName == "repo_acti
on_list" and .status == "failure")) | map({repo_name: .entityInfo.repo_name, details: .details | fromjson})'
[
  {
    "repo_name": "testrepo",
    "details": [
      {
        "actions_not_allowed": [
          "docker/build-push-action",
          "docker/login-action",
          "docker/metadata-action",
          "docker/setup-buildx-action"
        ]
      }
    ]
  },
  {
    "repo_name": "bad-go",
    "details": [
      {
        "actions_not_allowed": [
          "docker/build-push-action",
          "docker/login-action",
          "docker/metadata-action",
          "docker/setup-buildx-action"
        ]
      }
    ]
  }
]

Adds an optional parameter for the rego evaluator that allows specifying
that if the constraints mode is used, then the constraints message
should be a valid JSON object with a key and a value so that it decodes
into `map[string]any`.

This is done by passing an outputFormat into the rule, the usage can be
seen in unit tests.

The default is still "text" to keep backwards compatibility.

If the evaluator asks for JSON, but back comes just a string, we can
assume that the policy doesn't support JSON output, so we marshall the
string ourvelves into `{ "msg": $response }`.

The main use-case is rules that print a list of items violating a
policy, those can then be summarized using jq like this:
```
./bin/minder profile_status list --provider=github -i actions-github-profile -d -ojson 2>/dev/null | jq '.ruleEvaluationStatus | map(select(.ruleName == "repo_acti
on_list" and .status == "failure")) | map({repo_name: .entityInfo.repo_name, details: .details | fromjson})'
[
  {
    "repo_name": "testrepo",
    "details": [
      {
        "actions_not_allowed": [
          "docker/build-push-action",
          "docker/login-action",
          "docker/metadata-action",
          "docker/setup-buildx-action"
        ]
      }
    ]
  },
  {
    "repo_name": "bad-go",
    "details": [
      {
        "actions_not_allowed": [
          "docker/build-push-action",
          "docker/login-action",
          "docker/metadata-action",
          "docker/setup-buildx-action"
        ]
      }
    ]
  }
]
```
jhrozek added a commit to jhrozek/minder-rules-and-profiles that referenced this pull request Nov 24, 2023
…t a set of allowed actions

Adds a new rule_type that allows specifying an allow list of actions
used in repositories. The evaluation uses rego and is able to print a
JSON representation of the actions that are not allowed.

Example profile:
```
version: v1
type: profile
name: actions-github-profile
context:
  provider: github
alert: "off"
remediate: "off"
repository:
  - type: repo_action_list
    def:
      actions:
        - actions/checkout
        - docker/build-push-action
        - docker/login-action
        - docker/metadata-action
        - docker/setup-buildx-action
        - sigstore/cosign-installer
```

Specifying an empty list allows you to list the actions used in the repo:
```
version: v1
type: profile
name: actions-github-profile
context:
  provider: github
alert: "off"
remediate: "off"
repository:
  - type: repo_action_list
    def:
      actions: []
```

Note that this PR depends on mindersec/minder#1728
@jhrozek
Copy link
Contributor Author

jhrozek commented Nov 24, 2023

to test, feel free to use mindersec/minder-rules-and-profiles#20

@jhrozek jhrozek merged commit 7d3b950 into mindersec:main Nov 27, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants