Skip to content

Commit

Permalink
Fixed storage account name rule with Azure Cloud Shell microsoft#47
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Jan 2, 2021
1 parent 4055eec commit 2b993a7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ What's changed since pre-release v0.1.0-B2101004:
- Engineering:
- Bump PSRule dependency to v1.0.1. [#49](https://github.com/microsoft/PSRule.Rules.CAF/issues/49)
- Bump PSRule.Rules.Azure dependency to v0.19.0. [#49](https://github.com/microsoft/PSRule.Rules.CAF/issues/49)
- Bug fixes:
- Fixed storage account name rule with Azure Cloud Shell. [#47](https://github.com/microsoft/PSRule.Rules.CAF/issues/47)

## v0.1.0-B2101004 (pre-release)

Expand Down
17 changes: 17 additions & 0 deletions src/PSRule.Rules.CAF/rules/CAF.Common.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ function global:CAF_IsManagedLB {
)
}
}

# Determines if the object is a managed storage account created by Azure
function global:CAF_IsManagedStorage {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
# Check for managed storage accounts
if ($Assert.HasFieldValue($TargetObject, 'Tags.ms-resource-usage', 'azure-cloud-shell').Result) {
return $True;
}
return $False;
}
}
2 changes: 1 addition & 1 deletion src/PSRule.Rules.CAF/rules/CAF.Name.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Rule 'CAF.Name.VM' -Type 'Microsoft.Compute/virtualMachines' {
}

# Synopsis: Use standard storage accounts names.
Rule 'CAF.Name.Storage' -Type 'Microsoft.Storage/storageAccounts' {
Rule 'CAF.Name.Storage' -Type 'Microsoft.Storage/storageAccounts' -If { !(CAF_IsManagedStorage) } {
$Assert.StartsWith($PSRule, 'TargetName', $Configuration.CAF_StoragePrefix, $True);
$Assert.IsLower($PSRule, 'TargetName');
}
Expand Down
14 changes: 14 additions & 0 deletions tests/PSRule.Rules.CAF.Tests/CAF.Name.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Describe 'CAF.Name' -Tag 'name' {
$invalidNames = @(
'sTest001'
'testst001'
'cs001'
)
$testObject = [PSCustomObject]@{
Name = ''
Expand All @@ -356,6 +357,19 @@ Describe 'CAF.Name' -Tag 'name' {
$ruleResult.Outcome | Should -Be 'Fail';
}
}

It 'Cloud Shell' {
$testObject = [PSCustomObject]@{
Name = 'cs001'
ResourceType = 'Microsoft.Storage/storageAccounts'
Tags = @{
'ms-resource-usage' = 'azure-cloud-shell'
}
}
$ruleResult = $testObject | Invoke-PSRule @invokeParams -Name 'CAF.Name.Storage' -Outcome All;
$ruleResult | Should -Not -BeNullOrEmpty;
$ruleResult.Outcome | Should -Be 'None';
}
}

Context 'CAF.Name.PublicIP' {
Expand Down

0 comments on commit 2b993a7

Please sign in to comment.