Skip to content

fix(networkfirewall): validate replacement before replacing state - #4114

Merged
pgermosen merged 2 commits into
floci-io:mainfrom
danielvanza:fix/networkfirewall-atomic-update
Sep 30, 2026
Merged

pgermosen merged 2 commits into
floci-io:mainfrom
danielvanza:fix/networkfirewall-atomic-update

Conversation

@danielvanza

@danielvanza danielvanza commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Audit finding F-027.

UpdateRuleGroup and UpdateFirewallPolicy deleted the existing resource and then rebuilt it, so a
rejected update (a name collision, or a missing or invalid field) left the resource gone. This
builds and validates the replacement first and only then swaps it into the store, so a rejected
update leaves the old resource intact.

The update path also now accepts the shape the Terraform provider sends: the resource is identified
by ARN alone, with no name (and no Type for a rule group). When the request omits the name or the
type, the value is taken from the stored resource. The ARN-versus-name mismatch check runs only when
a name was actually sent. Type is still validated against the modelled values whenever it is
present.

Type of change

  • Bug fix (fix:)
  • New feature (feat:)
  • Breaking change (feat!: or fix!:)
  • Docs / chore

AWS Compatibility

Before: a rejected UpdateRuleGroup/UpdateFirewallPolicy returned an error but deleted the
target, and an ARN-only update (as Terraform sends) failed with InvalidRequestException because
the name (and rule group Type) was required up front. After: a rejected update keeps the original
resource and its ARN, and an update identified by ARN alone replaces the stored definition,
carrying the stored name and type.

Test evidence

  • ./mvnw test -Dtest='NetworkFirewallIntegrationTest,NetworkFirewallConcurrencyTest': 52 run, 0 failures.
    • A rejected rule group update leaves the original rule group and ARN intact; a rejected firewall policy update leaves the original policy intact.
    • A rule group update by ARN alone, with no name and no Type, replaces the definition and keeps the stored name, type, and ARN.
    • A firewall policy update by ARN alone, with no name, replaces the definition and keeps the stored name.
  • Full suite: CI runs the suite sharded; the focused tests pass locally.

Checklist

  • ./mvnw test passes locally
  • New or updated integration test added
  • Commit messages follow Conventional Commits

@github-actions github-actions Bot added the over-pr-limit Author has more than 2 open pull requests (see CONTRIBUTING.md) label Sep 21, 2026
@github-actions

Copy link
Copy Markdown

👋 Thanks for the pull request!

It looks like you now have 8 open pull requests in this repository, which is above our advisory limit of 5. To keep the review backlog focused and make sure every contribution gets proper attention, we ask contributors to shepherd their existing PRs to the finish line before opening new ones — responding to review feedback, rebasing, or closing any that are no longer needed.

Here is your list of open PRs. Nothing is blocked and this PR stays open — this is just a friendly nudge. The over-pr-limit label helps maintainers see at a glance which PRs to prioritize.

If some of these PRs are stacked or intentionally related, just say so here and a maintainer will take that into account.

@hectorvent hectorvent added bug Something isn't working networkfirewall AWS Network Firewall labels Sep 22, 2026
@danielvanza
danielvanza marked this pull request as ready for review September 24, 2026 09:26

@pgermosen pgermosen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! Validating the replacement before touching the store is the right fix. Today a bad update deletes the rule group or policy and then fails to recreate it, so the resource is just gone. Keeping the existing ARN and returning ResourceNotFoundException for a missing resource both match the model too.

One gap is left, though, and it's the path Terraform uses. For both operations the model says "You must specify the ARN or the name, and you can specify both", and for rule groups Type "is required for requests that do not include the RuleGroupARN". The update still requires the name (and Type for rule groups) up front: ruleGroupArn(request, ...) is evaluated before replaceNamed runs, updateFirewallPolicy calls requiredText(request, "FirewallPolicyName"), and buildNamed requires it again. The Terraform provider's aws_networkfirewall_firewall_policy update sends only FirewallPolicyArn, and aws_networkfirewall_rule_group sends RuleGroupArn plus Type, with no name in either. So every Terraform update of these two resources still gets a 400. It's a lot better than losing the resource, but it's still a failure.

