[PATCH v2 05/11] kconfig-audit: Split policy schema for component reuse
From: Ahmed S. Darwish
Date: Thu Dec 03 2026 - 17:49:58 EST
Normal kconfig-audit policy files require every rule to define both
'expected' and 'severity'. Overlay files are different: an overlay rule
may override only selected fields of an existing base rule. Thus, a schema
for overlay files will permit these fields to be omitted.
Move common rule definitions into schema/rule.yml, where it can be used by
multiple schemas. Add a strict schema/policy-v1.yml wrapper for complete
policy files.
Teach the checker to build a Draft7Validator with local file:// URIs so
that policy-v1.yml can resolve the new rule.yml file.
Signed-off-by: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
---
tools/kconfig-audit/audit.py | 56 ++++++++---
tools/kconfig-audit/schema/policy-v1.yml | 117 ++---------------------
tools/kconfig-audit/schema/rule.yml | 117 +++++++++++++++++++++++
3 files changed, 170 insertions(+), 120 deletions(-)
create mode 100644 tools/kconfig-audit/schema/rule.yml
diff --git a/tools/kconfig-audit/audit.py b/tools/kconfig-audit/audit.py
index eac25740beca..94ff427715d1 100755
--- a/tools/kconfig-audit/audit.py
+++ b/tools/kconfig-audit/audit.py
@@ -78,7 +78,7 @@ import textwrap
from dataclasses import dataclass, field
from enum import Enum, auto
from pathlib import Path
-from typing import Any, ClassVar, Final, Mapping, NoReturn, Optional
+from typing import Any, ClassVar, Final, Mapping, NoReturn, Optional, Protocol
#
# Initial sanity checks:
@@ -249,12 +249,7 @@ class Helpers:
def validate_yaml(yaml: YamlDict, schema: Schema, f_name: str) -> None:
"""Validate a parsed YAML mapping against passed JSON Schema"""
try:
- # For regex validation, manually enforce a format checker
- jsonschema.validate(
- yaml,
- schema,
- format_checker=jsonschema.FormatChecker(),
- )
+ schema.validator.validate(yaml)
except jsonschema.exceptions.ValidationError as e:
error(
f'{f_name} does not match its schema\n'
@@ -322,31 +317,68 @@ class VersionedYamlFile:
# Schema model:
#
+class YamlValidator(Protocol):
+ """Protocol for JSON Schema validators accepted by the Schema model"""
+ def validate(self, instance: YamlDict) -> None:
+ ...
+
class Schema(YamlDict):
"""Modeled policy schema, as a YAML dictionary mapping"""
+ validator: YamlValidator
+
+ def __init__(self, mapping: YamlDict, validator: YamlValidator):
+ super().__init__(mapping)
+ self.validator = validator
+
class SchemaFile:
"""json-schema.org policy schema parser (to Schema model)"""
def __init__(self, schema_f: Path):
self.schema_f = schema_f
- self.yaml = Helpers.load_yaml(schema_f)
+ self.schema_y = Helpers.load_yaml(schema_f)
def parse(self) -> Schema:
"""Parse and validate a policy schema file"""
- schema = Schema(self.yaml)
+ rule_schema_f = self.schema_f.parent / 'rule.yml'
+ rule_schema_y = Helpers.load_yaml(rule_schema_f)
+
+ self._check_schema(rule_schema_f, rule_schema_y)
+ self._check_schema(self.schema_f, self.schema_y)
+
+ validator = self._create_validator({
+ rule_schema_f.resolve().as_uri(): rule_schema_y,
+ })
- # Validate the schema against its own draft-07 grammar
+ return Schema(self.schema_y, validator)
+
+ @staticmethod
+ def _check_schema(schema_f: Path, schema: YamlDict) -> None:
+ """Validate passed schema against its own draft-07 metaschema"""
try:
jsonschema.Draft7Validator.check_schema(schema)
except jsonschema.exceptions.SchemaError as e:
error(
- f'Invalid schema {self.schema_f}:\nReason: {e.message}',
+ f'Invalid schema {schema_f}:\nReason: {e.message}',
os.EX_DATAERR,
)
- return schema
+ def _create_validator(self, store: dict[str, YamlDict]) -> YamlValidator:
+ """Create a schema validator for resolving sibling '$ref' entries"""
+
+ resolver = jsonschema.RefResolver(
+ base_uri=self.schema_f.resolve().as_uri(),
+ referrer=self.schema_y,
+ store=store,
+ )
+
+ return jsonschema.Draft7Validator(
+ self.schema_y,
+ resolver=resolver,
+ # For regex entries validation, manually enforce a format checker
+ format_checker=jsonschema.FormatChecker(),
+ )
#
# Policy model:
diff --git a/tools/kconfig-audit/schema/policy-v1.yml b/tools/kconfig-audit/schema/policy-v1.yml
index 10672226415b..2eacb5d2f485 100644
--- a/tools/kconfig-audit/schema/policy-v1.yml
+++ b/tools/kconfig-audit/schema/policy-v1.yml
@@ -17,8 +17,8 @@ properties:
const: Policy
#
- # Optional base URL for the documentation page used by rationale
- # links. See `docs_hint`.
+ # Optional base URL for the rules rationale documentation page.
+ # See `docs_hint` @ schema/rule.yml.
#
docs_url:
type: string
@@ -28,112 +28,13 @@ properties:
type: array
minItems: 1
items:
- type: object
- oneOf:
- - required: [name]
- - required: [name_pattern]
allOf:
- - if:
- required: [name]
- then:
- not:
- required: [pattern_prio]
- required: [expected, severity]
- additionalProperties: false
-
- properties:
- #
- # Exact kernel configuration symbol to check.
- #
- # Example: CONFIG_PREEMPT_RT
- #
- name:
- type: string
- pattern: '^CONFIG_[A-Z0-9_]+$'
-
- #
- # Regex matching one or more kernel configuration symbols.
- #
- # Example: CONFIG_DEBUG_.*
- #
- name_pattern:
- type: string
- format: regex
-
- #
- # Optional priority for a pattern rule.
- #
- # Higher numbers imply a higher priority. All pattern rules have a
- # default priority of zero, so specifying a zero here is of no use.
- #
- pattern_prio:
- type: integer
- minimum: 1
- maximum: 100
-
- #
- # Expected value for the matching kernel configuration option.
- #
- # Values:
- #
- # enabled option is expected to be enabled (built-in or module).
- # disabled option is expected to be either absent or unset.
- # neutral option can be enabled or disabled. This is used to exempt
- # one or more symbols from a `name_pattern` rule.
- #
- expected:
- type: string
- enum: [enabled, disabled, neutral]
-
- #
- # Issue severity to report when the configuration rule is not met.
- #
- # Higher severities should be reserved for rules affecting correctness.
- #
- severity:
- type: string
- enum: [fatal, high, medium, low, info]
-
- #
- # Optional documentation anchor for this rule.
- #
- # The rationale URL is built from `docs_url` plus an HTML fragment
- # identifier.
- #
- # If `docs_hint` is set to `config`, then the HTML fragment id is
- # derived from the kernel configuration symbol name:
- #
- # CONFIG_DRM -> #config-drm
- #
- # For example, with below docs_url:
- #
- # https://docs.kernel.org/core-api/real-time/kernel-configuration.html
- #
- # and the symbol entry:
- #
- # - name: CONFIG_DRM
- # expected: disabled
- # severity: info
- # docs_hint: config
- #
- # the rationale URL becomes:
- #
- # https://docs.kernel.org/core-api/real-time/kernel-configuration.html#config-drm
- #
- # If `docs_hint` is any other string, that string is used directly as
- # the HTML fragment identifier.
- #
- docs_hint:
- type: string
- anyOf:
- - const: config
- - pattern: '^\S+$' # URI fragments cannot have spaces
-
- #
- # Optional human-readable explanation for the rule.
+ - $ref: "rule.yml#/definitions/rule"
#
- # This text should explain why the option is expected to have the given
- # value, and may span multiple lines.
+ # The base schema leaves `expected` and `severity` optional so that
+ # policy overlays can contain partial rule entries without
+ # restating fields they do not override. In a complete policy
+ # file, like this one, these fields are mandatory for every rule.
#
- rationale:
- type: string
+ - type: object
+ required: [expected, severity]
diff --git a/tools/kconfig-audit/schema/rule.yml b/tools/kconfig-audit/schema/rule.yml
new file mode 100644
index 000000000000..a6b1bfdb435b
--- /dev/null
+++ b/tools/kconfig-audit/schema/rule.yml
@@ -0,0 +1,117 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+"$schema": "https://json-schema.org/draft-07/schema#"
+
+title: Schema for a single kconfig-audit policy rule
+type: object
+
+definitions:
+ rule:
+ type: object
+ oneOf:
+ - required: [name]
+ - required: [name_pattern]
+ allOf:
+ - if:
+ required: [name]
+ then:
+ not:
+ required: [pattern_prio]
+ additionalProperties: false
+
+ properties:
+ #
+ # Exact kernel configuration symbol to check.
+ #
+ # Example: CONFIG_PREEMPT_RT
+ #
+ name:
+ type: string
+ pattern: '^CONFIG_[A-Z0-9_]+$'
+
+ #
+ # Regex matching one or more kernel configuration symbols.
+ #
+ # Example: CONFIG_DEBUG_.*
+ #
+ name_pattern:
+ type: string
+ format: regex
+
+ #
+ # Optional priority for a pattern rule.
+ #
+ # Higher numbers imply a higher priority. All pattern rules have a
+ # default priority of zero, so specifying a zero here is of no use.
+ #
+ pattern_prio:
+ type: integer
+ minimum: 1
+ maximum: 100
+
+ #
+ # Expected value for the matching kernel configuration option.
+ #
+ # Values:
+ #
+ # enabled option is expected to be enabled (built-in or module).
+ # disabled option is expected to be either absent or unset.
+ # neutral option can be enabled or disabled. This is used to exempt
+ # one or more symbols from a `name_pattern` rule.
+ #
+ expected:
+ type: string
+ enum: [enabled, disabled, neutral]
+
+ #
+ # Issue severity to report when the configuration rule is not met.
+ #
+ # Higher severities should be reserved for rules affecting correctness.
+ #
+ severity:
+ type: string
+ enum: [fatal, high, medium, low, info]
+
+ #
+ # Optional documentation anchor for this rule.
+ #
+ # The rationale URL is built from `docs_url` plus an HTML fragment
+ # identifier.
+ #
+ # If `docs_hint` is set to `config`, then the HTML fragment id is
+ # derived from the kernel configuration symbol name:
+ #
+ # CONFIG_DRM -> #config-drm
+ #
+ # For example, with below docs_url:
+ #
+ # https://docs.kernel.org/core-api/real-time/kernel-configuration.html
+ #
+ # and the symbol entry:
+ #
+ # - name: CONFIG_DRM
+ # expected: disabled
+ # severity: info
+ # docs_hint: config
+ #
+ # the rationale URL becomes:
+ #
+ # https://docs.kernel.org/core-api/real-time/kernel-configuration.html#config-drm
+ #
+ # If `docs_hint` is any other string, that string is used directly as
+ # the HTML fragment identifier.
+ #
+ docs_hint:
+ type: string
+ anyOf:
+ - const: config
+ - pattern: '^\S+$' # URI fragments cannot have spaces
+
+ #
+ # Optional human-readable explanation for the rule.
+ #
+ # This text should explain why the option is expected to have the given
+ # value, and may span multiple lines.
+ #
+ rationale:
+ type: string
--
2.56.0