|
| 1 | +--- |
| 2 | +description: This topic covers how you can use custom rules to test Azure Infrastructure as Code. |
| 3 | +author: BernieWhite |
| 4 | +--- |
| 5 | + |
| 6 | +# Using custom rules |
| 7 | + |
| 8 | +PSRule for Azure covers common use cases that align to the [Microsoft Azure Well-Architected Framework (WAF)][1]. |
| 9 | +In addition to WAF alignment you may have a requirement to enforce organization specific rules. |
| 10 | + |
| 11 | +For example: |
| 12 | + |
| 13 | +- Required tags on a resource group. |
| 14 | +- Code ownership for sensitive resource types. |
| 15 | +- Apply similar controls to Infrastructure as Code that are deployed via Azure Policies. |
| 16 | + |
| 17 | +PSRule allows custom rules to be layered on. |
| 18 | +These custom rules work side-by-side with PSRule for Azure. |
| 19 | + |
| 20 | + [1]: https://learn.microsoft.com/azure/well-architected/ |
| 21 | + |
| 22 | +!!! Abstract |
| 23 | + This topic covers how you can use custom rules to test Azure Infrastructure as Code (IaC). |
| 24 | + |
| 25 | +## Requirements |
| 26 | + |
| 27 | +For custom rules to work with IaC the following requirements must be configured: |
| 28 | + |
| 29 | +1. Set a binding configuration. |
| 30 | +2. Configure expansion for processing Bicep or ARM templates. |
| 31 | +3. Include the `PSRule.Rules.Azure` module. |
| 32 | + |
| 33 | +### Set binding configuration |
| 34 | + |
| 35 | +Rules packaged within PSRule for Azure will automatically detect Azure resources by their type properties. |
| 36 | +Standalone rules will get their type binding configuration from `ps-rule.yaml` instead. |
| 37 | +When binding is not configured, custom rules will typically be ignored. |
| 38 | + |
| 39 | +To configure type binding: |
| 40 | + |
| 41 | +- Create/ update the `ps-rule.yaml` file within the root of the repository. |
| 42 | +- Add the following configuration snippet. |
| 43 | + |
| 44 | +```yaml title="ps-rule.yaml" |
| 45 | +# Configure binding options |
| 46 | +binding: |
| 47 | + targetType: |
| 48 | + - 'resourceType' |
| 49 | + - 'type' |
| 50 | +``` |
| 51 | +
|
| 52 | +### Configuring expansion |
| 53 | +
|
| 54 | +PSRule for Azure performs [expansion][2] on Bicep and ARM template files it finds in your repository. |
| 55 | +Enabling expansion is required for testing any IaC in your repository. |
| 56 | +The requirements for custom rules are no different then using the built-in rules included within PSRule for Azure. |
| 57 | +
|
| 58 | +To configure expansion see either: |
| 59 | +
|
| 60 | +- [Using Bicep source](../using-bicep.md) |
| 61 | +- [Using templates](../using-templates.md) |
| 62 | +
|
| 63 | + [2]: ../faq.md#what-is-expansion |
| 64 | +
|
| 65 | +### Including PSRule for Azure |
| 66 | +
|
| 67 | +When creating custom rules to test Azure IaC including PSRule for Azure is required for most scenarios. |
| 68 | +PSRule for Azure performs [expansion][2] on Bicep and ARM template files it finds in your repository. |
| 69 | +
|
| 70 | +You can include PSRule for Azure by specifying `PSRule.Rules.Azure` in one of the following: |
| 71 | + |
| 72 | +- **Pipeline** — The `modules:` parameter in [GitHub Actions or Azure Pipelines][3]. |
| 73 | +- **PowerShell** — The `-Module` parameter with the [PowerShell cmdlets][4]. |
| 74 | +- **Options** — - The `Include.Module` [option][5]. |
| 75 | + |
| 76 | + [3]: ../creating-your-pipeline.md |
| 77 | + [4]: ../creating-your-pipeline.md |
| 78 | + [5]: https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#includemodule |
| 79 | + |
| 80 | +## Using a standard file path |
| 81 | + |
| 82 | +Rules can be standalone or packaged within a module. |
| 83 | +Standalone rules are ideal for a single project such as an Infrastructure as Code (IaC) repository. |
| 84 | +To reuse rules across multiple projects consider packaging these as a module. |
| 85 | + |
| 86 | +The instructions for packaging rules in a module can be found here: |
| 87 | + |
| 88 | +- [Packaging rules in a module][6] |
| 89 | + |
| 90 | +To store standalone rules we recommend that you: |
| 91 | + |
| 92 | +- **Use .ps-rule/** — Create a sub-directory called `.ps-rule` in the root of your repository. |
| 93 | + Use all lower-case in the sub-directory name. |
| 94 | + Put any custom rules within this sub-directory. |
| 95 | +- **Use files ending with .Rule.ps1 | .Rule.yaml | .Rule.jsonc** — |
| 96 | + PSRule uses a file naming convention to discover rules. |
| 97 | + We recommend using a file name that ends in `.Rule.ps1` or `.Rule.yaml` or `.Rule.jsonc`. |
| 98 | + |
| 99 | +!!! note |
| 100 | + Build pipelines are often case-sensitive or run on Linux-based systems. |
| 101 | + Using the casing rule above reduces confusion latter when you configure continuous integration (CI). |
| 102 | + |
| 103 | + [6]: https://microsoft.github.io/PSRule/stable/authoring/packaging-rules/ |
| 104 | + |
| 105 | +## Naming rules |
| 106 | + |
| 107 | +When running PSRule, rule names must be unique. |
| 108 | +PSRule for Azure uses the name prefix of `Azure.` on all rules and resources included in the module. |
| 109 | + |
| 110 | +!!! example |
| 111 | + The following names are examples of rules included within PSRule for Azure: |
| 112 | + |
| 113 | + - `Azure.AKS.Version` |
| 114 | + - `Azure.AKS.AuthorizedIPs` |
| 115 | + - `Azure.SQL.MinTLS` |
| 116 | + |
| 117 | +When naming custom rules we recommend that you: |
| 118 | + |
| 119 | +- **Use a standard prefix** — You can use the `Local.` or `Org.` prefix for standalone rules. |
| 120 | + - Alternatively choose a short prefix that identifies your organization. |
| 121 | +- **Use dotted notation** — Use dots to separate rule name. |
| 122 | +- **Use a maximum length of 35 characters** — The default view of `Invoke-PSRule` truncates longer names. |
| 123 | + PSRule supports longer rule names however if `Invoke-PSRule` is called directly consider using `Format-List`. |
| 124 | + |
| 125 | +## Related content |
| 126 | + |
| 127 | +- [Using Bicep source](using-bicep.md) |
| 128 | +- [Using templates](using-templates.md) |
| 129 | +- [Creating your pipeline](creating-your-pipeline.md) |
| 130 | + |
| 131 | +*[IaC]: Azure Resource Manager |
0 commit comments