Skip to content

Commit ce06038

Browse files
committed
Support clssic storages in data security cmdlets
Solving: #3392 (comment)
1 parent 671b9a4 commit ce06038

File tree

6 files changed

+157
-9
lines changed

6 files changed

+157
-9
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/AuditingTests.cs

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

15-
using Microsoft.Azure.Commands.ScenarioTest.Mocks;
1615
using Microsoft.Azure.Commands.ScenarioTest.SqlTests;
1716
using Microsoft.Azure.ServiceManagemenet.Common.Models;
1817
using Microsoft.WindowsAzure.Commands.ScenarioTest;
@@ -38,6 +37,13 @@ public AuditingTests(ITestOutputHelper output)
3837
{
3938
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
4039
}
40+
41+
[Fact]
42+
[Trait(Category.AcceptanceType, Category.CheckIn)]
43+
public void AuditingUpdatePolicyWithClassicStorage()
44+
{
45+
RunPowerShellTest("Test-AuditingUpdatePolicyWithClassicStorage");
46+
}
4147

4248
[Fact]
4349
[Trait(Category.AcceptanceType, Category.CheckIn)]

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,61 @@
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
1414

15+
<#
16+
.SYNOPSIS
17+
Tests setting and getting auditing policy with classic storage
18+
#>
19+
function Test-AuditingUpdatePolicyWithClassicStorage
20+
{
21+
# Setup
22+
$testSuffix = 1015636
23+
Create-AuditingClassicTestEnvironment $testSuffix
24+
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix
25+
26+
try
27+
{
28+
# Test - Table database Auditing
29+
Set-AzureRmSqlDatabaseAuditingPolicy -AuditType Table -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName -StorageAccountName $params.storageAccount
30+
$policy = Get-AzureRmSqlDatabaseAuditingPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
31+
32+
# Assert
33+
Assert-AreEqual $policy.StorageAccountName $params.storageAccount
34+
Assert-AreEqual $policy.AuditState "Enabled"
35+
Assert-AreEqual $policy.UseServerDefault "Disabled"
36+
37+
# Test - Table server Auditing
38+
Set-AzureRmSqlServerAuditingPolicy -AuditType Table -ResourceGroupName $params.rgname -ServerName $params.serverName -StorageAccountName $params.storageAccount
39+
$policy = Get-AzureRmSqlServerAuditingPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName
40+
41+
# Assert
42+
Assert-AreEqual $policy.StorageAccountName $params.storageAccount
43+
Assert-AreEqual $policy.AuditState "Enabled"
44+
45+
46+
# Test - Blob database Auditing
47+
Set-AzureRmSqlDatabaseAuditingPolicy -AuditType Blob -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName -StorageAccountName $params.storageAccount
48+
$policy = Get-AzureRmSqlDatabaseAuditingPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
49+
50+
# Assert
51+
Assert-AreEqual $policy.StorageAccountName $params.storageAccount
52+
Assert-AreEqual $policy.AuditState "Enabled"
53+
Assert-AreEqual $policy.UseServerDefault "Disabled"
54+
55+
# Test - Blob server Auditing
56+
Set-AzureRmSqlServerAuditingPolicy -AuditType Blob -ResourceGroupName $params.rgname -ServerName $params.serverName -StorageAccountName $params.storageAccount
57+
$policy = Get-AzureRmSqlServerAuditingPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName
58+
59+
# Assert
60+
Assert-AreEqual $policy.StorageAccountName $params.storageAccount
61+
Assert-AreEqual $policy.AuditState "Enabled"
62+
}
63+
finally
64+
{
65+
# Cleanup
66+
Remove-AuditingTestEnvironment $testSuffix
67+
}
68+
}
69+
1570
<#
1671
.SYNOPSIS
1772
Tests that when setting the storage account property's value in a database's auditing policy, that value is later fetched properly

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ Creates the test environment needed to perform the Sql auditing tests
6666
function Create-AuditingTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0")
6767
{
6868
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix
69-
Create-TestEnvironmentWithParams $params $location $serverVersion
69+
Create-TestEnvironmentWithParams $params $location $serverVersion
70+
}
71+
72+
<#
73+
.SYNOPSIS
74+
Creates the test environment needed to perform the Sql auditing tests with classic storage
75+
#>
76+
function Create-AuditingClassicTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0")
77+
{
78+
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix
79+
Create-ClassicTestEnvironmentWithParams $params $location $serverVersion
7080
}
7181

