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

[EventHubs]: Adding cmdlets to support manual approval of private endpoints #18724

Merged
merged 10 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected EventHubTestRunner(ITestOutputHelper output)
helper.GetRMModulePath("AzureRM.EventHub.psd1"),
helper.GetRMModulePath("AzureRM.KeyVault.psd1"),
helper.GetRMModulePath("AzureRM.ManagedServiceIdentity.psd1"),
helper.GetRMModulePath("AzureRM.Network.psd1"),
})
.WithRecordMatcher(
(ignoreResourcesClient, resourceProviders, userAgentsToIgnore) =>
Expand Down
9 changes: 8 additions & 1 deletion src/EventHub/EventHub.Test/ScenarioTests/PaginationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PaginationTests(Xunit.Abstractions.ITestOutputHelper output)
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.CheckIn, Category.CheckIn)]
public void ApplicationGroupPaginationTests()
{
TestRunner.RunTestScript("ApplicationGroupPagination");
Expand All @@ -37,5 +37,12 @@ public void NamespacePaginationTests()
{
TestRunner.RunTestScript("NamespacePagination");
}

[Fact]
[Trait(Category.CheckIn, Category.CheckIn)]
public void PrivateEndpointPaginationTests()
{
TestRunner.RunTestScript("PrivateEndpointPagination");
}
}
}
14 changes: 14 additions & 0 deletions src/EventHub/EventHub.Test/ScenarioTests/PaginationTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ function ApplicationGroupPagination{
Assert-ThrowsContains { New-AzEventHubApplicationGroup -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name test -ClientAppGroupIdentifier SASKeyName=test -ThrottlingPolicyConfig $t1 } "Operation returned an invalid status code 'BadRequest'"
}

function PrivateEndpointPagination{
#Max allowed private endpoints : 120
$resourceGroupName = "ps-testing"
$namespaceName = "ps-pagination-testing"

$listOfPrivateEndpoints = Get-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName

Assert-AreEqual 120 $listOfPrivateEndpoints.Count

$namespace = Get-AzEventHubNamespace -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName

Assert-AreEqual 120 $namespace.PrivateEndpointConnections.Count
}

