Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for more complex rule pre-conditions #649

Closed
BernieWhite opened this issue Mar 1, 2021 · 0 comments · Fixed by #675
Closed

Add support for more complex rule pre-conditions #649

BernieWhite opened this issue Mar 1, 2021 · 0 comments · Fixed by #675
Assignees
Labels
enhancement New feature or request feature: language Issues that affect language such keywords and variables
Milestone

Comments

@BernieWhite
Copy link
Member

Currently pre-conditions for rules can be written in script and/ or based on type binding. Type binding is effective in many simple scenarios however often needs to be combined with script pre-conditions for complex matching such as schema.

For example:

Rule 'Azure.Template.TemplateFile' -Type '.json' -If { (IsTemplateFile) } {
    # Rule body
}

# Determines if the object is a Azure Resource Manager template file
function global:IsTemplateFile {
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param (
        [Parameter(Mandatory = $False)]
        [String]$Suffix
    )
    process {
        if ($TargetObject.Extension -ne '.json') {
            return $False;
        }
        try {
            $jsonObject = $PSRule.GetContent($TargetObject)[0];
            [String]$targetSchema = $jsonObject.'$schema';
            $schemas = @(
                # Https
                "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json`#"
                "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json`#"
                "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json`#"
                "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json`#"
                "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json`#"

                # Http
                "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json`#"
                "http://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json`#"
                "http://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json`#"
                "http://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json`#"
                "http://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json`#"
            )
            return $targetSchema -in $schemas -and ([String]::IsNullOrEmpty($Suffix) -or $targetSchema.Trim("`#").EndsWith($Suffix));
        }
        catch {
            return $False;
        }
    }
}

While this approach works, it would be helpful to:

  • Allow for more complex matching without using scripts.
  • Increase reusability of matching by allowing use within dependant modules.
@BernieWhite BernieWhite added enhancement New feature or request feature: language Issues that affect language such keywords and variables labels Mar 1, 2021
BernieWhite added a commit to BernieWhite/PSRule that referenced this issue Mar 30, 2021
@BernieWhite BernieWhite self-assigned this Mar 30, 2021
@BernieWhite BernieWhite added this to the v1.2.0 milestone Mar 30, 2021
BernieWhite added a commit that referenced this issue Mar 30, 2021
* Added selectors and strong apiVersion #649 #647

* Update schema and docs
This was referenced Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature: language Issues that affect language such keywords and variables
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant