Issue creating App Gateway using New-AzApplicationGateway with invalid certificate data

Stan Spotts 15 Reputation points
2023-12-23T01:15:26.1633333+00:00

I am trying to create an app gateway using New-AzApplicationGateway but keep getting an error that says "Data for certificate /subscriptions.../trustedRootCertificates/allowlistcert1 is invalid." I have two .cer files; one from my network admin and another downloaded from AKV from a .pxm file. I am using the following PowerShell script:

$trustedRootCert = New-AzApplicationGatewayTrustedRootCertificate -Name "allowlistcert1" -CertificateFile '.\wildcard.cer'

If I use the certificate from my admin, the command completes successfully, but if I use the other certificate, I receive an error that states "Cannot find the requested object." The only difference I see between the two certificates is that the one giving me an error does not have the "-----BEGIN CERTIFICATE-----" and "----END CERTIFICATE-----" lines, and it's one long line, while the other includes the lines and has line breaks every 64 characters.

I have set up everything documented here: https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-integrate-internal-vnet-appgateway. I execute the following setup:

$trustedRootCert = New-AzApplicationGatewayTrustedRootCertificate -Name "allowlistcert1" -CertificateFile $trustedRootCertCerPath  

# configure HTTP backend settings for app gateway 
$apimPoolGatewaySetting = New-AzApplicationGatewayBackendHttpSettings -Name "apimPoolGatewaySetting" `
   -Port 443 -Protocol "Https" -CookieBasedAffinity "Disabled" -Probe $apimGatewayProbe `
   -TrustedRootCertificate $trustedRootCert -PickHostNameFromBackendAddress -RequestTimeout 180

$apimPoolPortalSetting = New-AzApplicationGatewayBackendHttpSettings -Name "apimPoolPortalSetting" `
   -Port 443 -Protocol "Https" -CookieBasedAffinity "Disabled" -Probe $apimPortalProbe `
   -TrustedRootCertificate $trustedRootCert -PickHostNameFromBackendAddress -RequestTimeout 180

$apimPoolManagementSetting = New-AzApplicationGatewayBackendHttpSettings -Name "apimPoolManagementSetting" `
   -Port 443 -Protocol "Https" -CookieBasedAffinity "Disabled" -Probe $apimManagementProbe `
   -TrustedRootCertificate $trustedRootCert -PickHostNameFromBackendAddress -RequestTimeout 180

# Configure backend IP address pool for each APIM endpoint 

$apimGatewayBackendPool = New-AzApplicationGatewayBackendAddressPool -Name "gatewaybackend" `
   -BackendFqdns $gatewayHostname 

$apimPortalBackendPool = New-AzApplicationGatewayBackendAddressPool -Name "portalbackend" `
   -BackendFqdns $portalHostname 

$apimManagementBackendPool = New-AzApplicationGatewayBackendAddressPool -Name "managementbackend" `
   -BackendFqdns $managementHostname    

# Create basic routing rules for app gateway 
$gatewayRule = New-AzApplicationGatewayRequestRoutingRule -Name "gatewayrule" `
   -RuleType Basic -HttpListener $gatewayListener -BackendAddressPool $apimGatewayBackendPool `
   -BackendHttpSettings $apimPoolGatewaySetting -Priority 10 

$portalRule = New-AzApplicationGatewayRequestRoutingRule -Name "portalrule" `
   -RuleType Basic -HttpListener $portalListener -BackendAddressPool $apimPortalBackendPool `
   -BackendHttpSettings $apimPoolPortalSetting -Priority 20 

$managementRule = New-AzApplicationGatewayRequestRoutingRule -Name "managementrule" `
   -RuleType Basic -HttpListener $managementListener -BackendAddressPool $apimManagementBackendPool `
   -BackendHttpSettings $apimPoolManagementSetting -Priority 30

# configure number of instances and tier for app gateway 
$sku = New-AzApplicationGatewaySku -Name "WAF_v2" -Tier "WAF_v2" -Capacity 2  

# configure the WAF mode 
$config = New-AzApplicationGatewayWebApplicationFirewallConfiguration -Enabled $true -FirewallMode "Prevention"  

# Set TLS 2.0 policy 
$policy = New-AzApplicationGatewaySslPolicy -PolicyType Predefined -PolicyName AppGwSslPolicy20220101

This complete without errors, but when I attempt to create the App Gateway using the code below, I receive the invalid certificate data error message.

$appgw = New-AzApplicationGateway -Name $appgwName -ResourceGroupName $resGroupName -Location $location `
   -BackendAddressPools $apimGatewayBackendPool,$apimPortalBackendPool,$apimManagementBackendPool `
   -BackendHttpSettingsCollection $apimPoolGatewaySetting, $apimPoolPortalSetting, $apimPoolManagementSetting `
   -FrontendIpConfigurations $fipconfig01 -GatewayIpConfigurations $gipconfig -FrontendPorts $fp01 `
   -HttpListeners $gatewayListener,$portalListener,$managementListener `
   -RequestRoutingRules $gatewayRule,$portalRule,$managementRule `
   -Sku $sku -WebApplicationFirewallConfig $config -SslCertificates  $certGateway, $certPortal, $certManagement `
   -TrustedRootCertificate $trustedRootCert -Probes $apimGatewayProbe, $apimPortalProbe, $apimManagementProbe `
   -SslPolicy $policy

When I look at $trustedRootCert, it shows that the cert data seems valid.

$trustedRootCert | fl

Data              : MIIG+jCCBeKgAwIBAgIQCQKKo5Ed9TuX+jxfNIneKDA (the rest of the cert data, 
						zU=
ProvisioningState :
Type              :
Name              : allowlistcert1
Etag              :
Id                : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ResourceGroupNotSet/providers/Microsoft.Network/applicationGateways/ApplicationGatewayNameNotSet/trustedRootCertificates/allowlistcert1

I have tried various certificates but haven't had any success. The certificate is a wildcard cert used for SSL on web sites. Can anyone help me resolve this issue?

Azure Application Gateway
Azure Application Gateway
An Azure service that provides a platform-managed, scalable, and highly available application delivery controller as a service.
1,213 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.