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

Updated Import-APIMSwaggerApiDefinition script #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions Infrastructure-Scripts/Import-ApimSwaggerApiDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
.PARAMETER ImportRetries
(optional) The number of times to retry importing the API definition, defaults to 3

.PARAMETER AddXForwardedAuthorization
(optional) The boolean for adding a new header called X-Forwarded-Authorization that is set to be the original inbound Authorization value

.EXAMPLE
Import-ApimSwaggerApiDefinition -ApimResourceGroup das-at-foobar-rg -InstanceName das-at-foobar-apim -AppServiceResourceGroup das-at-foobar-rg -ApiVersionSetName foobar-api -ApiBaseUrl "https://at-foobar-api.apprenticeships.education.gov.uk" -ApiPath "foo-bar" -ApplicationIdentifierUri "https://<tenant>.onmicrosoft.com/das-at-foobar-as-ar" -ProductId ProductId
#>
Expand All @@ -61,7 +64,9 @@ Param(
[Parameter(Mandatory = $false)]
[bool]$SandboxEnabled = $false,
[Parameter(Mandatory = $false)]
[int]$ImportRetries = 3
[int]$ImportRetries = 3,
[Parameter(Mandatory = $false)]
[bool]$AddXForwardedAuthorization = $false
)

function Invoke-RetryWebRequest ($ApiUrl) {
Expand Down Expand Up @@ -207,7 +212,12 @@ function Import-Api {

$SwaggerSpecificationFilePath = "./swagger-specification.json"

$PolicyString = "<policies><inbound><base/><authentication-managed-identity resource=`"$ApplicationIdentifierUri`"/></inbound><backend><base/></backend><outbound><base/></outbound><on-error><base/></on-error></policies>"
if ($AddXForwardedAuthorization) {
Write-Verbose "Setting new inbound policy to add new header X-Forwarded-Authorization"
$XForwardedAuthorizationHeaderPolicy = '<set-header name="X-Forwarded-Authorization" exists-action="override"><value>@(context.Request.Headers.FirstOrDefault(x=>x.Key=="Authorization").Value?.FirstOrDefault())</value></set-header>'
}

$PolicyString = "<policies><inbound><base/>$XForwardedAuthorizationHeaderPolicy<authentication-managed-identity resource=`"$ApplicationIdentifierUri`"/></inbound><backend><base/></backend><outbound><base/></outbound><on-error><base/></on-error></policies>"

$ApimInstanceExists = Get-AzApiManagement -ResourceGroupName $ApimResourceGroup -Name $InstanceName
if (!$ApimInstanceExists) {
Expand Down
Loading