Skip to content

Commit d1bc67a

Browse files
authored
Support auditing to a storage account in VNet. (#11500)
* Aupport Auditing to Storage Account in VNet * Fix tests * Support auditing to a storage account in VNet. * Undo a breaking change. * Add records * Update records * Update SqlAuditAdapter.cs * Update test records
1 parent a94d789 commit d1bc67a

File tree

42 files changed

+34837
-28262
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+34837
-28262
lines changed

src/Sql/Sql.Test/ScenarioTests/AuditTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,19 @@ public void TestRemoveServerAuditingSettingsMultipleDiagnosticSettings()
240240
{
241241
RunPowerShellTest("Test-RemoveServerAuditingSettingsMultipleDiagnosticSettings");
242242
}
243+
244+
[Fact]
245+
[Trait(Category.AcceptanceType, Category.CheckIn)]
246+
public void TestServerAuditingToStorageInVNet()
247+
{
248+
RunPowerShellTest("Test-ServerAuditingToStorageInVNet");
249+
}
250+
251+
[Fact]
252+
[Trait(Category.AcceptanceType, Category.CheckIn)]
253+
public void TestDatabaseAuditingToStorageInVNet()
254+
{
255+
RunPowerShellTest("Test-DatabaseAuditingToStorageInVNet");
256+
}
243257
}
244258
}

src/Sql/Sql.Test/ScenarioTests/AuditTests.ps1

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,105 @@ function Test-RemoveAuditOnDatabase
11841184
}
11851185
}
11861186

1187+
<#
1188+
.SYNOPSIS
1189+
Test Server Auditing to storage acount in VNet
1190+
#>
1191+
function Test-ServerAuditingToStorageInVNet
1192+
{
1193+
# Setup
1194+
$testSuffix = getAssetName
1195+
Create-BlobAuditingTestEnvironment $testSuffix "West Central US" "12.0" $True
1196+
$params = Get-SqlBlobAuditingTestEnvironmentParameters $testSuffix
1197+
$subscriptionId = (Get-AzContext).Subscription.Id
1198+
1199+
try
1200+
{
1201+
# Enable Server Auditing to storage in VNet, and verify.
1202+
$profile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
1203+
$cmdlet = New-Object -TypeName Microsoft.Azure.Commands.Sql.Auditing.Cmdlet.SetAzSqlServerAudit
1204+
$cmdlet.DefaultProfile = $profile
1205+
$cmdlet.CommandRuntime = $PSCmdlet.CommandRuntime
1206+
$cmdlet.ResourceGroupName = $params.rgname
1207+
$cmdlet.ServerName = $params.serverName
1208+
$cmdlet.BlobStorageTargetState = "Enabled"
1209+
$cmdlet.StorageAccountResourceId = $params.storageAccountResourceId
1210+
$cmdlet.RoleAssignmentId = "B6C2E345-234A-421A-ADB2-4E81DD4470D6"
1211+
$cmdlet.ExecuteCmdlet()
1212+
1213+
$policy = Get-AzSqlServerAudit -ResourceGroupName $params.rgname -ServerName $params.serverName
1214+
Assert-AreEqual "Enabled" $policy.BlobStorageTargetState
1215+
Assert-AreEqual 3 $policy.AuditActionGroup.Length
1216+
Assert-AreEqual "" $policy.PredicateExpression
1217+
Assert-AreEqual $params.storageAccountResourceId $policy.StorageAccountResourceId
1218+
Assert-AreEqual 0 $policy.RetentionInDays
1219+
1220+
# Disable Server Auditing and verify.
1221+
Get-AzSqlServer -ResourceGroupName $params.rgname -ServerName $params.serverName | Set-AzSqlServerAudit -BlobStorageTargetState Disabled
1222+
$policy = Get-AzSqlServerAudit -ResourceGroupName $params.rgname -ServerName $params.serverName
1223+
Assert-AreEqual "Disabled" $policy.BlobStorageTargetState
1224+
Assert-AreEqual 3 $policy.AuditActionGroup.Length
1225+
Assert-Null $policy.StorageAccountResourceId
1226+
Assert-AreEqual "" $policy.PredicateExpression
1227+
Assert-Null $policy.RetentionInDays
1228+
}
1229+
finally
1230+
{
1231+
# Cleanup
1232+
Remove-BlobAuditingTestEnvironment $testSuffix
1233+
}
1234+
}
1235+
1236+
<#
1237+
.SYNOPSIS
1238+
Test Database Auditing to storage acount in VNet
1239+
#>
1240+
function Test-DatabaseAuditingToStorageInVNet
1241+
{
1242+
# Setup
1243+
$testSuffix = getAssetName
1244+
Create-BlobAuditingTestEnvironment $testSuffix "West Central US" "12.0" $True
1245+
$params = Get-SqlBlobAuditingTestEnvironmentParameters $testSuffix
1246+
$subscriptionId = (Get-AzContext).Subscription.Id
1247+
1248+
try
1249+
{
1250+
# Enable Database Auditing to storage in VNet, and verify.
1251+
$profile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
1252+
$cmdlet = New-Object -TypeName Microsoft.Azure.Commands.Sql.Auditing.Cmdlet.SetAzSqlDatabaseAudit
1253+
$cmdlet.DefaultProfile = $profile
1254+
$cmdlet.CommandRuntime = $PSCmdlet.CommandRuntime
1255+
$cmdlet.ResourceGroupName = $params.rgname
1256+
$cmdlet.ServerName = $params.serverName
1257+
$cmdlet.DatabaseName = $params.databaseName
1258+
$cmdlet.BlobStorageTargetState = "Enabled"
1259+
$cmdlet.StorageAccountResourceId = $params.storageAccountResourceId
1260+
$cmdlet.RoleAssignmentId = "F9CFE83C-552B-4ED1-BC58-741EF3A620AE"
1261+
$cmdlet.ExecuteCmdlet()
1262+
1263+
$policy = Get-AzSqlDatabaseAudit -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
1264+
Assert-AreEqual "Enabled" $policy.BlobStorageTargetState
1265+
Assert-AreEqual 3 $policy.AuditActionGroup.Length
1266+
Assert-AreEqual "" $policy.PredicateExpression
1267+
Assert-AreEqual $params.storageAccountResourceId $policy.StorageAccountResourceId
1268+
Assert-AreEqual 0 $policy.RetentionInDays
1269+
1270+
# Disable Database Auditing and verify.
1271+
Get-AzSqlDatabase -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName | Set-AzSqlDatabaseAudit -BlobStorageTargetState Disabled
1272+
$policy = Get-AzSqlDatabaseAudit -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
1273+
Assert-AreEqual "Disabled" $policy.BlobStorageTargetState
1274+
Assert-AreEqual 3 $policy.AuditActionGroup.Length
1275+
Assert-Null $policy.StorageAccountResourceId
1276+
Assert-AreEqual "" $policy.PredicateExpression
1277+
Assert-Null $policy.RetentionInDays
1278+
}
1279+
finally
1280+
{
1281+
# Cleanup
1282+
Remove-BlobAuditingTestEnvironment $testSuffix
1283+
}
1284+
}
1285+
11871286
<#
11881287
.SYNOPSIS
11891288
Test for all auditing settings on a server

