Skip to content

Pluggable offline resource linting

NF provides a provider-independent rule runner in nf.core.linting. Policy packs are separate distributions: install netherframe.lint.hashicorp.aws for the hashicorp.aws pack (nf.lint.hashicorp.aws), targeting the hashicorp/aws provider. Core has no AWS lint dependency. Linting is explicit: installing a pack does not silently change constructor, plan or apply behavior.

Run all matching checks

nf lint --app my_project:app --all-failures
nf lint --app my_project:app --pack hashicorp.aws --json
nf lint --app my_project:app --first-failure

--app accepts an App or zero-argument factory in trusted executable Python. Every matching rule is evaluated for every registered resource/data source. All failures display by default. --first-failure limits text output only; the summary and JSON always retain complete counts/results. No provider is configured, no state is read, and no infrastructure operation is performed. Constructing user application code can itself perform side effects, so lint only trusted applications. Runtime gates and arbitrary Python objects are not lint targets.

The Python API can check a local app using sample configuration values.

from nf.core import App, Provider, Resource, Stack
from nf.lint.hashicorp.aws import rule_pack

with App() as app, Stack(nf_id="monitoring"):
    Provider(nf_id="aws", nf_type="aws", region="us-east-1")
    Resource(
        nf_id="logs",
        nf_type="aws_cloudwatch_log_group",
        name="/app/logs",
        retention_in_days=365,
        kms_key_id="arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012",
    )

report = app.lint(packs=(rule_pack(),))
report.assert_valid()  # raises with every failing lint
report.assert_valid(require_resolved=True)  # also reject unknown checked values

app.lint() discovers all installed packs. Supplying rules= or packs= selects exactly those checks without discovering additional plugins. The CLI offers repeatable --pack selection. Plugin discovery/factory errors fail visibly; no installed rules or zero evaluated checks cannot establish validity. Exit code 1 indicates findings/invalid reports (also deferred values under --require-resolved); Click argument errors use 2.

Resource batching and performance

from nf.core.linting import Linter, ResourceConfig

linter = Linter.from_plugins(["hashicorp.aws"])  # load and index once; reuse across runs
resource = ResourceConfig(
    "logs",
    "aws_cloudwatch_log_group",
    {
        "name": "/app/logs",
        "retention_in_days": 365,
    },
)
report = linter.lint_resource(resource)  # accepts an NF construct or ResourceConfig
# All matching lints run, including after a failure.
# For graph rules, pass ResourceConfig context via related_resources=(...).
# For a complete batch, use linter.lint_resources(snapshots) or lint_app(app).

The runner indexes rule selectors by provider resource type once, rather than scanning the whole catalogue for every object. It compiles each app resource's provider arguments and reference links into one snapshot. Graph queries index resource types and reuse direct/reverse relationship lookups within each run. Graph-only context passed to lint_resource is not itself linted. Each batch builds fresh graph caches, so a reused Linter does not keep stale app relationships. No threads or provider processes are needed for static checks. Prefer a complete batch over repeatedly constructing linters for individual resources.

Author a pack

A Rule checks values at a provider-argument path (* traverses collection members). A PolicyRule receives a resource and graph and returns a Decision. All rules declare stable IDs and matching resource types. Duplicate rule IDs, pack names and resource addresses are errors. Predicate implementation exceptions propagate; they are not converted into compliance findings.

from nf.core.linting import Rule, RulePack


def rule_pack():
    return RulePack("example", (Rule("example_retention", "example_log", ("retention_days",), lambda days: days >= 365, "Keep logs for a year", missing_status="failed"),))

Register the zero-argument factory in the pack's pyproject.toml:

[project.entry-points."nf.lint_packs"]
example = "my_policies:rule_pack"

A pack can optionally prepare the collected resource snapshots through RulePack.prepare_resources. Preparation must remain offline and preserve addresses, types and reference links. AWS uses this only to substitute reviewed, explicitly configured identity names; generated IDs, ARNs and data-source results remain unknown. Packs are trusted Python code, not sandboxed.

Results and limits

Checks return passed, failed, deferred or not_applicable. A Rule predicate returns True, False or None (unknown). Missing fields follow the rule's explicit missing_status; otherwise they are counted as absent rather than assumed valid. Report tracks uncovered resources and resources with schema-only coverage. Deferred references never automatically pass. Direct provider-argument references prove graph links; depends_on and transformed/literal external IDs do not prove that a related resource configures the target.

The AWS pack is MIT licensed and targets hashicorp/aws configuration. Its native Python catalogue contains 1,899 default policy/argument checks alongside 2,455 structural schemas. Nine further checks are disabled by default. Public checks use stable NF_AWS_* IDs. Resource scopes and default settings are pinned by the NF coverage contract; implementations follow AWS service and provider requirements.

aws_rules(sources=("native",)) selects policies and argument checks; sources=("schema",) selects structural checks. aws_inventory()["rules"] reports one NF requirement per row, including scopes and default settings. Scanner aliases and import inventories have been removed. For explicit naming policies, use bucket_prefix_rule(prefix); tag requirements still come from the operator's lint_tag_policy configuration.

All runtime requirements and interface facts are native Python. The package contains no policy JSON, bundled scanner material or source-reference snapshots. Structural checks use the AWS 6.73.0 public configuration interface: types, required/computed flags, nested blocks and cardinality. Linting does not execute or configure providers or query AWS. EKS and Lambda calendars retain freshness checks and defer unknown future versions. See the package README for maintenance and interface-refresh instructions.

Schemas do not replace provider semantic validation or live security testing. Check messages should name fields and rules, not configured values that may hold secrets. The default JSON report contains diagnostics and coverage, not raw inputs.