Skip to content

Commit c18b690

Browse files
author
Maddie Clayton
authored
Merge pull request Azure#8523 from shblum/master
Support Advanced Threat Protection policy management
2 parents c0d2884 + 57a2dbd commit c18b690

File tree

15 files changed

+1041
-14
lines changed

15 files changed

+1041
-14
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.ScenarioTest;
16+
using Microsoft.Azure.ServiceManagement.Common.Models;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using Xunit;
19+
20+
namespace Microsoft.Azure.Commands.Security.Test.ScenarioTests
21+
{
22+
public class SecurityAdvancedThreatProtectionTests
23+
{
24+
private readonly XunitTracingInterceptor _logger;
25+
26+
public SecurityAdvancedThreatProtectionTests(Xunit.Abstractions.ITestOutputHelper output)
27+
{
28+
_logger = new XunitTracingInterceptor(output);
29+
XunitTracingInterceptor.AddToContext(_logger);
30+
TestExecutionHelpers.SetUpSessionAndProfile();
31+
}
32+
33+
[Fact]
34+
[Trait(Category.AcceptanceType, Category.CheckIn)]
35+
public void GetResourceId()
36+
{
37+
TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzSecurityThreatProtection-ResourceId");
38+
}
39+
40+
}
41+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Get a security contact by resource ID
18+
#>
19+
function Test-AzSecurityThreatProtection-ResourceId
20+
{
21+
# Setup
22+
$testPrefix = "psstorage"
23+
$testParams = Get-AdvancedThreatProtectionTestEnvironmentParameters $testPrefix
24+
$resourceId = "/subscriptions/" + $testParams.subscriptionId + "/resourceGroups/" + $testParams.rgName + "/providers/Microsoft.Storage/storageAccounts/" + $testParams.accountName
25+
Create-TestEnvironmentWithParams $testParams
26+
27+
#Enable
28+
$policy = Set-AzSecurityThreatProtection -ResourceId $resourceId -Enable
29+
$fetchedPolicy = Get-AzSecurityThreatProtection -ResourceId $resourceId
30+
Assert-AreEqual $policy.IsEnabled $True
31+
Assert-AreEqual $True $fetchedPolicy.IsEnabled
32+
33+
#Disable
34+
$policy = Set-AzSecurityThreatProtection -ResourceId $resourceId -Disable
35+
$fetchedPolicy = Get-AzSecurityThreatProtection -ResourceId $resourceId
36+
Assert-AreEqual $policy.IsEnabled $False
37+
Assert-AreEqual $False $fetchedPolicy.IsEnabled
38+
}
39+
40+
<#
41+
.SYNOPSIS
42+
Gets the values of the parameters used at the tests
43+
#>
44+
function Get-AdvancedThreatProtectionTestEnvironmentParameters ($testPrefix)
45+
{
46+
return @{ subscriptionId = (Get-AzContext).Subscription.Id;
47+
rgName = getAssetName ($testPrefix);
48+
accountName = getAssetName ($testPrefix);
49+
storageSku = "Standard_GRS";
50+
location = Get-Location "Microsoft.Resources" "resourceGroups" "West US"
51+
}
52+
}
53+
54+
<#
55+
.SYNOPSIS
56+
Creates the basic test environment needed to perform the threat protection tests - resource group and storage account
57+
#>
58+
function Create-TestEnvironmentWithParams ($testParams)
59+
{
60+
# Create a new resource group.
61+
New-AzResourceGroup -Name $testParams.rgName -Location $testParams.location
62+
63+
# Create the storage account.
64+
$storageAccount = New-AzStorageAccount -ResourceGroupName $testParams.rgName -Name $testParams.accountName -Location $testParams.location -Type $testParams.storageSku
65+
}

src/Security/Security.Test/ScenarioTests/TestController.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.Collections.Generic;
17-
using System.Diagnostics;
18-
using System.IO;
19-
using System.Linq;
2015
using Microsoft.Azure.Commands.Common.Authentication;
16+
using Microsoft.Azure.Management.Internal.Resources;
2117
using Microsoft.Azure.Management.Security;
18+
using Microsoft.Azure.Management.Storage.Version2017_10_01;
2219
using Microsoft.Azure.Test.HttpRecorder;
2320
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2421
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2522
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
23+
using System;
24+
using System.Collections.Generic;
25+
using System.Diagnostics;
26+
using System.IO;
27+
using System.Linq;
2628

2729
namespace Microsoft.Azure.Commands.Security.Test.ScenarioTests
2830
{
2931
public class TestController : RMTestBase
3032
{
3133
private readonly EnvironmentSetupHelper _helper;
3234

33-
public SecurityCenterClient SecurityCenterClient { get; private set; }
34-
3535
public static TestController NewInstance => new TestController();
3636

3737
protected TestController()
@@ -63,21 +63,33 @@ public void RunPowerShellTest(ServiceManagement.Common.Models.XunitTracingInterc
6363
_helper.RMProfileModule,
6464
_helper.GetRMModulePath(@"AzureRM.Security.psd1"),
6565
"ScenarioTests\\Common.ps1",
66-
"ScenarioTests\\" + callingClassName + ".ps1");
66+
"ScenarioTests\\" + callingClassName + ".ps1",
67+
"AzureRM.Storage.ps1",
68+
"AzureRM.Resources.ps1");
6769

6870
_helper.RunPowerShellTest(scripts);
6971
}
7072
}
7173

