2014-03-11 23:45:04 +02:00
|
|
|
#
|
2021-04-12 10:32:23 +03:00
|
|
|
# Copyright (C) 2021 GNS3 Technologies Inc.
|
2014-03-11 23:45:04 +02:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2021-04-12 10:32:23 +03:00
|
|
|
Reads the configuration file and store the settings for the server.
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2019-04-01 11:53:39 +03:00
|
|
|
import shutil
|
2021-04-12 10:32:23 +03:00
|
|
|
import secrets
|
2014-03-11 23:45:04 +02:00
|
|
|
import configparser
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
from pydantic import ValidationError
|
|
|
|
from .schemas import ServerConfig
|
2019-04-01 11:53:39 +03:00
|
|
|
from .version import __version_info__
|
2016-06-10 18:51:19 +03:00
|
|
|
from .utils.file_watcher import FileWatcher
|
|
|
|
|
2014-03-11 23:45:04 +02:00
|
|
|
import logging
|
2021-04-13 12:16:50 +03:00
|
|
|
|
2014-03-11 23:45:04 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-08-22 18:21:03 +03:00
|
|
|
class Config:
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
|
|
|
Configuration file management using configparser.
|
2015-02-02 16:01:48 +02:00
|
|
|
|
2016-08-22 18:21:03 +03:00
|
|
|
:param files: Array of configuration files (optional)
|
2021-04-12 10:32:23 +03:00
|
|
|
:param profile: Profile settings (default use standard config file)
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
|
|
|
|
2016-09-07 18:44:51 +03:00
|
|
|
def __init__(self, files=None, profile=None):
|
2015-02-02 16:01:48 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
self._settings = None
|
2015-02-02 16:01:48 +02:00
|
|
|
self._files = files
|
2016-09-07 18:44:51 +03:00
|
|
|
self._profile = profile
|
2021-04-12 10:32:23 +03:00
|
|
|
|
2016-09-30 18:34:28 +03:00
|
|
|
if files and len(files):
|
2023-06-20 14:56:21 +03:00
|
|
|
if not os.access(files[0], os.R_OK) or not os.path.isfile(files[0]):
|
|
|
|
raise SystemExit(f"Unable to read configuration file: {files[0]}")
|
2019-11-10 21:41:39 +02:00
|
|
|
directory_name = os.path.dirname(files[0])
|
|
|
|
if not directory_name or directory_name == "":
|
2019-11-11 12:20:51 +02:00
|
|
|
files[0] = os.path.dirname(os.path.abspath(files[0])) + os.path.sep + files[0]
|
2016-09-30 18:34:28 +03:00
|
|
|
self._main_config_file = files[0]
|
|
|
|
else:
|
|
|
|
self._main_config_file = None
|
2015-02-02 16:08:46 +02:00
|
|
|
|
|
|
|
# Monitor configuration files for changes
|
2015-02-02 16:01:48 +02:00
|
|
|
self._watched_files = {}
|
2017-03-22 19:29:08 +02:00
|
|
|
self._watch_callback = []
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2019-04-01 11:53:39 +03:00
|
|
|
appname = "GNS3"
|
2021-04-13 12:07:58 +03:00
|
|
|
version = f"{__version_info__[0]}.{__version_info__[1]}"
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2022-01-19 13:58:36 +02:00
|
|
|
# On UNIX-like platforms, the configuration file location can be one of the following:
|
|
|
|
# 1: $HOME/.config/GNS3/gns3_server.conf
|
|
|
|
# 2: $HOME/.config/GNS3.conf
|
|
|
|
# 3: /etc/xdg/GNS3/gns3_server.conf
|
|
|
|
# 4: /etc/xdg/GNS3.conf
|
|
|
|
# 5: gns3_server.conf in the current working directory
|
|
|
|
|
|
|
|
home = os.path.expanduser("~")
|
|
|
|
server_filename = "gns3_server.conf"
|
|
|
|
|
|
|
|
if self._profile:
|
|
|
|
legacy_user_dir = os.path.join(home, ".config", appname, "profiles", self._profile)
|
|
|
|
versioned_user_dir = os.path.join(home, ".config", appname, version, "profiles", self._profile)
|
2014-03-11 23:45:04 +02:00
|
|
|
else:
|
2022-01-19 13:58:36 +02:00
|
|
|
legacy_user_dir = os.path.join(home, ".config", appname)
|
|
|
|
versioned_user_dir = os.path.join(home, ".config", appname, version)
|
|
|
|
|
|
|
|
if self._files is None and not hasattr(sys, "_called_from_test"):
|
|
|
|
self._files = [
|
|
|
|
os.path.join(os.getcwd(), server_filename),
|
|
|
|
os.path.join(versioned_user_dir, server_filename),
|
|
|
|
os.path.join(home, ".config", appname + ".conf"),
|
|
|
|
os.path.join("/etc/gns3", server_filename),
|
|
|
|
os.path.join("/etc/xdg", appname, server_filename),
|
|
|
|
os.path.join("/etc/xdg", appname + ".conf"),
|
|
|
|
]
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2015-06-09 17:25:36 +03:00
|
|
|
if self._files is None:
|
|
|
|
self._files = []
|
2016-09-30 18:34:28 +03:00
|
|
|
|
|
|
|
if self._main_config_file is None:
|
2019-04-01 11:53:39 +03:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
# TODO: migrate versioned config file from a previous version of GNS3 (for instance 2.2 -> 3.0) + support profiles
|
2019-04-05 13:44:31 +03:00
|
|
|
# migrate post version 2.2.0 config files if they exist
|
2019-04-01 11:53:39 +03:00
|
|
|
os.makedirs(versioned_user_dir, exist_ok=True)
|
|
|
|
try:
|
|
|
|
# migrate the server config file
|
|
|
|
old_server_config = os.path.join(legacy_user_dir, server_filename)
|
2019-04-14 16:39:55 +03:00
|
|
|
new_server_config = os.path.join(versioned_user_dir, server_filename)
|
|
|
|
if not os.path.exists(new_server_config) and os.path.exists(old_server_config):
|
2019-04-05 13:44:31 +03:00
|
|
|
shutil.copyfile(old_server_config, new_server_config)
|
2019-04-01 11:53:39 +03:00
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.error(f"Cannot migrate old config files: {e}")
|
2019-04-01 11:53:39 +03:00
|
|
|
|
|
|
|
self._main_config_file = os.path.join(versioned_user_dir, server_filename)
|
2016-09-30 18:34:28 +03:00
|
|
|
for file in self._files:
|
|
|
|
if os.path.exists(file):
|
|
|
|
self._main_config_file = file
|
|
|
|
break
|
|
|
|
|
2015-03-17 16:40:58 +02:00
|
|
|
self.clear()
|
|
|
|
self._watch_config_file()
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
@property
|
|
|
|
def settings(self) -> ServerConfig:
|
|
|
|
"""
|
|
|
|
Return the settings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._settings
|
|
|
|
|
2017-03-22 19:29:08 +02:00
|
|
|
def listen_for_config_changes(self, callback):
|
|
|
|
"""
|
|
|
|
Call the callback when the configuration file change
|
|
|
|
"""
|
|
|
|
self._watch_callback.append(callback)
|
|
|
|
|
2016-08-22 18:21:03 +03:00
|
|
|
@property
|
2016-09-07 18:44:51 +03:00
|
|
|
def profile(self):
|
2016-08-22 18:21:03 +03:00
|
|
|
"""
|
2016-09-07 18:44:51 +03:00
|
|
|
Settings profile
|
2016-08-22 18:21:03 +03:00
|
|
|
"""
|
2016-09-07 18:44:51 +03:00
|
|
|
return self._profile
|
2016-08-22 18:21:03 +03:00
|
|
|
|
2016-09-30 18:34:28 +03:00
|
|
|
@property
|
|
|
|
def config_dir(self):
|
2021-04-12 10:32:23 +03:00
|
|
|
"""
|
|
|
|
Return the directory where the configuration file is located.
|
|
|
|
"""
|
2019-04-01 11:53:39 +03:00
|
|
|
|
2016-09-30 18:34:28 +03:00
|
|
|
return os.path.dirname(self._main_config_file)
|
|
|
|
|
2023-09-06 12:28:46 +03:00
|
|
|
@property
|
|
|
|
def controller_vars(self):
|
|
|
|
"""
|
|
|
|
Return the controller variables file path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller_vars_filename = "gns3_controller.vars"
|
|
|
|
return os.path.join(self.config_dir, controller_vars_filename)
|
|
|
|
|
2020-12-16 09:54:21 +02:00
|
|
|
@property
|
|
|
|
def server_config(self):
|
2021-04-12 10:32:23 +03:00
|
|
|
"""
|
|
|
|
Return the server configuration file path.
|
|
|
|
"""
|
2020-12-16 09:54:21 +02:00
|
|
|
|
2022-01-19 13:58:36 +02:00
|
|
|
server_config_filename = "gns3_server.conf"
|
2020-12-16 09:54:21 +02:00
|
|
|
return os.path.join(self.config_dir, server_config_filename)
|
|
|
|
|
2015-03-17 16:40:58 +02:00
|
|
|
def clear(self):
|
2020-12-16 09:54:21 +02:00
|
|
|
"""
|
|
|
|
Restart with a clean config
|
|
|
|
"""
|
|
|
|
|
2014-03-11 23:45:04 +02:00
|
|
|
self.read_config()
|
2015-03-17 11:21:52 +02:00
|
|
|
|
|
|
|
def _watch_config_file(self):
|
2021-04-12 10:32:23 +03:00
|
|
|
"""
|
|
|
|
Add config files to be monitored for changes.
|
|
|
|
"""
|
|
|
|
|
2016-06-10 18:51:19 +03:00
|
|
|
for file in self._files:
|
2018-01-08 13:07:15 +02:00
|
|
|
if os.path.exists(file):
|
|
|
|
self._watched_files[file] = FileWatcher(file, self._config_file_change)
|
2015-03-17 11:21:52 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
def _config_file_change(self, file_path):
|
|
|
|
"""
|
|
|
|
Callback when a config file has been updated.
|
|
|
|
"""
|
|
|
|
|
|
|
|
log.info(f"'{file_path}' has been updated, reloading the config...")
|
2016-06-10 18:51:19 +03:00
|
|
|
self.read_config()
|
2017-03-22 19:29:08 +02:00
|
|
|
for callback in self._watch_callback:
|
|
|
|
callback()
|
2015-02-02 16:01:48 +02:00
|
|
|
|
2015-03-02 21:46:05 +02:00
|
|
|
def reload(self):
|
2015-02-02 16:01:48 +02:00
|
|
|
"""
|
2015-03-02 21:46:05 +02:00
|
|
|
Reload configuration
|
2015-02-02 16:01:48 +02:00
|
|
|
"""
|
|
|
|
|
2015-03-02 21:46:05 +02:00
|
|
|
self.read_config()
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2015-02-28 04:35:31 +02:00
|
|
|
def get_config_files(self):
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
2021-04-12 10:32:23 +03:00
|
|
|
Return the config files in use.
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
return self._watched_files
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
def _load_jwt_secret_key(self):
|
2020-12-16 09:54:21 +02:00
|
|
|
"""
|
2021-04-12 10:32:23 +03:00
|
|
|
Load the JWT secret key.
|
2020-12-16 09:54:21 +02:00
|
|
|
"""
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
jwt_secret_key_path = os.path.join(self._settings.Server.secrets_dir, "gns3_jwt_secret_key")
|
|
|
|
if not os.path.exists(jwt_secret_key_path):
|
|
|
|
log.info(f"No JWT secret key configured, generating one in '{jwt_secret_key_path}'...")
|
|
|
|
try:
|
|
|
|
with open(jwt_secret_key_path, "w+", encoding="utf-8") as fd:
|
|
|
|
fd.write(secrets.token_hex(32))
|
|
|
|
except OSError as e:
|
|
|
|
log.error(f"Could not create JWT secret key file '{jwt_secret_key_path}': {e}")
|
2020-12-16 09:54:21 +02:00
|
|
|
try:
|
2021-04-12 10:32:23 +03:00
|
|
|
with open(jwt_secret_key_path, encoding="utf-8") as fd:
|
|
|
|
jwt_secret_key_content = fd.read()
|
|
|
|
self._settings.Controller.jwt_secret_key = jwt_secret_key_content
|
2020-12-16 09:54:21 +02:00
|
|
|
except OSError as e:
|
2021-04-12 10:32:23 +03:00
|
|
|
log.error(f"Could not read JWT secret key file '{jwt_secret_key_path}': {e}")
|
2020-12-16 09:54:21 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
def _load_secret_files(self):
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
2021-04-12 10:32:23 +03:00
|
|
|
Load the secret files.
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
if not self._settings.Server.secrets_dir:
|
|
|
|
self._settings.Server.secrets_dir = os.path.dirname(self.server_config)
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
self._load_jwt_secret_key()
|
2014-03-11 23:45:04 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
def read_config(self):
|
2015-01-23 18:39:17 +02:00
|
|
|
"""
|
2021-04-12 10:32:23 +03:00
|
|
|
Read the configuration files and validate the settings.
|
2015-01-23 18:39:17 +02:00
|
|
|
"""
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
config = configparser.ConfigParser(interpolation=None)
|
|
|
|
try:
|
|
|
|
parsed_files = config.read(self._files, encoding="utf-8")
|
|
|
|
except configparser.Error as e:
|
|
|
|
log.error("Can't parse configuration file: %s", str(e))
|
|
|
|
return
|
|
|
|
if not parsed_files:
|
|
|
|
log.warning("No configuration file could be found or read")
|
2021-04-28 08:14:34 +03:00
|
|
|
self._settings = ServerConfig()
|
2021-04-12 10:32:23 +03:00
|
|
|
return
|
2015-01-23 18:39:17 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
for file in parsed_files:
|
2021-04-12 16:56:42 +03:00
|
|
|
log.info(f"Configuration file '{file}' loaded")
|
2021-04-12 10:32:23 +03:00
|
|
|
self._watched_files[file] = os.stat(file).st_mtime
|
2015-03-16 16:03:41 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
try:
|
|
|
|
self._settings = ServerConfig(**config._sections)
|
|
|
|
except ValidationError as e:
|
2021-04-12 16:56:42 +03:00
|
|
|
log.critical(f"Could not validate configuration file settings: {e}")
|
|
|
|
raise
|
2015-03-16 16:03:41 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
self._load_secret_files()
|
2015-03-16 16:03:41 +02:00
|
|
|
|
2014-03-11 23:45:04 +02:00
|
|
|
@staticmethod
|
2016-08-22 18:21:03 +03:00
|
|
|
def instance(*args, **kwargs):
|
2014-03-11 23:45:04 +02:00
|
|
|
"""
|
2015-03-13 02:44:05 +02:00
|
|
|
Singleton to return only one instance of Config.
|
2014-03-11 23:45:04 +02:00
|
|
|
|
|
|
|
:returns: instance of Config
|
|
|
|
"""
|
|
|
|
|
2015-02-13 12:15:11 +02:00
|
|
|
if not hasattr(Config, "_instance") or Config._instance is None:
|
2016-08-22 18:21:03 +03:00
|
|
|
Config._instance = Config(*args, **kwargs)
|
2014-03-11 23:45:04 +02:00
|
|
|
return Config._instance
|
2015-02-13 12:15:11 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reset():
|
|
|
|
"""
|
|
|
|
Reset singleton
|
|
|
|
"""
|
|
|
|
|
|
|
|
Config._instance = None
|