Skip to content

Commit b5f6e66

Browse files
committed
working on #884 - add invalid owner and AsymmetricKeySize
1 parent fe9427e commit b5f6e66

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

checks/Databasev5.Tests.ps1

+18-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,24 @@ Describe "Valid Database Owner" -Tag ValidDatabaseOwner, Medium, Database -ForEa
8383
}
8484

8585

86-
#and can evey check have a skip policy.GROUP.UNIQUETAG - if it doesnt have one already and that will live on the line below the describe
86+
Describe "Invalid Database Owner" -Tag InvalidDatabaseOwner, Medium, Database -ForEach $InstancesToTest {
87+
$skip = Get-DbcConfigValue skip.database.invaliddatabaseowner
88+
Context "Testing Database Owners on <_.Name>" {
89+
90+
It "Database <_.Name> - owner '<_.Owner>' should not be in this list ( <_.ConfigValues.invaliddbownername> ) ) on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.invaliddbownerexclude -notcontains $PsItem.Name } } {
91+
$psitem.Owner | Should -Not -BeIn $psitem.ConfigValues.invaliddbownername -Because "The database owner was one specified as incorrect"
92+
}
93+
}
94+
}
8795

96+
Describe "AsymmetricKeySize" -Tag AsymmetricKeySize, CIS, Database -ForEach $InstancesToTest {
97+
$skip = Get-DbcConfigValue skip.security.asymmetrickeysize
98+
Context "Testing Asymmetric Key Size is 2048 or higher on <_.Name>" {
99+
It "Database <_.Name> asymmetric key size should be at least 2048 on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.asymmetrickeysizeexclude -notcontains $PsItem.Name } } {
100+
$psitem.AsymmetricKeySize | Should -Be 0 -Because "Asymmetric keys should have a key length greater than or equal to 2048"
101+
#$psitem.AsymmetricKeySize | Should -BeGreaterOrEqual 2048 -Because "Asymmetric keys should have a key length greater than or equal to 2048"
102+
}
103+
}
104+
}
88105

89106

internal/configurations/configuration.ps1

+6
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ Set-PSFConfig -Module dbachecks -Name policy.build.behind -Value $null -Initiali
228228
# for full options
229229
# 1 for Sunday 127 for every day
230230

231+
# exclude databases
232+
Set-PSFConfig -Module dbachecks -Name policy.asymmetrickeysize.excludedb -Value @('master', 'msdb', 'tempdb') -Initialize -Description "Databases to exclude from asymmetric key size checks"
233+
234+
235+
231236
# skips - these are for whole checks that should not run by default or internal commands that can't be skipped using ExcludeTag
232237
Set-PSFConfig -Module dbachecks -Name skip.dbcc.datapuritycheck -Validation bool -Value $false -Initialize -Description "Skip data purity check in last good dbcc command"
233238
Set-PSFConfig -Module dbachecks -Name skip.backup.testing -Validation bool -Value $true -Initialize -Description "Don't run Test-DbaLastBackup by default (it's not read-only)"
@@ -247,6 +252,7 @@ Set-PSFConfig -Module dbachecks -Name skip.diffbackuptest -Validation bool -Valu
247252
Set-PSFConfig -Module dbachecks -Name skip.database.filegrowthdisabled -Validation bool -Value $true -Initialize -Description "Skip validation of datafiles which have growth value equal to zero."
248253
Set-PSFConfig -Module dbachecks -Name skip.database.logfilecounttest -Validation bool -Value $false -Initialize -Description "Skip the logfilecount test"
249254
Set-PSFConfig -Module dbachecks -Name skip.database.validdatabaseowner -Validation bool -Value $false -Initialize -Description "Skip the valid database owner test"
255+
Set-PSFConfig -Module dbachecks -Name skip.database.invaliddatabaseowner -Validation bool -Value $false -Initialize -Description "Skip the invalid database owner test"
250256
Set-PSFConfig -Module dbachecks -Name skip.database.databasecollation -Validation bool -Value $false -Initialize -Description "Skip the database collation test"
251257
Set-PSFConfig -Module dbachecks -Name skip.database.suspectpage -Validation bool -Value $false -Initialize -Description "Skip the suspect pages test"
252258

internal/functions/Get-AllDatabaseInfo.ps1

+21-8
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,23 @@ function Get-AllDatabaseInfo {
4949
# Using there so that if the instance is not contactable, no point carrying on with gathering more information
5050
switch ($tags) {
5151

52+
'AsymmetricKeySize' {
53+
$asymmetrickey = $true
54+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'asymmetrickeysizeexclude' -Value (Get-DbcConfigValue policy.asymmetrickeysize.excludedb)
55+
}
56+
5257
'ValidDatabaseOwner' {
5358
$owner = $true
5459
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'validdbownername' -Value (Get-DbcConfigValue policy.validdbowner.name)
5560
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'validdbownerexclude' -Value (Get-DbcConfigValue policy.validdbowner.excludedb)
5661
}
5762

63+
'InvalidDatabaseOwner' {
64+
$owner = $true
65+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'invaliddbownername' -Value (Get-DbcConfigValue policy.invaliddbowner.name)
66+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'invaliddbownerexclude' -Value (Get-DbcConfigValue policy.invaliddbowner.excludedb)
67+
}
68+
5869
'DatabaseCollation' {
5970
$collation = $true
6071
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'wrongcollation' -Value (Get-DbcConfigValue policy.database.wrongcollation)
@@ -73,16 +84,18 @@ function Get-AllDatabaseInfo {
7384
ComputerName = $Instance.ComputerName
7485
InstanceName = $Instance.DbaInstanceName
7586
Name = $Instance.Name
76-
ConfigValues = $ConfigValues # can we move this out?
87+
ConfigValues = $ConfigValues # can we move this out to here?
7788
Databases = $Instance.Databases.Foreach{
7889
[PSCustomObject]@{
79-
Name = $psitem.Name
80-
SqlInstance = $Instance.Name
81-
Owner = if ($owner) { $psitem.owner }
82-
ServerCollation = if ($collation) { $Instance.collation }
83-
Collation = if ($collation) { $psitem.collation }
84-
SuspectPage = if ($suspectPage) { (Get-DbaSuspectPage -SqlInstance $Instance -Database $psitem.Name | Measure-Object).Count }
85-
ConfigValues = $ConfigValues # can we move this out?
90+
Name = $psitem.Name
91+
SqlInstance = $Instance.Name
92+
Owner = if ($owner) { $psitem.owner }
93+
ServerCollation = if ($collation) { $Instance.collation }
94+
Collation = if ($collation) { $psitem.collation }
95+
SuspectPage = if ($suspectPage) { (Get-DbaSuspectPage -SqlInstance $Instance -Database $psitem.Name | Measure-Object).Count }
96+
ConfigValues = $ConfigValues # can we move this out?
97+
AsymmetricKeySize = if ($asymmetrickey) { ($psitem.AsymmetricKeys | Where-Object { $_.KeyLength -lt 2048} | Measure-Object).Count }
98+
#AsymmetricKeySize = if ($asymmetrickey) { $psitem.AsymmetricKeys.KeyLength } # doing this I got $null if there wasn't a key
8699
}
87100
}
88101
}

0 commit comments

Comments
 (0)