nac-validate 1.2.0__tar.gz → 2.0.0b2__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.
Files changed (54) hide show
  1. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/.github/workflows/release.yml +1 -1
  2. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/.github/workflows/test.yml +4 -4
  3. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/.pre-commit-config.yaml +8 -0
  4. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/CHANGELOG.md +11 -0
  5. nac_validate-2.0.0b2/PKG-INFO +303 -0
  6. nac_validate-2.0.0b2/README.md +263 -0
  7. nac_validate-2.0.0b2/nac_validate/__init__.py +17 -0
  8. nac_validate-2.0.0b2/nac_validate/cli/main.py +200 -0
  9. nac_validate-2.0.0b2/nac_validate/cli/options.py +157 -0
  10. nac_validate-2.0.0b2/nac_validate/constants.py +97 -0
  11. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/nac_validate/exceptions.py +39 -2
  12. nac_validate-2.0.0b2/nac_validate/models.py +58 -0
  13. nac_validate-2.0.0b2/nac_validate/output_formatter.py +409 -0
  14. nac_validate-2.0.0b2/nac_validate/validator.py +475 -0
  15. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/pyproject.toml +13 -13
  16. nac_validate-2.0.0b2/scripts/license-headers.sh +247 -0
  17. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_merge/result.yaml +0 -1
  18. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/test_integration.py +87 -4
  19. nac_validate-2.0.0b2/tests/unit/test_cli.py +147 -0
  20. nac_validate-2.0.0b2/tests/unit/test_output_formatter.py +421 -0
  21. nac_validate-2.0.0b2/tests/unit/test_validator.py +218 -0
  22. nac_validate-2.0.0b2/uv.lock +1207 -0
  23. nac_validate-1.2.0/PKG-INFO +0 -155
  24. nac_validate-1.2.0/README.md +0 -115
  25. nac_validate-1.2.0/nac_validate/__init__.py +0 -6
  26. nac_validate-1.2.0/nac_validate/cli/defaults.py +0 -7
  27. nac_validate-1.2.0/nac_validate/cli/main.py +0 -170
  28. nac_validate-1.2.0/nac_validate/validator.py +0 -190
  29. nac_validate-1.2.0/scripts/license-headers.sh +0 -198
  30. nac_validate-1.2.0/uv.lock +0 -1090
  31. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/.github/dependabot.yml +0 -0
  32. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/.gitignore +0 -0
  33. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/.pre-commit-hooks.yaml +0 -0
  34. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/LICENSE +0 -0
  35. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/nac_validate/__main__.py +0 -0
  36. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/nac_validate/cli/__init__.py +0 -0
  37. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/__init__.py +0 -0
  38. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/__init__.py +0 -0
  39. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/additional_data/data.yaml +0 -0
  40. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/additional_data_schema/schema.yaml +0 -0
  41. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data/data.yaml +0 -0
  42. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_empty/data.yaml +0 -0
  43. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_empty/empty.yaml +0 -0
  44. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_env/data.yaml +0 -0
  45. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_merge/file1.yaml +0 -0
  46. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_merge/file2.yaml +0 -0
  47. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_non_strict/data.yaml +0 -0
  48. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_semantic_error/data.yaml +0 -0
  49. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_syntax_error/data.yaml +0 -0
  50. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/data_vault/data.yaml +0 -0
  51. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/rules/rule_101.py +0 -0
  52. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/rules_schema/rule_101.py +0 -0
  53. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/integration/fixtures/schema/schema.yaml +0 -0
  54. {nac_validate-1.2.0 → nac_validate-2.0.0b2}/tests/unit/__init__.py +0 -0
@@ -9,7 +9,7 @@ jobs:
9
9
  build:
10
10
  runs-on: ubuntu-latest
11
11
  steps:
12
- - uses: actions/checkout@v5
12
+ - uses: actions/checkout@v6
13
13
  - name: Install uv
14
14
  uses: astral-sh/setup-uv@v7
15
15
 
@@ -13,7 +13,7 @@ jobs:
13
13
  timeout-minutes: 5
14
14
  steps:
15
15
  - name: Checkout
16
- uses: actions/checkout@v5
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@v5
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@v5
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
@@ -87,7 +87,7 @@ jobs:
87
87
  - "3.13"
