Skip to content

Commit c8b109f

Browse files
committed
Threat detection tests + error handling
1 parent 05ec3a9 commit c8b109f

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/ThreatDetectionTests.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Tests that when turning off auditing or marking it as "use server default" , thr
129129
function Test-DisablingThreatDetection
130130
{
131131
# Setup
132-
$testSuffix = 7011
132+
$testSuffix = 7019
133133
Create-TestEnvironment $testSuffix "Japan East"#Create-ThreatDetectionTestEnvironmentWithStorageV2 $testSuffix
134134
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix #Get-SqlThreatDetectionTestEnvironmentParameters $testSuffix
135135

@@ -207,13 +207,13 @@ function Test-InvalidArgumentsThreatDetection
207207

208208
<#
209209
.SYNOPSIS
210-
Tests that thread detection doesn't work on 0.2 servers
210+
Tests that thread detection doesn't work on 2.0 servers
211211
#>
212212
function Test-ThreatDetectionOnV2Server
213213
{
214214
# Setup
215-
$testSuffix = 5007
216-
Create-TestEnvironment $testSuffix "Japan East" "2.0" #Create-ThreatDetectionTestEnvironmentWithStorageV2 $testSuffix "2.0"
215+
$testSuffix = 5023
216+
Create-TestEnvironment $testSuffix "West Europe" "2.0" #Create-ThreatDetectionTestEnvironmentWithStorageV2 $testSuffix "2.0"
217217
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix #Get-SqlThreatDetectionTestEnvironmentParameters $testSuffix
218218

219219
try
@@ -224,7 +224,7 @@ function Test-ThreatDetectionOnV2Server
224224

225225
Set-AzureRmSqlServerAuditingPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName -StorageAccountName $params.storageAccount
226226
Assert-Throws {Set-AzureRmSqlServerThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName}
227-
Assert-Throws {Get-AzureRmSqlDatabaseThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName}
227+
Assert-Throws {Get-AzureRmSqlServerThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName}
228228

229229
}
230230
finally

src/ResourceManager/Sql/Commands.Sql/Auditing/Cmdlet/SetAzureSqlDatabaseAuditingPolicy.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,11 @@ protected override AuditingPolicyModel PersistChanges(AuditingPolicyModel model)
188188
Action swapAuditType = () => { AuditType = AuditType == AuditType.Blob ? AuditType.Table : AuditType.Blob; };
189189
swapAuditType();
190190
var otherAuditingTypePolicyModel = GetEntity();
191-
otherAuditingTypePolicyModel.AuditState = AuditStateType.Disabled;
192-
base.PersistChanges(otherAuditingTypePolicyModel);
191+
if (otherAuditingTypePolicyModel != null)
192+
{
193+
otherAuditingTypePolicyModel.AuditState = AuditStateType.Disabled;
194+
base.PersistChanges(otherAuditingTypePolicyModel);
195+
}
193196
swapAuditType();
194197
return model;
195198
}

src/ResourceManager/Sql/Commands.Sql/Auditing/Cmdlet/SetAzureSqlServerAuditingPolicy.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,11 @@ protected override AuditingPolicyModel PersistChanges(AuditingPolicyModel model)
189189
Action swapAuditType = () => { AuditType = AuditType == AuditType.Blob ? AuditType.Table : AuditType.Blob; };
190190
swapAuditType();
191191
var otherAuditingTypePolicyModel = GetEntity();
192-
otherAuditingTypePolicyModel.AuditState = AuditStateType.Disabled;
193-
base.PersistChanges(otherAuditingTypePolicyModel);
192+
if (otherAuditingTypePolicyModel != null)
193+
{
194+
otherAuditingTypePolicyModel.AuditState = AuditStateType.Disabled;
195+
base.PersistChanges(otherAuditingTypePolicyModel);
196+
}
194197
swapAuditType();
195198
return model;
196199
}

src/ResourceManager/Sql/Commands.Sql/Auditing/Cmdlet/SqlDatabaseAuditingCmdletBase.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using Microsoft.Azure.Commands.Common.Authentication.Models;
1617
using Microsoft.Azure.Commands.Sql.Auditing.Model;
1718
using Microsoft.Azure.Commands.Sql.Auditing.Services;
@@ -35,11 +36,25 @@ protected override AuditingPolicyModel GetEntity()
3536
if (AuditType == AuditType.Table)
3637
{
3738
DatabaseAuditingPolicyModel model;
38-
ModelAdapter.GetDatabaseAuditingPolicy(ResourceGroupName, ServerName, DatabaseName, clientRequestId, out model);
39-
return model;
39+
try
40+
{
41+
ModelAdapter.GetDatabaseAuditingPolicy(ResourceGroupName, ServerName, DatabaseName, clientRequestId, out model);
42+
return model;
43+
}
44+
catch
45+
{
46+
return null;
47+
}
4048
}
4149
DatabaseBlobAuditingPolicyModel blobModel;
42-
ModelAdapter.GetDatabaseAuditingPolicy(ResourceGroupName, ServerName, DatabaseName, clientRequestId, out blobModel);
50+
try
51+
{
52+
ModelAdapter.GetDatabaseAuditingPolicy(ResourceGroupName, ServerName, DatabaseName, clientRequestId, out blobModel);
53+
}
54+
catch
55+
{
56+
return null;
57+
}
4358
return blobModel;
4459
}
4560

src/ResourceManager/Sql/Commands.Sql/Auditing/Cmdlet/SqlDatabaseServerAuditingCmdletBase.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,26 @@ protected override AuditingPolicyModel GetEntity()
4343
if (AuditType == AuditType.Table)
4444
{
4545
ServerAuditingPolicyModel model;
46-
ModelAdapter.GetServerAuditingPolicy(ResourceGroupName, ServerName, this.clientRequestId, out model);
47-
return model;
46+
try
47+
{
48+
ModelAdapter.GetServerAuditingPolicy(ResourceGroupName, ServerName, clientRequestId, out model);
49+
return model;
50+
}
51+
catch
52+
{
53+
return null;
54+
}
4855
}
4956
ServerBlobAuditingPolicyModel blobModel;
50-
ModelAdapter.GetServerAuditingPolicy(ResourceGroupName, ServerName, this.clientRequestId, out blobModel);
51-
return blobModel;
57+
try
58+
{
59+
ModelAdapter.GetServerAuditingPolicy(ResourceGroupName, ServerName, clientRequestId, out blobModel);
60+
return blobModel;
61+
}
62+
catch
63+
{
64+
return null;
65+
}
5266
}
5367

5468
/// <summary>

0 commit comments

Comments
 (0)