Skip to content

Commit bf7bd22

Browse files
committed
feat: add set-matrix-server action
1 parent af6fbe8 commit bf7bd22

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

Containerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ COPY imageroot /imageroot
2121
# copy ui from ui_builder
2222
COPY --from=ui_builder /app/dist /ui
2323
ENTRYPOINT [ "/" ]
24-
LABEL org.nethserver.authorizations="traefik@any:fulladm node:fwadm,portsadm nethvoice-proxy@any:routeadm"
24+
LABEL org.nethserver.authorizations="traefik@any:fulladm node:fwadm,portsadm nethvoice-proxy@any:routeadm matrix@any:matrixadm"
2525
LABEL org.nethserver.tcp-ports-demand="36"
2626
LABEL org.nethserver.rootfull="0"
2727
LABEL org.nethserver.min-core="3.6.2-0"

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,44 @@ It raises an event named `phonebook-settings-changed` with the following payload
6565

6666
Consumers of the events must then run the `get-phonebook-credentials` action on `module_id` to get the updated phonebook credentials.
6767

68+
## Matrix integration
69+
70+
This module provides optional integration with a Matrix server module (for example `matrix5`). Integration is implemented so the Matrix module can call into FreePBX to authenticate or validate users.
71+
72+
A helper action `set-matrix-server` was added to manage this integration. The action performs the following steps:
73+
74+
- Resolve the target Matrix module using the module UUID stored in Redis under `module/<module_id>/environment` as the `MODULE_UUID` field. The action only uses this method for resolution and will fail if the UUID is not found.
75+
- Calls `get-configuration` on the local NethVoice module to obtain `nethvoice_host`.
76+
- Calls `get-configuration` on the target Matrix module to obtain its current configuration.
77+
- Sets the `nethvoice_auth_url` field to either `https://<nethvoice_host>/freepbx/rest/testextauth` when integration is enabled, or to the empty string when disabled.
78+
- Calls `configure-module` on the Matrix module with the merged configuration (the Matrix module's existing `get-configuration` response with `nethvoice_auth_url` overridden).
79+
80+
This approach ensures the Matrix module receives a full, consistent payload suitable for its `configure-module` handler while only changing the authentication endpoint field required for NethVoice integration.
81+
82+
### Action: `set-matrix-server`
83+
84+
- Location: `imageroot/actions/set-matrix-server/10setenvs`
85+
- Input schema: `imageroot/actions/set-matrix-server/validate-input.json`
86+
- Parameters:
87+
- `module_uuid` (string, required): the UUID of the Matrix module instance to configure. The action will locate the module id by scanning `module/*/environment` for the `MODULE_UUID` value.
88+
- `enable_integration` (boolean, required): `true` to enable integration (sets the `nethvoice_auth_url`), `false` to disable it (clears the field).
89+
90+
### Example usage
91+
92+
Run the action as a cluster task (example JSON payload):
93+
94+
```
95+
{
96+
"module_uuid": "cf50b191-95d5-435b-bf34-0905bf7dba55",
97+
"enable_integration": true
98+
}
99+
```
100+
101+
The action will find the module id (for example `matrix5`) by scanning Redis, fetch both configurations, set `nethvoice_auth_url` to `https://<nethvoice_host>/freepbx/rest/testextauth` and call `configure-module` on the `module/matrix5` agent.
102+
103+
If you prefer to run this manually for testing, use the `agent.tasks.run` equivalent on the cluster admin node or invoke the module task from your management tooling.
104+
105+
68106
## Uninstall
69107

70108
To uninstall the instance:
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# Copyright (C) 2025 Nethesis S.r.l.
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
#
7+
8+
import json
9+
import sys
10+
import os
11+
12+
import agent
13+
14+
request = json.load(sys.stdin)
15+
16+
def resolve_module_id_from_uuid(uuid):
17+
# Redis stores module info under module/{id}/uuid -> value
18+
# We need to search for the module id that has this uuid
19+
with agent.redis_connect() as rdb:
20+
# Only method: check environment hashes where MODULE_UUID is stored (user-provided format)
21+
for key in rdb.scan_iter(match='module/*/environment'):
22+
try:
23+
k = key.decode() if isinstance(key, bytes) else key
24+
module_uuid_val = rdb.hget(k, 'MODULE_UUID')
25+
if module_uuid_val and module_uuid_val.decode() == uuid:
26+
parts = k.split('/')
27+
if len(parts) >= 2:
28+
return parts[1]
29+
except Exception:
30+
continue
31+
return None
32+
33+
34+
module_uuid = request.get('module_uuid')
35+
enable_integration = request.get('enable_integration', False)
36+
37+
if not module_uuid:
38+
print(json.dumps({'exit_code': 2, 'message': 'module_uuid is required'}))
39+
sys.exit(2)
40+
41+
# Resolve module uuid to module id
42+
module_id = resolve_module_id_from_uuid(module_uuid)
43+
if module_id is None:
44+
print(json.dumps({'exit_code': 2, 'message': f'module_uuid {module_uuid} not found via module/*/environment'}))
45+
sys.exit(2)
46+
47+
# Build agent id for module
48+
matrix_agent_id = f'module/{module_id}'
49+
50+
# Call get-configuration on current nethvoice module to obtain hostname
51+
response = agent.tasks.run(agent_id=os.environ['AGENT_ID'], action='get-configuration', data={})
52+
agent.assert_exp(response['exit_code'] == 0)
53+
54+
nconfig = response.get('result', {})
55+
nethvoice_host = nconfig.get('nethvoice_host', os.environ.get('NETHVOICE_HOST', ''))
56+
57+
# Call get-configuration on the matrix module and use its response for configure-module
58+
response = agent.tasks.run(agent_id=matrix_agent_id, action='get-configuration', data={})
59+
agent.assert_exp(response['exit_code'] == 0)
60+
61+
matrix_config = response.get('result', {}) or {}
62+
63+
if enable_integration:
64+
nethvoice_auth_url = f'https://{nethvoice_host}/freepbx/rest/testextauth' if nethvoice_host else '/freepbx/rest/testextauth'
65+
else:
66+
nethvoice_auth_url = ''
67+
68+
# Merge/override nethvoice_auth_url into matrix configuration
69+
matrix_config['nethvoice_auth_url'] = nethvoice_auth_url
70+
71+
# Call configure-module on matrix module with merged configuration
72+
response = agent.tasks.run(agent_id=matrix_agent_id, action='configure-module', data=matrix_config)
73+
agent.assert_exp(response['exit_code'] == 0)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Set Matrix server integration",
4+
"$id": "http://nethserver.org/json-schema/task/input/nethvoice/set-matrix-server",
5+
"description": "Enable or disable NethVoice integration on Matrix server",
6+
"type": "object",
7+
"required": ["module_uuid", "enable_integration"],
8+
"properties": {
9+
"module_uuid": {
10+
"description": "Module UUID of the Matrix module to configure",
11+
"type": "string"
12+
},
13+
"enable_integration": {
14+
"description": "Enable integration with NethVoice",
15+
"type": "boolean"
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)