88
88
  steps:
89
89
  - name: Checkout
90
- uses: actions/checkout@v5
90
+ uses: actions/checkout@v6
91
91
 
92
92
  - name: Python Setup
93
93
  uses: actions/setup-python@v6
@@ -1,4 +1,12 @@
1
1
  repos:
2
+ - repo: local
3
+ hooks:
4
+ - id: license-headers
5
+ name: Check license headers
6
+ entry: ./scripts/license-headers.sh
7
+ language: script
8
+ types: [python]
9
+
2
10
  - repo: https://github.com/astral-sh/ruff-pre-commit
3
11
  rev: v0.12.12
4
12
  hooks:
@@ -1,3 +1,14 @@
1
+ # Unreleased
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,303 @@
1
+ Metadata-Version: 2.4
2
+ Name: nac-validate
3
+ Version: 2.0.0b2
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: Topic :: Software Development :: Testing
24
+ Requires-Python: >=3.10
25
+ Requires-Dist: jmespath>=1.1.0
26
+ Requires-Dist: nac-yaml>=2.0.0b3
27
+ Requires-Dist: ruamel-yaml>=0.19.1
28
+ Requires-Dist: typer>=0.25.1
29
+ Requires-Dist: yamale>=6.1.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: ansible-core>=2.17.6; extra == 'dev'
32
+ Requires-Dist: bandit[toml]>=1.9.1; extra == 'dev'
33
+ Requires-Dist: mypy>=1.18.2; extra == 'dev'
34
+ Requires-Dist: pre-commit>=4.4.0; extra == 'dev'
35
+ Requires-Dist: pytest-cov>=7.0.0; extra == 'dev'
36
+ Requires-Dist: pytest-mock>=3.15.1; extra == 'dev'
37
+ Requires-Dist: pytest>=9.0.1; extra == 'dev'
38
+ Requires-Dist: ruff>=0.14.5; extra == 'dev'
39
+ Description-Content-Type: text/markdown
40
+
41
+ [![Tests](https://github.com/netascode/nac-validate/actions/workflows/test.yml/badge.svg)](https://github.com/netascode/nac-validate/actions/workflows/test.yml)
42
+ ![Python Support](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-informational "Python Support: 3.10, 3.11, 3.12, 3.13")
43
+
44
+ # nac-validate
45
+
46
+ A CLI tool to perform syntactic and semantic validation of YAML files.
47
+
48
+ ```
49
+ $ nac-validate --help
50
+
51
+ Usage: nac-validate [OPTIONS] [PATHS]...
52
+
53
+ A CLI tool to perform syntactic and semantic validation of YAML files.
54
+
55
+ Arguments:
56
+ [PATHS]... List of paths pointing to YAML files or directories
57
+
58
+ Options:
59
+ -v, --verbosity [DEBUG|INFO|WARNING|ERROR|CRITICAL]
60
+ Verbosity level [env: NAC_VALIDATE_VERBOSITY] [default: WARNING]
61
+ -s, --schema FILE Path to schema file [env: NAC_VALIDATE_SCHEMA] [default: .schema.yaml]
62
+ -r, --rules DIRECTORY Path(s) to directories with semantic validation rules (repeatable)
63
+ [env: NAC_VALIDATE_RULES]
64
+ -o, --output FILE Write merged content from YAML files to a new YAML file
65
+ [env: NAC_VALIDATE_OUTPUT]
66
+ --non-strict Accept unexpected elements in YAML files
67
+ [env: NAC_VALIDATE_NON_STRICT]
68
+ -f, --format [text|json]
69
+ Output format for validation results
70
+ [env: NAC_VALIDATE_FORMAT] [default: text]
71
+ --no-color Disable colored output [env: NO_COLOR]
72
+ --compact Use compact output format without rule context details
73
+ [env: NAC_VALIDATE_COMPACT]
74
+ --version Display version number
75
+ --list-rules List all available validation rules and exit
76
+ --help Show this message and exit
77
+ ```
78
+
79
+ ## Exit Codes
80
+
81
+ The CLI uses specific exit codes to help automation distinguish between error types:
82
+
83
+ | Exit Code | Meaning |
84
+ |-----------|---------|
85
+ | 0 | Validation passed |
86
+ | 1 | Semantic validation failed (business rule violations) |
87
+ | 2 | Syntax validation failed (YAML syntax or schema errors) |
88
+ | 3 | Configuration error (missing schema, invalid rules, etc.) |
89
+
90
+ ## How It Works
91
+
92
+ 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.
93
+
94
+ ## Writing Validation Rules
95
+
96
+ 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.
97
+
98
+ ### Simple Rules (String List)
99
+
100
+ For simple validations, rules can return a list of strings describing each violation:
101
+
102
+ ```python
103
+ from nac_validate import RuleBase
104
+
105
+
106
+ class Rule(RuleBase):
107
+ id = "101"
108
+ description = "Verify child naming restrictions"
109
+ severity = "HIGH"
110
+
111
+ @classmethod
112
+ def match(cls, data):
113
+ results = []
114
+ try:
115
+ for child in data["root"]["children"]:
116
+ if child["name"] == "FORBIDDEN":
117
+ results.append("root.children.name" + " - " + str(child["name"]))
118
+ except KeyError:
119
+ pass
120
+ return results
121
+ ```
122
+
123
+ ### Structured Rules (Recommended)
124
+
125
+ For richer output with context, explanations, and recommendations, subclass `RuleBase` and set the rich context attributes as class-level fields:
126
+
127
+ ```python
128
+ from nac_validate import RuleBase, Violation
129
+
130
+
131
+ class Rule(RuleBase):
132
+ id = "301"
133
+ description = "Verify Infra VLAN Is Defined When Referenced by AAEPs"
134
+ severity = "HIGH"
135
+
136
+ # Rich context displayed in terminal output
137
+ title = "INFRA VLAN CONFIGURATION WARNING"
138
+ affected_items_label = "Affected AAEPs"
139
+ explanation = """\
140
+ The Infrastructure VLAN (Infra VLAN) is critical for APIC-to-leaf
141
+ communication. When infra_vlan is enabled on an AAEP, the global
142
+ infra_vlan value must be explicitly defined."""
143
+ recommendation = """\
144
+ Define the Infra VLAN in your access_policies configuration:
145
+
146
+ apic:
147
+ access_policies:
148
+ infra_vlan: 3967
149
+ aaeps:
150
+ - name: INFRA-AAEP
151
+ infra_vlan: true"""
152
+ references = [
153
+ "https://www.cisco.com/c/en/us/td/docs/dcn/aci/apic/all/apic-fabric-access-policies.html"
154
+ ]
155
+
156
+ @classmethod
157
+ def match(cls, inventory):
158
+ violations = []
159
+
160
+ aaeps = inventory.get("apic", {}).get("access_policies", {}).get("aaeps", [])
161
+ if aaeps is None:
162
+ aaeps = []
163
+
164
+ # Find AAEPs with infra_vlan enabled
165
+ affected_aaeps = [
166
+ aaep.get("name", "unnamed")
167
+ for aaep in aaeps
168
+ if aaep.get("infra_vlan", False)
169
+ ]
170
+
171
+ if affected_aaeps:
172
+ infra_vlan = (
173
+ inventory.get("apic", {})
174
+ .get("access_policies", {})
175
+ .get("infra_vlan", 0)
176
+ )
177
+ if infra_vlan == 0:
178
+ for aaep_name in affected_aaeps:
179
+ violations.append(
180
+ Violation(
181
+ message=f"AAEP '{aaep_name}' has infra_vlan enabled but global infra_vlan is not defined",
182
+ path=f"apic.access_policies.aaeps[name={aaep_name}].infra_vlan",
183
+ details={
184
+ "aaep_name": aaep_name,
185
+ "infra_vlan_enabled": True,
186
+ "global_infra_vlan_defined": False,
187
+ },
188
+ )
189
+ )
190
+
191
+ return violations
192
+ ```
193
+
194
+ ### RuleBase Attributes
195
+
196
+ All rules must subclass `RuleBase` and set at minimum `id` and `description`. The following class attributes are available:
197
+
198
+ | Attribute | Required | Default | Description |
199
+ |-----------|----------|---------|-------------|
200
+ | `id` | Yes | — | Unique rule identifier |
201
+ | `description` | Yes | — | Short description of the rule |
202
+ | `severity` | No | `"HIGH"` | Severity level (`HIGH`, `MEDIUM`, `LOW`) |
203
+ | `title` | No | `""` | Header displayed in violation output |
204
+ | `explanation` | No | `""` | Detailed explanation of why this matters |
205
+ | `recommendation` | No | `""` | How to fix the issue, with examples |
206
+ | `affected_items_label` | No | `"Affected Items"` | Label for the violations list |
207
+ | `references` | No | `[]` | Links to documentation |
208
+
209
+ 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.
210
+
211
+ **`Violation`** - Represents a single validation failure:
212
+
213
+ - `message` (str): Human-readable description of the issue
214
+ - `path` (str): Location in the YAML structure (e.g., `apic.tenants[name=PROD].vrfs[0]`)
215
+ - `details` (dict, optional): Machine-readable metadata for automation
216
+
217
+ ## JSON Output
218
+
219
+ Use `--format json` for machine-readable output suitable for CI/CD pipelines:
220
+
221
+ ```bash
222
+ nac-validate data/ -s schema.yaml -r rules/ --format json
223
+ ```
224
+
225
+ Output structure:
226
+
227
+ ```json
228
+ {
229
+ "syntax_errors": [],
230
+ "semantic_errors": [
231
+ {
232
+ "rule_id": "301",
233
+ "description": "Verify Infra VLAN Is Defined When Referenced by AAEPs",
234
+ "errors": [
235
+ "apic.access_policies.aaeps[name=INFRA-AAEP].infra_vlan - AAEP 'INFRA-AAEP' has infra_vlan enabled but global infra_vlan is not defined"
236
+ ]
237
+ }
238
+ ]
239
+ }
240
+ ```
241
+
242
+ ## Installation
243
+
244
+ 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/).
245
+
246
+ `nac-validate` can be installed in a virtual environment using `pip` or `uv`:
247
+
248
+ ```bash
249
+ # Using pip
250
+ pip install nac-validate
251
+
252
+ # Using uv (recommended)
253
+ uv tools install nac-validate
254
+ ```
255
+
256
+ ## Pre-Commit Hook
257
+
258
+ 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:
259
+
260
+ ```yaml
261
+ repos:
262
+ - repo: https://github.com/netascode/nac-validate
263
+ rev: v1.0.0
264
+ hooks:
265
+ - id: nac-validate
266
+ ```
267
+
268
+ In case the schema or validation rules are located somewhere else the required CLI arguments can be added like this:
269
+
270
+ ```yaml
271
+ repos:
272
+ - repo: https://github.com/netascode/nac-validate
273
+ rev: v1.0.0
274
+ hooks:
275
+ - id: nac-validate
276
+ args:
277
+ - '-s'
278
+ - 'my_schema.yaml'
279
+ - '-r'
280
+ - 'rules/'
281
+ ```
282
+
283
+ ## Ansible Vault Support
284
+
285
+ 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:
286
+
287
+ ```
288
+ export ANSIBLE_VAULT_ID=dev
289
+ export ANSIBLE_VAULT_PASSWORD=Password123
290
+ ```
291
+
292
+ `ANSIBLE_VAULT_ID` is optional, and if not defined will be omitted.
293
+
294
+ ## Additional Tags
295
+
296
+ ### Reading Environment Variables
297
+
298
+ The `!env` YAML tag can be used to read values from environment variables.
299
+
300
+ ```yaml
301
+ root:
302
+ name: !env VAR_NAME
303
+ ```
@@ -0,0 +1,263 @@
1
+ [![Tests](https://github.com/netascode/nac-validate/actions/workflows/test.yml/badge.svg)](https://github.com/netascode/nac-validate/actions/workflows/test.yml)
2
+ ![Python Support](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-informational "Python Support: 3.10, 3.11, 3.12, 3.13")
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 `pip` or `uv`:
207
+
208
+ ```bash
209
+ # Using pip
210
+ pip install nac-validate
211
+
212
+ # Using uv (recommended)
213
+ uv tools 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
+ ]