PSRule

repository·main·Indexed 19 days ago

https://github.com/microsoft/psrule

A cross-platform engine used to validate Infrastructure as Code (IaC) and objects against custom rules. PSRule integrates into DevOps workflows via Azure Pipelines, GitHub Actions, and Visual Studio Code to provide fast feedback and quality gates. It supports rule definitions in YAML, JSON, and PowerShell, and is compatible with Linux, MacOS, and Windows. Key features include a Domain Specific Language (DSL) for assertions, a .NET SDK, and a command-line tool (Microsoft.PSRule.Tool).

Tokens
115.2K
Snippets
382
Records
505
Agent score
66%

What's inside PSRule

  1. What is PSRule and what can it validate?

    main

    PSRule is a PowerShell-based engine designed to validate Infrastructure as Code (IaC) and any arbitrary PowerShell object. It acts as a validation engine that processes object graphs against a set of defined rules.

    PSRule natively supports the following file formats:

    • YAML (.yaml or .yml)
    • JSON (.json)
    • PowerShell Data Files (.psd1)
    • Markdown front matter (.md or .markdown)

    Common IaC tools like Terraform can be integrated by exporting their state or plans to JSON (e.g., terraform show -json) and passing that output to PSRule.

  2. What is PSRule?

    main

    PSRule is a rules engine designed for testing Infrastructure as Code (IaC). It performs static analysis on various IaC artifacts to ensure they adhere to defined standards and best practices.

    Supported artifacts for analysis include:

    • Templates
    • Manifests
    • Pipelines
    • Workflows
  3. Overview of PSRule features and objectives

    main

    PSRule is a cross-platform module designed for validating infrastructure as code (IaC).

    Key Features

    • DevOps Support: Built to provide fast feedback in pull requests and implement quality gates between environments (dev, test, prod).
    • Extensibility: Rules can be defined using YAML, JSON, or PowerShell formats.
    • Reusability: Rules can be shared across teams or organizations and can be disabled where they are not applicable.

    Core Design Principles

    • Cross-platform: Supports rule validation and authoring on Linux, MacOS, and Windows. It can also run in a Linux container for CI systems that do not support PowerShell natively.
  4. Use Microsoft.PSRule.Badges for badge creation

    main
    The Microsoft.PSRule.Badges library provides the underlying implementation and support for creating badges within the PSRule badge API. It is intended to be used as a dependency when working with the PSRule badge ecosystem to generate visual indicators for rule compliance or status.
  5. Consume PSRule for .NET using the SDK

    main
    The PSRule SDK for .NET allows developers to integrate PSRule capabilities directly into .NET applications. This package provides the necessary interfaces and logic to consume PSRule's rule-based validation and analysis within the .NET ecosystem.
  6. Explore PSRule related projects and ecosystem

    main

    The PSRule ecosystem is distributed across several specialized repositories for CI/CD integration, IDE support, monitoring, and specific rule sets. Depending on your needs, you may want to use specific modules for Azure, Kubernetes, or GitHub validation.

    ### Official Microsoft Projects
    
    | Name | Description |
    | :--- | :--- |
    | [ps-rule](https://github.com/microsoft/ps-rule) | GitHub continuous integration using GitHub Actions |
    | [PSRule-pipelines](https://github.com/microsoft/PSRule-pipelines) | Azure DevOps continuous integration using Azure Pipelines |
    | [PSRule-vscode](https://github.com/microsoft/PSRule-vscode) | Support for running and authoring rules within Visual Studio Code |
    | [PSRule.Monitor](https://github.com/microsoft/PSRule.Monitor) | Support for logging PSRule analysis results to Azure Monitor |
    | [PSRule.Rules.Azure](https://aka.ms/ps-rule-azure) | Rules to validate Azure resources and IaC |
    | [PSRule.Rules.Azure-quickstart](https://github.com/Azure/PSRule.Rules.Azure-quickstart) | Sample code for Azure validation |
    | [PSRule.Rules.CAF](https://github.com/microsoft/PSRule.Rules.CAF) | Rules for Azure Cloud Adoption Framework (CAF) |
    | [PSRule.Rules.GitHub](https://github.com/microsoft/PSRule.Rules.GitHub) | Rules to validate GitHub repositories |
    | [PSRule.Rules.Kubernetes](https://github.com/microsoft/PSRule.Rules.Kubernetes) | Rules to validate Kubernetes resources |
    | [PSRule.Rules.MSFT.OSS](https://github.com/microsoft/PSRule.Rules.MSFT.OSS) | Rules for Microsoft OSS requirements |
  7. What is a Selector and how to use it

    main

    A Selector is a filtering mechanism used to determine if a rule should be executed or skipped. Selectors use YAML or JSON expressions to evaluate properties on a target object.

    • If the selector evaluates to true, the rule is run and will either pass or fail.
    • If the selector evaluates to false, the rule is skipped.

    Selectors are useful for narrowing down the scope of rules (e.g., only running a rule on services that have an 'Automatic' start type).

    ---
    # Synopsis: Find services with an automatic start type.
    apiVersion: github.com/microsoft/PSRule/2025-01-01
    kind: Selector
    metadata:
      name: Yaml.IsAutomaticService
    spec:
      if:
        field: StartType
        startsWith: Automatic
        convert: true