-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Policy parser will now be cached and updated only when changes in policy directories occured.
- Loading branch information
Showing
4 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# | ||
# The Qubes OS Project, http://www.qubes-os.org | ||
# | ||
# Copyright (C) 2017 Marta Marczykowska-Górecka | ||
# <[email protected]> | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library 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 | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, see <https://www.gnu.org/licenses/>. | ||
# | ||
import asyncio | ||
import pyinotify | ||
from ..policy import parser | ||
from .. import POLICYPATH, POLICYPATH_OLD | ||
|
||
|
||
class PolicyCache: | ||
def __init__(self, path): | ||
self.path = path | ||
self.outdated = False | ||
self.policy = parser.FilePolicy(policy_path=self.path) | ||
|
||
# default policy paths are listed manually, for compatibility with R4.0 | ||
# to be removed in Qubes 5.0 | ||
self.default_policy_paths = [str(POLICYPATH), str(POLICYPATH_OLD)] | ||
|
||
self.wm = None | ||
self.watches = [] | ||
self.notifier = None | ||
|
||
self.initialize_watcher() | ||
|
||
def initialize_watcher(self): | ||
self.wm = pyinotify.WatchManager() | ||
mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
self.notifier = pyinotify.AsyncioNotifier( | ||
self.wm, loop, default_proc_fun=PolicyWatcher(self)) | ||
|
||
if str(self.path) not in self.default_policy_paths: | ||
self.watches.append( | ||
self.wm.add_watch( | ||
str(self.path), mask, rec=True, auto_add=True)) | ||
|
||
for p in self.default_policy_paths: | ||
self.watches.append( | ||
self.wm.add_watch(str(p), mask, rec=True, auto_add=True)) | ||
|
||
def cleanup(self): | ||
for wdd in self.watches: | ||
self.wm.rm_watch(wdd.values()) | ||
|
||
self.notifier.stop() | ||
|
||
def get_policy(self): | ||
if self.outdated: | ||
self.policy = parser.FilePolicy(policy_path=self.path) | ||
self.outdated = False | ||
|
||
return self.policy | ||
|
||
|
||
class PolicyWatcher(pyinotify.ProcessEvent): | ||
def __init__(self, cache): | ||
self.cache = cache | ||
super(PolicyWatcher, self).__init__() | ||
|
||
def process_IN_CREATE(self, _): | ||
self.cache.outdated = True | ||
|
||
def process_IN_DELETE(self, _): | ||
self.cache.outdated = True | ||
|
||
def process_IN_MODIFY(self, _): | ||
self.cache.outdated = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters