Skip to content

firewall manager, fix parameters#58

Closed
vsilent wants to merge 6 commits intotrydirect:masterfrom
vsilent:issues/6-firewall-management
Closed

firewall manager, fix parameters#58
vsilent wants to merge 6 commits intotrydirect:masterfrom
vsilent:issues/6-firewall-management

Conversation

@vsilent
Copy link
Collaborator

@vsilent vsilent commented Mar 9, 2026

No description provided.

Copilot AI review requested due to automatic review settings March 9, 2026 13:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a firewall management capability to the agent’s command system, wiring a new firewall policy into both the local API server state and the long-polling daemon so firewall-related commands can be executed with config-driven restrictions.

Changes:

  • Add a new configure_firewall stacker command and implement iptables-based rule management with persistence support.
  • Extend agent configuration with an optional firewall section and propagate a derived FirewallPolicy through AppState and daemon polling context.
  • Update test configs and AppState::new call sites to include the new firewall-related parameters.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/security_integration.rs Updates test config + AppState::new call to match new state constructor signature.
tests/http_routes.rs Updates test config + router construction to include new firewall config field and AppState::new arg.
src/comms/local_api.rs Adds firewall_policy to AppState, constructs it from config + API port, and passes it into stacker execution.
src/commands/stacker.rs Adds ConfigureFirewall command parsing and plumbs FirewallPolicy into stacker execution path.
src/commands/mod.rs Exposes the new firewall module.
src/commands/firewall.rs Implements firewall command handling, policy checks, rate limiting, iptables rule application, and persistence filtering.
src/agent/daemon.rs Introduces PollingContext (incl. firewall policy) and plumbs it into command execution/reporting.
src/agent/config.rs Adds FirewallConfig + optional firewall field to Config.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@vsilent
Copy link
Collaborator Author

vsilent commented Mar 9, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.

@vsilent
Copy link
Collaborator Author

vsilent commented Mar 9, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Collaborator Author

@vsilent vsilent left a comment

Choose a reason for hiding this comment

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

looks ok

@vsilent vsilent self-assigned this Mar 9, 2026
@vsilent
Copy link
Collaborator Author

vsilent commented Mar 9, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

@vsilent vsilent requested a review from GAS373 March 9, 2026 15:12
Copy link
Collaborator

@GAS373 GAS373 left a comment

Choose a reason for hiding this comment

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

Ok

@vsilent vsilent requested review from GAS373 and Copilot March 9, 2026 15:16
Copy link
Collaborator

@GAS373 GAS373 left a comment

Choose a reason for hiding this comment

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

Ok

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

vsilent and others added 3 commits March 9, 2026 17:23
PR works?

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
sounds reasonable

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
sounds good

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@vsilent
Copy link
Collaborator Author

vsilent commented Mar 9, 2026

@copilot tests do not pass, because of the status change. Must be fixed.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Comment on lines +1437 to +1441
fn determine_status(rules: &[FirewallRuleResult], errors: &[CommandError]) -> String {
if errors.is_empty() && rules.iter().all(|r| r.applied) {
// All rules applied successfully, no errors: overall command is a success.
"success".to_string()
} else if rules.iter().any(|r| r.applied) {
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

determine_status currently returns only "success"/"failed", but the unit tests below assert "ok"/"partial_success". As written, these tests will fail (and it’s unclear which behavior is intended). Either update the tests to match the "success"/"failed" contract, or change the function to return the detailed status and introduce a separate mapping for CommandResult.status.

Copilot uses AI. Check for mistakes.
Comment on lines +529 to +537
if has_docker && !is_public {
let docker_ok = add_docker_user_rules(rule, &comment, &mut errors).await;
if !docker_ok {
warn!(
port = rule.port,
"DOCKER-USER rule failed; INPUT rule was applied"
);
}
}
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

When applying private-port rules with Docker present, docker_ok can be false (DOCKER-USER rules failed), but the code still records a FirewallRuleResult with applied: true later. This reports success even though container-published traffic may remain unrestricted. Consider marking the rule as not fully applied (or adding a distinct per-rule status/message) when DOCKER-USER rule application fails.

Copilot uses AI. Check for mistakes.
Comment on lines +1425 to +1427
// Docker interface references in POSTROUTING/PREROUTING (docker0, br-*)
if line.contains("docker0") || line.contains("br-") {
return true;
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

is_docker_rule treats any rule containing the substring "br-" as Docker-managed. This can match non-Docker bridge interfaces (e.g., br-lan) and cause legitimate user rules to be stripped during persistence. Narrow the heuristic (e.g., detect Docker bridge naming patterns or only strip when the interface is known Docker-managed).

Copilot uses AI. Check for mistakes.
Comment on lines +1267 to +1275
/// Docker-managed chain names that must NOT be persisted.
/// Docker recreates these on every daemon start; saving them leads to stale /
/// duplicate rules and broken container networking after reboot.
const DOCKER_CHAINS: &[&str] = &[
"DOCKER",
"DOCKER-ISOLATION-STAGE-1",
"DOCKER-ISOLATION-STAGE-2",
"DOCKER-USER",
];
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

persist_rules filters out the DOCKER-USER chain entirely (DOCKER_CHAINS includes it). But this module also adds stacker-managed rules into DOCKER-USER for private ports, so persist: true won’t actually persist the container-traffic restrictions across reboot. Consider persisting stacker-tagged rules in DOCKER-USER (while still stripping Docker’s own chains/rules), or otherwise make it explicit in behavior/response that private-port Docker rules are not persisted.

Copilot uses AI. Check for mistakes.
Comment on lines +824 to +825
result.status = status.to_string();
if !errors.is_empty() {
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

handle_flush assigns result.status to values like "ok"/"partial_success", but transport::CommandResult.status is documented as "success" | "failed" | "timeout". This will break dashboard reporting/consumers. Keep the outer CommandResult.status to the supported values (e.g., map ok/partial_success -> "success", and put the detailed status only inside the JSON payload).

Copilot uses AI. Check for mistakes.
@vsilent vsilent closed this Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants