Skip to content

fix(aws-network-mcp-server): detect_tgw_inspection KeyError on VpcId#4291

Open
arimu1 wants to merge 2 commits into
awslabs:mainfrom
arimu1:fix/4286-tgw-inspection-vpcid
Open

fix(aws-network-mcp-server): detect_tgw_inspection KeyError on VpcId#4291
arimu1 wants to merge 2 commits into
awslabs:mainfrom
arimu1:fix/4286-tgw-inspection-vpcid

Conversation

@arimu1

@arimu1 arimu1 commented Jul 21, 2026

Copy link
Copy Markdown

Summary

detect_tgw_inspection raised an unhandled KeyError: 'VpcId' any time the account/region had at least one AWS Network Firewall — regardless of the Transit Gateway's attachment mix.

Root cause: nfw_client.list_firewalls()['Firewalls'] items only ever contain FirewallName/FirewallArn/TransitGatewayAttachmentId (see the ListFirewalls API reference) — VpcId is never present there. VpcId only shows up in the DescribeFirewall response. The unguarded fw['VpcId'] access at line 68 of detect_transit_gateway_inspection.py therefore failed on every call that returned any firewalls, independent of the TGW's attachment types — the TGW-attachment filtering code (ResourceType/ResourceId) was already correct and unaffected.

Fix

Look up each listed firewall's VpcId via describe_firewall() (the same call already used a few lines down for TGW network-function attachments), guarding with .get('VpcId') so non-VPC firewalls or describe failures degrade gracefully instead of crashing the whole tool call.

Closes #4286

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

Test plan

  • Added a regression test (test_vpc_firewall_detection_no_vpc_id_on_list_firewalls) that mocks list_firewalls() with the real API shape (no VpcId) and asserts the tool still correctly finds the VPC-attached firewall via describe_firewall().
  • Updated existing fixtures/tests that previously (incorrectly) assumed list_firewalls() returns VpcId.
  • Verified the regression test fails with the exact reported error (ToolError: Error detecting firewall attachments: 'VpcId'.) against the pre-fix code, and passes after the fix.
  • uv run pytest tests/ — 245 passed
  • uv run ruff check / uv run ruff format --check on changed files — clean

list_firewalls() only returns FirewallName/FirewallArn, never VpcId,
so any account with at least one AWS Network Firewall made
detect_tgw_inspection() crash with KeyError: 'VpcId' regardless of
the Transit Gateway's attachment mix. VpcId is only available from
describe_firewall(), so look it up there instead.

Closes awslabs#4286
test_vpc_firewall_detection already fails pre-fix now that
sample_firewalls matches the real ListFirewalls shape (no VpcId), so
the separate no_vpc_id regression test duplicated it with a one-off
inline dict. Drop it in favor of the existing test carrying regression
coverage.

Also convert _describe_firewall_side_effect from a staticmethod to a
fixture, matching every other shared value in this test class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

detect_tgw_inspection throws KeyError: 'VpcId' on valid Transit Gateways

1 participant