2014-08-31 11:06:39 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-31 11:06:39 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
2015-11-12 06:58:48 -05:00
|
|
|
import errno
|
2014-08-31 11:06:39 -04:00
|
|
|
import os
|
2015-04-29 23:24:44 -04:00
|
|
|
from textwrap import dedent
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2018-04-06 18:37:36 -04:00
|
|
|
from six import integer_types
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import yaml
|
|
|
|
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2014-09-02 05:48:05 -04:00
|
|
|
class ConfigError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-11-19 08:05:51 -05:00
|
|
|
# We split these messages out to allow packages to override with package
|
|
|
|
# specific instructions.
|
2015-11-18 13:37:03 -05:00
|
|
|
MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS = """\
|
|
|
|
Please opt in or out of reporting anonymized homeserver usage statistics, by
|
|
|
|
setting the `report_stats` key in your config file to either True or False.
|
|
|
|
"""
|
|
|
|
|
|
|
|
MISSING_REPORT_STATS_SPIEL = """\
|
|
|
|
We would really appreciate it if you could help our project out by reporting
|
|
|
|
anonymized usage statistics from your homeserver. Only very basic aggregate
|
|
|
|
data (e.g. number of users) will be reported, but it helps us to track the
|
|
|
|
growth of the Matrix community, and helps us to make Matrix a success, as well
|
|
|
|
as to convince other networks that they should peer with us.
|
|
|
|
|
|
|
|
Thank you.
|
|
|
|
"""
|
|
|
|
|
|
|
|
MISSING_SERVER_NAME = """\
|
|
|
|
Missing mandatory `server_name` config option.
|
|
|
|
"""
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2015-09-22 07:57:40 -04:00
|
|
|
|
2015-11-18 13:37:03 -05:00
|
|
|
class Config(object):
|
2015-02-11 10:01:15 -05:00
|
|
|
@staticmethod
|
2015-04-30 11:04:02 -04:00
|
|
|
def parse_size(value):
|
2018-04-06 18:37:36 -04:00
|
|
|
if isinstance(value, integer_types):
|
2015-04-30 11:04:02 -04:00
|
|
|
return value
|
2015-02-11 10:01:15 -05:00
|
|
|
sizes = {"K": 1024, "M": 1024 * 1024}
|
|
|
|
size = 1
|
2015-04-30 11:04:02 -04:00
|
|
|
suffix = value[-1]
|
2015-02-11 10:01:15 -05:00
|
|
|
if suffix in sizes:
|
2015-04-30 11:04:02 -04:00
|
|
|
value = value[:-1]
|
2015-02-11 10:01:15 -05:00
|
|
|
size = sizes[suffix]
|
2015-04-30 11:04:02 -04:00
|
|
|
return int(value) * size
|
2015-02-11 10:01:15 -05:00
|
|
|
|
2015-04-29 23:24:44 -04:00
|
|
|
@staticmethod
|
2015-04-30 11:04:02 -04:00
|
|
|
def parse_duration(value):
|
2018-04-06 18:37:36 -04:00
|
|
|
if isinstance(value, integer_types):
|
2015-04-30 11:04:02 -04:00
|
|
|
return value
|
2015-04-29 23:24:44 -04:00
|
|
|
second = 1000
|
2016-11-12 03:10:23 -05:00
|
|
|
minute = 60 * second
|
|
|
|
hour = 60 * minute
|
2015-04-29 23:24:44 -04:00
|
|
|
day = 24 * hour
|
|
|
|
week = 7 * day
|
|
|
|
year = 365 * day
|
2016-11-12 03:10:23 -05:00
|
|
|
sizes = {"s": second, "m": minute, "h": hour, "d": day, "w": week, "y": year}
|
2015-04-29 23:24:44 -04:00
|
|
|
size = 1
|
2015-04-30 11:04:02 -04:00
|
|
|
suffix = value[-1]
|
2015-04-29 23:24:44 -04:00
|
|
|
if suffix in sizes:
|
2015-04-30 11:04:02 -04:00
|
|
|
value = value[:-1]
|
2015-04-29 23:24:44 -04:00
|
|
|
size = sizes[suffix]
|
2015-04-30 11:04:02 -04:00
|
|
|
return int(value) * size
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2014-09-01 10:51:15 -04:00
|
|
|
@staticmethod
|
|
|
|
def abspath(file_path):
|
|
|
|
return os.path.abspath(file_path) if file_path else file_path
|
|
|
|
|
2017-10-17 09:46:17 -04:00
|
|
|
@classmethod
|
|
|
|
def path_exists(cls, file_path):
|
|
|
|
"""Check if a file exists
|
|
|
|
|
|
|
|
Unlike os.path.exists, this throws an exception if there is an error
|
|
|
|
checking if the file exists (for example, if there is a perms error on
|
|
|
|
the parent dir).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the file exists; False if not.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
os.stat(file_path)
|
|
|
|
return True
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise e
|
|
|
|
return False
|
|
|
|
|
2014-09-02 05:48:05 -04:00
|
|
|
@classmethod
|
|
|
|
def check_file(cls, file_path, config_name):
|
|
|
|
if file_path is None:
|
2018-10-19 20:16:55 -04:00
|
|
|
raise ConfigError("Missing config for %s." % (config_name,))
|
2017-10-17 09:46:17 -04:00
|
|
|
try:
|
|
|
|
os.stat(file_path)
|
|
|
|
except OSError as e:
|
2014-09-02 05:48:05 -04:00
|
|
|
raise ConfigError(
|
2017-10-17 09:46:17 -04:00
|
|
|
"Error accessing file '%s' (config for %s): %s"
|
|
|
|
% (file_path, config_name, e.strerror)
|
2014-09-02 05:48:05 -04:00
|
|
|
)
|
|
|
|
return cls.abspath(file_path)
|
|
|
|
|
2015-02-09 13:29:36 -05:00
|
|
|
@classmethod
|
|
|
|
def ensure_directory(cls, dir_path):
|
|
|
|
dir_path = cls.abspath(dir_path)
|
2015-11-12 06:58:48 -05:00
|
|
|
try:
|
2014-12-02 14:51:47 -05:00
|
|
|
os.makedirs(dir_path)
|
2016-03-07 15:13:10 -05:00
|
|
|
except OSError as e:
|
2015-11-12 06:58:48 -05:00
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2014-12-02 14:51:47 -05:00
|
|
|
if not os.path.isdir(dir_path):
|
2018-10-19 20:16:55 -04:00
|
|
|
raise ConfigError("%s is not a directory" % (dir_path,))
|
2014-12-02 14:51:47 -05:00
|
|
|
return dir_path
|
|
|
|
|
2014-09-02 05:48:05 -04:00
|
|
|
@classmethod
|
|
|
|
def read_file(cls, file_path, config_name):
|
|
|
|
cls.check_file(file_path, config_name)
|
2014-08-31 11:06:39 -04:00
|
|
|
with open(file_path) as file_stream:
|
|
|
|
return file_stream.read()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def read_config_file(file_path):
|
2014-09-01 15:44:43 -04:00
|
|
|
with open(file_path) as file_stream:
|
2019-03-22 06:20:17 -04:00
|
|
|
return yaml.safe_load(file_stream)
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2015-04-29 23:24:44 -04:00
|
|
|
def invoke_all(self, name, *args, **kargs):
|
|
|
|
results = []
|
|
|
|
for cls in type(self).mro():
|
|
|
|
if name in cls.__dict__:
|
|
|
|
results.append(getattr(cls, name)(self, *args, **kargs))
|
|
|
|
return results
|
2014-08-31 11:06:39 -04:00
|
|
|
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
def generate_config(
|
2018-12-21 10:04:57 -05:00
|
|
|
self,
|
|
|
|
config_dir_path,
|
|
|
|
data_dir_path,
|
|
|
|
server_name,
|
|
|
|
generate_secrets=False,
|
|
|
|
report_stats=None,
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
):
|
2018-12-21 10:04:57 -05:00
|
|
|
"""Build a default configuration file
|
|
|
|
|
|
|
|
This is used both when the user explicitly asks us to generate a config file
|
|
|
|
(eg with --generate_config), and before loading the config at runtime (to give
|
|
|
|
a base which the config files override)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
config_dir_path (str): The path where the config files are kept. Used to
|
|
|
|
create filenames for things like the log config and the signing key.
|
|
|
|
|
|
|
|
data_dir_path (str): The path where the data files are kept. Used to create
|
|
|
|
filenames for things like the database and media store.
|
|
|
|
|
|
|
|
server_name (str): The server name. Used to initialise the server_name
|
|
|
|
config param, but also used in the names of some of the config files.
|
|
|
|
|
|
|
|
generate_secrets (bool): True if we should generate new secrets for things
|
|
|
|
like the macaroon_secret_key. If False, these parameters will be left
|
|
|
|
unset.
|
|
|
|
|
|
|
|
report_stats (bool|None): Initial setting for the report_stats setting.
|
|
|
|
If None, report_stats will be left unset.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the yaml config file
|
|
|
|
"""
|
2019-03-04 12:14:58 -05:00
|
|
|
default_config = "\n\n".join(
|
2018-10-19 20:16:55 -04:00
|
|
|
dedent(conf)
|
|
|
|
for conf in self.invoke_all(
|
|
|
|
"default_config",
|
|
|
|
config_dir_path=config_dir_path,
|
2018-12-21 10:04:57 -05:00
|
|
|
data_dir_path=data_dir_path,
|
2018-10-19 20:16:55 -04:00
|
|
|
server_name=server_name,
|
2018-12-21 10:04:57 -05:00
|
|
|
generate_secrets=generate_secrets,
|
2018-10-19 20:16:55 -04:00
|
|
|
report_stats=report_stats,
|
|
|
|
)
|
|
|
|
)
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2018-12-21 10:04:57 -05:00
|
|
|
return default_config
|
2014-08-31 11:06:39 -04:00
|
|
|
|
|
|
|
@classmethod
|
2016-06-09 13:50:38 -04:00
|
|
|
def load_config(cls, description, argv):
|
2018-10-19 20:16:55 -04:00
|
|
|
config_parser = argparse.ArgumentParser(description=description)
|
2016-06-09 13:50:38 -04:00
|
|
|
config_parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-c",
|
|
|
|
"--config-path",
|
2016-06-09 13:50:38 -04:00
|
|
|
action="append",
|
|
|
|
metavar="CONFIG_FILE",
|
|
|
|
help="Specify config file. Can be given multiple times and"
|
2018-10-19 20:16:55 -04:00
|
|
|
" may specify directories containing *.yaml files.",
|
2016-06-09 13:50:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
config_parser.add_argument(
|
|
|
|
"--keys-directory",
|
|
|
|
metavar="DIRECTORY",
|
|
|
|
help="Where files such as certs and signing keys are stored when"
|
2018-10-19 20:16:55 -04:00
|
|
|
" their location is given explicitly in the config."
|
|
|
|
" Defaults to the directory containing the last config file",
|
2016-06-09 13:50:38 -04:00
|
|
|
)
|
|
|
|
|
2019-03-13 13:33:54 -04:00
|
|
|
obj = cls()
|
|
|
|
|
|
|
|
obj.invoke_all("add_arguments", config_parser)
|
|
|
|
|
2016-06-09 13:50:38 -04:00
|
|
|
config_args = config_parser.parse_args(argv)
|
|
|
|
|
|
|
|
config_files = find_config_files(search_paths=config_args.config_path)
|
|
|
|
|
|
|
|
obj.read_config_files(
|
2018-10-19 20:16:55 -04:00
|
|
|
config_files, keys_directory=config_args.keys_directory, generate_keys=False
|
2016-06-09 13:50:38 -04:00
|
|
|
)
|
2019-03-13 13:33:54 -04:00
|
|
|
|
|
|
|
obj.invoke_all("read_arguments", config_args)
|
|
|
|
|
2016-06-09 13:50:38 -04:00
|
|
|
return obj
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2016-06-09 13:50:38 -04:00
|
|
|
@classmethod
|
|
|
|
def load_or_generate_config(cls, description, argv):
|
2014-08-31 11:06:39 -04:00
|
|
|
config_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
config_parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-c",
|
|
|
|
"--config-path",
|
2015-04-30 08:48:15 -04:00
|
|
|
action="append",
|
2014-08-31 11:06:39 -04:00
|
|
|
metavar="CONFIG_FILE",
|
2015-08-25 11:25:54 -04:00
|
|
|
help="Specify config file. Can be given multiple times and"
|
2018-10-19 20:16:55 -04:00
|
|
|
" may specify directories containing *.yaml files.",
|
2014-08-31 11:06:39 -04:00
|
|
|
)
|
2014-09-01 10:51:15 -04:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--generate-config",
|
2015-04-30 08:48:15 -04:00
|
|
|
action="store_true",
|
2018-10-19 20:16:55 -04:00
|
|
|
help="Generate a config file for the server name",
|
2014-09-01 10:51:15 -04:00
|
|
|
)
|
2015-09-22 07:57:40 -04:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--report-stats",
|
|
|
|
action="store",
|
2016-06-09 13:50:38 -04:00
|
|
|
help="Whether the generated config reports anonymized usage statistics",
|
2018-10-19 20:16:55 -04:00
|
|
|
choices=["yes", "no"],
|
2015-09-22 07:57:40 -04:00
|
|
|
)
|
2015-08-07 11:42:27 -04:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--generate-keys",
|
|
|
|
action="store_true",
|
2018-10-19 20:16:55 -04:00
|
|
|
help="Generate any missing key files then exit",
|
2015-08-07 11:42:27 -04:00
|
|
|
)
|
2015-08-25 11:58:01 -04:00
|
|
|
config_parser.add_argument(
|
2015-08-25 12:31:22 -04:00
|
|
|
"--keys-directory",
|
2015-08-25 11:58:01 -04:00
|
|
|
metavar="DIRECTORY",
|
2015-08-25 12:31:22 -04:00
|
|
|
help="Used with 'generate-*' options to specify where files such as"
|
2019-02-12 05:37:00 -05:00
|
|
|
" signing keys should be stored, unless explicitly"
|
2018-10-19 20:16:55 -04:00
|
|
|
" specified in the config.",
|
2015-08-25 11:58:01 -04:00
|
|
|
)
|
2015-04-30 08:48:15 -04:00
|
|
|
config_parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-H", "--server-name", help="The server name to generate a config file for"
|
2015-04-30 08:48:15 -04:00
|
|
|
)
|
2014-08-31 11:06:39 -04:00
|
|
|
config_args, remaining_args = config_parser.parse_known_args(argv)
|
|
|
|
|
2016-06-09 13:50:38 -04:00
|
|
|
config_files = find_config_files(search_paths=config_args.config_path)
|
|
|
|
|
2015-08-12 06:57:37 -04:00
|
|
|
generate_keys = config_args.generate_keys
|
|
|
|
|
2016-06-09 13:50:38 -04:00
|
|
|
obj = cls()
|
2015-08-25 11:25:54 -04:00
|
|
|
|
2014-09-01 10:51:15 -04:00
|
|
|
if config_args.generate_config:
|
2015-09-22 07:57:40 -04:00
|
|
|
if config_args.report_stats is None:
|
|
|
|
config_parser.error(
|
2018-10-19 20:16:55 -04:00
|
|
|
"Please specify either --report-stats=yes or --report-stats=no\n\n"
|
|
|
|
+ MISSING_REPORT_STATS_SPIEL
|
2015-09-22 07:57:40 -04:00
|
|
|
)
|
2015-08-25 11:25:54 -04:00
|
|
|
if not config_files:
|
2015-05-05 12:38:10 -04:00
|
|
|
config_parser.error(
|
|
|
|
"Must supply a config file.\nA config file can be automatically"
|
2019-06-20 05:32:02 -04:00
|
|
|
' generated using "--generate-config -H SERVER_NAME'
|
|
|
|
' -c CONFIG-FILE"'
|
2015-05-05 12:38:10 -04:00
|
|
|
)
|
2015-08-25 11:25:54 -04:00
|
|
|
(config_path,) = config_files
|
2017-10-17 09:46:17 -04:00
|
|
|
if not cls.path_exists(config_path):
|
2015-08-25 12:31:22 -04:00
|
|
|
if config_args.keys_directory:
|
|
|
|
config_dir_path = config_args.keys_directory
|
2015-08-25 11:58:01 -04:00
|
|
|
else:
|
|
|
|
config_dir_path = os.path.dirname(config_path)
|
2015-08-12 06:57:37 -04:00
|
|
|
config_dir_path = os.path.abspath(config_dir_path)
|
|
|
|
|
|
|
|
server_name = config_args.server_name
|
|
|
|
if not server_name:
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
raise ConfigError(
|
|
|
|
"Must specify a server_name to a generate config for."
|
|
|
|
" Pass -H server.name."
|
|
|
|
)
|
2019-03-04 12:14:58 -05:00
|
|
|
|
|
|
|
config_str = obj.generate_config(
|
|
|
|
config_dir_path=config_dir_path,
|
|
|
|
data_dir_path=os.getcwd(),
|
|
|
|
server_name=server_name,
|
|
|
|
report_stats=(config_args.report_stats == "yes"),
|
|
|
|
generate_secrets=True,
|
|
|
|
)
|
|
|
|
|
2017-10-17 09:46:17 -04:00
|
|
|
if not cls.path_exists(config_dir_path):
|
2015-08-12 06:57:37 -04:00
|
|
|
os.makedirs(config_dir_path)
|
2018-04-06 19:39:45 -04:00
|
|
|
with open(config_path, "w") as config_file:
|
2019-06-20 05:32:02 -04:00
|
|
|
config_file.write("# vim:ft=yaml\n\n")
|
2018-04-06 19:39:45 -04:00
|
|
|
config_file.write(config_str)
|
2019-03-04 12:14:58 -05:00
|
|
|
|
2019-03-22 06:20:17 -04:00
|
|
|
config = yaml.safe_load(config_str)
|
2019-03-04 12:14:58 -05:00
|
|
|
obj.invoke_all("generate_files", config)
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
print(
|
|
|
|
(
|
|
|
|
"A config file has been generated in %r for server name"
|
2019-02-12 05:37:00 -05:00
|
|
|
" %r. Please review this file and customise it"
|
2018-10-19 20:16:55 -04:00
|
|
|
" to your needs."
|
|
|
|
)
|
|
|
|
% (config_path, server_name)
|
|
|
|
)
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
return
|
2015-08-12 06:57:37 -04:00
|
|
|
else:
|
2018-10-19 20:16:55 -04:00
|
|
|
print(
|
|
|
|
(
|
|
|
|
"Config file %r already exists. Generating any missing key"
|
|
|
|
" files."
|
|
|
|
)
|
|
|
|
% (config_path,)
|
|
|
|
)
|
2015-08-12 06:57:37 -04:00
|
|
|
generate_keys = True
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2015-05-05 12:38:10 -04:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
parents=[config_parser],
|
|
|
|
description=description,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
)
|
|
|
|
|
|
|
|
obj.invoke_all("add_arguments", parser)
|
|
|
|
args = parser.parse_args(remaining_args)
|
|
|
|
|
2015-08-25 11:25:54 -04:00
|
|
|
if not config_files:
|
2015-05-05 12:38:10 -04:00
|
|
|
config_parser.error(
|
|
|
|
"Must supply a config file.\nA config file can be automatically"
|
2019-06-20 05:32:02 -04:00
|
|
|
' generated using "--generate-config -H SERVER_NAME'
|
|
|
|
' -c CONFIG-FILE"'
|
2015-05-05 12:38:10 -04:00
|
|
|
)
|
|
|
|
|
2016-06-09 13:50:38 -04:00
|
|
|
obj.read_config_files(
|
|
|
|
config_files,
|
|
|
|
keys_directory=config_args.keys_directory,
|
|
|
|
generate_keys=generate_keys,
|
|
|
|
)
|
|
|
|
|
|
|
|
if generate_keys:
|
|
|
|
return None
|
|
|
|
|
|
|
|
obj.invoke_all("read_arguments", args)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
def read_config_files(self, config_files, keys_directory=None, generate_keys=False):
|
2016-06-09 13:50:38 -04:00
|
|
|
if not keys_directory:
|
|
|
|
keys_directory = os.path.dirname(config_files[-1])
|
|
|
|
|
2019-01-23 03:39:06 -05:00
|
|
|
self.config_dir_path = os.path.abspath(keys_directory)
|
2015-05-05 12:38:10 -04:00
|
|
|
|
2015-04-30 08:48:15 -04:00
|
|
|
specified_config = {}
|
2015-08-25 11:25:54 -04:00
|
|
|
for config_file in config_files:
|
2016-06-09 13:50:38 -04:00
|
|
|
yaml_config = self.read_config_file(config_file)
|
2015-04-30 08:48:15 -04:00
|
|
|
specified_config.update(yaml_config)
|
|
|
|
|
2015-11-18 13:37:03 -05:00
|
|
|
if "server_name" not in specified_config:
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
raise ConfigError(MISSING_SERVER_NAME)
|
2015-11-18 13:37:03 -05:00
|
|
|
|
2015-04-30 08:48:15 -04:00
|
|
|
server_name = specified_config["server_name"]
|
2018-12-21 10:04:57 -05:00
|
|
|
config_string = self.generate_config(
|
2019-01-23 03:39:06 -05:00
|
|
|
config_dir_path=self.config_dir_path,
|
2018-12-21 10:04:57 -05:00
|
|
|
data_dir_path=os.getcwd(),
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
server_name=server_name,
|
2018-12-21 10:04:57 -05:00
|
|
|
generate_secrets=False,
|
2015-09-22 07:57:40 -04:00
|
|
|
)
|
2019-03-22 06:20:17 -04:00
|
|
|
config = yaml.safe_load(config_string)
|
2015-04-30 11:52:57 -04:00
|
|
|
config.pop("log_config")
|
2015-04-30 08:48:15 -04:00
|
|
|
config.update(specified_config)
|
2016-06-09 13:50:38 -04:00
|
|
|
|
2015-09-22 07:57:40 -04:00
|
|
|
if "report_stats" not in config:
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
raise ConfigError(
|
2018-10-19 20:16:55 -04:00
|
|
|
MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS
|
|
|
|
+ "\n"
|
|
|
|
+ MISSING_REPORT_STATS_SPIEL
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
)
|
2015-04-30 08:48:15 -04:00
|
|
|
|
2015-08-12 06:57:37 -04:00
|
|
|
if generate_keys:
|
2016-06-09 13:50:38 -04:00
|
|
|
self.invoke_all("generate_files", config)
|
Error if macaroon key is missing from config
Currently we store all access tokens in the DB, and fall back to that
check if we can't validate the macaroon, so our fallback works here, but
for guests, their macaroons don't get persisted, so we don't get to
find them in the database. Each restart, we generate a new ephemeral
key, so guests lose access after each server restart.
I tried to fix up the config stuff to be less insane, but gave up, so
instead I bolt on yet another piece of custom one-off insanity.
Also, add some basic tests for config generation and loading.
2016-02-04 20:58:23 -05:00
|
|
|
return
|
2015-08-12 06:57:37 -04:00
|
|
|
|
2019-03-15 11:50:37 -04:00
|
|
|
self.parse_config_dict(config)
|
|
|
|
|
|
|
|
def parse_config_dict(self, config_dict):
|
|
|
|
self.invoke_all("read_config", config_dict)
|
2016-06-09 13:50:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
def find_config_files(search_paths):
|
|
|
|
"""Finds config files using a list of search paths. If a path is a file
|
|
|
|
then that file path is added to the list. If a search path is a directory
|
|
|
|
then all the "*.yaml" files in that directory are added to the list in
|
|
|
|
sorted order.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
search_paths(list(str)): A list of paths to search.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list(str): A list of file paths.
|
|
|
|
"""
|
|
|
|
|
|
|
|
config_files = []
|
|
|
|
if search_paths:
|
|
|
|
for config_path in search_paths:
|
|
|
|
if os.path.isdir(config_path):
|
|
|
|
# We accept specifying directories as config paths, we search
|
|
|
|
# inside that directory for all files matching *.yaml, and then
|
|
|
|
# we apply them in *sorted* order.
|
|
|
|
files = []
|
|
|
|
for entry in os.listdir(config_path):
|
|
|
|
entry_path = os.path.join(config_path, entry)
|
|
|
|
if not os.path.isfile(entry_path):
|
2018-10-19 20:16:55 -04:00
|
|
|
err = "Found subdirectory in config directory: %r. IGNORING."
|
|
|
|
print(err % (entry_path,))
|
2016-06-09 13:50:38 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not entry.endswith(".yaml"):
|
2018-10-19 20:16:55 -04:00
|
|
|
err = (
|
|
|
|
"Found file in config directory that does not end in "
|
|
|
|
"'.yaml': %r. IGNORING."
|
|
|
|
)
|
|
|
|
print(err % (entry_path,))
|
2016-06-09 13:50:38 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
files.append(entry_path)
|
|
|
|
|
|
|
|
config_files.extend(sorted(files))
|
|
|
|
else:
|
|
|
|
config_files.append(config_path)
|
|
|
|
return config_files
|