From 4516e41fe0db68ef190179366321cd88633cb564 Mon Sep 17 00:00:00 2001 From: Paul Yuknewicz Date: Mon, 2 Nov 2026 07:23:34 -0800 Subject: [PATCH] Add optional backendSubnetResourceId and allowedCallerCidrRanges in Bicep Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4448b1aa-1ea5-4c5f-af63-7ec98b7acb9e --- infra/README.md | 11 +-- infra/ai-gateway/README.md | 9 ++- infra/ai-gateway/main.bicep | 78 ++++++++++++++------- infra/main.bicep | 37 +++++++--- tests/test-ai-gateway-model-registration.sh | 10 +++ 5 files changed, 103 insertions(+), 42 deletions(-) diff --git a/infra/README.md b/infra/README.md index 223d8fa..2939d17 100644 --- a/infra/README.md +++ b/infra/README.md @@ -25,10 +25,13 @@ of deployment capacity (20,000 for `gpt-latest` and `gpt-6-sol`; 200,000 for `gpt-mini-latest`), a `$10.00/month` `costLimit` per caller identity (`monthly-caller-budget`), and `Medium` `contentSafety` thresholds for Hate, Self-Harm, Sexual, and Violence. The same content-safety thresholds are applied -to the `toolServers/github` MCP server. The `gpt-latest` alias fails over across -`gpt-6.1-sol` (priority 0), `gpt-6-sol` (priority 1), and `gpt-5.4-mini` -(priority 2). See the [AI Gateway module README](ai-gateway/README.md) for -module usage and configuration. +to the `toolServers/github` MCP server. You can also pass optional +`backendSubnetResourceId` (`properties.backend.subnet.id`, delegated to +`Microsoft.App/environments`) for outbound VNet integration and +`allowedCallerCidrRanges` for inbound caller IP filtering (`ipFilter`). The +`gpt-latest` alias fails over across `gpt-6.1-sol` (priority 0), `gpt-6-sol` +(priority 1), and `gpt-5.4-mini` (priority 2). See the [AI Gateway module +README](ai-gateway/README.md) for module usage and configuration. ## Deploy the full stack diff --git a/infra/ai-gateway/README.md b/infra/ai-gateway/README.md index 207adba..f63e242 100644 --- a/infra/ai-gateway/README.md +++ b/infra/ai-gateway/README.md @@ -29,8 +29,13 @@ deployment capacity per minute. With the sample capacities, `gpt-latest` and identity (`id: monthly-caller-budget`) and `Medium` `contentSafety` thresholds for Hate, Self-Harm, Sexual, and Violence. The postprovision configuration adds the same `Medium` content-safety thresholds to the `toolServers/github` MCP -server. The `gpt-latest` alias routes through `gpt-6.1-sol` (priority 0), -`gpt-6-sol` (priority 1), and `gpt-5.4-mini` (priority 2). +server. You can optionally pass `allowedCallerCidrRanges` (for example +`['10.0.0.0/8']`) to add an inline `ipFilter` policy (`action: Allow`) to each +model registration, and `backendSubnetResourceId` (pointing to a subnet in the +same subscription and region delegated to `Microsoft.App/environments`) to +enable outbound VNet integration (`properties.backend.subnet.id`). The +`gpt-latest` alias routes through `gpt-6.1-sol` (priority 0), `gpt-6-sol` +(priority 1), and `gpt-5.4-mini` (priority 2). ## Gateway without a Foundry provider diff --git a/infra/ai-gateway/main.bicep b/infra/ai-gateway/main.bicep index 914973e..e7b0b94 100644 --- a/infra/ai-gateway/main.bicep +++ b/infra/ai-gateway/main.bicep @@ -25,6 +25,10 @@ param foundryAgentsProjectPrincipalId string = '' param principalId string = '' param useManagedIdentityForFoundryConnections bool = true param aiGatewayAudience string = 'https://azure-api.net/aigateway' +@description('Optional resource ID of an existing subnet in the same subscription and region, delegated to Microsoft.App/environments, for outbound VNet integration.') +param backendSubnetResourceId string = '' +@description('Optional CIDR ranges allowed to call registered models via an inline ipFilter policy. Leave empty to allow all caller IPs.') +param allowedCallerCidrRanges array = [] @secure() param toolServerConfigs object = {} @@ -34,6 +38,15 @@ var logAnalyticsApiVersion = '2023-09-01' var telemetryWorkspaceName = 'default' var aiGatewayUserRoleDefinitionId = 'caad7a21-134e-43f4-a550-c5ffc17e051c' var normalizedFoundryEndpoint = endsWith(foundryEndpoint, '/') ? foundryEndpoint : '${foundryEndpoint}/' +var optionalIpFilterPolicies = empty(allowedCallerCidrRanges) + ? [] + : [ + { + type: 'ipFilter' + action: 'Allow' + cidrRanges: allowedCallerCidrRanges + } + ] #disable-next-line BCP081 resource aiGateway 'Microsoft.ApiManagement/aigateways@2025-09-01-preview' = if (deployApimGatewayViaBicep) { name: gatewayName @@ -45,7 +58,15 @@ resource aiGateway 'Microsoft.ApiManagement/aigateways@2025-09-01-preview' = if sku: { name: 'Premium' } - properties: {} + properties: empty(backendSubnetResourceId) + ? {} + : { + backend: { + subnet: { + id: backendSubnetResourceId + } + } + } } #disable-next-line BCP081 @@ -130,32 +151,35 @@ resource foundryCatalogModels 'Microsoft.ApiManagement/aigateways/workspaces/mod modelName: last(split(model.resourceId, '/')) modelVersion: model.?modelVersion ?? null } - policies: model.?policies ?? [ - { - type: 'tokenLimit' - period: 'minute' - count: model.?tokenLimit ?? 10000 - counterKey: [ - 'identity' - ] - } - { - id: 'monthly-caller-budget' - type: 'costLimit' - period: 'month' - amount: model.?costLimit ?? 10 - counterKey: [ - 'identity' - ] - } - { - type: 'contentSafety' - hateSeverity: model.?contentSafetySeverity ?? 'Medium' - selfHarmSeverity: model.?contentSafetySeverity ?? 'Medium' - sexualSeverity: model.?contentSafetySeverity ?? 'Medium' - violenceSeverity: model.?contentSafetySeverity ?? 'Medium' - } - ] + policies: model.?policies ?? concat( + [ + { + type: 'tokenLimit' + period: 'minute' + count: model.?tokenLimit ?? 10000 + counterKey: [ + 'identity' + ] + } + { + id: 'monthly-caller-budget' + type: 'costLimit' + period: 'month' + amount: model.?costLimit ?? 10 + counterKey: [ + 'identity' + ] + } + { + type: 'contentSafety' + hateSeverity: model.?contentSafetySeverity ?? 'Medium' + selfHarmSeverity: model.?contentSafetySeverity ?? 'Medium' + sexualSeverity: model.?contentSafetySeverity ?? 'Medium' + violenceSeverity: model.?contentSafetySeverity ?? 'Medium' + } + ], + optionalIpFilterPolicies + ) } }] diff --git a/infra/main.bicep b/infra/main.bicep index 7df4dac..91993ca 100644 --- a/infra/main.bicep +++ b/infra/main.bicep @@ -84,6 +84,12 @@ param agentLogAnalyticsWorkspaceName string = '' @description('AI Gateway resource name. Defaults to a deterministic name.') param aiGatewayName string = '' +@description('Optional resource ID of an existing subnet in the same subscription and region, delegated to Microsoft.App/environments, for AI Gateway outbound VNet integration.') +param backendSubnetResourceId string = '' + +@description('Optional CIDR ranges allowed to call registered models and MCP tool servers via an inline ipFilter policy. Leave empty to allow all caller IPs.') +param allowedCallerCidrRanges array = [] + @description('Default GitHub repository for the daily digest automation.') param githubRepository string = 'microsoft/agent-framework' @@ -272,6 +278,8 @@ module aiGateway './ai-gateway/main.bicep' = { foundryAgentsAccountPrincipalId: foundryAgents.outputs.aiServicesPrincipalId foundryAgentsProjectPrincipalId: foundryAgents.outputs.aiProjectPrincipalId principalId: principalId + backendSubnetResourceId: backendSubnetResourceId + allowedCallerCidrRanges: allowedCallerCidrRanges tags: tags enableMonitoring: enableMonitoring deployApimGatewayViaBicep: true @@ -326,15 +334,26 @@ module aiGateway './ai-gateway/main.bicep' = { description: 'Read-only GitHub tools for repository digests.' type: 'mcp' failureMode: 'failClosed' - policies: [ - { - type: 'contentSafety' - hateSeverity: 'Medium' - selfHarmSeverity: 'Medium' - sexualSeverity: 'Medium' - violenceSeverity: 'Medium' - } - ] + policies: concat( + [ + { + type: 'contentSafety' + hateSeverity: 'Medium' + selfHarmSeverity: 'Medium' + sexualSeverity: 'Medium' + violenceSeverity: 'Medium' + } + ], + empty(allowedCallerCidrRanges) + ? [] + : [ + { + type: 'ipFilter' + action: 'Allow' + cidrRanges: allowedCallerCidrRanges + } + ] + ) endpoints: [ { namespace: 'github' diff --git a/tests/test-ai-gateway-model-registration.sh b/tests/test-ai-gateway-model-registration.sh index 8c2132e..e7722f3 100755 --- a/tests/test-ai-gateway-model-registration.sh +++ b/tests/test-ai-gateway-model-registration.sh @@ -261,6 +261,16 @@ if ! jq -e ' exit 1 fi +if ! jq -e ' + .parameters.backendSubnetResourceId.defaultValue == "" + and .parameters.allowedCallerCidrRanges.defaultValue == [] + and (.variables.optionalIpFilterPolicies | tostring | contains("ipFilter") and contains("allowedCallerCidrRanges")) + and (.resources[] | select(.type == "Microsoft.ApiManagement/aigateways") | .properties | tostring | contains("backend") and contains("subnet") and contains("backendSubnetResourceId")) +' "$compiled_template" >/dev/null; then + echo "AI Gateway module must expose optional backendSubnetResourceId (properties.backend.subnet.id) and allowedCallerCidrRanges (ipFilter policy)." >&2 + exit 1 +fi + for hook in "$repo_root/infra/scripts/configure-ai-gateway.sh" "$repo_root/infra/scripts/configure-ai-gateway.ps1"; do if grep -Eq 'azd[[:space:]]+ai[[:space:]]+connection[[:space:]]+create|--custom-key' "$hook"; then echo "Postprovision must not create the Bicep-owned connection or pass its key to a connection command." >&2