7274
protected void SetupManagementClients(MockContext context)
7375
{
74-
SecurityCenterClient = GetSecurityCenterClient(context);
75-
_helper.SetupManagementClients(SecurityCenterClient);
76+
var resourcesClient = GetResourcesClient(context);
77+
var securityCenterClient = GetSecurityCenterClient(context);
78+
var storageClient = GetStorageManagementClient(context);
79+
_helper.SetupManagementClients(securityCenterClient, resourcesClient, storageClient);
7680
}
7781

7882
private static SecurityCenterClient GetSecurityCenterClient(MockContext context)
7983
{
8084
return context.GetServiceClient<SecurityCenterClient>(TestEnvironmentFactory.GetTestEnvironment());
8185
}
86+
private static ResourceManagementClient GetResourcesClient(MockContext context)
87+
{
88+
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
89+
}
90+
private static StorageManagementClient GetStorageManagementClient(MockContext context)
91+
{
92+
return context.GetServiceClient<StorageManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
93+
}
8294
}
8395
}

src/Security/Security.Test/Security.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.SecurityCenter" Version="0.10.0-preview" />
14+
<PackageReference Include="Microsoft.Azure.Management.SecurityCenter" Version="0.11.0-preview" />
1515
</ItemGroup>
1616

1717
</Project>

src/Security/Security.Test/SessionRecords/Microsoft.Azure.Commands.Security.Test.ScenarioTests.SecurityAdvancedThreatProtectionTests/GetResourceId.json

Lines changed: 535 additions & 0 deletions
Large diffs are not rendered by default.

src/Security/Security/Az.Security.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ CmdletsToExport = 'Get-AzSecurityAlert', 'Set-AzSecurityAlert',
8585
'Get-AzSecurityContact', 'Set-AzSecurityContact',
8686
'Remove-AzSecurityContact', 'Get-AzSecurityTask',
8787
'Get-AzSecurityWorkspaceSetting', 'Set-AzSecurityWorkspaceSetting',
88-
'Remove-AzSecurityWorkspaceSetting'
88+
'Remove-AzSecurityWorkspaceSetting',
89+
'Get-AzSecurityThreatProtection',
90+
'Set-AzSecurityThreatProtection'
8991

