semgr8ns / semgr8s

Semgrep-based Policy Controller for Kubernetes
https://semgr8ns.github.io/semgr8s/
Apache License 2.0
5 stars 0 forks source link

use form of rule schema to deny broken rules #120

Open xopham opened 7 months ago

xopham commented 7 months ago

semgr8s loads rules from remote and configmaps. a single broken rule could break the policy engine as a whole. rules are also frequently updated. rules must be scanned against a schema to ensure broken rules cannot enter the system.

also important: document this schema, specifically 1 rule in 1 configmap, multiple rules in multiple configmaps, multiple rules in 1 configmap

xopham commented 5 months ago

it is possible to use semgreps own validation: https://semgrep.dev/docs/writing-rules/testing-rules#validating-rules

semgrep scan --metrics=off --validate --config /app/rules --json -o broken_rules.json

Broken rules would result in the following broken_rules.json:

{
  "errors": [
    {
      "code": 4,
      "level": "error",
      "long_msg": "One of these properties is missing: 'languages'",
      "short_msg": "Invalid rule schema",
      "spans": [
        {
          "end": {
            "col": 1,
            "line": 27,
            "offset": -1
          },
          "file": "rules/deny-default-namespace.yaml",
          "source_hash": "c16ac57d9db7bb7c762e3775cf1982c20eb2161542acf11b32b04edc26730dea",
          "start": {
            "col": 3,
            "line": 2,
            "offset": -1
          }
        }
      ],
      "type": "InvalidRuleSchemaError"
    },
    {
      "code": 2,
      "level": "error",
      "message": "Semgrep match found at line ./rules/deny-default-namespace.yaml:2:\n Please include a 'languages' field for your rule $RULEID!",
      "path": "./rules/deny-default-namespace.yaml",
      "type": "Semgrep match found"
    },
    {
      "code": 2,
      "level": "error",
      "message": "Rule parse error in rule restrict-image-registry:\n Missing required field regex",
      "rule_id": "restrict-image-registry",
      "type": "Rule parse error"
    }
  ],
  "paths": {
    "scanned": []
  },
  "results": [],
  "skipped_rules": [],
  "version": "1.68.0"
}

There is no definite way to determine the path to a broken rule when scanning multiple rules.

In summary, some considerations are necessary: