RFC: Implement a module-agnostic mod_admin_rest
#5
RemiBardon
started this conversation in
Ideas
Replies: 0 comments
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Why?
In 5252af0, I added support for 4 functions from
mod_groups_internal
tomod_admin_rest
. It took me 100 lines of Lua and 100 lines of documentation… for literally 4 lines of business logic.The
mod_admin_rest
we use is a fork from wltsmrz/mod_admin_rest, for which the last commit it 7 years old. During this period, Prosody evolved a lot, and integrated better authentication and more granular roles.mod_admin_rest
uses basic authentication, which is okay for us since we use it for inter-process communication, but we wouldn't say no to a more secure and granular alternative.What?
I just had a thought: why don't we create a general-purpose REST API which could interact with any enabled prosody module?
How?
One route:
POST /<module_name>/<function_name>
where<module_name>
is the module name without themod_
prefix and<function_name>
is the fully qualified function name (i.e.object.method
is allowed).Because Lua functions can take arguments and aren't idempotent, the route uses the
POST
HTTP method and accepts a body. This body is a JSON1 array containing the function arguments (scalar types, arrays or objects).The route returns a JSON1 array containing all the values returned by the function (scalar types, arrays or objects). This could be done using table constructors in Lua.
Finally, since there is no standard way of handling errors in Lua, the route will always return
200 Success
(unless authorization failed) and it'll be to the API user to interpret the response.Security considerations
prosody:operator
role (which our admin account has).mod_admin_rest
is responsible for the consequences of their module function invocations. If some module exposes a public function which should belocal
or which breaks things when invoked,mod_admin_rest
shouldn't prohibit its usage. A Prosody operator would have broken things the same way, with or withoutmod_admin_rest
.mod_admin_rest
. If the module is both enabled and allowed, then the call succeeds.Notes
nil
elements are not lost when serializing Lua objects to JSON.mod_admin_rest
.Footnotes
At first,
mod_admin_rest
's route will only accept and return JSON payloads. However, given the very simple data structure it accepts and returns, we could easily support other content types. ↩ ↩2Beta Was this translation helpful? Give feedback.
All reactions