NextCloud LDAP integration using FreeIPA and Docker

Hand's drawing a wireframe diagram
Photo by Kelly Sikkema on Unsplash

Introduction

In this tutorial you will learn how to run NextCloud and FreeIPA in seperate Docker containers and integrate them to enable a user saved in FreeIPA to log into NextCloud with the same credentials.

Video Tutorial

Contents

Install Docker

This guide includes installing Docker engine on Ubuntu. For other operating systems, Linux platforms, and alternative install methods I'll point you to these guides to

To install Docker on Ubuntu, first uninstall older versions of Docker with the following command:

sudo apt-get remove docker docker-engine docker.io containerd runc

Next set up the Docker repository:

sudo apt-get update
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

We can now install the latest version of Docker Engine and containerd

sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

FreeIPA setup

First we'll create the directory which will store server data on the host. Open a new terminal and run

sudo mkdir /var/lib/ipa-data

We'll then run the Free-IPA container with the following command (note some options are optional as explained below):

sudo docker run -d -h ipa.example.test --name my-freeipa-server -p 80:80 -p 443:443 -p 389:389 -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v /var/lib/ipa-data:/data:Z -e PASSWORD=Secret123 --sysctl net.ipv6.conf.all.disable_ipv6=0 freeipa/freeipa-server:centos-8-4.8.7 ipa-server-install -U -r EXAMPLE.TEST --no-ntp

Let's run through each command in turn:

-d
Optional. Runs the container in detached mode, allowing you to continue using this terminal.
-h ipa.example.test
Sets the hostname.
--name my-freeipa-server
Optional. Sets the container name to be used for Docker inspections etc.
-p 80:80 -p 443:443 -p 389:389
Maps ports in the contianer to ports on the host machine.
-v /sys/fs/cgroup:/sys/fs/cgroup:ro
May not be necessary depending on Docker version.
-v /var/lib/ipa-data:/data:Z
Configuration and data on volume mounted to /data directory in the container.
-e PASSWORD=Secret123
This will be the admin password.
--sysctl net.ipv6.conf.all.disable_ipv6=0
May not be neccessary. Add if you receive an error like "IPv6 stack is enabled in the kernel but there is no interface that has ::1 address assigned"
freeipa/freeipa-server:centos-8-4.8.7
FreeIPA image to pull from DockerHub. The tag can be changed depending on which version you want to use.
ipa-server-install -U -r EXAMPLE.TEST --no-ntp
FreeIPA specific install commands.

FreeIPA's Docker page provides the following commands to enroll the host to the FreeIPA server running in the container.

SERVER_IP_ADDRESS=$( docker inspect --format '{{ .NetworkSettings.IPAddress }}' my-freeipa-server)
echo $SERVER_IP_ADDRESS ipa.example.test >> /etc/hosts

Alternatively, you can edit /etc/hosts manually by running

sudo nano /etc/hosts

And adding the line

127.0.0.1    ipa.example.test   ipa-server

Replacing 127.0.0.1 with the IP address of your host machine.

We can now access the FreeIPA web interface at https://ipa.example.test/ipa/ui/

Screenshot of FreeIPA login page

You can now enter your admin password enterred in the Docker run command (Secret123 in our case) to access the identity management interface.

NextCloud setup

To setup NextCloud we'll run

sudo docker run --name my-nextcloud-container -p 9000:80 nextcloud

We map port 80 of the container to port 9000 of our development machine since FreeIPA is already using our local port 80.

We can now access the NextCloud dashboard at http://localhost:9000

Screenshot of NextCloud login page

NextCloud-FreeIPA LDAP integration

When you have created an admin account as you first log in to NextCloud you can then enable LDAP by going to the apps page and enabling "LDAP user and group backend"

NextCloud dropdown menu showing Apps option highlighted NextCloud option to enable LDAP user and group backend

You can access the LDAP configuration panel via the settings under LDAP/AD integration

NextCloud dropdown menu showing Settings option highlighted NextCloud settings menu showing LDAP/AD inegration highlighted

We can gather some LDAP configuration details from the FreeIPA container:

sudo docker exec my-freeipa-server ldapsearch -x uid admin
Results in temrninal of running sudo docker exec my-freeipa-server ldapsearch -x uid admin

We'll copy "uid=admin,cn=users,cn=accounts, dc=example,dc=test" as we'll need that for the User DN in NextCloud.

We'll now populate the server tab with configuration details, using Secret123 for the password

Host is 172.17.0.1 Port is 80
    User DN is uid=admin,cn=users,cn=accounts,dc=example,dc=test
    Password is Secret123
    Base DN is dc=example,dc=test

The address 172.17.0.1 can be replaced with host.docker.internal on Windows and MacOS as explained in this StackOverflow discussion.

On the Users tab we'll edit the LDAP query to (|(objectclass=*))

LDAP query is (|(objectclass=*))

On the Login Attributes tab we'll edit the LDAP query to (&(|(objectclass=*))(uid=%uid))

LDAP query is (&(|(objectclass=*))(uid=%uid))

On the Groups tab we'll edit the LDAP query to (|(cn=ipausers))

LDAP query is (|(cn=ipausers))

Next, we'll go to the Advanced tab and ensure the Configuration Active box is checked

Checked Configuration Active box

Under directory settings we'll set Base User Tree to cn=users,cn=accounts,dc=example,dc=test

And we'll set Base Group Tree to cn=groups,cn=accounts,dc=example,dc=test

We'll also set Group-Member association to uniqueMember

Direcotry Settings tab showing Base User Tree as
    cn=users,cn=accounts,dc=example,dc=test and Base Group Tree as cn=groups,cn=accounts,dc=example,dc=test and Group-Member association set to uniqueMember

In the Special Attributes section we'll set the Email Field to mail and User Home Folder Naming Rule to cn

Special Attributes tab showing Email Field set to mail and User Home Folder Naming Rule set to cn

You should now see the Configuration OK message

Configuration OK message

Users can now log into NextCloud using their credentials stored in FreeIPA. You can test this by adding a new user to FreeIPA or logging into NextCloud with your FreeIPA admin credentials (username: admin, password: Secret123).

NextCloud message showing Good Afternoon, Administrator

Possible Issues

I ran into a few issues which I'll list here with possible solutions and/or workarounds.

Can't connect to server

Firefox error page that says: We’re having trouble finding that site.

Container naming conflict

docker: Error response from daemon: Conflict. The container name "/my-freeipa-container" is already in use...

Either of the following two options should work

SEC_ERROR_REUSED_ ISSUER_AND_SERIAL

Firefox error page showing the message secure connection failed with the error code: SEC_ERROR_REUSED_ISSUER_AND_SERIAL

This error occurs when the ipa-install command is run with the same arguments twice. This can occur if you have removed /var/lib/ipa-data

PR_END_OF_FILE_ ERROR

Firefox error page with the message secure connection failed and the error code: PR_END_OF_FILE_ERROR

This happens when you have updated /etc/hosts and your Docker run commands correctly but have not deleted the old configuration information in /var/lib/ipa-data