src/Sql/Sql.Test/ScenarioTests/Common.ps1

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ Gets the values of the parameters used at the blob auditing tests
3232
function Get-SqlBlobAuditingTestEnvironmentParameters ($testSuffix)
3333
{
3434
$subscriptionId = (Get-AzContext).Subscription.Id
35-
return @{ rgname = "blob-audit-cmdlet-test-rg" + $testSuffix;
36-
serverName = "blob-audit-cmdlet-server" + $testSuffix;
37-
databaseName = "blob-audit-cmdlet-db" + $testSuffix;
35+
return @{ rgname = "audit-cmdlet-test-rg" + $testSuffix;
36+
serverName = "audit-cmdlet-server" + $testSuffix;
37+
databaseName = "audit-cmdlet-db" + $testSuffix;
3838
storageAccount = "blobaudit" + $testSuffix
3939
eventHubNamespace = "audit-cmdlet-event-hub-ns" + $testSuffix
4040
workspaceName = "audit-cmdlet-workspace" +$testSuffix
41-
storageAccountResourceId = "/subscriptions/" + $subscriptionId + "/resourceGroups/" + "blob-audit-cmdlet-test-rg" + $testSuffix + "/providers/Microsoft.Storage/storageAccounts/" + "blobaudit" + $testSuffix
41+
storageAccountResourceId = "/subscriptions/" + $subscriptionId + "/resourceGroups/" + "audit-cmdlet-test-rg" + $testSuffix + "/providers/Microsoft.Storage/storageAccounts/" + "blobaudit" + $testSuffix
4242
}
4343
}
4444

@@ -77,24 +77,14 @@ function Get-SqlDataMaskingTestEnvironmentParameters ($testSuffix)
7777
}
7878
}
7979

80-
<#
81-
.SYNOPSIS
82-
Creates the test environment needed to perform the Sql auditing tests
83-
#>
84-
function Create-AuditingTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0")
85-
{
86-
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix
87-
Create-TestEnvironmentWithParams $params $location $serverVersion
88-
}
89-
9080
<#
9181
.SYNOPSIS
9282
Creates the test environment needed to perform the Sql blob auditing tests
9383
#>
94-
function Create-BlobAuditingTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0")
84+
function Create-BlobAuditingTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0", $denyAsNetworkRuleDefaultAction = $False)
9585
{
9686
$params = Get-SqlBlobAuditingTestEnvironmentParameters $testSuffix
97-
Create-TestEnvironmentWithParams $params $location $serverVersion
87+
Create-TestEnvironmentWithParams $params $location $serverVersion $denyAsNetworkRuleDefaultAction
9888
New-AzOperationalInsightsWorkspace -ResourceGroupName $params.rgname -Name $params.workspaceName -Sku "Standard" -Location "eastus"
9989
New-AzEventHubNamespace -ResourceGroupName $params.rgname -NamespaceName $params.eventHubNamespace -Location $location
10090
}
@@ -143,10 +133,10 @@ function Create-ThreatDetectionClassicTestEnvironment ($testSuffix, $location =
143133
.SYNOPSIS
144134
Creates the test environment needed to perform the Sql auditing tests
145135
#>
146-
function Create-TestEnvironmentWithParams ($params, $location, $serverVersion)
136+
function Create-TestEnvironmentWithParams ($params, $location, $serverVersion, $denyAsNetworkRuleDefaultAction = $False)
147137
{
148138
Create-BasicTestEnvironmentWithParams $params $location $serverVersion
149-
New-AzStorageAccount -StorageAccountName $params.storageAccount -ResourceGroupName $params.rgname -Location $location -Type Standard_GRS
139+
New-AzStorageAccount -StorageAccountName $params.storageAccount -ResourceGroupName $params.rgname -Location $location -Type Standard_GRS -DenyAsNetworkRuleDefaultAction $denyAsNetworkRuleDefaultAction
150140
Wait-Seconds 10
151141
}
152142

0 commit comments

Comments
 (0)