Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit c8561e5

Browse files
committed
Merge pull request #6069 from matrix-org/rav/fix_attribute_mapping
2 parents c1dad64 + bb82be9 commit c8561e5

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

changelog.d/6069.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug which caused SAML attribute maps to be overridden by defaults.

synapse/config/saml2_config.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# Copyright 2018 New Vector Ltd
3+
# Copyright 2019 The Matrix.org Foundation C.I.C.
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
56
# you may not use this file except in compliance with the License.
@@ -12,11 +13,41 @@
1213
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314
# See the License for the specific language governing permissions and
1415
# limitations under the License.
16+
1517
from synapse.python_dependencies import DependencyException, check_requirements
18+
from synapse.util.module_loader import load_python_module
1619

1720
from ._base import Config, ConfigError
1821

1922

23+
def _dict_merge(merge_dict, into_dict):
24+
"""Do a deep merge of two dicts
25+
26+
Recursively merges `merge_dict` into `into_dict`:
27+
* For keys where both `merge_dict` and `into_dict` have a dict value, the values
28+
are recursively merged
29+
* For all other keys, the values in `into_dict` (if any) are overwritten with
30+
the value from `merge_dict`.
31+
32+
Args:
33+
merge_dict (dict): dict to merge
34+
into_dict (dict): target dict
35+
"""
36+
for k, v in merge_dict.items():
37+
if k not in into_dict:
38+
into_dict[k] = v
39+
continue
40+
41+
current_val = into_dict[k]
42+
43+
if isinstance(v, dict) and isinstance(current_val, dict):
44+
_dict_merge(v, current_val)
45+
continue
46+
47+
# otherwise we just overwrite
48+
into_dict[k] = v
49+
50+
2051
class SAML2Config(Config):
2152
def read_config(self, config, **kwargs):
2253
self.saml2_enabled = False
@@ -36,15 +67,20 @@ def read_config(self, config, **kwargs):
3667

3768
self.saml2_enabled = True
3869

39-
import saml2.config
40-
41-
self.saml2_sp_config = saml2.config.SPConfig()
42-
self.saml2_sp_config.load(self._default_saml_config_dict())
43-
self.saml2_sp_config.load(saml2_config.get("sp_config", {}))
70+
saml2_config_dict = self._default_saml_config_dict()
71+
_dict_merge(
72+
merge_dict=saml2_config.get("sp_config", {}), into_dict=saml2_config_dict
73+
)
4474

4575
config_path = saml2_config.get("config_path", None)
4676
if config_path is not None:
47-
self.saml2_sp_config.load_file(config_path)
77+
mod = load_python_module(config_path)
78+
_dict_merge(merge_dict=mod.CONFIG, into_dict=saml2_config_dict)
79+
80+
import saml2.config
81+
82+
self.saml2_sp_config = saml2.config.SPConfig()
83+
self.saml2_sp_config.load(saml2_config_dict)
4884

4985
# session lifetime: in milliseconds
5086
self.saml2_session_lifetime = self.parse_duration(

synapse/util/module_loader.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
# limitations under the License.
1515

1616
import importlib
17+
import importlib.util
1718

1819
from synapse.config._base import ConfigError
1920

2021

2122
def load_module(provider):
22-
""" Loads a module with its config
23+
""" Loads a synapse module with its config
2324
Take a dict with keys 'module' (the module name) and 'config'
2425
(the config dict).
2526
@@ -38,3 +39,20 @@ def load_module(provider):
3839
raise ConfigError("Failed to parse config for %r: %r" % (provider["module"], e))
3940

4041
return provider_class, provider_config
42+
43+
44+
def load_python_module(location: str):
45+
"""Load a python module, and return a reference to its global namespace
46+
47+
Args:
48+
location (str): path to the module
49+
50+
Returns:
51+
python module object
52+
"""
53+
spec = importlib.util.spec_from_file_location(location, location)
54+
if spec is None:
55+
raise Exception("Unable to load module at %s" % (location,))
56+
mod = importlib.util.module_from_spec(spec)
57+
spec.loader.exec_module(mod)
58+
return mod

0 commit comments

Comments
 (0)