Skip to content

Commit

Permalink
Add form_secret_path config option
Browse files Browse the repository at this point in the history
  • Loading branch information
V02460 committed Jan 15, 2025
1 parent 39bd6e2 commit 9441339
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/18090.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `form_secret_path` config option.
16 changes: 16 additions & 0 deletions docs/usage/configuration/config_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,22 @@ Example configuration:
```yaml
form_secret: <PRIVATE STRING>
```
---
### `form_secret_path`

An alternative to [`form_secret`](#form_secret):
allows the secret to be specified in an external file.

The file should be a plain text file, containing only the secret.
Synapse reads the secret from the given file once at startup.

Example configuration:
```yaml
form_secret_path: /path/to/secrets/file
```

_Added in Synapse 1.123.0._

---
## Signing Keys
Config options relating to signing keys
Expand Down
10 changes: 10 additions & 0 deletions synapse/config/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
both defined in config file.
"""

CONFLICTING_FORM_SECRET_OPTS_ERROR = """\
Conflicting options 'form_secret' and 'form_secret_path' are both defined in
config file.
"""

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -193,6 +198,11 @@ def read_config(
# a secret which is used to calculate HMACs for form values, to stop
# falsification of values
self.form_secret = config.get("form_secret", None)
form_secret_path = config.get("form_secret_path", None)
if form_secret_path:
if self.form_secret:
raise ConfigError(CONFLICTING_FORM_SECRET_OPTS_ERROR)
self.form_secret = read_file(form_secret_path, "form_secret_path").strip()

def generate_config_section(
self,
Expand Down
7 changes: 6 additions & 1 deletion tests/config/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_depreciated_identity_server_flag_throws_error(self) -> None:
"turn_shared_secret_path: /does/not/exist",
"registration_shared_secret_path: /does/not/exist",
"macaroon_secret_key_path: /does/not/exist",
"form_secret_path: /does/not/exist",
*["redis:\n enabled: true\n password_path: /does/not/exist"]
* (hiredis is not None),
]
Expand All @@ -157,6 +158,10 @@ def test_secret_files_missing(self, config_str: str) -> None:
"macaroon_secret_key_path: {}",
lambda c: c.key.macaroon_secret_key,
),
(
"form_secret_path: {}",
lambda c: c.key.form_secret.encode("utf-8"),
),
*[
(
"redis:\n enabled: true\n password_path: {}",
Expand All @@ -170,7 +175,7 @@ def test_secret_files_existing(
self, config_line: str, get_secret: Callable[[RootConfig], str]
) -> None:
self.generate_config_and_remove_lines_containing(
["registration_shared_secret", "macaroon_secret_key"]
["form_secret", "macaroon_secret_key", "registration_shared_secret"]
)
with tempfile.NamedTemporaryFile(buffering=0) as secret_file:
secret_file.write(b"53C237")
Expand Down

0 comments on commit 9441339

Please sign in to comment.