7282
<#
@@ -76,7 +86,17 @@ Creates the test environment needed to perform the Sql threat detecion tests
7686
function Create-ThreatDetectionTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0")
7787
{
7888
$params = Get-SqlThreatDetectionTestEnvironmentParameters $testSuffix
79-
Create-TestEnvironmentWithParams $params $location $serverVersion
89+
Create-TestEnvironmentWithParams $params $location $serverVersion
90+
}
91+
92+
<#
93+
.SYNOPSIS
94+
Creates the test environment needed to perform the Sql threat detecion tests with classic storage
95+
#>
96+
function Create-ThreatDetectionClassicTestEnvironment ($testSuffix, $location = "West Central US", $serverVersion = "12.0")
97+
{
98+
$params = Get-SqlThreatDetectionTestEnvironmentParameters $testSuffix
99+
Create-ClassicTestEnvironmentWithParams $params $location $serverVersion
80100
}
81101

82102
<#
@@ -85,16 +105,33 @@ Creates the test environment needed to perform the Sql auditing tests
85105
#>
86106
function Create-TestEnvironmentWithParams ($params, $location, $serverVersion)
87107
{
88-
New-AzureRmResourceGroup -Name $params.rgname -Location $location
108+
Create-BasicTestEnvironmentWithParams $params $location $serverVersion
109+
New-AzureRmStorageAccount -StorageAccountName $params.storageAccount -ResourceGroupName $params.rgname -Location $location -Type Standard_GRS
110+
}
89111

90-
New-AzureRmStorageAccount -StorageAccountName $params.storageAccount -ResourceGroupName $params.rgname -Location $location -Type Standard_GRS
91-
112+
<#
113+
.SYNOPSIS
114+
Creates the test environment needed to perform the Sql auditing tests
115+
#>
116+
function Create-ClassicTestEnvironmentWithParams ($params, $location, $serverVersion)
117+
{
118+
Create-BasicTestEnvironmentWithParams $params $location $serverVersion
119+
New-AzureRmResource -ResourceName $params.storageAccount -ResourceGroupName $params.rgname -ResourceType "Microsoft.ClassicStorage/StorageAccounts" -Location $location -Properties @{ AccountType = "Standard_GRS" } -ApiVersion "2014-06-01" -Force
120+
}
121+
122+
<#
123+
.SYNOPSIS
124+
Creates the basic test environment needed to perform the Sql data security tests - resource group, server and database
125+
#>
126+
function Create-BasicTestEnvironmentWithParams ($params, $location, $serverVersion)
127+
{
128+
New-AzureRmResourceGroup -Name $params.rgname -Location $location
92129
$serverName = $params.serverName
93130
$serverLogin = "testusername"
94131
$serverPassword = "t357ingP@s5w0rd!Sec"
95132
$credentials = new-object System.Management.Automation.PSCredential($serverLogin, ($serverPassword | ConvertTo-SecureString -asPlainText -Force))
96-
New-AzureRmSqlServer -ResourceGroupName $params.rgname -ServerName $params.serverName -Location $location -ServerVersion $serverVersion -SqlAdministratorCredentials $credentials
97-
New-AzureRmSqlDatabase -DatabaseName $params.databaseName -ResourceGroupName $params.rgname -ServerName $params.serverName -Edition Basic
133+
New-AzureRmSqlServer -ResourceGroupName $params.rgname -ServerName $params.serverName -Location $location -ServerVersion $serverVersion -SqlAdministratorCredentials $credentials
134+
New-AzureRmSqlDatabase -DatabaseName $params.databaseName -ResourceGroupName $params.rgname -ServerName $params.serverName -Edition Basic
98135
}
99136

