"""What the modules of the web service share: the paths of the configuration and of the state linux-manager-setup writes, the limit of request bodies, the HTTP statuses of the failures of the server's modules, reading the configuration and the state, changing settings of the configuration, the state of a unit, and the log.""" import json import sys import linuxmanager import linuxmanager_auth from linuxmanager_status import run STATE_FILE = '/run/linux-manager/web.json' CONFIG_FILE = '/etc/linux-manager/linux-manager.json' #: The largest request body accepted, in bytes; a CSR or a certificate chain in PEM is a few kilobytes. BODY_LIMIT = 65536 #: The HTTP status for each status of a linuxmanager_users.Failure; any other is 500. USER_STATUS = {2: 400, 3: 404, 4: 409, 5: 401} #: The HTTP status for each status of a linuxmanager_pki.Failure; any other is 500. PKI_STATUS = {2: 400, 3: 404, 4: 409} #: The HTTP status for each status of a linuxmanager_sites.Failure; any other is 500. SITE_STATUS = {2: 400, 3: 404, 4: 409} #: The bytes in a MiB, the unit of sites.upload_max_mb. MIB = 1024 * 1024 def read_json(path): """The object in a JSON file, or {} when the file is missing or holds no object.""" try: with open(path, encoding='utf-8') as handle: data = json.load(handle) except (OSError, ValueError): return {} return data if isinstance(data, dict) else {} def section(configuration, name): """A section of the configuration, or {} when it is missing or not an object.""" value = configuration.get(name) return value if isinstance(value, dict) else {} def current(): """What a request needs, read now: the features and facts setup resolved, and the signing key. Before setup has run the server has no features, and without a key the protected paths and /auth are not served. """ return dict(read_json(STATE_FILE), key=linuxmanager_auth.key()) def unit_state(unit): result = run('systemctl', 'is-active', unit) return result.stdout.strip() if result and result.stdout.strip() else 'unknown' def settings_change(changes): """Change settings of sections of the configuration in one step, section -> {setting: value}, keeping only the settings of SCHEMA, validated as a whole, and run setup; the configuration, or the HTTP status and the error document as a tuple.""" try: with linuxmanager.config_lock(): data = linuxmanager.read_settings() for name, body in changes.items(): current = data.get(name) data[name] = {**(current if isinstance(current, dict) else {}), **{option: body[option] for option in linuxmanager.SCHEMA[name] if option in body}} cfg = linuxmanager.with_defaults(data) linuxmanager.validate(cfg) linuxmanager.save_config(data) except linuxmanager.ConfigError as exc: return 400, {'error': str(exc)} except OSError as exc: return 500, {'error': f'the configuration cannot be written: {exc}'} except SystemExit as exc: return 500, {'error': str(exc.code)} linuxmanager.request_setup() return cfg def section_change(name, body): """Change any of the settings of the configuration's section as /backup changes its own; the section, or the HTTP status and the error document as a tuple.""" changed = settings_change({name: body}) return changed if isinstance(changed, tuple) else changed[name] def log(message): print(f'linux-manager-web: {message}', file=sys.stderr, flush=True)