Skip to content

Commit 21719e6

Browse files
ariklinArik RiklinVeryEarly
authored
Introducing Az.Security AllowedConnection cmdlets (#12233)
* Introducing SecurityTopologies cmdlets * Update ChangeLog.md * Update ChangeLog.md * Change cmdlet name to Get-AzSecurityTopology * remove AllowedConnections * change name * fix conflicy * fix conflict * Update ChangeLog.md * Introducing Az.Security AllowedConnections cmdlets * update change.log * remove SecurityTopology * fix version * Fix comments * fix location * fix comment * fix comment * fix version number Co-authored-by: Arik Riklin <[email protected]> Co-authored-by: Yabo Hu <[email protected]>
1 parent 9a4af9d commit 21719e6

File tree

13 files changed

+901
-2
lines changed

13 files changed

+901
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.Azure.Commands.ScenarioTest;
2+
using Microsoft.Azure.ServiceManagement.Common.Models;
3+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
4+
using Xunit;
5+
6+
namespace Microsoft.Azure.Commands.Security.Test.ScenarioTests
7+
{
8+
public class AllowedConnectionTests
9+
{
10+
private readonly XunitTracingInterceptor _logger;
11+
12+
public AllowedConnectionTests (Xunit.Abstractions.ITestOutputHelper output)
13+
{
14+
_logger = new XunitTracingInterceptor(output);
15+
XunitTracingInterceptor.AddToContext(_logger);
16+
TestExecutionHelpers.SetUpSessionAndProfile();
17+
}
18+
19+
[Fact]
20+
[Trait(Category.AcceptanceType, Category.CheckIn)]
21+
public void GetSubscriptionScope()
22+
{
23+
TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzureRmAllowedConnection-SubscriptionScope");
24+
}
25+
26+
[Fact]
27+
[Trait(Category.AcceptanceType, Category.CheckIn)]
28+
public void GetResourceGroupLevelResource()
29+
{
30+
TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzureRmAllowedConnection-ResourceGroupLevelResource");
31+
}
32+
33+
[Fact]
34+
[Trait(Category.AcceptanceType, Category.CheckIn)]
35+
public void GetResourceId()
36+
{
37+
TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzureRmAllowedConnection-ResourceId");
38+
}
39+
}
40+
}
41+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 Allowed Connections on a subscription scope
18+
#>
19+
function Get-AzureRmAllowedConnection-SubscriptionScope
20+
{
21+
$AllowedConnection = Get-AzAllowedConnection
22+
Validate-AllowedConnection $AllowedConnection
23+
}
24+
25+
<#
26+
.SYNOPSIS
27+
Get Allowed Connections
28+
#>
29+
function Get-AzureRmAllowedConnection-ResourceGroupLevelResource
30+
{
31+
$AllowedConnection = Get-AzAllowedConnection | Select -First 1
32+
$rgName = Extract-ResourceGroup -ResourceId $AllowedConnection.Id
33+
$location = Extract-ResourceLocation -ResourceId $AllowedConnection.Id
34+
35+
$fetchedAllowedConnection = Get-AzAllowedConnection -ResourceGroupName $rgName -Location $location -Name $AllowedConnection.Name
36+
Validate-AllowedConnection $fetchedAllowedConnection
37+
}
38+
39+
<#
40+
.SYNOPSIS
41+
Get Allowed Connections by a resource ID
42+
#>
43+
function Get-AzureRmAllowedConnection-ResourceId
44+
{
45+
$AllowedConnection = Get-AzAllowedConnection | Select -First 1
46+
47+
$AllowedConnection = Get-AzAllowedConnection -ResourceId $AllowedConnection.Id
48+
Validate-AllowedConnection $AllowedConnection
49+
}
50+
51+
<#
52+
.SYNOPSIS
53+
Validates a list of Allowed Connections
54+
#>
55+
function Validate-AllowedConnection
56+
{
57+
param($AllowedConnection)
58+
59+
Assert-True { $AllowedConnection.Count -gt 0 }
60+
61+
Foreach($AllowedConnection in $AllowedConnection)
62+
{
63+
Validate-AllowedConnection $AllowedConnection
64+
}
65+
}
66+
67+
<#
68+
.SYNOPSIS
69+
Validates a single Allowed Connection
70+
#>
71+
function Validate-AllowedConnection
72+
{
73+
param($AllowedConnection)
74+
75+
Assert-NotNull $AllowedConnection
76+
}

src/Security/Security.Test/SessionRecords/Microsoft.Azure.Commands.Security.Test.ScenarioTests.AllowedConnectionTests/GetResourceGroupLevelResource.json

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

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

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

src/Security/Security.Test/SessionRecords/Microsoft.Azure.Commands.Security.Test.ScenarioTests.AllowedConnectionTests/GetSubscriptionScope.json

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

src/Security/Security/Az.Security.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ CmdletsToExport = 'Get-AzSecurityAlert', 'Set-AzSecurityAlert',
114114
'Set-AzSecurityAssessmentMetadata',
115115
'Remove-AzSecurityAssessmentMetadata',
116116
'Get-AzSecuritySubAssessment',
117-
'Get-AzSecurityTopology'
118-
117+
'Get-AzSecurityTopology',
118+
'Get-AzAllowedConnection'
119119

120120
# Variables to export from this module
121121
# VariablesToExport = @()

src/Security/Security/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added new cmdlet: 'Get-AzAllowedConnection'
2122
* Added new cmdlet: 'Get-AzSecurityTopology'
2223

24+
2325
## Version 0.7.9
2426
* Add new cmdlets: 'Get-AzSecurityAssessment',
2527
'Set-AzSecurityAssessment',
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
using System.Linq;
15+
using System.Management.Automation;
16+
using Commands.Security;
17+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
18+
using Microsoft.Azure.Commands.Security.Common;
19+
using Microsoft.Azure.Commands.Security.Models.AllowedConnection;
20+
using Microsoft.Azure.Commands.SecurityCenter.Common;
21+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
22+
using Microsoft.Rest.Azure;
23+
24+
namespace Microsoft.Azure.Commands.Security.Cmdlets.AllowedConnection
25+
{
26+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "AllowedConnection", DefaultParameterSetName = ParameterSetNames.SubscriptionScope), OutputType(typeof(PSSecurityAllowedConnection))]
27+
public class AllowedConnection : SecurityCenterCmdletBase
28+
{
29+
[Parameter(ParameterSetName = ParameterSetNames.ResourceGroupLevelResource, Mandatory = true, HelpMessage = ParameterHelpMessages.ResourceGroupName)]
30+
[ValidateNotNullOrEmpty]
31+
public string ResourceGroupName { get; set; }
32+
33+
[Parameter(ParameterSetName = ParameterSetNames.ResourceGroupLevelResource, Mandatory = true, HelpMessage = ParameterHelpMessages.ResourceName)]
34+
[ValidateNotNullOrEmpty]
35+
public string Name { get; set; }
36+
37+
[Parameter(ParameterSetName = ParameterSetNames.ResourceGroupLevelResource, Mandatory = true, HelpMessage = ParameterHelpMessages.Location)]
38+
[ValidateNotNullOrEmpty]
39+
[LocationCompleter("Microsoft.Security/allowedConnections")]
40+
public string Location { get; set; }
41+
42+
[Parameter(ParameterSetName = ParameterSetNames.ResourceId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = ParameterHelpMessages.ResourceId)]
43+
[ValidateNotNullOrEmpty]
44+
public string ResourceId { get; set; }
45+
46+
public override void ExecuteCmdlet()
47+
{
48+
switch (ParameterSetName)
49+
{
50+
case ParameterSetNames.SubscriptionScope:
51+
var tors = SecurityCenterClient.AllowedConnections.ListWithHttpMessagesAsync().GetAwaiter().GetResult().Body;
52+
WriteObject(tors.ConvertToPSType(), enumerateCollection: true);
53+
break;
54+
case ParameterSetNames.ResourceGroupLevelResource:
55+
SecurityCenterClient.AscLocation = Location;
56+
57+
var tor = SecurityCenterClient.AllowedConnections.GetWithHttpMessagesAsync(ResourceGroupName, Name).GetAwaiter().GetResult().Body;
58+
WriteObject(tor.ConvertToPSType(), enumerateCollection: false);
59+
break;
60+
case ParameterSetNames.ResourceId:
61+
SecurityCenterClient.AscLocation = AzureIdUtilities.GetResourceLocation(ResourceId);
62+
63+
tor = SecurityCenterClient.AllowedConnections.GetWithHttpMessagesAsync(AzureIdUtilities.GetResourceGroup(ResourceId), AzureIdUtilities.GetResourceName(ResourceId)).GetAwaiter().GetResult().Body;
64+
WriteObject(tor.ConvertToPSType(), enumerateCollection: false);
65+
break;
66+
default:
67+
throw new PSInvalidOperationException();
68+
}
69+
}
70+
}
71+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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;
16+
using System.Collections.Generic;
17+
18+
namespace Microsoft.Azure.Commands.Security.Models.AllowedConnection
19+
{
20+
public class PSSecurityAllowedConnection
21+
{
22+
public string Id { get; set; }
23+
24+
public string Name { get; set; }
25+
26+
public string Type { get; set; }
27+
28+
public DateTime? CalculatedDateTime { get; set; }
29+
30+
public IList<PSSecurityConnectableResources> ConnectableResources { get; set; }
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Commands.Security.Models.AllowedConnection
6+
{
7+
public class PSSecurityConnectableResources
8+
{
9+
/// <summary>
10+
/// Gets azure resource id
11+
/// </summary>
12+
public string Id { get; set; }
13+
14+
/// <summary>
15+
/// Gets The list of Azure resources that the resource has inbound allowed connection from
16+
/// </summary>
17+
public IList<PSSecurityConnectedResource> InboundConnectedResources { get; set; }
18+
19+
/// <summary>
20+
/// The list of Azure resources that the resource has outbound allowed connection to
21+
/// </summary>
22+
public IList<PSSecurityConnectedResource> OutboundConnectedResources { get; set; }
23+
24+
}
25+
}

0 commit comments

Comments
 (0)