Skip to content

Commit 16638f9

Browse files
Merge branch 'main' into instancecheckscontinued
2 parents e30f598 + d3b866e commit 16638f9

File tree

11 files changed

+323
-18
lines changed

11 files changed

+323
-18
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"TylerLeonhardt.vscode-inline-values-powershell",
2525
"cschleiden.vscode-github-actions",
2626
"ms-mssql.mssql",
27-
"Gruntfuggly.todo-tree"
27+
"Gruntfuggly.todo-tree",
28+
"streetsidesoftware.code-spell-checker"
2829
],
2930
"settings": {
3031
"editor.renderWhitespace": "all",

.devcontainer/docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: "3"
22
services:
33
dbachecks1:
4-
image: dbachecks/sqlinstance1:v2.36.0
4+
image: dbachecks/sqlinstance1:v2.38.0
55
volumes:
66
- mydata:/var/opt/backups
77
- shared:/shared
@@ -13,7 +13,7 @@ services:
1313
hostname: dbachecks1
1414

1515
dbachecks2:
16-
image: dbachecks/sqlinstance2:v2.36.0
16+
image: dbachecks/sqlinstance2:v2.38.0
1717
volumes:
1818
- mydata:/var/opt/backups
1919
- shared:/shared
@@ -27,7 +27,7 @@ services:
2727

2828
# This is our SQL2022 container
2929
dbachecks3:
30-
image: dbachecks/sqlinstance3:v2.36.0
30+
image: dbachecks/sqlinstance3:v2.38.0
3131
volumes:
3232
- mydata:/var/opt/backups
3333
- shared:/shared

.github/workflows/PR.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
path: ${{ env.buildFolderName }}
6060
- name: Run Tests
6161
shell: pwsh
62-
run: ./build.ps1 -tasks noop; ipmo dbatools ; ./build.ps1 -tasks test # to get around dbatools failing to load XE.core.dll if
62+
run: ./build.ps1 -tasks noop ; ./build.ps1 -tasks test # to get around dbatools failing to load XE.core.dll if
6363
- name: Publish Test Artifact
6464
uses: actions/upload-artifact@v3
6565
with:

.vscode/settings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@
4747
"[markdown]": {
4848
"editor.trimAutoWhitespace": false,
4949
"files.trimTrailingWhitespace": false
50-
}
51-
}
50+
},
51+
"cSpell.enableFiletypes": [
52+
"powershell"
53+
]
54+
}

CONTRIBUTING.md

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,171 @@
11
# Contributing
22

3-
## TODO
3+
## Welcome
4+
5+
Before we go any further, thanks for being here. Thanks for using dbachecks and especially thanks
6+
for being here and looking into how you can help!
7+
8+
## Important resources
9+
10+
- docs
11+
- bugs
12+
- communicate with the team
13+
- slack
14+
- github discussions?
15+
- presentations\blogs?
416

517
## Running the Tests
618

