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

[Feature Request]: Integrate PSRule PoC #2026

Closed
4 of 9 tasks
eriqua opened this issue Sep 14, 2022 · 9 comments
Closed
4 of 9 tasks

[Feature Request]: Integrate PSRule PoC #2026

eriqua opened this issue Sep 14, 2022 · 9 comments
Assignees
Labels
[cat] modules category: modules [cat] testing category: testing enhancement New feature or request hackathon

Comments

@eriqua
Copy link
Contributor

eriqua commented Sep 14, 2022

Description

Related issues #1186 #1580

Leveraging this issue as a starting point for a PoC to integrate PSRule with the CARML CI environment pipelines.
The goal is to enable and test it for at least 3 modules already converted to the new dependencies approach (see issue #1791)

Workstreams and goals

Pre-flight - Test before resources are deployed from templates.

  • CARML PSRule (sub)set:
    • Identify if any baseline rule does not apply to CARML and a customized set is required.
    • Scope: identify if the test target should be repo/module/test specific or a combination of the 3.
  • Pester + PSrule:
    - Integrate current static validation with PSRule, identify any overlap and adjust the set of Pester tests/rules accordingly.

The goals of this workstream are:

  • Identify the right set of rules to run
  • Integrate related jobs in the pipelines
  • Run tests and generate output (see ref)
  • Act on results accordingly (e.g. update module)

Stretch goals

Cross platform

  • Start with GH workflows and extend to ADO pipelines
  • Extend to ARM/JSON converted modules

Custom rules

  • Inspect the need for custom rules and develop accordingly

In-flight

  • Test after resources are deployed to an Azure subscription.
    • Discuss if useful to add and what would be the added value when using together with pre-flight
    • Identify how to integrate in-flight tests with current deployment validation steps, before resources get cleaned up.

References

@eriqua
Copy link
Contributor Author

eriqua commented Sep 19, 2022

Day 1

  • Create and protect a dev branch hack/topic6, linked to this issue.
  • New self-contained dependencies approach overview. Already converted modules (ResourceGroups, KeyVaults, VirtualNetworks) will be the target of the PoC.
  • Individual time for each team member to get familiar with PSRule fundamentals.
  • Integrate PSRule action
    • Module validation GH workflows
    • Linter workflow triggered on pull requests
  • Divide in 2 groups to analyze and solve the following 2 challenges

Challenges

  1. Token replacement: needs to run before the PSRule check, otherwise rules checking correct naming will fail. Token <<namePrefix>> is seen as an intended parameter value and normally not compliant with any resource name.
    1. Group 1 implemented and integrated a fix in the ResourceGroup workflow.
    2. PR merged to the dev branch [Hackathon] Token replacement for the resource group module #2068
  2. Run subset of rules: how to exclude specific rules from the default baseline?
    1. Group 2 looked into customizing general settings in ps-rule.yaml. Here we can ignore specific files/patterns, add exclusion of specific rules, add suppression of specific rules for specific resources. Tested in the KeyVault workflow.
    2. PR merged to the dev branch [Hackaton] First test for PSRule exclusions on KeyVault #2067

PSRule feedback

Start collecting possible feedback to discuss with the PSRule team:

  • Logs show a WARNING: Target object 'modules/Microsoft.Resources/resourceGroups/.test/common/deploy.test.bicep' has not been processed because no matching rules were found. although the file is then checked successfully
  • PSRule VSC extension: cannot be used due to tokenized parameters

Next steps

  • Refine/clean up token replacement code
  • Define the set of rules we want to run
  • Analyze PSRule action options.

    Example: Multiple ps-rule.yaml files can be created and placed in specific folders (e.g. root/module/test scope) and specified in a relative path using workflow variables. We should anyway aim for as less configuration files as possible, leveraging tokens or wildcards (see next point)

  • Verify if possible to use wildcards/tokens (e.g. in the root ps-rule.yaml setting file) to skip specific rules on specific resources. Ref SuppressionGroups

    Example: Resource names containing min are the ones specifying only the required set of parameters. They will likely fail on rules suggesting the use of tags for all resources.

  • Add output (PSRule action option), discuss and select the best format. Test GH job summaries
  • Custom rules

@eriqua
Copy link
Contributor Author

eriqua commented Sep 20, 2022

Day 2

Ref Vnet Workflow run

PSRule feedback

  • SuppressionGroups do not allow the use of wildcards for rule names

    Example: skip Azure.KeyVault.*

Next steps

@eriqua
Copy link
Contributor Author

eriqua commented Sep 21, 2022

Day 3

Ref #2094 running on pull request

PSRule feedback

  • Provide better markdown output to the PSRule action already. Ref PSRule issue

Next steps

  • Integrate csv parser in all workflows
  • Create GitHub composite action to reduce code duplication
  • Extend to ADO
  • Update module tests to comply with failing rules

@eriqua
Copy link
Contributor Author

eriqua commented Sep 22, 2022

Day 4

Ref Vnet run workflow

PSRule feedback

  • Same as here. The PSRule extension prerequisite may be a blocker to adopting the analyzer in ADO pipelines. A workaround to be discussed for CARML is to replace the action with custom script using PSRule module and cmdlets

Next steps

  • Extend to ADO
  • Uncomment deployment and publishing steps in GH and test the whole workflow
  • Add condition to the deployment job: running even if psrule fails (for the time being)
  • Update module tests to comply with failing rules
  • CSV parser synopsis and optimization
  • Open a draft PR to main to get feedback

@eriqua
Copy link
Contributor Author

eriqua commented Sep 26, 2022

Failing rules and target resources before adding suppression groups:

image

Next steps

@BernieWhite
Copy link

Logs show a WARNING: Target object 'modules/Microsoft.Resources/resourceGroups/.test/common/deploy.test.bicep' has not been processed because no matching rules were found. although the file is then checked successfully

Currently this is by design, although we are aware that it is a common point of confusion and may change this behvaiour for PSRule v3. microsoft/PSRule#1179

However you can disable this warning by setting Execution.NotProcessedWarning.

https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#executionnotprocessedwarning

@BernieWhite
Copy link

PSRule VSC extension: cannot be used due to tokenized parameters.

Interested in finding more about this one.

@BernieWhite
Copy link

SuppressionGroups do not allow the use of wildcards for rule names
Example: skip Azure.KeyVault.*

Interested in more detail about this or a feature request.

@AlexanderSehr
Copy link
Contributor

Can be closed in favor of #2151 . Next steps such as the ADO extension should be adressed seperatly as we're blocked until that becomes available.

@github-project-automation github-project-automation bot moved this from In progress to Done in Backlog Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[cat] modules category: modules [cat] testing category: testing enhancement New feature or request hackathon
Projects
Status: Done
Development

When branches are created from issues, their pull requests are automatically linked.

8 participants