Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions core/imageroot/usr/local/agent/pypkg/agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,37 @@ def remove_custom_zone(name):
)
return response['exit_code'] == 0

def add_rich_rules(rich_rules):
"""
Apply an array of firewall rich rules on the node using firewall-cmd.
Each element of `rich_rules` should be a complete rich-rule string as
accepted by `--add-rich-rule`. Example:
'rule family=ipv4 forward-port port=5060 protocol=udp to-port=5060 to-addr=192.168.1.100'
"""
node_id = os.environ['NODE_ID']
data = {'rich_rules': rich_rules}
response = agent.tasks.run(
agent_id=f'node/{node_id}',
action='add-rich-rules',
data=data
)
return response['exit_code'] == 0

def remove_rich_rules(rich_rules):
"""
Remove an array of firewall rich rules on the node using firewall-cmd.
Each element of `rich_rules` should be a complete rich-rule string as
accepted by `--remove-rich-rule` (the same format used for add).
"""
node_id = os.environ['NODE_ID']
data = {'rich_rules': rich_rules}
response = agent.tasks.run(
agent_id=f'node/{node_id}',
action='remove-rich-rules',
data=data
)
return response['exit_code'] == 0


def list_service_providers(rdb, service, transport='*', filters={}):
"""Look up the endpoint information about a given service. Filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ cluster.grants.grant(rdb, "add-public-service", f'node/{node_id}', "fwadm")
cluster.grants.grant(rdb, "remove-public-service", f'node/{node_id}', "fwadm")
cluster.grants.grant(rdb, "add-custom-zone", f'node/{node_id}', "fwadm")
cluster.grants.grant(rdb, "remove-custom-zone", f'node/{node_id}', "fwadm")
cluster.grants.grant(rdb, "add-rich-rules", f'node/{node_id}', "fwadm")
cluster.grants.grant(rdb, "remove-rich-rules", f'node/{node_id}', "fwadm")

cluster.grants.grant(rdb, "add-public-service", f'node/{node_id}', "tunadm")
cluster.grants.grant(rdb, "remove-public-service", f'node/{node_id}', "tunadm")
Expand All @@ -200,6 +202,8 @@ cluster.grants.grant(rdb, "add-public-service", f'node/{node_id}', "fwadm,portsa
cluster.grants.grant(rdb, "remove-public-service", f'node/{node_id}', "fwadm,portsadm")
cluster.grants.grant(rdb, "add-custom-zone", f'node/{node_id}', "fwadm,portsadm")
cluster.grants.grant(rdb, "remove-custom-zone", f'node/{node_id}', "fwadm,portsadm")
cluster.grants.grant(rdb, "add-rich-rules", f'node/{node_id}', "fwadm,portsadm")
cluster.grants.grant(rdb, "remove-rich-rules", f'node/{node_id}', "fwadm,portsadm")
cluster.grants.grant(rdb, "allocate-ports", f'node/{node_id}', "tunadm,portsadm")
cluster.grants.grant(rdb, "deallocate-ports", f'node/{node_id}', "tunadm,portsadm")
cluster.grants.grant(rdb, "add-tun", f'node/{node_id}', "tunadm,portsadm")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3

#
# Copyright (C) 2025 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import sys
import json
import agent

request = json.load(sys.stdin)

rich_rules = request.get('rich_rules', [])


for rule in rich_rules:
# Add the rich rule as specified
print(agent.SD_INFO + f'Adding rich-rule: {rule}', file=sys.stderr)
agent.run_helper('firewall-cmd', '--permanent', f'--add-rich-rule={rule}')

# Apply the configuration
agent.run_helper('firewall-cmd', '--reload').check_returncode()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "add-rich-rules input",
"$id": "http://schema.nethserver.org/node/add-rich-rules-input.json",
"description": "Add firewall rich rules",
"examples": [
{
"rich_rules": [
"rule family=ipv4 forward-port port=5060 protocol=udp to-port=5060",
"rule family=ipv6 forward-port port=5060 protocol=udp to-port=5060 to-addr=2001:db8::1",
"rule family=ipv4 forward-port port=5060 protocol=udp to-port=5060 source address=1.2.3.4"
]
}
],
"type": "object",
"required": ["rich_rules"],
"properties": {
"rich_rules": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

#
# Copyright (C) 2025 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import sys
import json
import agent

request = json.load(sys.stdin)

rich_rules = request.get('rich_rules', [])


for rule in rich_rules:
print(agent.SD_INFO + f'Removing rich-rule: {rule}', file=sys.stderr)
agent.run_helper('firewall-cmd', '--permanent', f'--remove-rich-rule={rule}')

# Apply the configuration
agent.run_helper('firewall-cmd', '--reload').check_returncode()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "remove-rich-rules input",
"$id": "http://schema.nethserver.org/node/remove-rich-rules-input.json",
"description": "Remove firewall rich rules",
"examples": [
{
"rich_rules": [
"rule family=ipv4 forward-port port=5060 protocol=udp to-port=5060",
"rule family=ipv6 forward-port port=5060 protocol=udp to-port=5060 to-addr=2001:db8::1",
"rule family=ipv4 forward-port port=5060 protocol=udp to-port=5060 source address=1.2.3.4"
]
}
],
"type": "object",
"required": ["rich_rules"],
"properties": {
"rich_rules": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
}
}
2 changes: 2 additions & 0 deletions core/imageroot/var/lib/nethserver/node/install-finalize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ cluster.grants.grant(rdb, action_clause="add-public-service", to_clause="tunadm
cluster.grants.grant(rdb, action_clause="remove-public-service", to_clause="tunadm,portsadm", on_clause='node/1')
cluster.grants.grant(rdb, action_clause="add-custom-zone", to_clause="tunadm,portsadm", on_clause='node/1')
cluster.grants.grant(rdb, action_clause="remove-custom-zone", to_clause="tunadm,portsadm", on_clause='node/1')
cluster.grants.grant(rdb, action_clause="add-rich-rules", to_clause="fwadm,portsadm", on_clause='node/1')
cluster.grants.grant(rdb, action_clause="remove-rich-rules", to_clause="fwadm,portsadm", on_clause='node/1')

cluster.grants.grant(rdb, action_clause="update-routes", to_clause="accountprovider", on_clause='cluster')
cluster.grants.grant(rdb, action_clause="bind-user-domains", to_clause="accountconsumer", on_clause='cluster')
Expand Down