719
If want to know how to run this module's tests you can look at the [Testing Guidelines](https://dsccommunity.org/guidelines/testing-guidelines/#running-tests)
20+
21+
## Environment details
22+
23+
We strongly believe that 'every repo should have a devcontainer' and therefore we've built one
24+
for this project that includes 3 SQL Servers and everything you need to develop and build the
25+
dbachecks module.
26+
27+
It's magic!
28+
29+
### Prerequisites:
30+
31+
In order to use the devcontainer there are a few things you need to get started.
32+
33+
- [Docker](https://www.docker.com/get-started)
34+
- [git](https://git-scm.com/downloads)
35+
- [VSCode](https://code.visualstudio.com/download)
36+
- [Remote Development Extension for VSCode](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack)
37+
38+
### Setup
39+
40+
Once the prerequisites are in place follow these steps to download the repo and start up the
41+
devcontainer. The first time you build the devcontainer it will need to pull down the images
42+
so that could take a hot second depending on your internet speeds.
43+
44+
1. Download the repo from GitHub
45+
```PowerShell
46+
# change directory to where you'd like the repo to go
47+
cd C:\GitHub\
48+
49+
# clone the repo from GitHub
50+
git clone https://github.com/dataplat/dbachecks
51+
52+
# move into the folder
53+
cd .\dbachecks\
54+
55+
# open VSCode
56+
code .
57+
```
58+
59+
754662. Once code opens, there should be a toast in the bottom right that suggests you 'ReOpen in Container'.
60+
1. The first time you do this it may take a little, and you'll need an internet connection, as it'll download the container images used in our demos
61+
62+
### Develop & Build
63+
64+
We are using the [Sampler](https://github.com/gaelcolas/Sampler) Powershell Module to structure our module.
65+
This makes it easier to develop and test the module locally.
66+
67+
The workflow for using this and developing the code - for example to add a new Database level check you could follow
68+
this guide.
69+
70+
1. Download the repo locally and create a new branch to develop on
71+
```PowerShell
72+
git checkout -b newStuff # give it a proper name!
73+
```
74+
75+
1. Develop in the source repository, to add a check you need to add the following code:
76+
- add check code to `source/checks/DatabaseV5.Tests.ps1`
77+
- add required configurations to `source/internal/configurations/configuration.ps1`
78+
- `skip.database.checkName`
79+
- `policy.database.checkNameExcludeDb`
80+
- add required properties to object info to `source/internal/functions/Get-AllDatabaseInfo.ps1`
81+
82+
1. Build the module
83+
```PowerShell
84+
./build.ps1 -Tasks build
85+
```
86+
87+
1. Sampler automatically adds the new version to your path you can prove that with the following code:
88+
```PowerShell
89+
get-module dbachecks -ListAvailable | Select-Object Name, ModuleBase
90+
```
91+
92+
1. Import new version of the module
93+
```PowerShell
94+
Import-Module dbachecks -force
95+
```
96+
97+
1. Test out the new code
98+
```PowerShell
99+
# save the password to make for easy connections
100+
$password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force
101+
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password
102+
103+
$show = 'All'
104+
$checks = 'RecoveryModel' # <-- change this to your new check name
105+
106+
$sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403'
107+
#$sqlinstances = 'dbachecks1', 'dbachecks2', 'dbachecks3' # need client aliases for this to work New-DbaClientAlias
108+
109+
# Run v5 checks
110+
$v5code = Invoke-DbcCheck -SqlInstance $Sqlinstances -SqlCredential $cred -Check $Checks -legacy $false -Show $show -PassThru -Verbose
111+
```
112+
113+
1. If you are working on the v4 --> v5 upgrade you can also confirm your v5 test results match v4 with the following
114+
```PowerShell
115+
# save the password to make for easy connections
116+
$password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force
117+
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password
118+
119+
$show = 'All'
120+
$checks = 'RecoveryModel' # <-- change this to your new check name
121+
122+
$sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403'
123+
#$sqlinstances = 'dbachecks1', 'dbachecks2', 'dbachecks3' # need client aliases for this to work New-DbaClientAlias
124+
125+
# Check results of the tests - are we testing the same things with the same results for v4 & v5
126+
Invoke-PerfAndValidateCheck -SQLInstances $sqlinstances -Checks $Checks
127+
# Include the specific details for the perf testing
128+
Invoke-PerfAndValidateCheck -SQLInstances $sqlinstances -Checks $Checks -PerfDetail
129+
# Include the test results - this helps troubleshooting if your tests aren't the same
130+
Invoke-PerfAndValidateCheck -SQLInstances $sqlinstances -Checks $Checks -showTestResults
131+
```
132+
133+
1. Once you are happy with your code, push your branch to GitHub and create a PR against the dbachecks repo.
134+
135+
1. Thanks!
136+
137+
### Rebuild your devcontainer
138+
139+
The only way to properly rebuild to ensure that all volumes etc are removed is to open up a console
140+
or PowerShell window outside of the devcontainer and run the following:
141+
142+
```PowerShell
143+
cd \path-of-dbachecks-folder\.devcontainer
144+
145+
docker-compose -f "docker-compose.yml" -p "bitsdbatools_devcontainer" down
146+
```
147+
148+
## How to submit changes:
149+
TODO:
150+
Pull Request protocol etc. You might also include what response they'll get back from the team on submission, or any caveats about the speed of response.
151+
152+
## How to report a bug:
153+
TODO:
154+
Bugs are problems in code, in the functionality of an application or in its UI design; you can submit them through "bug trackers" and most projects invite you to do so, so that they may "debug" with more efficiency and the input of a contributor. Take a look at Atom's example for how to teach people to report bugs to your project.
155+
156+
## Templates:
157+
TODO:
158+
in this section of your file, you might also want to link to a bug report "template" like this one here which contributors can copy and add context to; this will keep your bugs tidy and relevant.
159+
160+
## Style Guide
161+
TODO:
162+
include extensions and vscode settings we use to keep things neat
163+
164+
## Code of Conduct
165+
TODO: maybe beef this out - stolen from data sat repo for now.
166+
167+
We expect and demand that you follow some basic rules. Nothing dramatic here. There will be a proper code of conduct for the websites added soon, but in this repository
168+
169+
BE EXCELLENT TO EACH OTHER
170+
171+
Do I need to say more? If your behaviour or communication does not fit into this statement, we do not wish for you to help us.

containers/JessAndBeard.psm1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,13 +2351,19 @@ The Tags are the same"
23512351
FailedChange = 0 # + or - the number of tests failed for v5
23522352
SkippedChange = 0 # + or - the number of tests skipped for v5
23532353
},
2354-
@{
2354+
@{
23552355
Name = 'SupportedBuild'
23562356
RunChange = -3 # + or - the number of tests run for v5
23572357
PassedChange = -3 # + or - the number of tests passed for v5
23582358
FailedChange = 0 # + or - the number of tests failed for v5
23592359
SkippedChange = 0 # + or - the number of tests skipped for v5
2360-
2360+
},
2361+
@{
2362+
Name = 'GuestUserConnect'
2363+
RunChange = 0 # + or - the number of tests run for v5
2364+
PassedChange = +2 # + or - the number of tests passed for v5
2365+
FailedChange = -2 # + or - the number of tests failed for v5
2366+
SkippedChange = 0 # + or - the number of tests skipped for v5
23612367
}
23622368
)
23632369
$runchange = 0

containers/base/dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM dbachecks/sqlinstance1
33
USER root
44

55
# remove old dbatools directory so 2.0 will import
6-
RUN rm -rf /usr/local/share/powershell/Modules/dbatools
6+
RUN rm -rf /usr/local/share/powershell/Modules/dbatools/1.1.145
77

88
# Copy Profile
99

developing/PSConfEU demo.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# PSConfEU demo
2+
3+
1. Develop in the source repository
4+
- copy existing check & rewrite - add check to `source/checks/Databasev5.Tests.ps1`
5+
- add configuration to `source/internal/configurations/configuration.ps1`
6+
- `skip.database.pseudosimple`
7+
- `policy.database.pseudosimpleexcludedb`
8+
- add object info to `source/internal/functions/Get-AllDatabaseInfo.ps1`
9+
10+
11+
2. Build the module
12+
```PowerShell
13+
./build.ps1 -Tasks build
14+
```
15+
16+
3. Sampler automatically adds the new version to your path
17+
```PowerShell
18+
get-module dbachecks -ListAvailable | select name, modulebase
19+
```
20+
21+
4. Import new version of the module (if you get a bogus error the first time retry it)
22+
```PowerShell
23+
Import-Module dbachecks -force
24+
```
25+
26+
5. Test out the new code
27+
28+
```PowerShell
29+
# save the password to make for easy connections
30+
$password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force
31+
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password
32+
33+
$show = 'All'
34+
$checks = 'RecoveryModel'
35+
36+
#$sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403'
37+
$sqlinstances = 'dbachecks1', 'dbachecks2', 'dbachecks3' # need client aliases for this to work New-DbaClientAlias
38+
39+
# Run v4 checks
40+
$v4code = Invoke-DbcCheck -SqlInstance $Sqlinstances -SqlCredential $cred -Check $Checks -legacy $true -Show $show -PassThru
41+
# Run v5 checks
42+
$v5code = Invoke-DbcCheck -SqlInstance $Sqlinstances -SqlCredential $cred -Check $Checks -legacy $false -Show $show -PassThru -Verbose
43+
44+
Invoke-PerfAndValidateCheck -SQLInstances $sqlinstances -Checks $Checks
45+
Invoke-PerfAndValidateCheck -SQLInstances $sqlinstances -Checks $Checks -PerfDetail
46+
Invoke-PerfAndValidateCheck -SQLInstances $sqlinstances -Checks $Checks -showTestResults
47+
```
48+

source/checks/Databasev5.Tests.ps1

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ Describe "Suspect Page" -Tag SuspectPage, High , Database -ForEach $InstancesToT
5858
}
5959

6060
Describe "Database Collation" -Tag DatabaseCollation, High, Database -ForEach $InstancesToTest {
61+
#TODO: Should we have a skip option for each IT block?
6162
$skip = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.database.databasecollation' }).Value
6263
Context "Testing database collation on <_.Name>" {
6364
It "Database <_.Name> collation <_.Collation> should match server collation <_.ServerCollation> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.wrongcollation -notcontains $PsItem.Name } } {
6465
$psitem.ServerCollation | Should -Be $psitem.Collation -Because "You will get collation conflict errors in tempdb"
6566
}
6667

6768
# wrong collation set
68-
It "Database <_.Name> collation <_.Collation> should not match server collation <_.ServerCollation> on <_.SqlInstance>" -ForEach $psitem.Databases.Where{ $_.Name -in $psitem.ConfigValues.wrongcollation } {
69+
It "Database <_.Name> collation <_.Collation> should not match server collation <_.ServerCollation> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ $_.Name -in $psitem.ConfigValues.wrongcollation } {
6970
$psitem.ServerCollation | Should -Not -Be $psitem.Collation -Because "You have defined the database to have another collation then the server. You will get collation conflict errors in tempdb"
7071
}
71-
7272
}
7373
}
7474

@@ -222,3 +222,55 @@ Describe "Compatibility Level" -Tag CompatibilityLevel, High, Database -ForEach
222222
}
223223
}
224224
}
225+
226+
Describe "Guest User" -Tag GuestUserConnect, Security, CIS, Medium, Database -ForEach $InstancesToTest {
227+
$Skip = ($__dbcconfig | Where-Object Name -EQ 'skip.security.guestuserconnect').Value
228+
229+
Context "Testing Guest user has CONNECT permission" {
230+
It "Database Guest user should return no CONNECT permissions in <_.Name> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.guestuserexclude -notcontains $psitem.Name } } {
231+
$psitem.GuestUserConnect | Should -BeFalse -Because "we don't want the guest user to have connect access to our database."
232+
}
233+
}
234+
}
235+
236+
Describe "Recovery Model" -Tag RecoveryModel, DISA, Medium, Database -ForEach $InstancesToTest {
237+
$Skip = ($__dbcconfig | Where-Object Name -EQ 'skip.database.recoverymodel').Value
238+
239+
Context "Testing Recovery Model" {
240+
It "Database <_.Name> should be set to <_.ConfigValues.recoverymodeltype> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.recoverymodelexclude -notcontains $psitem.Name } } {
241+
$psitem.RecoveryModel | Should -Be $psitem.ConfigValues.recoverymodeltype -Because "You expect this recovery model."
242+
}
243+
}
244+
}
245+
246+
Describe "PseudoSimple Recovery Model" -Tag PseudoSimple, Medium, Database -ForEach $InstancesToTest {
247+
$Skip = ($__dbcconfig | Where-Object Name -EQ 'skip.database.pseudosimple').Value
248+
249+
Context "Testing database is not in PseudoSimple recovery model" {
250+
It "Database <_.Name> has PseudoSimple recovery model equal false on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database -and $_.RecoveryModel -eq 'Full' } else { $psitem.ConfigValues.pseudosimpleexclude -notcontains $psitem.Name -and $_.RecoveryModel -eq 'Full' } } {
251+
$psitem.PseudoSimple | Should -BeFalse -Because "PseudoSimple means that a FULL backup has not been taken and the database is still effectively in SIMPLE mode"
252+
}
253+
}
254+
}
255+
256+
Describe "Contained Database Auto Close" -Tag ContainedDBAutoClose, CIS, Database -ForEach $InstancesToTest {
257+
$Skip = ($__dbcconfig | Where-Object Name -EQ 'skip.security.containedbautoclose').Value
258+
259+
Context "Testing contained database auto close option" {
260+
It "Database <_.Name> should have auto close set to false on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database -and $_.ContainmentType -ne "NONE" } else { $psitem.ConfigValues.contdbautocloseexclude -notcontains $psitem.Name -and $_.ContainmentType -ne "NONE" } } {
261+
$psitem.ContainedDbAutoClose | Should -BeFalse -Because "Contained Databases should have auto close set to false for CIS compliance."
262+
}
263+
}
264+
}
265+
266+
Describe "Contained Database SQL Authenticated Users" -Tag ContainedDBSQLAuth, CIS, Database -ForEach $InstancesToTest {
267+
$Skip = ($__dbcconfig | Where-Object Name -EQ 'skip.security.ContainedDBSQLAuth').Value
268+
269+
#if ($version -lt 13 ) { $skip = $true }
270+
271+
Context "Testing contained database to see if sql authenticated users exist" {
272+
It "Database <_.Name> should have no sql authenticated users on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database -and $_.ContainmentType -ne "NONE" } else { $psitem.ConfigValues.contdbsqlauthexclude -notcontains $psitem.Name -and $_.ContainmentType -ne "NONE" } } {
273+
$psitem.ContainedDbSqlAuthUsers | Should -Be 0 -Because "We expect there to be no sql authenticated users in contained database."
274+
}
275+
}
276+
}

0 commit comments

Comments
 (0)