Since replaceNamed already looks up existing, the fix can stay in there: when the request leaves out the name (or the type), take it from the stored resource, and only do the ARN-versus-name check when a name was actually sent. A test that updates each resource by ARN alone would pin it.

Could you also fill in the Summary and AWS Compatibility sections of the description? Right now it's just the empty template.

@pgermosen pgermosen added the waiting-author Review posted; waiting on the PR author to respond label Sep 24, 2026
@danielvanza
danielvanza force-pushed the fix/networkfirewall-atomic-update branch from 310d7ff to ebc3369 Compare September 26, 2026 08:33
@danielvanza

Copy link
Copy Markdown
Contributor Author

All three in ebc3369: updates are identified by ARN alone, taking the name (and the rule group Type) from the stored resource and only running the ARN-versus-name check when a name is sent, with tests updating a rule group (name and type both omitted) and a firewall policy by ARN alone; Type is still validated when present. The description is filled in and the branch is rebased on main. Note the Terraform ARN-only shape is now covered: aws_networkfirewall_rule_group sends RuleGroupArn + Type, and aws_networkfirewall_firewall_policy sends FirewallPolicyArn alone.

@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Adds validation to network firewall update operations.

The PR appears safe to merge; no outstanding previous finding or actionable new regression was identified.

Findings

  1. P1 Stale updates overwrite newer changes ▶
  2. P1 Type can contradict ARN ▶
  3. P1 Missing body erases definition ▶
Summary

The PR changes rule-group and firewall-policy updates to check the current token, validate the replacement, and retain stored identity and unchanged fields before writing. The three previous findings—stale updates, type changes that contradict the ARN, and missing bodies that erase definitions—are fixed in the current code. Concurrency and integration tests cover these paths.

Reviews (2) · Last reviewed commit: "fix(networkfirewall): require UpdateToke..."

Comment on lines +494 to +496
ObjectNode replacement = buildNamed(effectiveRequest, nameField, requestBodyField, responseField,
arnField, idField, existingArn, store, existingArn);
store.put(existingArn, replacement);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Stale updates overwrite newer changes

When two rule-group or firewall-policy updates overlap, this path writes the replacement without comparing the request's UpdateToken with the stored token. buildNamed then discards the supplied token and generates a new one. A stale update can therefore overwrite a newer definition instead of returning InvalidTokenException; updates with no token are accepted too.

