nac-validate 1.2.0__tar.gz → 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nac_validate-1.2.0 → nac_validate-2.0.0}/.github/workflows/release.yml +1 -1
- {nac_validate-1.2.0 → nac_validate-2.0.0}/.github/workflows/test.yml +5 -4
- {nac_validate-1.2.0 → nac_validate-2.0.0}/.pre-commit-config.yaml +8 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/CHANGELOG.md +11 -0
- nac_validate-2.0.0/PKG-INFO +304 -0
- nac_validate-2.0.0/README.md +263 -0
- nac_validate-2.0.0/nac_validate/__init__.py +17 -0
- nac_validate-2.0.0/nac_validate/cli/main.py +207 -0
- nac_validate-2.0.0/nac_validate/cli/options.py +157 -0
- nac_validate-2.0.0/nac_validate/constants.py +97 -0
- nac_validate-2.0.0/nac_validate/exceptions.py +95 -0
- nac_validate-2.0.0/nac_validate/models.py +58 -0
- nac_validate-2.0.0/nac_validate/output_formatter.py +424 -0
- nac_validate-2.0.0/nac_validate/validator.py +488 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/pyproject.toml +14 -13
- nac_validate-2.0.0/scripts/license-headers.sh +247 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_merge/result.yaml +0 -1
- nac_validate-2.0.0/tests/integration/fixtures/rules/rule_102.py +34 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/test_integration.py +124 -4
- nac_validate-2.0.0/tests/unit/test_cli.py +147 -0
- nac_validate-2.0.0/tests/unit/test_output_formatter.py +524 -0
- nac_validate-2.0.0/tests/unit/test_validator.py +218 -0
- nac_validate-2.0.0/uv.lock +1195 -0
- nac_validate-1.2.0/PKG-INFO +0 -155
- nac_validate-1.2.0/README.md +0 -115
- nac_validate-1.2.0/nac_validate/__init__.py +0 -6
- nac_validate-1.2.0/nac_validate/cli/defaults.py +0 -7
- nac_validate-1.2.0/nac_validate/cli/main.py +0 -170
- nac_validate-1.2.0/nac_validate/exceptions.py +0 -48
- nac_validate-1.2.0/nac_validate/validator.py +0 -190
- nac_validate-1.2.0/scripts/license-headers.sh +0 -198
- nac_validate-1.2.0/uv.lock +0 -1090
- {nac_validate-1.2.0 → nac_validate-2.0.0}/.github/dependabot.yml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/.gitignore +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/.pre-commit-hooks.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/LICENSE +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/nac_validate/__main__.py +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/nac_validate/cli/__init__.py +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/__init__.py +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/__init__.py +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/additional_data/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/additional_data_schema/schema.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_empty/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_empty/empty.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_env/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_merge/file1.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_merge/file2.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_non_strict/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_semantic_error/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_syntax_error/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/data_vault/data.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/rules/rule_101.py +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/rules_schema/rule_101.py +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/integration/fixtures/schema/schema.yaml +0 -0
- {nac_validate-1.2.0 → nac_validate-2.0.0}/tests/unit/__init__.py +0 -0
|
@@ -13,7 +13,7 @@ jobs:
|
|
|
13
13
|
timeout-minutes: 5
|
|
14
14
|
steps:
|
|
15
15
|
- name: Checkout
|
|
16
|
-
uses: actions/checkout@
|
|
16
|
+
uses: actions/checkout@v6
|
|
17
17
|
|
|
18
18
|
- name: Install uv
|
|
19
19
|
uses: astral-sh/setup-uv@v7
|
|
@@ -27,7 +27,7 @@ jobs:
|
|
|
27
27
|
run: bandit -r nac_validate/ -ll -f json -o bandit-security-report.json
|
|
28
28
|
|
|
29
29
|
- name: Upload security report
|
|
30
|
-
uses: actions/upload-artifact@
|
|
30
|
+
uses: actions/upload-artifact@v7
|
|
31
31
|
if: always()
|
|
32
32
|
with:
|
|
33
33
|
name: bandit-security-report
|
|
@@ -41,7 +41,7 @@ jobs:
|
|
|
41
41
|
contents: write # Required for dependabot to push lock file updates
|
|
42
42
|
steps:
|
|
43
43
|
- name: Checkout
|
|
44
|
-
uses: actions/checkout@
|
|
44
|
+
uses: actions/checkout@v6
|
|
45
45
|
with:
|
|
46
46
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
47
47
|
# For fork PRs, use merge commit; for same-repo PRs, use head ref
|
|
@@ -85,9 +85,10 @@ jobs:
|
|
|
85
85
|
- "3.11"
|
|
86
86
|
- "3.12"
|
|
87
87
|
- "3.13"
|
|
88
|
+
- "3.14"
|
|
88
89
|
steps:
|
|
89
90
|
- name: Checkout
|
|
90
|
-
uses: actions/checkout@
|
|
91
|
+
uses: actions/checkout@v6
|
|
91
92
|
|
|
92
93
|
- name: Python Setup
|
|
93
94
|
uses: actions/setup-python@v6
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
# 2.0.0
|
|
2
|
+
|
|
3
|
+
- Add `RuleBase` base class for semantic validation rules
|
|
4
|
+
- Overhaul CLI output: validation results now show structured errors with file paths, line context, and rule identifiers, followed by a checklist-style summary per rule
|
|
5
|
+
- Add `--json` CLI option for machine-readable JSON validation output
|
|
6
|
+
- Add `--list-rules` CLI option to display available validation rules
|
|
7
|
+
- Add `--no-color` CLI option to disable colored terminal output
|
|
8
|
+
- Add `--compact` CLI option to reduce output verbosity
|
|
9
|
+
- BREAKING CHANGE (via `nac-yaml`): Rework list merge logic — within-file duplicates are now preserved; if any file contains duplicate dict items in a list, merging is disabled for that entire list and items are concatenated instead
|
|
10
|
+
- BREAKING CHANGE (via `nac-yaml`): Two dict items in a list now merge when all shared primitive fields match, even if both sides have additional unique primitive fields
|
|
11
|
+
|
|
1
12
|
# 1.2.0
|
|
2
13
|
|
|
3
14
|
- Add `jmespath` as a direct dependency
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nac-validate
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: A CLI tool to perform syntactic and semantic validation of YAML files.
|
|
5
|
+
Project-URL: homepage, https://github.com/netascode/nac-validate
|
|
6
|
+
Project-URL: repository, https://github.com/netascode/nac-validate
|
|
7
|
+
Project-URL: documentation, https://github.com/netascode/nac-validate
|
|
8
|
+
Author-email: Daniel Schmidt <danischm@cisco.com>
|
|
9
|
+
Maintainer-email: Daniel Schmidt <danischm@cisco.com>
|
|
10
|
+
License: MPL-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: automation,cli,testing,validation,yaml
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Software Development :: Testing
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: jmespath>=1.1.0
|
|
27
|
+
Requires-Dist: nac-yaml>=2.0.0
|
|
28
|
+
Requires-Dist: ruamel-yaml>=0.19.1
|
|
29
|
+
Requires-Dist: typer>=0.25.1
|
|
30
|
+
Requires-Dist: yamale>=6.1.0
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: ansible-core>=2.17.6; extra == 'dev'
|
|
33
|
+
Requires-Dist: bandit[toml]>=1.9.1; extra == 'dev'
|
|
34
|
+
Requires-Dist: mypy>=1.18.2; extra == 'dev'
|
|
35
|
+
Requires-Dist: pre-commit>=4.4.0; extra == 'dev'
|
|
36
|
+
Requires-Dist: pytest-cov>=7.0.0; extra == 'dev'
|
|
37
|
+
Requires-Dist: pytest-mock>=3.15.1; extra == 'dev'
|
|
38
|
+
Requires-Dist: pytest>=9.0.1; extra == 'dev'
|
|
39
|
+
Requires-Dist: ruff>=0.14.5; extra == 'dev'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
[](https://github.com/netascode/nac-validate/actions/workflows/test.yml)
|
|
43
|
+

|
|
44
|
+
|
|
45
|
+
# nac-validate
|
|
46
|
+
|
|
47
|
+
A CLI tool to perform syntactic and semantic validation of YAML files.
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
$ nac-validate --help
|
|
51
|
+
|
|
52
|
+
Usage: nac-validate [OPTIONS] [PATHS]...
|
|
53
|
+
|
|
54
|
+
A CLI tool to perform syntactic and semantic validation of YAML files.
|
|
55
|
+
|
|
56
|
+
Arguments:
|
|
57
|
+
[PATHS]... List of paths pointing to YAML files or directories
|
|
58
|
+
|
|
59
|
+
Options:
|
|
60
|
+
-v, --verbosity [DEBUG|INFO|WARNING|ERROR|CRITICAL]
|
|
61
|
+
Verbosity level [env: NAC_VALIDATE_VERBOSITY] [default: WARNING]
|
|
62
|
+
-s, --schema FILE Path to schema file [env: NAC_VALIDATE_SCHEMA] [default: .schema.yaml]
|
|
63
|
+
-r, --rules DIRECTORY Path(s) to directories with semantic validation rules (repeatable)
|
|
64
|
+
[env: NAC_VALIDATE_RULES]
|
|
65
|
+
-o, --output FILE Write merged content from YAML files to a new YAML file
|
|
66
|
+
[env: NAC_VALIDATE_OUTPUT]
|
|
67
|
+
--non-strict Accept unexpected elements in YAML files
|
|
68
|
+
[env: NAC_VALIDATE_NON_STRICT]
|
|
69
|
+
-f, --format [text|json]
|
|
70
|
+
Output format for validation results
|
|
71
|
+
[env: NAC_VALIDATE_FORMAT] [default: text]
|
|
72
|
+
--no-color Disable colored output [env: NO_COLOR]
|
|
73
|
+
--compact Use compact output format without rule context details
|
|
74
|
+
[env: NAC_VALIDATE_COMPACT]
|
|
75
|
+
--version Display version number
|
|
76
|
+
--list-rules List all available validation rules and exit
|
|
77
|
+
--help Show this message and exit
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Exit Codes
|
|
81
|
+
|
|
82
|
+
The CLI uses specific exit codes to help automation distinguish between error types:
|
|
83
|
+
|
|
84
|
+
| Exit Code | Meaning |
|
|
85
|
+
|-----------|---------|
|
|
86
|
+
| 0 | Validation passed |
|
|
87
|
+
| 1 | Semantic validation failed (business rule violations) |
|
|
88
|
+
| 2 | Syntax validation failed (YAML syntax or schema errors) |
|
|
89
|
+
| 3 | Configuration error (missing schema, invalid rules, etc.) |
|
|
90
|
+
|
|
91
|
+
## How It Works
|
|
92
|
+
|
|
93
|
+
Syntactic validation is done by basic YAML syntax validation (e.g., indentation) and by providing a [Yamale](https://github.com/23andMe/Yamale) schema and validating all YAML files against that schema. Semantic validation is done by providing a set of rules (implemented in Python) which are then validated against the YAML data. Every rule is implemented as a Python class and should be placed in a `.py` file located in the `--rules` path.
|
|
94
|
+
|
|
95
|
+
## Writing Validation Rules
|
|
96
|
+
|
|
97
|
+
Each `.py` file must have a single class named `Rule` that subclasses `RuleBase`. This class must set `id` and `description` as class attributes. It must implement a `classmethod()` named `match` that has a single function argument `data` which is the data read from all YAML files. It can optionally also have a second argument `schema` which would then provide the `Yamale` schema.
|
|
98
|
+
|
|
99
|
+
### Simple Rules (String List)
|
|
100
|
+
|
|
101
|
+
For simple validations, rules can return a list of strings describing each violation:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from nac_validate import RuleBase
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class Rule(RuleBase):
|
|
108
|
+
id = "101"
|
|
109
|
+
description = "Verify child naming restrictions"
|
|
110
|
+
severity = "HIGH"
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
def match(cls, data):
|
|
114
|
+
results = []
|
|
115
|
+
try:
|
|
116
|
+
for child in data["root"]["children"]:
|
|
117
|
+
if child["name"] == "FORBIDDEN":
|
|
118
|
+
results.append("root.children.name" + " - " + str(child["name"]))
|
|
119
|
+
except KeyError:
|
|
120
|
+
pass
|
|
121
|
+
return results
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Structured Rules (Recommended)
|
|
125
|
+
|
|
126
|
+
For richer output with context, explanations, and recommendations, subclass `RuleBase` and set the rich context attributes as class-level fields:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from nac_validate import RuleBase, Violation
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class Rule(RuleBase):
|
|
133
|
+
id = "301"
|
|
134
|
+
description = "Verify Infra VLAN Is Defined When Referenced by AAEPs"
|
|
135
|
+
severity = "HIGH"
|
|
136
|
+
|
|
137
|
+
# Rich context displayed in terminal output
|
|
138
|
+
title = "INFRA VLAN CONFIGURATION WARNING"
|
|
139
|
+
affected_items_label = "Affected AAEPs"
|
|
140
|
+
explanation = """\
|
|
141
|
+
The Infrastructure VLAN (Infra VLAN) is critical for APIC-to-leaf
|
|
142
|
+
communication. When infra_vlan is enabled on an AAEP, the global
|
|
143
|
+
infra_vlan value must be explicitly defined."""
|
|
144
|
+
recommendation = """\
|
|
145
|
+
Define the Infra VLAN in your access_policies configuration:
|
|
146
|
+
|
|
147
|
+
apic:
|
|
148
|
+
access_policies:
|
|
149
|
+
infra_vlan: 3967
|
|
150
|
+
aaeps:
|
|
151
|
+
- name: INFRA-AAEP
|
|
152
|
+
infra_vlan: true"""
|
|
153
|
+
references = [
|
|
154
|
+
"https://www.cisco.com/c/en/us/td/docs/dcn/aci/apic/all/apic-fabric-access-policies.html"
|
|
155
|
+
]
|
|
156
|
+
|
|
157
|
+
@classmethod
|
|
158
|
+
def match(cls, inventory):
|
|
159
|
+
violations = []
|
|
160
|
+
|
|
161
|
+
aaeps = inventory.get("apic", {}).get("access_policies", {}).get("aaeps", [])
|
|
162
|
+
if aaeps is None:
|
|
163
|
+
aaeps = []
|
|
164
|
+
|
|
165
|
+
# Find AAEPs with infra_vlan enabled
|
|
166
|
+
affected_aaeps = [
|
|
167
|
+
aaep.get("name", "unnamed")
|
|
168
|
+
for aaep in aaeps
|
|
169
|
+
if aaep.get("infra_vlan", False)
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
if affected_aaeps:
|
|
173
|
+
infra_vlan = (
|
|
174
|
+
inventory.get("apic", {})
|
|
175
|
+
.get("access_policies", {})
|
|
176
|
+
.get("infra_vlan", 0)
|
|
177
|
+
)
|
|
178
|
+
if infra_vlan == 0:
|
|
179
|
+
for aaep_name in affected_aaeps:
|
|
180
|
+
violations.append(
|
|
181
|
+
Violation(
|
|
182
|
+
message=f"AAEP '{aaep_name}' has infra_vlan enabled but global infra_vlan is not defined",
|
|
183
|
+
path=f"apic.access_policies.aaeps[name={aaep_name}].infra_vlan",
|
|
184
|
+
details={
|
|
185
|
+
"aaep_name": aaep_name,
|
|
186
|
+
"infra_vlan_enabled": True,
|
|
187
|
+
"global_infra_vlan_defined": False,
|
|
188
|
+
},
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
return violations
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### RuleBase Attributes
|
|
196
|
+
|
|
197
|
+
All rules must subclass `RuleBase` and set at minimum `id` and `description`. The following class attributes are available:
|
|
198
|
+
|
|
199
|
+
| Attribute | Required | Default | Description |
|
|
200
|
+
|-----------|----------|---------|-------------|
|
|
201
|
+
| `id` | Yes | — | Unique rule identifier |
|
|
202
|
+
| `description` | Yes | — | Short description of the rule |
|
|
203
|
+
| `severity` | No | `"HIGH"` | Severity level (`HIGH`, `MEDIUM`, `LOW`) |
|
|
204
|
+
| `title` | No | `""` | Header displayed in violation output |
|
|
205
|
+
| `explanation` | No | `""` | Detailed explanation of why this matters |
|
|
206
|
+
| `recommendation` | No | `""` | How to fix the issue, with examples |
|
|
207
|
+
| `affected_items_label` | No | `"Affected Items"` | Label for the violations list |
|
|
208
|
+
| `references` | No | `[]` | Links to documentation |
|
|
209
|
+
|
|
210
|
+
Rich terminal output sections (title header, explanation, recommendation, references) are rendered individually for whichever attributes are set. Setting any of `title`, `explanation`, or `recommendation` enables enhanced output; only populated sections are shown.
|
|
211
|
+
|
|
212
|
+
**`Violation`** - Represents a single validation failure:
|
|
213
|
+
|
|
214
|
+
- `message` (str): Human-readable description of the issue
|
|
215
|
+
- `path` (str): Location in the YAML structure (e.g., `apic.tenants[name=PROD].vrfs[0]`)
|
|
216
|
+
- `details` (dict, optional): Machine-readable metadata for automation
|
|
217
|
+
|
|
218
|
+
## JSON Output
|
|
219
|
+
|
|
220
|
+
Use `--format json` for machine-readable output suitable for CI/CD pipelines:
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
nac-validate data/ -s schema.yaml -r rules/ --format json
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Output structure:
|
|
227
|
+
|
|
228
|
+
```json
|
|
229
|
+
{
|
|
230
|
+
"syntax_errors": [],
|
|
231
|
+
"semantic_errors": [
|
|
232
|
+
{
|
|
233
|
+
"rule_id": "301",
|
|
234
|
+
"description": "Verify Infra VLAN Is Defined When Referenced by AAEPs",
|
|
235
|
+
"errors": [
|
|
236
|
+
"apic.access_policies.aaeps[name=INFRA-AAEP].infra_vlan - AAEP 'INFRA-AAEP' has infra_vlan enabled but global infra_vlan is not defined"
|
|
237
|
+
]
|
|
238
|
+
}
|
|
239
|
+
]
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Installation
|
|
244
|
+
|
|
245
|
+
Python 3.10+ is required to install `nac-validate`. Don't have Python 3.10 or later? See [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/).
|
|
246
|
+
|
|
247
|
+
`nac-validate` can be installed in a virtual environment using `uv` or `pip`:
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
# Using uv (recommended)
|
|
251
|
+
uv tools install nac-validate
|
|
252
|
+
|
|
253
|
+
# Using pip
|
|
254
|
+
pip install nac-validate
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Pre-Commit Hook
|
|
258
|
+
|
|
259
|
+
The tool can be integrated via a [pre-commit](https://pre-commit.com/) hook with the following config (`.pre-commit-config.yaml`), assuming the default values (`.schema.yaml`, `.rules/`) are appropriate:
|
|
260
|
+
|
|
261
|
+
```yaml
|
|
262
|
+
repos:
|
|
263
|
+
- repo: https://github.com/netascode/nac-validate
|
|
264
|
+
rev: v1.0.0
|
|
265
|
+
hooks:
|
|
266
|
+
- id: nac-validate
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
In case the schema or validation rules are located somewhere else the required CLI arguments can be added like this:
|
|
270
|
+
|
|
271
|
+
```yaml
|
|
272
|
+
repos:
|
|
273
|
+
- repo: https://github.com/netascode/nac-validate
|
|
274
|
+
rev: v1.0.0
|
|
275
|
+
hooks:
|
|
276
|
+
- id: nac-validate
|
|
277
|
+
args:
|
|
278
|
+
- '-s'
|
|
279
|
+
- 'my_schema.yaml'
|
|
280
|
+
- '-r'
|
|
281
|
+
- 'rules/'
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Ansible Vault Support
|
|
285
|
+
|
|
286
|
+
Values can be encrypted using [Ansible Vault](https://docs.ansible.com/ansible/latest/user_guide/vault.html). This requires Ansible (`ansible-vault` command) to be installed and the following two environment variables to be defined:
|
|
287
|
+
|
|
288
|
+
```
|
|
289
|
+
export ANSIBLE_VAULT_ID=dev
|
|
290
|
+
export ANSIBLE_VAULT_PASSWORD=Password123
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
`ANSIBLE_VAULT_ID` is optional, and if not defined will be omitted.
|
|
294
|
+
|
|
295
|
+
## Additional Tags
|
|
296
|
+
|
|
297
|
+
### Reading Environment Variables
|
|
298
|
+
|
|
299
|
+
The `!env` YAML tag can be used to read values from environment variables.
|
|
300
|
+
|
|
301
|
+
```yaml
|
|
302
|
+
root:
|
|
303
|
+
name: !env VAR_NAME
|
|
304
|
+
```
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
[](https://github.com/netascode/nac-validate/actions/workflows/test.yml)
|
|
2
|
+

|
|
3
|
+
|
|
4
|
+
# nac-validate
|
|
5
|
+
|
|
6
|
+
A CLI tool to perform syntactic and semantic validation of YAML files.
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
$ nac-validate --help
|
|
10
|
+
|
|
11
|
+
Usage: nac-validate [OPTIONS] [PATHS]...
|
|
12
|
+
|
|
13
|
+
A CLI tool to perform syntactic and semantic validation of YAML files.
|
|
14
|
+
|
|
15
|
+
Arguments:
|
|
16
|
+
[PATHS]... List of paths pointing to YAML files or directories
|
|
17
|
+
|
|
18
|
+
Options:
|
|
19
|
+
-v, --verbosity [DEBUG|INFO|WARNING|ERROR|CRITICAL]
|
|
20
|
+
Verbosity level [env: NAC_VALIDATE_VERBOSITY] [default: WARNING]
|
|
21
|
+
-s, --schema FILE Path to schema file [env: NAC_VALIDATE_SCHEMA] [default: .schema.yaml]
|
|
22
|
+
-r, --rules DIRECTORY Path(s) to directories with semantic validation rules (repeatable)
|
|
23
|
+
[env: NAC_VALIDATE_RULES]
|
|
24
|
+
-o, --output FILE Write merged content from YAML files to a new YAML file
|
|
25
|
+
[env: NAC_VALIDATE_OUTPUT]
|
|
26
|
+
--non-strict Accept unexpected elements in YAML files
|
|
27
|
+
[env: NAC_VALIDATE_NON_STRICT]
|
|
28
|
+
-f, --format [text|json]
|
|
29
|
+
Output format for validation results
|
|
30
|
+
[env: NAC_VALIDATE_FORMAT] [default: text]
|
|
31
|
+
--no-color Disable colored output [env: NO_COLOR]
|
|
32
|
+
--compact Use compact output format without rule context details
|
|
33
|
+
[env: NAC_VALIDATE_COMPACT]
|
|
34
|
+
--version Display version number
|
|
35
|
+
--list-rules List all available validation rules and exit
|
|
36
|
+
--help Show this message and exit
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Exit Codes
|
|
40
|
+
|
|
41
|
+
The CLI uses specific exit codes to help automation distinguish between error types:
|
|
42
|
+
|
|
43
|
+
| Exit Code | Meaning |
|
|
44
|
+
|-----------|---------|
|
|
45
|
+
| 0 | Validation passed |
|
|
46
|
+
| 1 | Semantic validation failed (business rule violations) |
|
|
47
|
+
| 2 | Syntax validation failed (YAML syntax or schema errors) |
|
|
48
|
+
| 3 | Configuration error (missing schema, invalid rules, etc.) |
|
|
49
|
+
|
|
50
|
+
## How It Works
|
|
51
|
+
|
|
52
|
+
Syntactic validation is done by basic YAML syntax validation (e.g., indentation) and by providing a [Yamale](https://github.com/23andMe/Yamale) schema and validating all YAML files against that schema. Semantic validation is done by providing a set of rules (implemented in Python) which are then validated against the YAML data. Every rule is implemented as a Python class and should be placed in a `.py` file located in the `--rules` path.
|
|
53
|
+
|
|
54
|
+
## Writing Validation Rules
|
|
55
|
+
|
|
56
|
+
Each `.py` file must have a single class named `Rule` that subclasses `RuleBase`. This class must set `id` and `description` as class attributes. It must implement a `classmethod()` named `match` that has a single function argument `data` which is the data read from all YAML files. It can optionally also have a second argument `schema` which would then provide the `Yamale` schema.
|
|
57
|
+
|
|
58
|
+
### Simple Rules (String List)
|
|
59
|
+
|
|
60
|
+
For simple validations, rules can return a list of strings describing each violation:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from nac_validate import RuleBase
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Rule(RuleBase):
|
|
67
|
+
id = "101"
|
|
68
|
+
description = "Verify child naming restrictions"
|
|
69
|
+
severity = "HIGH"
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def match(cls, data):
|
|
73
|
+
results = []
|
|
74
|
+
try:
|
|
75
|
+
for child in data["root"]["children"]:
|
|
76
|
+
if child["name"] == "FORBIDDEN":
|
|
77
|
+
results.append("root.children.name" + " - " + str(child["name"]))
|
|
78
|
+
except KeyError:
|
|
79
|
+
pass
|
|
80
|
+
return results
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Structured Rules (Recommended)
|
|
84
|
+
|
|
85
|
+
For richer output with context, explanations, and recommendations, subclass `RuleBase` and set the rich context attributes as class-level fields:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from nac_validate import RuleBase, Violation
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class Rule(RuleBase):
|
|
92
|
+
id = "301"
|
|
93
|
+
description = "Verify Infra VLAN Is Defined When Referenced by AAEPs"
|
|
94
|
+
severity = "HIGH"
|
|
95
|
+
|
|
96
|
+
# Rich context displayed in terminal output
|
|
97
|
+
title = "INFRA VLAN CONFIGURATION WARNING"
|
|
98
|
+
affected_items_label = "Affected AAEPs"
|
|
99
|
+
explanation = """\
|
|
100
|
+
The Infrastructure VLAN (Infra VLAN) is critical for APIC-to-leaf
|
|
101
|
+
communication. When infra_vlan is enabled on an AAEP, the global
|
|
102
|
+
infra_vlan value must be explicitly defined."""
|
|
103
|
+
recommendation = """\
|
|
104
|
+
Define the Infra VLAN in your access_policies configuration:
|
|
105
|
+
|
|
106
|
+
apic:
|
|
107
|
+
access_policies:
|
|
108
|
+
infra_vlan: 3967
|
|
109
|
+
aaeps:
|
|
110
|
+
- name: INFRA-AAEP
|
|
111
|
+
infra_vlan: true"""
|
|
112
|
+
references = [
|
|
113
|
+
"https://www.cisco.com/c/en/us/td/docs/dcn/aci/apic/all/apic-fabric-access-policies.html"
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
@classmethod
|
|
117
|
+
def match(cls, inventory):
|
|
118
|
+
violations = []
|
|
119
|
+
|
|
120
|
+
aaeps = inventory.get("apic", {}).get("access_policies", {}).get("aaeps", [])
|
|
121
|
+
if aaeps is None:
|
|
122
|
+
aaeps = []
|
|
123
|
+
|
|
124
|
+
# Find AAEPs with infra_vlan enabled
|
|
125
|
+
affected_aaeps = [
|
|
126
|
+
aaep.get("name", "unnamed")
|
|
127
|
+
for aaep in aaeps
|
|
128
|
+
if aaep.get("infra_vlan", False)
|
|
129
|
+
]
|
|
130
|
+
|
|
131
|
+
if affected_aaeps:
|
|
132
|
+
infra_vlan = (
|
|
133
|
+
inventory.get("apic", {})
|
|
134
|
+
.get("access_policies", {})
|
|
135
|
+
.get("infra_vlan", 0)
|
|
136
|
+
)
|
|
137
|
+
if infra_vlan == 0:
|
|
138
|
+
for aaep_name in affected_aaeps:
|
|
139
|
+
violations.append(
|
|
140
|
+
Violation(
|
|
141
|
+
message=f"AAEP '{aaep_name}' has infra_vlan enabled but global infra_vlan is not defined",
|
|
142
|
+
path=f"apic.access_policies.aaeps[name={aaep_name}].infra_vlan",
|
|
143
|
+
details={
|
|
144
|
+
"aaep_name": aaep_name,
|
|
145
|
+
"infra_vlan_enabled": True,
|
|
146
|
+
"global_infra_vlan_defined": False,
|
|
147
|
+
},
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
return violations
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### RuleBase Attributes
|
|
155
|
+
|
|
156
|
+
All rules must subclass `RuleBase` and set at minimum `id` and `description`. The following class attributes are available:
|
|
157
|
+
|
|
158
|
+
| Attribute | Required | Default | Description |
|
|
159
|
+
|-----------|----------|---------|-------------|
|
|
160
|
+
| `id` | Yes | — | Unique rule identifier |
|
|
161
|
+
| `description` | Yes | — | Short description of the rule |
|
|
162
|
+
| `severity` | No | `"HIGH"` | Severity level (`HIGH`, `MEDIUM`, `LOW`) |
|
|
163
|
+
| `title` | No | `""` | Header displayed in violation output |
|
|
164
|
+
| `explanation` | No | `""` | Detailed explanation of why this matters |
|
|
165
|
+
| `recommendation` | No | `""` | How to fix the issue, with examples |
|
|
166
|
+
| `affected_items_label` | No | `"Affected Items"` | Label for the violations list |
|
|
167
|
+
| `references` | No | `[]` | Links to documentation |
|
|
168
|
+
|
|
169
|
+
Rich terminal output sections (title header, explanation, recommendation, references) are rendered individually for whichever attributes are set. Setting any of `title`, `explanation`, or `recommendation` enables enhanced output; only populated sections are shown.
|
|
170
|
+
|
|
171
|
+
**`Violation`** - Represents a single validation failure:
|
|
172
|
+
|
|
173
|
+
- `message` (str): Human-readable description of the issue
|
|
174
|
+
- `path` (str): Location in the YAML structure (e.g., `apic.tenants[name=PROD].vrfs[0]`)
|
|
175
|
+
- `details` (dict, optional): Machine-readable metadata for automation
|
|
176
|
+
|
|
177
|
+
## JSON Output
|
|
178
|
+
|
|
179
|
+
Use `--format json` for machine-readable output suitable for CI/CD pipelines:
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
nac-validate data/ -s schema.yaml -r rules/ --format json
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Output structure:
|
|
186
|
+
|
|
187
|
+
```json
|
|
188
|
+
{
|
|
189
|
+
"syntax_errors": [],
|
|
190
|
+
"semantic_errors": [
|
|
191
|
+
{
|
|
192
|
+
"rule_id": "301",
|
|
193
|
+
"description": "Verify Infra VLAN Is Defined When Referenced by AAEPs",
|
|
194
|
+
"errors": [
|
|
195
|
+
"apic.access_policies.aaeps[name=INFRA-AAEP].infra_vlan - AAEP 'INFRA-AAEP' has infra_vlan enabled but global infra_vlan is not defined"
|
|
196
|
+
]
|
|
197
|
+
}
|
|
198
|
+
]
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Installation
|
|
203
|
+
|
|
204
|
+
Python 3.10+ is required to install `nac-validate`. Don't have Python 3.10 or later? See [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/).
|
|
205
|
+
|
|
206
|
+
`nac-validate` can be installed in a virtual environment using `uv` or `pip`:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
# Using uv (recommended)
|
|
210
|
+
uv tools install nac-validate
|
|
211
|
+
|
|
212
|
+
# Using pip
|
|
213
|
+
pip install nac-validate
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Pre-Commit Hook
|
|
217
|
+
|
|
218
|
+
The tool can be integrated via a [pre-commit](https://pre-commit.com/) hook with the following config (`.pre-commit-config.yaml`), assuming the default values (`.schema.yaml`, `.rules/`) are appropriate:
|
|
219
|
+
|
|
220
|
+
```yaml
|
|
221
|
+
repos:
|
|
222
|
+
- repo: https://github.com/netascode/nac-validate
|
|
223
|
+
rev: v1.0.0
|
|
224
|
+
hooks:
|
|
225
|
+
- id: nac-validate
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
In case the schema or validation rules are located somewhere else the required CLI arguments can be added like this:
|
|
229
|
+
|
|
230
|
+
```yaml
|
|
231
|
+
repos:
|
|
232
|
+
- repo: https://github.com/netascode/nac-validate
|
|
233
|
+
rev: v1.0.0
|
|
234
|
+
hooks:
|
|
235
|
+
- id: nac-validate
|
|
236
|
+
args:
|
|
237
|
+
- '-s'
|
|
238
|
+
- 'my_schema.yaml'
|
|
239
|
+
- '-r'
|
|
240
|
+
- 'rules/'
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Ansible Vault Support
|
|
244
|
+
|
|
245
|
+
Values can be encrypted using [Ansible Vault](https://docs.ansible.com/ansible/latest/user_guide/vault.html). This requires Ansible (`ansible-vault` command) to be installed and the following two environment variables to be defined:
|
|
246
|
+
|
|
247
|
+
```
|
|
248
|
+
export ANSIBLE_VAULT_ID=dev
|
|
249
|
+
export ANSIBLE_VAULT_PASSWORD=Password123
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
`ANSIBLE_VAULT_ID` is optional, and if not defined will be omitted.
|
|
253
|
+
|
|
254
|
+
## Additional Tags
|
|
255
|
+
|
|
256
|
+
### Reading Environment Variables
|
|
257
|
+
|
|
258
|
+
The `!env` YAML tag can be used to read values from environment variables.
|
|
259
|
+
|
|
260
|
+
```yaml
|
|
261
|
+
root:
|
|
262
|
+
name: !env VAR_NAME
|
|
263
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (c) 2025 Daniel Schmidt
|
|
3
|
+
|
|
4
|
+
from importlib.metadata import version
|
|
5
|
+
|
|
6
|
+
from .models import RuleBase, Violation
|
|
7
|
+
from .validator import Validator
|
|
8
|
+
|
|
9
|
+
__version__ = version(__name__)
|
|
10
|
+
|
|
11
|
+
# Public API for rule authors
|
|
12
|
+
__all__ = [
|
|
13
|
+
"Violation",
|
|
14
|
+
"RuleBase",
|
|
15
|
+
"Validator",
|
|
16
|
+
"__version__",
|
|
17
|
+
]
|