Major changes, please review code - #1
Conversation
|
Thanks for your improvments ! This module is in testing phase, and I had some issues with configuration settings. |
|
See my comments in your changes, I think we could have a good LDAP module after that ! :) 👍 |
|
Sorry, I was a little lazy while committing... Unfortunately i cannot see any comments. Regarding the LDAP Credentials: Yes the kill() in init is only a workaround and surely should be improved.
With the settings().remove() and kill, you just can just restart with an disabled plugin |
|
|
||
| # The plugin's identifier, has to be unique | ||
| plugin_identifier = "auth_ldap" | ||
| plugin_identifier = "authldap" |
There was a problem hiding this comment.
Had to rename the plugin, as the settings could not be stored otherwise.
|
|
||
| <label class="control-label">{{ _('Search base pattern') }}</label> | ||
| <div class="controls"> | ||
| <input type="text" class="input-block-level" data-bind="value: settings.plugins.authldap.ldap_search_base" |
There was a problem hiding this comment.
This setting controls where to look for Users
|
|
||
| <label class="control-label">{{ _('LDAP Filter') }}</label> | ||
| <div class="controls"> | ||
| <input type="text" class="input-block-level" data-bind="value: settings.plugins.authldap.ldap_query" |
There was a problem hiding this comment.
As you maybe want to restrict which users can access OctoPrint, you should be able to set a filter.
If this filter returns a result, the user can log in.
{uid} will be replaced with the user trying to login.
I saw this kind of configuration also on other systems.
| <label class="control-label">{{ _('Default Roles:') }}</label> | ||
| <div class="controls"> | ||
| <input type="text" class="input-block-level" data-bind="value: settings.plugins.authldap.roles" | ||
| placeholder="Comma separated list of roles: user,admin"> |
There was a problem hiding this comment.
Self explanatory.
This will give the newly created users specific roles. Currently Octoprint only differs between user and administrator rights
| </div> | ||
| <div class="controls"> | ||
| <label class="checkbox"> | ||
| <input type="checkbox" data-bind="checked: settings.plugins.authldap.auto_activate">Automatically activate users? |
There was a problem hiding this comment.
In case you want new users to be created, but not able to login.
I'm not sure if it makes sense to disable this, maybe this functionality could be removed
| <h4>Authentication</h4> | ||
| <label class="control-label">{{ _('Binding User DN') }}</label> | ||
| <div class="controls"> | ||
| <input type="text" class="input-block-level" data-bind="value: settings.plugins.authldap.ldap_bind_user" |
There was a problem hiding this comment.
This is the service user, which is used for querying the LDAP. Do not confuse it with the user trying to login later.
Once OctoPrint has a more granular user access control, you could also query for groups, emails, etc.
| </div> | ||
| <label class="control-label">{{ _('Password') }}</label> | ||
| <div class="controls"> | ||
| <input type="password" class="input-block-level" data-bind="value: settings.plugins.authldap.ldap_bind_password"> |
There was a problem hiding this comment.
Password of service user. Should be stored differently...
| <label class="control-label">{{ _('Method:') }}</label> | ||
| <div class="controls"> | ||
| <select data-bind="value: settings.plugins.authldap.ldap_method"> | ||
| <option value="BASIC">Basic</option> |
There was a problem hiding this comment.
Only used to differ between LDAPs and LDAP, this helped a little bit debugging, but could be replaced with a regex on the uri
| </div> | ||
| <label class="control-label">{{ _('TLS certification check') }}</label> | ||
| <div class="controls"> | ||
| <select data-bind="value: settings.plugins.authldap.ldap_tls_reqcert"> |
| </div> | ||
| <div class="control-group"> | ||
| <h4>Activation</h4> | ||
| <p>{% trans %} |
There was a problem hiding this comment.
It makes sense to have a way to activate/deactivate the plugin in the settings.
As I'm not aware of any easy way to directly set the userManager setting from here, I implemented it with a local property and on_settings_save.
|
|
||
| global __plugin_hooks__ | ||
| __plugin_hooks__ = { | ||
| "octoprint.plugin.softwareupdate.check_config": |
There was a problem hiding this comment.
Removed the octoprint.users.factory hook and switched to the userManager setting.
|
|
||
| _localUserManager = FilebasedUserManager() | ||
|
|
||
| def __init__(self): |
There was a problem hiding this comment.
This function should check if the plugin is working as expected while loading.
If not it should fall back to the FilebasedUserManager.
This could be achieved by raising an exception, which however disables also the settings page, so fixing the configuration from the UI would not be possible.
| return | ||
| except: | ||
| pass | ||
| settings().remove(["accessControl", "userManager"]) |
| settings().remove(["accessControl", "userManager"]) | ||
| settings().remove(["plugins", "authldap", "active"]) | ||
| settings().save() | ||
| os.system('kill $PPID') |
There was a problem hiding this comment.
This is only to not start OctoPrint with a broken configuration.
| @@ -0,0 +1,271 @@ | |||
| # coding=utf-8 | |||
There was a problem hiding this comment.
Had to change folder name due to renaming the plugin...
| @@ -0,0 +1,79 @@ | |||
| <form class="form-horizontal"> | |||
There was a problem hiding this comment.
Changed filename to to renaming of the plugin, also implemented settings as proposed in the best practice
|
|
||
| def get_update_information(self): | ||
| return dict( | ||
| authldap=dict( |
There was a problem hiding this comment.
I guess this part was copied over. Corrected it.
| self._logger.debug("LDAP is using TLS, setting ldap options...") | ||
| ldap_verifypeer = settings().get( | ||
| ["plugins", "authldap", "ldap_tls_reqcert"]) | ||
| verifypeer = ldap.OPT_X_TLS_HARD |
There was a problem hiding this comment.
Added all other kinds of TLS Verification.
If OPT_X_TLS_* will be directly written in the settings, the if/elif could be removed
| ldap_bind_password = None | ||
| ) | ||
|
|
||
| def on_settings_save(self, data): |
There was a problem hiding this comment.
This part only registers/unregisters the UserManager in OctoPrint. A better solution would be to directly write into the accessControl.userManager setting, this is only quick and dirty, but redundant...
| password = ''.join([random.choice(string.lowercase) for i in range(10)]) | ||
| if settings().get(["plugins", "authldap", "roles"]) is not None: | ||
| roles = [x.strip() for x in settings().get(["plugins", "authldap", "roles"]).split(',')] | ||
| self._localUserManager.addUser(userid, password, active=settings().getBoolean(["plugins", "authldap", "auto_activate"]), roles=roles) |
There was a problem hiding this comment.
findUser is creating LDAP users once searched.
If (for some strange reasons) Octoprint is searching for random people, they all would be added to the Users list with default permissions.
I'm not sure if this could lead into a problem...
However once OctoPrint as a more sophisticated user account control, you could control LDAP users without them logged in once or manually created.
| def findUser(self, userid=None, apikey=None, session=None): | ||
| user = UserManager.findUser(self, userid=userid, session=session) | ||
|
|
||
| if user is not None: |
There was a problem hiding this comment.
Just some checks for already logged in users or api access
"Copied" from FilebasedUserManager
|
I hope this helps to understand my code. |
|
Many thanks for all your comments !!! I have no time to read all immediately but I will check this soon ! |
|
@Flow555 I add you to main project collabs, so you can participate to the new PR #3 and push directly on branch https://github.com/gillg/OctoPrint-LDAP/tree/initial-refactoring |
No description provided.