Skip to content

Commit 9025edc

Browse files
committed
Add property-set:template handler, refactor to reduce redundancy
Now when a user sets an AppVM to be based on a Whonix-Gateway or Whonix-Workstation template, it will automatically be tagged appropriately and have event buffering enabled if relevant. Fixes: QubesOS/qubes-issues#10645
1 parent f138000 commit 9025edc

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

qubeswhonix/__init__.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def get_template_dispvm(
8989
# If any VM in template chain has the special feature, use it.
9090
default_dispvm = feature
9191
elif (
92-
template is not None
93-
and (template.name + "-dvm") in app.domains
92+
template is not None and (template.name + "-dvm") in app.domains
9493
):
9594
# If we have a template, use it for assuming a name.
9695
default_dispvm = template.name + "-dvm"
@@ -176,6 +175,25 @@ def set_gw_dispvm(app, vm):
176175
if not default_dispvm.features.check_with_template("whonix-ws", None):
177176
vm.default_dispvm = None
178177

178+
def apply_tags_and_features(self, vm):
179+
"""Apply the appropriate tags and features to Whonix-Gateway and
180+
Whonix-Workstation VMs."""
181+
if not isinstance(vm, qubes.vm.LocalVM):
182+
return
183+
184+
if vm.features.check_with_template("whonix-gw", None):
185+
if "anon-gateway" not in vm.tags:
186+
vm.tags.add("anon-gateway")
187+
if "sdwdate-gui-server" not in vm.tags:
188+
vm.tags.add("sdwdate-gui-server")
189+
if vm.features.check_with_template("whonix-ws", None):
190+
if "anon-vm" not in vm.tags:
191+
vm.tags.add("anon-vm")
192+
if "sdwdate-gui-client" not in vm.tags:
193+
vm.tags.add("sdwdate-gui-client")
194+
if "gui-events-max-delay" not in vm.features:
195+
vm.features["gui-events-max-delay"] = 100
196+
179197
@qubes.ext.handler("domain-add", system=True)
180198
def on_domain_add(self, app, _event, vm, **_kwargs):
181199
"""Handle new AppVM created on whonix-ws/whonix-gw template and
@@ -184,23 +202,16 @@ def on_domain_add(self, app, _event, vm, **_kwargs):
184202
if not isinstance(vm, qubes.vm.LocalVM):
185203
return
186204

205+
self.apply_tags_and_features(vm)
206+
187207
if vm.features.check_with_template("whonix-gw", None):
188-
vm.tags.add("anon-gateway")
189-
vm.tags.add("sdwdate-gui-server")
190208
self.set_gw_dispvm(app, vm)
191209

192210
if vm.features.check_with_template("whonix-ws", None):
193211
# this is new VM based on whonix-ws, adjust its default settings
194-
195-
vm.tags.add("anon-vm")
196-
vm.tags.add("sdwdate-gui-client")
197-
198212
self.set_ws_netvm(app, vm)
199213
self.set_ws_dispvm(app, vm)
200214

201-
if "gui-events-max-delay" not in vm.features:
202-
vm.features["gui-events-max-delay"] = 100
203-
204215
@qubes.ext.handler("domain-feature-set:whonix-ws")
205216
def on_whonix_ws_feature_set(
206217
self,
@@ -255,18 +266,12 @@ def on_domain_load(self, vm, _event):
255266
"""Retroactively add tags to sys-whonix and anon-whonix. Also enable
256267
event buffering if it's not already enabled.
257268
"""
258-
if not isinstance(vm, qubes.vm.LocalVM):
259-
return
269+
self.apply_tags_and_features(vm)
260270

261-
if vm.features.check_with_template("whonix-gw", None):
262-
if "anon-gateway" not in vm.tags:
263-
vm.tags.add("anon-gateway")
264-
if "sdwdate-gui-server" not in vm.tags:
265-
vm.tags.add("sdwdate-gui-server")
266-
if vm.features.check_with_template("whonix-ws", None):
267-
if "anon-vm" not in vm.tags:
268-
vm.tags.add("anon-vm")
269-
if "sdwdate-gui-client" not in vm.tags:
270-
vm.tags.add("sdwdate-gui-client")
271-
if "gui-events-max-delay" not in vm.features:
272-
vm.features["gui-events-max-delay"] = 100
271+
@qubes.ext.handler("property-set:template")
272+
def on_property_set_template(
273+
self, vm, event, name, newvalue, oldvalue=None
274+
):
275+
# pylint: disable=too-many-positional-arguments, unused-argument
276+
"""Add tags to AppVMs that become based upon Whonix."""
277+
self.apply_tags_and_features(vm)

0 commit comments

Comments
 (0)