nickjer / singularity-rstudio

RStudio Server in a Singularity container
https://singularity-hub.org/collections/463
MIT License
54 stars 41 forks source link

Add script and instuctions for LDAP authentication #2

Closed nhoffman closed 6 years ago

nhoffman commented 6 years ago

Here is a solution that I came up with to provide authentication to RStudio sessions using LDAP. I completely understand if you'd prefer not to add (or field questions about) this functionality - if not, I can most likely find a way to provide the LDAP authentication script separately, or just maintain a fork that provides it.

Thanks a lot, Noah

nickjer commented 6 years ago

I can definitely add this. Although I have a few questions/comments:

Here is an example using the ldapsearch command since I am not experienced enough to use Python and the ldap3 package:

$ # First search for DN of uid=nickjer
$ ldapsearch -x -H 'ldaps://xxxxxx:636' -b 'ou=People,ou=xxx,o=xxx' uid=nickjer

dn: uid=nickjer,ou=People,ou=xxx,o=xxx
uidNumber: 12345
uid: nickjer
cn: nickjer
loginShell: /bin/bash
...

$ # We found an entry...
$ # Now we bind to the DN found previously and read in password from STDIN
$ ldapsearch -x -H 'ldaps://xxxxxx:636' -b 'ou=People,ou=xxx,o=xxx' -D 'uid=nickjer,ou=People,ou=xxx,o=xxx' -W uid=nickjer
Enter LDAP Password:

dn: uid=nickjer,ou=People,ou=xxx,o=xxx
uidNumber: 12345
uid: nickjer
cn: nickjer
loginShell: /bin/bash
...
nhoffman commented 6 years ago

@nickjer - sorry for the delay in responding to this, and thanks for reviewing. I attempted to rebase your changes in the interim; hopefully I did this correctly.

If you are willing to test this, I think that it should provide a fairly general solution without introducing additional dependencies. If need be I can provide environment variables for other parameters such as the port used by the ldap host (by default 636), but I doubt that this is frequently necessary.

Note that you can also test the LDAP auth in isolation:

cd singularity-rstudio 
python3 -m venv py3-env
source py3-env/bin/activate
pip install ldap3  # may require python3-pip
export LDAP_HOST=...
export LDAP_USER_DN=...
./ldap_auth.py <user> --cert-file /path/to/cert.pem
Password:
Success
nickjer commented 6 years ago

Thanks for updating it. I'll test it later this week with the LDAP server we use here and if all goes well merge it in. Although I did notice you are missing:

pip3 install ldap3

in the Singularity.3.4.3 file.

nhoffman commented 6 years ago

missing pip3 install ldap3 in the Singularity.3.4.3 file.

Thanks, fixed.

nickjer commented 6 years ago

Thanks @nhoffman this works great! But I do have one last suggestion. The place I work at uses valid SSL certificates so it is not necessary to supply a certificate file, as the system-wide certificate store supplied by Linux will work out of the box.

So would it be better to fallback to the default SSL certificate store if LDAP_CERT_FILE or its corresponding argument are not defined?

Otherwise I will need to run...

export LDAP_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

to use the system's certificates. I believe you can use system store if you specify None in the Python script.

nhoffman commented 6 years ago

@nickjer - great suggestion - I pushed a change to my fork that I think should work. Unfortunately the instructions for LDAP auth in the readme have become somewhat verbose - I hope they make sense.