Comment on lines +76 to +80
String type = textOrNull(request, "Type");
if (type != null) {
requireEnum(type, RULE_GROUP_TYPES, "Type");
}
return replaceNamed(request, "RuleGroupName", "RuleGroup", "RuleGroupResponse",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Type can contradict ARN

An update can set Type to STATELESS on an existing stateful rule group because this check validates only that the value is in the enum. The replacement keeps the old stateful-rulegroup ARN while storing the new type, so describe and type-filtered list responses expose a stateless group with a stateful ARN.

Comment on lines +494 to +496
ObjectNode replacement = buildNamed(effectiveRequest, nameField, requestBodyField, responseField,
arnField, idField, existingArn, store, existingArn);
store.put(existingArn, replacement);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Missing body erases definition

If an ARN-only update omits RuleGroup or FirewallPolicy, this path still writes a replacement. buildNamed leaves the body out when it is absent, so the update returns success but silently erases the stored rules or policy. Validate the update body before replacing the resource.

@pgermosen pgermosen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is exactly what I was after. An update by ARN alone now picks up the stored name and type, the ARN-versus-name check only runs when a name is sent, and the new tests cover both resources that way. The description reads well now too.

One small thing left. The firewall methods in NetworkFirewallService are synchronized, but the rule group and firewall policy create, update and delete methods aren't. replaceNamed reads the existing resource, validates, and then puts the replacement back, so a DeleteRuleGroup (or DeleteFirewallPolicy) that lands in between gets undone: the update writes the resource back after it was deleted. Marking those methods synchronized, like the firewall ones, would close it.

The red shard is Docker Hub rate limiting in the ECS container tests, nothing to do with this change.

danielvanza and others added 2 commits September 30, 2026 07:07
…n update

UpdateRuleGroup and UpdateFirewallPolicy now require the current
UpdateToken and reject a stale one with InvalidTokenException, as the
API reference documents. The replacement is built from the stored
resource, so Capacity and Tags, which neither update request models,
and any field the request leaves out carry over instead of being
dropped.

A rule group's Type can't change after creation, UpdateFirewallPolicy
requires FirewallPolicy, and UpdateRuleGroup requires exactly one of
RuleGroup or Rules, so a request without a definition no longer wipes
the stored one. Rule group and firewall policy create, update and
delete now share the service lock, so a delete can't be undone by an
overlapping update and two updates holding the same token can't both
apply.
@pgermosen
pgermosen force-pushed the fix/networkfirewall-atomic-update branch from ebc3369 to 402e6aa Compare September 30, 2026 12:18

@pgermosen pgermosen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielvanza thanks, this closes everything from last time. The rule group and policy methods now share the service lock, so a delete can't be undone by an update landing around it, and the update now requires a matching UpdateToken, keeps the type tied to the ARN, and refuses a request without the rule group or policy body, all as the model describes. Looks good to me.

@pgermosen pgermosen removed the waiting-author Review posted; waiting on the PR author to respond label Sep 30, 2026
@pgermosen
pgermosen merged commit 865dd1d into floci-io:main Sep 30, 2026
31 checks passed
omatheusmesmo pushed a commit to omatheusmesmo/floci that referenced this pull request Oct 12, 2026
…oci-io#4114)

* fix(networkfirewall): validate replacement before replacing state

* fix(networkfirewall): require UpdateToken and keep unchanged fields on update

UpdateRuleGroup and UpdateFirewallPolicy now require the current
UpdateToken and reject a stale one with InvalidTokenException, as the
API reference documents. The replacement is built from the stored
resource, so Capacity and Tags, which neither update request models,
and any field the request leaves out carry over instead of being
dropped.

A rule group's Type can't change after creation, UpdateFirewallPolicy
requires FirewallPolicy, and UpdateRuleGroup requires exactly one of
RuleGroup or Rules, so a request without a definition no longer wipes
the stored one. Rule group and firewall policy create, update and
delete now share the service lock, so a delete can't be undone by an
overlapping update and two updates holding the same token can't both
apply.

---------

Co-authored-by: Priamo <pgermosen@gmail.com>
Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
omatheusmesmo pushed a commit to omatheusmesmo/floci that referenced this pull request Oct 12, 2026
…oci-io#4114)

* fix(networkfirewall): validate replacement before replacing state

* fix(networkfirewall): require UpdateToken and keep unchanged fields on update

UpdateRuleGroup and UpdateFirewallPolicy now require the current
UpdateToken and reject a stale one with InvalidTokenException, as the
API reference documents. The replacement is built from the stored
resource, so Capacity and Tags, which neither update request models,
and any field the request leaves out carry over instead of being
dropped.

A rule group's Type can't change after creation, UpdateFirewallPolicy
requires FirewallPolicy, and UpdateRuleGroup requires exactly one of
RuleGroup or Rules, so a request without a definition no longer wipes
the stored one. Rule group and firewall policy create, update and
delete now share the service lock, so a delete can't be undone by an
overlapping update and two updates holding the same token can't both
apply.

---------

Co-authored-by: Priamo <pgermosen@gmail.com>
Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working networkfirewall AWS Network Firewall over-pr-limit Author has more than 2 open pull requests (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants