Slide 1

Slide 1 text

David Pazdera A practical guide to Test-Driven Development of infrastructure code

Slide 2

Slide 2 text

● What is Test-Driven Development? ● What benefits does it bring aka What’s in it for me? ● Can TDD be really used for Infrastructure as Code practice? ● Can I re-use existing skills or learn a ton of new stuff? ● Who’s this guy speaking? Questions

Slide 3

Slide 3 text

● cloud architect @ Devoteam M Cloud ● ex ’blue badge’ ● meetups, conferences, ACP, communities (ALZ, Azure Arc, Bicep, Terraform in Azure) ● sports & outdoor enthusiast ● GitHub | LinkedIn | Sessionize | SpeakerDeck | X : pazdedav handle ● Blog: https://pazdedav.blog About me

Slide 4

Slide 4 text

● DevOps engineer in an organization, responsible for Bicep configuration for a project. ● Current technology stack and tooling: ● Goal: improve infra code quality using TDD practice and introduce new tools ● Preferences: ○ cross-platform ○ free or freemium Scenario

Slide 5

Slide 5 text

Software development process relying on software requirements being converted to test cases before software is fully developed and tracking all software development by repeatedly testing the software against all test cases. This is as opposed to software being developed first and test cases created later. Test-driven development (TDD) Add a test Run all tests (should fail) Write the simplest code to pass tests All tests should pass Refactor code as needed Re-run tests

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

be valid (syntax and coding standards) follow security best practices be compliant with target environment’s policies follow cloud provider’s best practices like WAF provision required resources (functional requirements) We want our infra code to …

Slide 8

Slide 8 text

Tools and services map

Slide 9

Slide 9 text

Overview of tools Tool Need to write own tests? Built-in rules Custom rules Bicep linter No Yes No Bicep testing framework Yes No Yes Pester Yes No Yes PSRule for Azure No Yes Yes ARM-TTK No Yes No KICS, Snyk No Yes No PSRule for Azure + EPAC Generate rule collection from existing Azure Policies BenchPress Yes No Yes

Slide 10

Slide 10 text

Local dev environment Remote dev environment All tools installed locally + All tools in a Dev Container GitHub Codespaces Microsoft DevBox Coding environment

Slide 11

Slide 11 text

Coding “Landing Zone”

Slide 12

Slide 12 text

Code validation

Slide 13

Slide 13 text

Bicep linter VS Code extension code --install-extension ms-azuretools.vscode-bicep Local installation brew install azure-cli az bicep upgrade Local execution bicep build demo.bicep Configuration bicepconfig.json GH Action run: az bicep build

Slide 14

Slide 14 text

Bicep Native Testing Framework

Slide 15

Slide 15 text

Pester PowerShell Pester module New-PesterContainer Invoke-Pester -Container Pester Tests extension NUnit CI Format (ADO, GH) deploy.Tests.ps1 deploy.json

Slide 16

Slide 16 text

Pester VS Code extension code --install-extension pspester.pester-test Local installation Install-Module Pester -Force Local execution New-PesterContainer | Invoke-Pester -Container Configuration GH Action run: New-PesterContainer | Invoke-Pester -Container

Slide 17

Slide 17 text

Best practices validation

Slide 18

Slide 18 text

PSRule for Azure PowerShell PSRule module PSRule.Rules.Azure module 400+ pre-defined rules (tests) write your own tests (ps1, yaml, json) Assert-PSRule -InputPath . –Module ‘PSRule.Rules.Azure’ NUnit3 JSON, Yaml Markdown SARIF CSV Azure Monitor workspace deploy.bicep PSRule extension

Slide 19

Slide 19 text

PSRule for Azure VS Code extension code --install-extension bewhite.psrule-vscode Local installation Install-Module -Name 'PSRule' -Repository PSGallery Install-Module -Name 'PSRule.Rules.Azure’ -Repository PSGallery Local execution Assert-PSRule -InputPath path-to-main.tests.bicep –Module ‘PSRule.Rules.Azure’ Configuration ps-rule.yaml GH Action microsoft/[email protected], with: modules: 'PSRule.Rules.Azure'

Slide 20

Slide 20 text

Security testing

Slide 21

Slide 21 text

Security testing with Snyk Snyk CLI snyk iac test {path-to-arm-template.json} [--report] snyk.io

Slide 22

Slide 22 text

Security testing with Snyk VS Code extension code --install-extension snyk-security.snyk-vulnerability- scanner Local installation brew tap snyk/tap brew install snyk Local execution snyk auth snyk iac test {file_name}.json Configuration GH Action snyk/actions/iac@master

Slide 23

Slide 23 text

Security testing with KICS VS Code extension code --install-extension checkmarx.ast-results Local installation Docker Local execution docker run -t -v {path_to_host_folder_to_scan}:/path checkmarx/kics:latest scan -p /path -o "/path/" Configuration GH Action Checkmarx/[email protected]

Slide 24

Slide 24 text

Compliance validation

Slide 25

Slide 25 text

Compliance with PSRule + EPAC PowerShell PSRule module PSRule.Rules.Azure module EPAC module ps-rule.yaml global-settings.json Export-AzPolicyResources -DefinitionsRootFolder .\ -Mode psrule -OutputFolder .\ psrule.assignment.json Assert-PSRule –InputPath .\ -Module “PSRule.Rules.Azure” –Format File Export-AzPolicyAssignmentRuleData –AssignmentFile .\psrule.assignment.json –OutputPath .\ definitions-export- {guid}.Rule.jsonc

Slide 26

Slide 26 text

Compliance with PSRule + EPAC VS Code extension code --install-extension bewhite.psrule-vscode Local installation Install-Module -Name 'PSRule' -Repository PSGallery Install-Module -Name 'PSRule.Rules.Azure’ -Repository PSGallery Install-Module -Name ’EnterprisePolicyAsCode’ - Repository PSGallery Local execution Assert-PSRule -InputPath path-to-main.tests.bicep Configuration psrule.yaml GH Action microsoft/[email protected], with: modules: 'PSRule.Rules.Azure'

Slide 27

Slide 27 text

OuterLoop

Slide 28

Slide 28 text

Deployment validation

Slide 29

Slide 29 text

PowerShell Pester module Invoke-Pester –Configuration $config NUnit CI Format (ADO, GH) Pester Tests extension BenchPress BenchPress.Azure module Az module $Env variables Deploy.Tests.ps1

Slide 30

Slide 30 text

BenchPress VS Code extension code --install-extension pspester.pester-test Local installation Install-Module Pester -Force Install-Module Az -Force Install-Module -Name ‘BenchPress.Azure' -Repository PSGallery Local execution Invoke-Pester -Path .\File.Tests.ps1 Configuration Pester Configuration object GH Action azure/powershell@v1

Slide 31

Slide 31 text

Wrapping things up…

Slide 32

Slide 32 text

● Template Analyzer – https://github.com/Azure/template- analyzer ○ Template scanner for security misconfiguration and best practices ○ Microsoft Security DevOps (Preview) ▪ CLI and GitHub action ▪ support for SARIF, integration with GHAS ▪ uses Template Analyzer in the background Honorable mentions

Slide 33

Slide 33 text

What is Test-Driven Development? What benefits does it bring aka What’s in it for me? Can TDD be really used for Infrastructure as Code practice? Can I re-use existing skills or learn a ton of new stuff? Who’s this guy speaking? Questions

Slide 34

Slide 34 text

https://github.com/pazdedav/nic-2023-project Code repository