Skip to main content

A CLI tool to perform syntactic and semantic validation of YAML files.

Project description

Tests Python Support

iac-validate

A CLI tool to perform syntactic and semantic validation of YAML files.

$ iac-validate -h
Usage: iac-validate [OPTIONS] [PATHS]...

  A CLI tool to perform syntactic and semantic validation of YAML files.

Options:
  --version              Show the version and exit.
  -v, --verbosity LVL    Either CRITICAL, ERROR, WARNING, INFO or DEBUG
  -s, --schema FILE      Path to schema file. (optional, default:
                         '.schema.yaml', env: IAC_VALIDATE_SCHEMA)
  -r, --rules DIRECTORY  Path to semantic rules. (optional, default:
                         '.rules/', env: IAC_VALIDATE_RULES)
  -o, --output FILE      Write merged content from YAML files to a new YAML
                         file. (optional, env: IAC_VALIDATE_OUTPUT)
  --non-strict           Accept unexpected elements in YAML files.
  -h, --help             Show this message and exit.

Syntactic validation is done by basic YAML syntax validation (e.g., indentation) and by providing a 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.

Each .py file must have a single class named Rule. This class must have the following attributes: id, description and severity. It must implement a classmethod() named match that has a single function argument data which is the data read from all YAML files. It should return a list of strings, one for each rule violation with a descriptive message. A sample rule can be found below.

class Rule:
    id = "101"
    description = "Verify child naming restrictions"
    severity = "HIGH"

    @classmethod
    def match(cls, data):
        results = []
        try:
            for child in data["root"]["children"]:
                if child["name"] == "FORBIDDEN":
                    results.append("root.children.name" + " - " + str(child["name"]))
        except KeyError:
            pass
        return results

Installation

Python 3.7+ is required to install iac-validate. Don't have Python 3.7 or later? See Python 3 Installation & Setup Guide.

iac-validate can be installed in a virtual environment using pip:

pip install iac-validate

Pre-Commit Hook

The tool can be integrated via a pre-commit hook with the following config (.pre-commit-config.yaml), assuming the default values (.schema.yaml, .rules/) are appropriate:

repos:
  - repo: https://github.com/netascode/iac-validate
    rev: v0.1.6
    hooks:
      - id: iac-validate

In case the schema or validation rules are located somewhere else the required CLI arguments can be added like this:

repos:
  - repo: https://github.com/netascode/iac-validate
    rev: v0.1.6
    hooks:
      - id: iac-validate
        args:
          - '-s'
          - 'my_schema.yaml'
          - '-r'
          - 'rules/'

Ansible Vault Support

Values can be encrypted using Ansible Vault. This requires Ansible (ansible-vault command) to be installed and the following two environment variables to be defined:

export ANSIBLE_VAULT_ID=dev
export ANSIBLE_VAULT_PASSWORD=Password123

ANSIBLE_VAULT_ID is optional, and if not defined will be omitted.

Additional Tags

Reading Environment Variables

The !env YAML tag can be used to read values from environment variables.

root:
  name: !env VAR_NAME

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iac_validate-0.2.6.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

iac_validate-0.2.6-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file iac_validate-0.2.6.tar.gz.

File metadata

  • Download URL: iac_validate-0.2.6.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for iac_validate-0.2.6.tar.gz
Algorithm Hash digest
SHA256 399e67fcf2c5e52025a36d0e850e0d6873a48835f14fa8de20cc45722dcea53b
MD5 38d55d4f7e6d0df88bdd26ac0599b868
BLAKE2b-256 f408c02c78321a3c15ae7338812b9bc659cb61db0774f314c2bf8b6aa8c400cb

See more details on using hashes here.

File details

Details for the file iac_validate-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: iac_validate-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for iac_validate-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 45c8b3086eacf162cc592679b6e4c78f179061861b2886ef478047823a5b5b57
MD5 1ac77b3b955a9e0c0548f82ee4314ab3
BLAKE2b-256 de1c18aac7dae59e31c88d87d768b0fa2c7c8150c3d50ff5d2b4192bfb01f9b4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page