100137
<#

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public ThreatDetectionTests(ITestOutputHelper output)
4141
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
4242
}
4343

44+
[Fact]
45+
[Trait(Category.AcceptanceType, Category.CheckIn)]
46+
public void ThreatDetectionUpdatePolicyWithClassicStorage()
47+
{
48+
RunPowerShellTest("Test-ThreatDetectionUpdatePolicyWithClassicStorage");
49+
}
50+
4451
[Fact]
4552
[Trait(Category.AcceptanceType, Category.CheckIn)]
4653
public void ThreatDetectionGetDefualtPolicy()

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,49 @@
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
1414

15+
<#
16+
.SYNOPSIS
17+
Tests setting and getting threat detection policy with classic storage
18+
#>
19+
function Test-ThreatDetectionUpdatePolicyWithClassicStorage
20+
{
21+
# Setup
22+
$testSuffix = 4996
23+
Create-ThreatDetectionClassicTestEnvironment $testSuffix
24+
$params = Get-SqlAuditingTestEnvironmentParameters $testSuffix
25+
26+
try
27+
{
28+
# Test - database poloicy
29+
Set-AzureRmSqlDatabaseThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName -NotificationRecipientsEmails "[email protected]" -EmailAdmins $false -ExcludedDetectionType "Sql_Injection_Vulnerability" -StorageAccountName $params.storageAccount
30+
$policy = Get-AzureRmSqlDatabaseThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
31+
32+
# Assert
33+
Assert-AreEqual $policy.ThreatDetectionState "Enabled"
34+
Assert-AreEqual $policy.NotificationRecipientsEmails "[email protected]"
35+
Assert-AreEqual $policy.StorageAccountName $params.storageAccount
36+
Assert-False {$policy.EmailAdmins}
37+
Assert-AreEqual $policy.ExcludedDetectionTypes.Length 1
38+
Assert-True {$policy.ExcludedDetectionTypes.Contains([Microsoft.Azure.Commands.Sql.ThreatDetection.Model.DetectionType]::Sql_Injection_Vulnerability)}
39+
40+
41+
# Test - server poloicy
42+
Set-AzureRmSqlServerThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName -NotificationRecipientsEmails "[email protected]" -EmailAdmins $false -ExcludedDetectionType Sql_Injection_Vulnerability -StorageAccountName $params.storageAccount
43+
$policy = Get-AzureRmSqlServerThreatDetectionPolicy -ResourceGroupName $params.rgname -ServerName $params.serverName
44+
45+
# Assert
46+
Assert-AreEqual $policy.ThreatDetectionState "Enabled"
47+
Assert-AreEqual $policy.NotificationRecipientsEmails "[email protected]"
48+
Assert-False {$policy.EmailAdmins}
49+
Assert-AreEqual $policy.ExcludedDetectionTypes.Length 1
50+
Assert-True {$policy.ExcludedDetectionTypes.Contains([Microsoft.Azure.Commands.Sql.ThreatDetection.Model.DetectionType]::Sql_Injection_Vulnerability)}
51+
}
52+
finally
53+
{
54+
# Cleanup
55+
Remove-ThreatDetectionTestEnvironment $testSuffix
56+
}
57+
}
1558

1659
<#
1760
.SYNOPSIS

src/ResourceManager/Sql/Commands.Sql/Common/AzureEndpointsCommunicator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task<Dictionary<StorageKeyKind, string>> GetStorageKeysAsync(string
8585
{
8686
SqlManagementClient client = GetCurrentSqlClient("none");
8787

88-
string url = Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement).ToString();
88+
string url = Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager).ToString();
8989
if (!url.EndsWith("/"))
9090
{
9191
url = url + "/";

0 commit comments

Comments
 (0)