Skip to content

Commit

Permalink
Error if macaroon key is missing from config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
illicitonion authored and review.rocks committed Feb 5, 2016
1 parent 34dda7c commit 6a9f120
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 23 deletions.
20 changes: 15 additions & 5 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import subprocess
import sys
import time
from synapse.config._base import ConfigError

from synapse.python_dependencies import (
check_requirements, DEPENDENCY_LINKS
Expand Down Expand Up @@ -350,11 +351,20 @@ def setup(config_options):
Returns:
HomeServer
"""
config = HomeServerConfig.load_config(
"Synapse Homeserver",
config_options,
generate_section="Homeserver"
)
try:
config = HomeServerConfig.load_config(
"Synapse Homeserver",
config_options,
generate_section="Homeserver"
)
except ConfigError as e:
sys.stderr.write("\n" + e.message + "\n")
sys.exit(1)

if not config:
# If a config isn't returned, and an exception isn't raised, we're just
# generating config files and shouldn't try to continue.
sys.exit(0)

config.setup_logging()

Expand Down
7 changes: 6 additions & 1 deletion synapse/config/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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.
from synapse.config._base import ConfigError

if __name__ == "__main__":
import sys
Expand All @@ -21,7 +22,11 @@

if action == "read":
key = sys.argv[2]
config = HomeServerConfig.load_config("", sys.argv[3:])
try:
config = HomeServerConfig.load_config("", sys.argv[3:])
except ConfigError as e:
sys.stderr.write("\n" + e.message + "\n")
sys.exit(1)

print getattr(config, key)
sys.exit(0)
Expand Down
35 changes: 22 additions & 13 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import errno
import os
import yaml
import sys
from textwrap import dedent


Expand Down Expand Up @@ -136,13 +135,20 @@ def invoke_all(self, name, *args, **kargs):
results.append(getattr(cls, name)(self, *args, **kargs))
return results

def generate_config(self, config_dir_path, server_name, report_stats=None):
def generate_config(
self,
config_dir_path,
server_name,
is_generating_file,
report_stats=None,
):
default_config = "# vim:ft=yaml\n"

default_config += "\n\n".join(dedent(conf) for conf in self.invoke_all(
"default_config",
config_dir_path=config_dir_path,
server_name=server_name,
is_generating_file=is_generating_file,
report_stats=report_stats,
))

Expand Down Expand Up @@ -244,15 +250,18 @@ def load_config(cls, description, argv, generate_section=None):

server_name = config_args.server_name
if not server_name:
print "Must specify a server_name to a generate config for."
sys.exit(1)
raise ConfigError(
"Must specify a server_name to a generate config for."
" Pass -H server.name."
)
if not os.path.exists(config_dir_path):
os.makedirs(config_dir_path)
with open(config_path, "wb") as config_file:
config_bytes, config = obj.generate_config(
config_dir_path=config_dir_path,
server_name=server_name,
report_stats=(config_args.report_stats == "yes"),
is_generating_file=True
)
obj.invoke_all("generate_files", config)
config_file.write(config_bytes)
Expand All @@ -266,7 +275,7 @@ def load_config(cls, description, argv, generate_section=None):
"If this server name is incorrect, you will need to"
" regenerate the SSL certificates"
)
sys.exit(0)
return
else:
print (
"Config file %r already exists. Generating any missing key"
Expand Down Expand Up @@ -302,25 +311,25 @@ def load_config(cls, description, argv, generate_section=None):
specified_config.update(yaml_config)

if "server_name" not in specified_config:
sys.stderr.write("\n" + MISSING_SERVER_NAME + "\n")
sys.exit(1)
raise ConfigError(MISSING_SERVER_NAME)

server_name = specified_config["server_name"]
_, config = obj.generate_config(
config_dir_path=config_dir_path,
server_name=server_name
server_name=server_name,
is_generating_file=False,
)
config.pop("log_config")
config.update(specified_config)
if "report_stats" not in config:
sys.stderr.write(
"\n" + MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS + "\n" +
MISSING_REPORT_STATS_SPIEL + "\n")
sys.exit(1)
raise ConfigError(
MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS + "\n" +
MISSING_REPORT_STATS_SPIEL
)

if generate_keys:
obj.invoke_all("generate_files", config)
sys.exit(0)
return

obj.invoke_all("read_config", config)

Expand Down
18 changes: 14 additions & 4 deletions synapse/config/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ def read_config(self, config):

self.registration_shared_secret = config.get("registration_shared_secret")
self.macaroon_secret_key = config.get("macaroon_secret_key")
if self.macaroon_secret_key is None:
raise Exception(
"Config is missing missing macaroon_secret_key - please set it"
" in your config file."
)
self.bcrypt_rounds = config.get("bcrypt_rounds", 12)
self.trusted_third_party_id_servers = config["trusted_third_party_id_servers"]
self.allow_guest_access = config.get("allow_guest_access", False)

def default_config(self, **kwargs):
def default_config(self, is_generating_file=False, **kwargs):
registration_shared_secret = random_string_with_symbols(50)

macaroon_line = ""
if is_generating_file:
macaroon_line += '\n macaroon_secret_key: "%s"\n' % (
random_string_with_symbols(50),
)

macaroon_secret_key = random_string_with_symbols(50)
return """\
## Registration ##
Expand All @@ -49,9 +61,7 @@ def default_config(self, **kwargs):
# If set, allows registration by anyone who also has the shared
# secret, even if registration is otherwise disabled.
registration_shared_secret: "%(registration_shared_secret)s"
macaroon_secret_key: "%(macaroon_secret_key)s"
%(macaroon_line)s
# Set the number of bcrypt rounds used to generate password hash.
# Larger numbers increase the work factor needed to generate the hash.
# The default number of rounds is 12.
Expand Down
14 changes: 14 additions & 0 deletions tests/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
#
# 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.
50 changes: 50 additions & 0 deletions tests/config/test_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
#
# 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 os.path
import shutil
import tempfile
from synapse.config.homeserver import HomeServerConfig
from tests import unittest


class ConfigGenerationTestCase(unittest.TestCase):

def setUp(self):
self.dir = tempfile.mkdtemp()
print self.dir
self.file = os.path.join(self.dir, "homeserver.yaml")

def tearDown(self):
shutil.rmtree(self.dir)

def test_generate_config_generates_files(self):
HomeServerConfig.load_config("", [
"--generate-config",
"-c", self.file,
"--report-stats=yes",
"-H", "lemurs.win"
])

self.assertSetEqual(
set([
"homeserver.yaml",
"lemurs.win.log.config",
"lemurs.win.signing.key",
"lemurs.win.tls.crt",
"lemurs.win.tls.dh",
"lemurs.win.tls.key",
]),
set(os.listdir(self.dir))
)
77 changes: 77 additions & 0 deletions tests/config/test_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
#
# 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 os.path
import shutil
import tempfile
import yaml
from synapse.config.homeserver import HomeServerConfig
from tests import unittest


class ConfigLoadingTestCase(unittest.TestCase):

def setUp(self):
self.dir = tempfile.mkdtemp()
print self.dir
self.file = os.path.join(self.dir, "homeserver.yaml")

def tearDown(self):
shutil.rmtree(self.dir)

def test_load_fails_if_server_name_missing(self):
self.generate_config_and_remove_lines_containing("server_name")
with self.assertRaises(Exception):
HomeServerConfig.load_config("", ["-c", self.file])

def test_generates_and_loads_macaroon_secret_key(self):
self.generate_config()

with open(self.file,
"r") as f:
raw = yaml.load(f)
self.assertIn("macaroon_secret_key", raw)

config = HomeServerConfig.load_config("", ["-c", self.file])
self.assertTrue(
hasattr(config, "macaroon_secret_key"),
"Want config to have attr macaroon_secret_key"
)
if len(config.macaroon_secret_key) < 5:
self.fail(
"Want macaroon secret key to be string of at least length 5,"
"was: %r" % (config.macaroon_secret_key,)
)

def test_load_fails_if_macaroon_secret_key_missing(self):
self.generate_config_and_remove_lines_containing("macaroon")
with self.assertRaises(Exception):
HomeServerConfig.load_config("", ["-c", self.file])

def generate_config(self):
HomeServerConfig.load_config("", [
"--generate-config",
"-c", self.file,
"--report-stats=yes",
"-H", "lemurs.win"
])

def generate_config_and_remove_lines_containing(self, needle):
self.generate_config()

with open(self.file, "r") as f:
contents = f.readlines()
contents = [l for l in contents if needle not in l]
with open(self.file, "w") as f:
f.write("".join(contents))

0 comments on commit 6a9f120

Please sign in to comment.