Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the optional feature to specify module_path using mosquitto config #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ to the rescue):

auth_opt_pyauth_module some_module

### Optional
Instead of specifying the search path for Python module in `PYTHONPATH` env variable,
add the following line to `mosquitto.conf`:

auth_opt_pyauth_module_path /home/user/module_path

Replace /home/user/module_path with the actual module path

`auth_opt_pyauth_module_path` is required if mosquitto broker is run using `service` e.g. sudo service mosquitto start

`service` removes all environment variables including `PYTHONPATH`

Python module
=============

Expand Down
8 changes: 8 additions & 0 deletions auth_plugin_pyauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

struct pyauth_data {
char *module_name;
char *module_path;
PyObject *module;
PyObject *plugin_cleanup_func;
PyObject *unpwd_check_func;
Expand Down Expand Up @@ -143,6 +144,9 @@ int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth
data->module_name = strdup(auth_opts[i].value);
debug("pyauth_module = %s", data->module_name);
}
else if (!strcmp(auth_opts[i].key, "pyauth_module_path")) {
data->module_path = strdup(auth_opts[i].value);
}
}
if (data->module_name == NULL)
die(false, "pyauth_module config param missing");
Expand All @@ -157,6 +161,9 @@ int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth
die(false, "failed to initialize auxiliary module");
#endif

if(data->module_path != NULL){
PySys_SetPath(data->module_path);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gripe with PySys_SetPath is that it replaces sys.path completely with new value. It's unintuitive for any user used to PYTHONPATH environment variable, which extends default path instead of replacing it. A C version of following code would be preferable:

import sys
sys.path.insert(0, data->module_path.split(':'))

If you feel like implementing it, then go ahead. Otherwise, ping me and I'll merge your PR and add my own fix on top of it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relacing PySys_SetPath(data->module_path);

with

PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyString_FromString(data->module_path));

yields the result you're after.

I got that code from here https://stackoverflow.com/a/8859538/277267 and checked if it works

/ # mosquitto -c /mosquitto/config/mosquitto.conf
1547320203: mosquitto version 1.5.5 starting
1547320203: Config loaded from /mosquitto/config/mosquitto.conf.
1547320203: plugin_init (opts: ((u'pyauth_module_append_path', u'/mosquitto/scripts'),))
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/pyt hon2.7/lib-dynload', '/usr/lib/python2.7/site-packages', '/mosquitto/scripts']

}
data->module = PyImport_ImportModule(data->module_name);
if (data->module == NULL)
die(true, "failed to import module: %s", data->module_name);
Expand Down Expand Up @@ -210,6 +217,7 @@ int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_auth_opt *au
Py_XDECREF(data->security_cleanup_func);
Py_XDECREF(data->psk_key_get_func);
free(data->module_name);
free(data->module_path);
free(data);
return MOSQ_ERR_SUCCESS;
}
Expand Down