スポンサーリンク

「(403) Forbidden」になる事象について

AppService にアクセス制限をかけると Releases Pipelines からのデプロイに「(403) Forbidden」でエラーとなるらしい。

Azure DevOpsのIPアドレスの登録を検討したが、下記ドキュメントに記載されているのは、

受信 IP アドレスとして使用されているものであり、Azure DevOps から外部に送信する際のIP アドレスは異なる。

Azure DevOps から送信する際のIP アドレスは、週次に動的で変更される可能性があるので登録は困難となる。

アクセス制限を解除する

いろいろ調べていると以下の Stack Overflow のページがヒットした。

It does not need additional restriction because url access already requires Microsoft credentials. If >restrictions are added, deploy will fail the firewall, hence the many complications I encountered.

scm.azurewebsites.net に制限をかけない方が良いらしい。
また、KudoサイトへはMicrosoft資格情報が必要なため制限をかける必要がないとのこと。

しかし、セキュリティ的な要件でアクセス制限をかけなければならない場合は、以下の方法で対応ができる。

スポンサーリンク

Self-hosted agentでデプロイを行う

VMでself-hosted agentを立て、VMからのアクセスを許可してデプロイを行う

環境構築が必要になるので、管理コストが掛かってしまう。

デプロイ時にアクセス制限を外す

無料で使える Microsoft-hosted でデプロイを検討したところ、以下の Stack Overflow のページに良い解決策が記載されていた。

デプロイ前にアクセス制限を削除して、デプロイ後にアクセス制限を設定する方法

少しコードに手直しを行い、以下のpipelineを作成して問題なくデプロイできました。

■ Releases Pipeliens

■ Azure PowerShell script: Remove SCM AccessRule:


variables: WebAppName: 'yantzn-test-webapps' ResourceGroupName: 'yantzn-test-webapps-rg' steps: - task: AzurePowerShell@3 displayName: 'Azure PowerShell script: Remove SCM AccessRule' inputs: azureSubscription: 'yantzn-test-webapps - Azure' ScriptType: InlineScript Inline: | $AppName = "$(WebAppName)" # デプロイスロットを指定する場合 #$slotName = "deploy" #$webSiteName = $AppName +"/"+ $slotName $webSiteName = $AppName $resourceGroupName = "$(ResourceGroupName)" $apiVersion = ((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Web).ResourceTypes | Where-Object ResourceTypeName -eq sites).ApiVersions $webAppConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/config -ResourceName $webSiteName -ResourceGroupName $resourceGroupName -apiVersion $apiVersion[0]) # デプロイスロットを指定する場合 #$webAppConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webSiteName -ResourceGroupName $resourceGroupName -apiVersion $apiVersion[0]) $webAppConfig.Properties.scmIpSecurityRestrictions = @() Set-AzureRmResource -ResourceId $webAppConfig.ResourceId -Properties $webAppConfig.Properties -apiVersion $apiVersion[0] -Force azurePowerShellVersion: LatestVersion

■ Azure PowerShell script: Add SCM AccessRule:


variables: WebAppName: 'yantzn-test-webapps' ResourceGroupName: 'yantzn-test-webapps-rg' steps: - task: AzurePowerShell@3 displayName: 'Azure PowerShell script: Add SCM AccessRule' inputs: azureSubscription: 'yantzn-test-webapps - Azure' ScriptType: InlineScript Inline: | $AppName = "$(WebAppName)" # デプロイスロットを指定する場合 #$slotName = "deploy" #$webSiteName = $AppName +"/"+ $slotName $webSiteName = $AppName $resourceGroupName = "$(ResourceGroupName)" [PSCustomObject] $scmRules = @( @{ipAddress="xxx.xxx.xxx.xxx/xx";action="Allow";tag="Default";priority="110";name="allow_01";description=""} ) $apiVersion = ((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Web).ResourceTypes | Where-Object ResourceTypeName -eq sites).ApiVersions $webAppConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/config -ResourceName $webSiteName -ResourceGroupName $resourceGroupName -apiVersion $apiVersion[0]) # デプロイスロットを指定する場合 #$webAppConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webSiteName -ResourceGroupName $resourceGroupName -apiVersion $apiVersion[0]) #$webAppConfig.Properties.scmIpSecurityRestrictions = @() foreach ($rules in $scmRules) { $restriction = @{} foreach ($key in $rules.Keys) { $restriction.Add($key,$rules[$key]) } $webAppConfig.Properties.scmIpSecurityRestrictions += $restriction Set-AzureRmResource -ResourceId $webAppConfig.ResourceId -PropertyObject $webAppConfig.Properties -apiVersion $apiVersion[0] -Force } azurePowerShellVersion: LatestVersion

■ デプロイ結果

スポンサーリンク

Twitterでフォローしよう