%PDF- %PDF-
Direktori : /opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/plugins/ |
Current File : //opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/plugins/config_watcher.py |
""" This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 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 <https://www.gnu.org/licenses/>. Copyright © 2019 Cloud Linux Software Inc. This software is also available under ImunifyAV commercial license, see <https://www.imunify360.com/legal/eula> """ import time from defence360agent.contracts import config from defence360agent.contracts.messages import MessageType from defence360agent.contracts.plugins import ( MessageSink, MessageSource, expect, ) from defence360agent.utils import recurring_check class ConfigWatcher(MessageSink, MessageSource): """Send ConfigUpdate message on [root's] config update. The config update is detected by polling config file's modification time. """ _POLLING_INTERVAL = 30 # seconds def __init__(self): self._config = config.ConfigFile() self._last_notify_time = 0 self._sink = None self._task = None async def create_sink(self, loop): "plugins.MessageSink method" @expect(MessageType.ConfigUpdate) async def on_config_update_message(self, message): # update the time, to avoid sending duplicate ConfigUpdate # messages after the "config update" command self._last_notify_time = message["timestamp"] async def create_source(self, loop, sink): self._sink = sink self._task = loop.create_task(self._check_config()) async def shutdown(self): if self._task is not None: t, self._task = self._task, None t.cancel() await t self._sink = None @recurring_check(_POLLING_INTERVAL) async def _check_config(self): if config.any_layer_modified_since(self._last_notify_time): # notify about the update message = MessageType.ConfigUpdate( conf=self._config, timestamp=time.time() ) await self._sink.process_message(message) # update the time here, in case ConfigUpdate might stuck # in the queue for longer than the polling interval self._last_notify_time = message["timestamp"]