summaryrefslogtreecommitdiff
path: root/src/helpers/vyos-nft-time-refresh.py
blob: c52567840df094277a0d074625907d88f873d385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# nft converts "meta hour" to UTC when a ruleset is loaded, using the UTC
# offset of that moment. After a daylight saving change every hour window is
# off by the difference until the rules are loaded again. This helper loads
# only the affected rules again, in place, so sets and counters are kept, and
# arms itself for the next change of the UTC offset.

import argparse
import json
import re
import sys

from datetime import datetime
from datetime import timedelta
from datetime import timezone
from zoneinfo import ZoneInfo

from vyos.defaults import nft_time_refresh
from vyos.utils.commit import acquire_commit_lock_file
from vyos.utils.commit import release_commit_lock_file
from vyos.utils.process import cmdl
from vyos.utils.process import rc_cmd

# rendered by conf_mode/firewall.py and conf_mode/policy_route.py
rule_files = ['/run/nftables.conf', '/run/nftables_policy.conf']
unit_prefix = 'vyos-nft-time-refresh'

table_re = re.compile(r'^\s*table\s+(\S+)\s+(\S+)\s*\{')
chain_re = re.compile(r'^\s*chain\s+(\S+)\s*\{')
comment_re = re.compile(r'comment "([^"]+)"')

def hour_rules(path: str) -> dict:
    """Return {(family, table, chain, comment): rule} for rules with an hour match"""
    rules = {}
    family = table = chain = None
    try:
        with open(path) as f:
            lines = f.read().splitlines()
    except FileNotFoundError:
        return rules

    for line in lines:
        if match := table_re.match(line):
            family, table = match.groups()
            chain = None
        elif match := chain_re.match(line):
            chain = match.group(1)
        elif chain and 'hour "' in line and (match := comment_re.search(line)):
            rules[(family, table, chain, match.group(1))] = line.strip()
    return rules

def all_hour_rules() -> dict:
    rules = {}
    for path in rule_files:
        rules.update(hour_rules(path))
    return rules

def rule_handles(family: str, table: str) -> dict:
    """Return {(family, table, chain, comment): handle} of a loaded table"""
    handles = {}
    rc, out = rc_cmd(f'nft -j -a list table {family} {table}')
    if rc != 0:
        return handles
    for item in json.loads(out)['nftables']:
        rule = item.get('rule')
        if rule and 'comment' in rule:
            key = (rule['family'], rule['table'], rule['chain'], rule['comment'])
            handles[key] = rule['handle']
    return handles

def replace_batch(rules: dict) -> list:
    """nft commands replacing every rendered hour rule found in the ruleset"""
    batch = []
    handles = {}
    for family, table in {(k[0], k[1]) for k in rules}:
        handles.update(rule_handles(family, table))
    for key, rule in sorted(rules.items()):
        if key in handles:
            family, table, chain, _ = key
            batch.append(f'replace rule {family} {table} {chain} handle {handles[key]} {rule}')
    return batch

def next_offset_change(now: datetime, tz: ZoneInfo, horizon_days=400):
    """First moment after now with a different UTC offset, or None"""
    offset = now.astimezone(tz).utcoffset()
    lo = now
    for _ in range(horizon_days):
        hi = lo + timedelta(days=1)
        if hi.astimezone(tz).utcoffset() != offset:
            # narrow down to the second
            while hi - lo > timedelta(seconds=1):
                mid = lo + (hi - lo) / 2
                if mid.astimezone(tz).utcoffset() == offset:
                    lo = mid
                else:
                    hi = mid
            return hi.replace(microsecond=0)
        lo = hi
    return None

def arm():
    """Schedule a refresh at the next UTC offset change if any rule needs it"""
    # stopping only the timers keeps a refresh which is running right now
    rc_cmd(f"systemctl stop '{unit_prefix}-*.timer'")
    if not all_hour_rules():
        return

    with open('/etc/localtime', 'rb') as f:
        tz = ZoneInfo.from_file(f)
    change = next_offset_change(datetime.now(timezone.utc), tz)
    if not change:
        return

    when = change + timedelta(seconds=5)
    # unique unit name - the unit of a running refresh still exists while it
    # arms the next one
    unit = f'{unit_prefix}-{int(when.timestamp())}'
    # timers default to AccuracySec=1min, which leaves a window up to a minute long
    cmdl(['systemd-run', f'--unit={unit}', '--timer-property=RemainAfterElapse=no',
          '--timer-property=AccuracySec=1s',
          f'--on-calendar={when:%Y-%m-%d %H:%M:%S} UTC', nft_time_refresh, '--refresh'])

def refresh():
    batch = replace_batch(all_hour_rules())
    if not batch:
        return
    rc, out = rc_cmd('nft -f -', input='\n'.join(batch) + '\n')
    if rc != 0:
        print(f'Failed to refresh time based rules: {out}', file=sys.stderr)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--arm', action='store_true', help='Schedule a refresh for the next UTC offset change')
    group.add_argument('--refresh', action='store_true', help='Load rules with an hour match again')
    args = parser.parse_args()

    if args.arm:
        arm()
        sys.exit(0)

    # a commit loads the rules itself and arms the next refresh
    lock, _ = acquire_commit_lock_file()
    if lock is None:
        sys.exit(0)
    try:
        refresh()
        arm()
    finally:
        release_commit_lock_file(lock)