@@ -974,3 +974,123 @@ function Test-VirtualNetworkGatewayConnectionGetIkeSa
974974 }
975975}
976976
977+ function Test-VirtualNetworkGatewayConnectionWithCertificateAuth
978+ {
979+ # Setup
980+ $rgname = Get-ResourceGroupName
981+ $vnetName = Get-ResourceName
982+ $localnetName = Get-ResourceName
983+ $vnetConnectionName = Get-ResourceName
984+ $vnetGatewayName = Get-ResourceName
985+ $publicIpName = Get-ResourceName
986+ $identityName = Get-ResourceName
987+ $vnetGatewayConfigName = Get-ResourceName
988+ $rglocation = Get-ProviderLocation ResourceManagement
989+ $resourceTypeParent = " Microsoft.Network/connections"
990+ $location = Get-ProviderLocation $resourceTypeParent
991+
992+ try
993+ {
994+
995+ $resourceGroup = New-AzResourceGroup - Name $rgname - Location $rglocation - Tags @ { testtag = " testval" }
996+
997+ # Create managed identity
998+ $identity = New-AzUserAssignedIdentity - ResourceGroupName $rgname - Name $identityName - Location $location
999+
1000+ $keyVaultName = " kv" + $rgname.Substring (0 , [Math ]::Min(15 , $rgname.Length ))
1001+ $keyVault = New-AzKeyVault - ResourceGroupName $rgname - VaultName $keyVaultName - Location $location - EnabledForDeployment - Sku Standard - DisableRbacAuthorization
1002+
1003+ # 2. Grant managed identity access to Key Vault certificates
1004+ Set-AzKeyVaultAccessPolicy - VaultName $keyVaultName - ObjectId $identity.PrincipalId - PermissionsToCertificates get, list - PermissionsToSecrets get, list
1005+
1006+ $currentUser = (Get-AzContext ).Account.Id
1007+ Set-AzKeyVaultAccessPolicy - VaultName $keyVaultName - UserPrincipalName $currentUser - PermissionsToCertificates get, list, create, delete, import
1008+
1009+ # 3. Import certificate
1010+ $certFilePath = " ./ScenarioTests/Data/VpnGatewayoutboundcert.pfx"
1011+ $certPassword = ConvertTo-SecureString - String " 12345" - Force - AsPlainText
1012+ Import-AzKeyVaultCertificate - VaultName $keyVaultName - Name " vpn-gateway-cert" `
1013+ - FilePath $certFilePath - Password $certPassword
1014+
1015+ # Create the Virtual Network
1016+ $subnet = New-AzVirtualNetworkSubnetConfig - Name " GatewaySubnet" - AddressPrefix 10.0 .0.0 / 24
1017+ $vnet = New-AzVirtualNetwork - Name $vnetName - ResourceGroupName $rgname - Location $location - AddressPrefix 10.0 .0.0 / 16 - Subnet $subnet
1018+ $vnet = Get-AzVirtualNetwork - Name $vnetName - ResourceGroupName $rgname
1019+ $subnet = Get-AzVirtualNetworkSubnetConfig - Name " GatewaySubnet" - VirtualNetwork $vnet
1020+
1021+ $publicip = New-AzPublicIpAddress - ResourceGroupName $rgname - name $publicIpName - location $location - AllocationMethod Static - DomainNameLabel $publicIpName
1022+
1023+ # Create VirtualNetworkGateway with managed identity
1024+ $vnetIpConfig = New-AzVirtualNetworkGatewayIpConfig - Name $vnetGatewayConfigName - PublicIpAddress $publicip - Subnet $subnet
1025+ $actual = New-AzVirtualNetworkGateway - ResourceGroupName $rgname - name $vnetGatewayName - location $location - IpConfigurations $vnetIpConfig - GatewayType Vpn - VpnType RouteBased - EnableBgp $false - GatewaySku VpnGw1 - UserAssignedIdentityId $identity.Id
1026+ $vnetGateway = Get-AzVirtualNetworkGateway - ResourceGroupName $rgname - name $vnetGatewayName
1027+
1028+ Assert-AreEqual " Succeeded" $vnetGateway.ProvisioningState
1029+ Assert-NotNull $vnetGateway.Identity
1030+
1031+ # Create LocalNetworkGateway
1032+ $localGateway = New-AzLocalNetworkGateway - ResourceGroupName $rgname - name $localnetName - location $location - AddressPrefix 192.168 .0.0 / 16 - GatewayIpAddress 192.168 .4.5
1033+
1034+ $cert = Get-AzKeyVaultCertificate - VaultName $keyVaultName - Name " vpn-gateway-cert"
1035+ $outboundCertUrl = $cert.Id
1036+ $certData = Get-AzKeyVaultCertificate - VaultName $keyVaultName - Name " vpn-gateway-cert"
1037+ $certBytes = [System.Convert ]::ToBase64String($certData.Certificate.RawData )
1038+ $subjectName = $certData.Certificate.Subject
1039+
1040+ $inboundCert1Path = " ./ScenarioTests/Data/VpnGatewayInboundCert.cer"
1041+ $inboundCert2Path = " ./ScenarioTests/Data/VpnGatewayAuthCert.cer"
1042+ $inboundCert1Data = Get-Content - Path $inboundCert1Path - Raw
1043+ $inboundCert2Data = Get-Content - Path $inboundCert2Path - Raw
1044+
1045+ # Remove PEM headers if present and get Base64 only
1046+ $inboundCert1Base64 = $inboundCert1Data -replace " -----BEGIN CERTIFICATE-----" , " " -replace " -----END CERTIFICATE-----" , " "
1047+ $inboundCert2Base64 = $inboundCert2Data -replace " -----BEGIN CERTIFICATE-----" , " " -replace " -----END CERTIFICATE-----" , " "
1048+ $certChain = @ ($inboundCert1Base64 , $inboundCert2Base64 )
1049+
1050+ $certAuth = New-AzVirtualNetworkGatewayCertificateAuthentication `
1051+ - OutboundAuthCertificate $outboundCertUrl `
1052+ - InboundAuthCertificateSubjectName $subjectName `
1053+ - InboundAuthCertificateChain $certChain
1054+
1055+ # Verify certificate authentication object properties
1056+ Assert-AreEqual $outboundCertUrl $certAuth.OutboundAuthCertificate
1057+ Assert-AreEqual $subjectName $certAuth.InboundAuthCertificateSubjectName
1058+ Assert-AreEqual 2 $certAuth.InboundAuthCertificateChain.Count
1059+ Assert-NotNull $certAuth.InboundAuthCertificateChain [0 ]
1060+ Assert-NotNull $certAuth.InboundAuthCertificateChain [1 ]
1061+
1062+ # Create VirtualNetworkGatewayConnection with Certificate Authentication
1063+ $actual = New-AzVirtualNetworkGatewayConnection - ResourceGroupName $rgname - name $vnetConnectionName - location $location - VirtualNetworkGateway1 $vnetGateway - LocalNetworkGateway2 $localGateway - ConnectionType IPsec - RoutingWeight 3 - AuthenticationType " Certificate" - CertificateAuthentication $certAuth
1064+
1065+ # Verify connection was created successfully
1066+ $connection = Get-AzVirtualNetworkGatewayConnection - ResourceGroupName $rgname - name $vnetConnectionName
1067+ Assert-AreEqual $connection.ResourceGroupName $actual.ResourceGroupName
1068+ Assert-AreEqual $connection.Name $actual.Name
1069+ Assert-AreEqual " Certificate" $connection.AuthenticationType
1070+ Assert-NotNull $connection.CertificateAuthentication
1071+ Assert-AreEqual $outboundCertUrl $connection.CertificateAuthentication.OutboundAuthCertificate
1072+ Assert-AreEqual $subjectName $connection.CertificateAuthentication.InboundAuthCertificateSubjectName
1073+ Assert-AreEqual 2 $connection.CertificateAuthentication.InboundAuthCertificateChain.Count
1074+
1075+ # Update with new certificate (just use same cert for test purposes)
1076+ $newCertAuth = New-AzVirtualNetworkGatewayCertificateAuthentication - OutboundAuthCertificate $outboundCertUrl - InboundAuthCertificateSubjectName $subjectName - InboundAuthCertificateChain $certChain
1077+
1078+ $updatedConnection = Set-AzVirtualNetworkGatewayConnection - VirtualNetworkGatewayConnection $connection - AuthenticationType " Certificate" - CertificateAuthentication $newCertAuth - Force
1079+
1080+ # Verify update
1081+ $verifyConnection = Get-AzVirtualNetworkGatewayConnection - ResourceGroupName $rgname - name $vnetConnectionName
1082+ Assert-AreEqual " Certificate" $verifyConnection.AuthenticationType
1083+ Assert-AreEqual $outboundCertUrl $verifyConnection.CertificateAuthentication.OutboundAuthCertificate
1084+ Assert-AreEqual $subjectName $verifyConnection.CertificateAuthentication.InboundAuthCertificateSubjectName
1085+
1086+ # List connections and verify
1087+ $list = Get-AzVirtualNetworkGatewayConnection - ResourceGroupName $rgname - Name " *"
1088+ Assert-True { $list.Count -ge 1 }
1089+
1090+ }
1091+ finally
1092+ {
1093+ # Cleanup
1094+ Clean - ResourceGroup $rgname
1095+ }
1096+ }
0 commit comments