9092
# Variables to export from this module
9193
# VariablesToExport = @()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ------------------------------------
14+
15+
using Commands.Security;
16+
using Microsoft.Azure.Commands.Security.Common;
17+
using Microsoft.Azure.Commands.Security.Models.Locations;
18+
using System.Management.Automation;
19+
20+
namespace Microsoft.Azure.Commands.Security.Cmdlets.AdvancedThreatProtection
21+
{
22+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SecurityThreatProtection", DefaultParameterSetName = ParameterSetNames.ResourceId), OutputType(typeof(PSSecurityLocation))]
23+
public class GetThreatProtectionPolicy : SecurityCenterCmdletBase
24+
{
25+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = ParameterHelpMessages.ResourceId)]
26+
[ValidateNotNullOrEmpty]
27+
public string ResourceId { get; set; }
28+
public override void ExecuteCmdlet()
29+
{
30+
var result = SecurityCenterClient.AdvancedThreatProtection.GetWithHttpMessagesAsync(ResourceId).GetAwaiter().GetResult().Body;
31+
WriteObject(result, enumerateCollection: true);
32+
}
33+
}
34+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ------------------------------------
14+
15+
using System.Management.Automation;
16+
using Commands.Security;
17+
using Microsoft.Azure.Commands.Security.Common;
18+
using Microsoft.Azure.Commands.Security.Models.ThreatProtection;
19+
20+
21+
namespace Microsoft.Azure.Commands.Security.Cmdlets.AdvancedThreatProtection
22+
{
23+
[Cmdlet(VerbsCommon.Set, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SecurityThreatProtection", DefaultParameterSetName = ParameterSetNames.PolicyOn, SupportsShouldProcess = true), OutputType(typeof(PSThreatProtection))]
24+
public class SetThreatProtectionPolicy : SecurityCenterCmdletBase
25+
{
26+
[Parameter(ParameterSetName = ParameterSetNames.PolicyOn, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = ParameterHelpMessages.ResourceId)]
27+
[Parameter(ParameterSetName = ParameterSetNames.PolicyOff, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = ParameterHelpMessages.ResourceId)]
28+
[ValidateNotNullOrEmpty]
29+
public string ResourceId { get; set; }
30+
31+
[Parameter(ParameterSetName = ParameterSetNames.PolicyOn, Mandatory = true, HelpMessage = ParameterHelpMessages.Enable)]
32+
[ValidateNotNullOrEmpty]
33+
public SwitchParameter Enable { get; set; }
34+
35+
[Parameter(ParameterSetName = ParameterSetNames.PolicyOff, Mandatory = true, HelpMessage = ParameterHelpMessages.Disable)]
36+
[ValidateNotNullOrEmpty]
37+
public SwitchParameter Disable { get; set; }
38+
39+
public override void ExecuteCmdlet()
40+
{
41+
bool policy;
42+
43+
switch (ParameterSetName)
44+
{
45+
case ParameterSetNames.PolicyOn:
46+
policy = true;
47+
break;
48+
case ParameterSetNames.PolicyOff:
49+
policy = false;
50+
break;
51+
default:
52+
throw new PSInvalidOperationException();
53+
}
54+
55+
var result = SecurityCenterClient.AdvancedThreatProtection.CreateWithHttpMessagesAsync(ResourceId, policy).GetAwaiter().GetResult().Body;
56+
WriteObject(result, enumerateCollection: true);
57+
}
58+
}
59+
}

src/Security/Security/Common/ParameterHelpMessages.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,12 @@ public static class ParameterHelpMessages
6868
public const string VirutalMachines = "Virtual Machines.";
6969

7070
#endregion
71+
72+
#region Threat Detection Settings
73+
74+
public const string Disable = "Disables Threat Protection Policy";
75+
public const string Enable = "Enables Threat Protection Policy";
76+
77+
#endregion
7178
}
7279
}

src/Security/Security/Common/ParameterSetNames.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ public static class ParameterSetNames
2424
public const string ResourceGroupLevelResource = "ResourceGroupLevelResource";
2525
public const string ResourceId = "ResourceId";
2626
public const string InputObject = "InputObject";
27+
public const string PolicyOn = "PolicyOn";
28+
public const string PolicyOff = "PolicyOff";
2729
}
2830
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Security.Models.ThreatProtection
16+
{
17+
public class PSThreatProtection
18+
{
19+
public string Id { get; set; }
20+
21+
public string Name { get; set; }
22+
}
23+
}

src/Security/Security/Security.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.SecurityCenter" Version="0.10.0-preview" />
14+
<PackageReference Include="Microsoft.Azure.Management.SecurityCenter" Version="0.11.0-preview" />
1515
</ItemGroup>
1616

1717
</Project>

src/Security/Security/help/Az.Security.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
Module Name: Az.Security
33
Module Guid: 5e312bb4-9d3a-4c88-94c3-8e5bbb2e3da4
44
Download Help Link: https://docs.microsoft.com/en-us/powershell/module/az.security
@@ -41,6 +41,9 @@ Gets the pricing tier data for Azure Security Center for a scope.
4141
### [Get-AzSecurityTask](Get-AzSecurityTask.md)
4242
Gets the security tasks that Azure Security Center recommends you to do in order to strengthen your security posture.
4343

44+
### [Get-AzSecurityThreatProtection](Get-AzSecurityThreatProtection.md)
45+
Gets the threat protection policy for a storage account.
46+
4447
### [Get-AzSecurityWorkspaceSetting](Get-AzSecurityWorkspaceSetting.md)
4548
Gets the configured security workspace settings on a subscription.
4649

@@ -68,6 +71,9 @@ Updates a security contact for a subscription.
6871
### [Set-AzSecurityPricing](Set-AzSecurityPricing.md)
6972
Sets the pricing of Azure Security Center tier for a scope.
7073

74+
### [Set-AzSecurityThreatProtection](Set-AzSecurityThreatProtection.md)
75+
Sets the threat protection policy for a storage account.
76+
7177
### [Set-AzSecurityWorkspaceSetting](Set-AzSecurityWorkspaceSetting.md)
7278
Updates the workspace settings for the subscription.
7379

0 commit comments

Comments
 (0)