function NamespacePagination{
$resourceGroupName = "testpaginationforps"

Expand Down
34 changes: 34 additions & 0 deletions src/EventHub/EventHub.Test/ScenarioTests/PrivateEndpointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ----------------------------------------------------------------------------------
//
// 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.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.EventHub.Test.ScenarioTests
{
public class PrivateEndpointTests : EventHubTestRunner
{

public PrivateEndpointTests(Xunit.Abstractions.ITestOutputHelper output)
: base(output)
{
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void PrivateEndpointManualApproval()
{
TestRunner.RunTestScript("PrivateEndpointTest");
}
}
}
224 changes: 224 additions & 0 deletions src/EventHub/EventHub.Test/ScenarioTests/PrivateEndpointTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
# ----------------------------------------------------------------------------------
#
# 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
Tests EventHubs Private Endpoint Manual Approval Operations
#>

function WaitforStatetoBeSucceded
{
param([string]$resourceGroupName,[string]$namespaceName,[string]$privateEndpointName)

$createdPrivateEndpoint = Get-AzEventHubPrivateEndpointConnection -ResourceGroup $resourceGroupName -Namespace $namespaceName -Name $privateEndpointName

while($createdPrivateEndpoint.ProvisioningState -ne "Succeeded")
{
Wait-Seconds 10
$createdPrivateEndpoint = Get-AzEventHubPrivateEndpointConnection -ResourceGroup $resourceGroupName -Namespace $namespaceName -Name $privateEndpointName
}

return $createdPrivateEndpoint
}

function PrivateEndpointTest
{
# Setup
$location = Get-Location
$resourceGroupName = getAssetName "RSG-Private-Endpoint"
$namespaceName = getAssetName "Eventhub-Namespace-"
$peConnectionName1 = getAssetName "pe-connection-ns1-"
$peConnectionName2 = getAssetName "pe-connection-ns2-"
$peConnectionName3 = getAssetName "pe-connection-ns3-"
$peConnectionName4 = getAssetName "pe-connection-ns4-"
$peName1 = getAssetName "pe-Name1-"
$peName2 = getAssetName "pe-Name2-"
$peName3 = getAssetName "pe-Name3-"
$peName4 = getAssetName "pe-Name4-"
$vnetName1 = getAssetName "vnet-ns1-"
$vnetName2 = getAssetName "vnet-ns2-"
$subnetName1 = "frontendsubnet"
$subnetName2 = "backendsubnet"

try{

# Create ResourceGroup
Write-Debug " Create resource group"
Write-Debug "Resource group name : $resourceGroupName"
New-AzResourceGroup -Name $resourceGroupName -Location $location -Force

# Create EventHub Namespace
Write-Debug " Create new Eventhub namespace"
Write-Debug "Namespace name : $namespaceName"
$result = New-AzEventHubNamespace -ResourceGroup $resourceGroupName -Name $namespaceName -Location $location

$frontendSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName1 -AddressPrefix "10.0.1.0/24" ## Create frontend subnet
$backendSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName2 -AddressPrefix "10.0.2.0/24"
$virtualNetwork = New-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Location $location -Name $vnetName1 -AddressPrefix 10.0.0.0/16 -Subnet $frontendSubnet, $backendSubnet


$privateEndpointConnection = New-AzPrivateLinkServiceConnection -Name $peConnectionName1 -PrivateLinkServiceId $result.Id -GroupId "namespace"
$virtualNetwork = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $vnetName1
$subnet1 = $virtualNetwork | Select -ExpandProperty subnets | Where-Object {$_.Name -eq $subnetName1}
$privateEndpoint = New-AzPrivateEndpoint -ResourceGroupName $resourceGroupName -Name $peName1 -Location $location -Subnet $subnet1 -PrivateLinkServiceConnection $privateEndpointConnection -ByManualRequest -Force

$privateEndpointConnection = New-AzPrivateLinkServiceConnection -Name $peConnectionName2 -PrivateLinkServiceId $result.Id -GroupId "namespace" -RequestMessage "Hello"
$virtualNetwork = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $vnetName1
$subnet2 = $virtualNetwork | Select -ExpandProperty subnets | Where-Object {$_.Name -eq $subnetName2}
$privateEndpoint = New-AzPrivateEndpoint -ResourceGroupName $resourceGroupName -Name $peName2 -Location $location -Subnet $subnet2 -PrivateLinkServiceConnection $privateEndpointConnection -ByManualRequest -Force

$listOfPrivateEndpoints = Get-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName

Assert-AreEqual 2 $listOfPrivateEndpoints.Count

$privateEndpointId1 = $listOfPrivateEndpoints[0].Name
$descriptionId1 = $listOfPrivateEndpoints[0].Description

$privateEndpointId2 = $listOfPrivateEndpoints[1].Name
$descriptionId2 = $listOfPrivateEndpoints[1].Description

$privateEndpoint1 = Get-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId1
Assert-AreEqual $privateEndpoint1.ConnectionState "Pending"
Assert-AreEqual $privateEndpoint1.Description $descriptionId1

$privateEndpoint2 = Get-AzEventHubPrivateEndpointConnection -ResourceId $listOfPrivateEndpoints[1].Id
Assert-AreEqual $privateEndpoint2.ConnectionState "Pending"
Assert-AreEqual $privateEndpoint2.Description $descriptionId2

$privateEndpoint1 = Approve-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId1

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1

Assert-AreEqual $privateEndpoint1.ConnectionState "Approved"
Assert-AreEqual $privateEndpoint1.ProvisioningState "Succeeded"
Assert-AreEqual $privateEndpoint1.Description ""

$privateEndpoint2 = Get-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId2
Assert-AreEqual $privateEndpoint2.ConnectionState "Pending"
Assert-AreEqual $privateEndpoint2.Description $descriptionId2

$privateEndpoint2 = Approve-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint2.Id

$privateEndpoint2 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId2

Assert-AreEqual $privateEndpoint2.ConnectionState "Approved"
Assert-AreEqual $privateEndpoint2.Description ""

#
$privateEndpoint1 = Get-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint1.Id
Assert-AreEqual $privateEndpoint1.ConnectionState "Approved"

$privateEndpoint2 = Get-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint2.Id
Assert-AreEqual $privateEndpoint2.ConnectionState "Approved"
Assert-AreEqual $privateEndpoint2.Description ""

$privateEndpoint2 = Get-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint2.Id.ToLower()
Assert-AreEqual $privateEndpoint2.ConnectionState "Approved"
Assert-AreEqual $privateEndpoint2.Description ""

$privateEndpoint1 = Deny-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint1.Id
Assert-AreEqual $privateEndpoint1.ProvisioningState "Updating"
Assert-AreEqual $privateEndpoint1.ConnectionState "Rejected"

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.ConnectionState "Rejected"
Assert-AreEqual $privateEndpoint1.Description ""

$privateEndpoint2 = Deny-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId2
Assert-AreEqual "Updating" $privateEndpoint2.ProvisioningState
Assert-AreEqual "Rejected" $privateEndpoint2.ConnectionState

$privateEndpoint2 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId2
Assert-AreEqual $privateEndpoint2.ConnectionState "Rejected"
Assert-AreEqual $privateEndpoint2.Description ""

Remove-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint1.Id
Remove-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId2

Wait-Seconds 40

$listOfPrivateEndpoints = Get-AzEventHubPrivateEndpointConnection -ResourceId $result.Id
Assert-AreEqual 0 $listOfPrivateEndpoints.Count

$privateEndpointConnection = New-AzPrivateLinkServiceConnection -Name $peConnectionName3 -PrivateLinkServiceId $result.Id -GroupId "namespace"
$privateEndpoint = New-AzPrivateEndpoint -ResourceGroupName $resourceGroupName -Name $peName3 -Location $location -Subnet $subnet2 -PrivateLinkServiceConnection $privateEndpointConnection -ByManualRequest -Force

$listOfPrivateEndpoints = Get-AzEventHubPrivateEndpointConnection -ResourceId $result.Id
Assert-AreEqual 1 $listOfPrivateEndpoints.Count

$privateEndpointId1 = $listOfPrivateEndpoints[0].Name

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.ConnectionState "Pending"

Approve-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId1 -Description "Approving this connection"

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.Description "Approving this connection"
Assert-AreEqual $privateEndpoint1.ConnectionState "Approved"
Assert-AreEqual $privateEndpoint1.ProvisioningState "Succeeded"

Deny-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId1 -Description "Rejecting this connection"

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.Description "Rejecting this connection"
Assert-AreEqual $privateEndpoint1.ConnectionState "Rejected"
Assert-AreEqual $privateEndpoint1.ProvisioningState "Succeeded"

Remove-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId1
Wait-Seconds 30

$privateEndpointConnection = New-AzPrivateLinkServiceConnection -Name $peConnectionName4 -PrivateLinkServiceId $result.Id -GroupId "namespace"
$privateEndpoint = New-AzPrivateEndpoint -ResourceGroupName $resourceGroupName -Name $peName4 -Location $location -Subnet $subnet2 -PrivateLinkServiceConnection $privateEndpointConnection -ByManualRequest -Force

$listOfPrivateEndpoints = Get-AzEventHubPrivateEndpointConnection -ResourceId $result.Id
Assert-AreEqual 1 $listOfPrivateEndpoints.Count

$privateEndpointId1 = $listOfPrivateEndpoints[0].Name

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.ConnectionState "Pending"

Approve-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint1.Id -Description "Approving this connection"

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.Description "Approving this connection"
Assert-AreEqual $privateEndpoint1.ConnectionState "Approved"
Assert-AreEqual $privateEndpoint1.ProvisioningState "Succeeded"

Deny-AzEventHubPrivateEndpointConnection -ResourceId $privateEndpoint1.Id -Description "Rejecting this connection"

$privateEndpoint1 = WaitforStatetoBeSucceded $resourceGroupName $namespaceName $privateEndpointId1
Assert-AreEqual $privateEndpoint1.Description "Rejecting this connection"
Assert-AreEqual $privateEndpoint1.ConnectionState "Rejected"
Assert-AreEqual $privateEndpoint1.ProvisioningState "Succeeded"

$privateLink = Get-AzEventHubPrivateLink -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName

Assert-AreEqual "namespace" $privateLink.GroupId
Assert-AreEqual "namespace" $privateLink.Name
Assert-AreEqual "Microsoft.EventHub/namespaces/privateLinkResources" $privateLink.Type
Assert-AreEqual 1 $privateLink.RequiredMembers.Count
Assert-AreEqual 1 $privateLink.RequiredZoneNames.Count

Remove-AzEventHubPrivateEndpointConnection -ResourceGroupName $resourceGroupName -NamespaceName $namespaceName -Name $privateEndpointId1
Wait-Seconds 30

}
finally{
Write-Debug " Delete resourcegroup"
Remove-AzResourceGroup -Name $resourceGroupName -Force
}

}
Loading