Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Advanced Threat Protection policy management #8523

Merged
merged 12 commits into from
Feb 15, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.ScenarioTest;
using Microsoft.Azure.ServiceManagement.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Security.Test.ScenarioTests
{
public class SecurityAdvancedThreatProtectionTests
{
private readonly XunitTracingInterceptor _logger;

public SecurityAdvancedThreatProtectionTests(Xunit.Abstractions.ITestOutputHelper output)
{
_logger = new XunitTracingInterceptor(output);
XunitTracingInterceptor.AddToContext(_logger);
TestExecutionHelpers.SetUpSessionAndProfile();
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetResourceId()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzSecurityThreatProtection-ResourceId");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------

<#
.SYNOPSIS
Get a security contact by resource ID
#>
function Test-AzSecurityThreatProtection-ResourceId
{
# Setup
$testSuffix = "pstest"
$testParams = Get-AdvancedThreatProtectionTestEnvironmentParameters $testSuffix
$resourceId = "/subscriptions/" + $testParams.subscriptionId + "/resourceGroups/" + $testParams.rgName + "/providers/Microsoft.Storage/storageAccounts/" + $testParams.accountName
Create-TestEnvironmentWithParams $testParams

#Enable
$policy = Set-AzSecurityThreatProtection -ResourceId $resourceId -Enable
$fetchedPolicy = Get-AzSecurityThreatProtection -ResourceId $resourceId
Assert-AreEqual $policy.IsEnabled $True
Assert-AreEqual $True $fetchedPolicy.IsEnabled

#Disable
$policy = Set-AzSecurityThreatProtection -ResourceId $resourceId -Disable
$fetchedPolicy = Get-AzSecurityThreatProtection -ResourceId $resourceId
Assert-AreEqual $policy.IsEnabled $False
Assert-AreEqual $False $fetchedPolicy.IsEnabled
}

<#
.SYNOPSIS
Gets the values of the parameters used at the tests
#>
function Get-AdvancedThreatProtectionTestEnvironmentParameters ($testSuffix)
{
return @{ subscriptionId = (Get-AzContext).Subscription.Id;
rgName = "storage-atp-cmdlet-test-rg" +$testSuffix;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "getAssetName" to add an element of randomness to your resource name generation.

accountName = "storage" +$testSuffix;
storageSku = "Standard_GRS";
location = "West Central US"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use GetLocation -ProviderNamespace <namespace> -ResourceType <resourcetype> -PreferredLocation "West Central US" to future proof the recording process.

}
}

<#
.SYNOPSIS
Creates the basic test environment needed to perform the threat protection tests - resource group and storage account
#>
function Create-TestEnvironmentWithParams ($testParams)
{
# Create a new resource group.
New-AzResourceGroup -Name $testParams.rgName -Location $testParams.location

# Create the storage account.
$storageAccount = New-AzStorageAccount -ResourceGroupName $testParams.rgName -AccountName $testParams.accountName -Location $testParams.location -SkuName $testParams.storageSku
}
32 changes: 22 additions & 10 deletions src/Security/Security.Test/ScenarioTests/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.Security;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace Microsoft.Azure.Commands.Security.Test.ScenarioTests
{
public class TestController : RMTestBase
{
private readonly EnvironmentSetupHelper _helper;

public SecurityCenterClient SecurityCenterClient { get; private set; }

public static TestController NewInstance => new TestController();

protected TestController()
Expand Down Expand Up @@ -63,21 +63,33 @@ public void RunPowerShellTest(ServiceManagement.Common.Models.XunitTracingInterc
_helper.RMProfileModule,
_helper.GetRMModulePath(@"AzureRM.Security.psd1"),
"ScenarioTests\\Common.ps1",
"ScenarioTests\\" + callingClassName + ".ps1");
"ScenarioTests\\" + callingClassName + ".ps1",
_helper.GetRMModulePath(@"AzureRM.Resources"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the Resources and Storage modules, please use our internal version of these cmdlets. This is just changing this import to the two ps1 files:

, and updating the client to the common version:
using Microsoft.Azure.Management.Storage.Version2017_10_01;
,
using Microsoft.Azure.Management.Internal.Resources;

_helper.GetRMModulePath(@"Az.Storage.psd1"));

_helper.RunPowerShellTest(scripts);
}
}

protected void SetupManagementClients(MockContext context)
{
SecurityCenterClient = GetSecurityCenterClient(context);
_helper.SetupManagementClients(SecurityCenterClient);
var resourcesClient = GetResourcesClient(context);
var securityCenterClient = GetSecurityCenterClient(context);
var storageClient = GetStorageManagementClient(context);
_helper.SetupManagementClients(securityCenterClient, resourcesClient, storageClient);
}

private static SecurityCenterClient GetSecurityCenterClient(MockContext context)
{
return context.GetServiceClient<SecurityCenterClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static ResourceManagementClient GetResourcesClient(MockContext context)
{
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static StorageManagementClient GetStorageManagementClient(MockContext context)
{
return context.GetServiceClient<StorageManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
}
}
3 changes: 2 additions & 1 deletion src/Security/Security.Test/Security.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.SecurityCenter" Version="0.10.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.SecurityCenter" Version="0.11.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="9.1.0-preview" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this once you update to the common client.

</ItemGroup>

</Project>
Loading