Skip to content

Commit

Permalink
fix: ldap connection with tls (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo authored Jan 14, 2025
1 parent 3221fa6 commit 0b72e85
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 26 deletions.
15 changes: 14 additions & 1 deletion adminsec/ldap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging as _logging
import ssl

import ldap3
from django.conf import settings
Expand Down Expand Up @@ -34,7 +35,19 @@ def connect(self):

if settings.ENABLE_LDAP:
logger.debug("LDAP enabled")
server1 = ldap3.Server(settings.AUTH_LDAP_SERVER_URI)
ssl_options = {}
url = settings.AUTH_LDAP_SERVER_URI

if settings.AUTH_LDAP_START_TLS and not self.test_mode:
url = settings.AUTH_LDAP_SERVER_URI.replace("ldap://", "ldaps://")
ssl_options = {
"tls": ldap3.Tls(
ca_certs_file=settings.AUTH_LDAP_CA_CERT_FILE,
validate=ssl.CERT_REQUIRED,
),
}

server1 = ldap3.Server(url, **ssl_options)

logger.debug("Connecting to LDAP server: %s", settings.AUTH_LDAP_SERVER_URI)
self.connection1 = ldap3.Connection(
Expand Down
1 change: 1 addition & 0 deletions adminsec/tests/test_ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
LDAP_DEFAULT_MOCKS = {
"ENABLE_LDAP": ENABLE_LDAP,
"ENABLE_LDAP_SECONDARY": ENABLE_LDAP_SECONDARY,
"AUTH_LDAP_START_TLS": False,
"AUTH_LDAP_SERVER_URI": AUTH_LDAP_SERVER_URI,
"AUTH_LDAP2_SERVER_URI": AUTH_LDAP2_SERVER_URI,
"AUTH_LDAP_BIND_DN": AUTH_LDAP_BIND_DN,
Expand Down
2 changes: 1 addition & 1 deletion usersec/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def clean(self):
"email",
(
"This is no institute email address. "
f"Valid domains are: {', '.join(valid_domains)}",
f"Valid domains are: {', '.join(valid_domains)}"
),
)
return
Expand Down
6 changes: 4 additions & 2 deletions usersec/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.conf import settings

from adminsec.rules import is_hpcadmin
from usersec.models import REQUEST_STATUS_ACTIVE, HpcGroupInvitation
from usersec.models import INVITATION_STATUS_PENDING, REQUEST_STATUS_ACTIVE, HpcGroupInvitation

# ------------------------------------------------------------------------------
# Predicates
Expand All @@ -25,7 +25,9 @@ def _has_pending_group_request(user):

@rules.predicate
def _has_group_invitation(user):
return HpcGroupInvitation.objects.filter(username=user.username).exists()
return HpcGroupInvitation.objects.filter(
username=user.username, status=INVITATION_STATUS_PENDING
).exists()


@rules.predicate
Expand Down
61 changes: 42 additions & 19 deletions usersec/templates/usersec/hpcgroupinvitation_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,50 @@

{% block content %}

<h1>
You've been invited to the BIH cluster to join group
<span class="text-danger">{{ object.hpcusercreaterequest.group.name }}</span>
</h1>
{% if object.status == "REJECTED" %}

<h3 class="mt-4">Terms &amp; Conditions</h3>
<h2 class="mt-4">
You've rejected your invitation to join group
<strong>{{ object.hpcusercreaterequest.group.name }}</strong>.
</h2>

{% for obj in terms_list %}
<h4 class="mt-4">{{ obj.title }}</h4>
<p>{{ obj.text }}</p>
<div class="input-group mb-4">
<span class="input-group-text">
<input type="checkbox" class="form-check-input m-0 consent" name="consent{{ forloop.counter }}" id="id_consent{{ forloop.counter }}">
</span>
<label for="id_consent{{ forloop.counter }}">
<span class="form-control fw-bold">
I consent to <em>{{ obj.title }}</em>.
<p>
<a class="btn btn-secondary" href="{% url 'home' %}">
Return to index page
</a>
</p>

{% else %}

<h2 class="mt-4">
You've been invited to the BIH cluster
</h2>

<p class="lead">
You've been invited to the HPC cluster to join the group
<strong>{{ object.hpcusercreaterequest.group.name }}</strong>.
You can accept or reject the invitation below.
Please confirm any conditions.
</p>

{% if terms_list %}
<h3 class="mt-4">Terms &amp; Conditions</h3>

{% for obj in terms_list %}
<h4 class="mt-4">{{ obj.title }}</h4>
<p>{{ obj.text }}</p>
<div class="input-group mb-4">
<span class="input-group-text">
<input type="checkbox" class="form-check-input m-0 consent" name="consent{{ forloop.counter }}" id="id_consent{{ forloop.counter }}">
</span>
</label>
</div>
{% endfor %}
<label for="id_consent{{ forloop.counter }}">
<span class="form-control fw-bold">
I consent to <em>{{ obj.title }}</em>.
</span>
</label>
</div>
{% endfor %}
{% endif %}

<a
class="btn btn-success disabled"
Expand All @@ -40,7 +63,7 @@ <h4 class="mt-4">{{ obj.title }}</h4>
<i class="iconify" data-icon="mdi:cancel"></i>
Reject
</a>

{% endif %}
{% endblock content %}

{% block inline_javascript %}
Expand Down
5 changes: 4 additions & 1 deletion usersec/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ def test_form_invalid_email_wrong_domain(self):
data_invalid = {**self.data_valid, "email": "[email protected]"}
form = HpcUserCreateRequestForm(user=self.user, data=data_invalid)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors["email"], ["No institute email address."])
self.assertEqual(
form.errors["email"],
["This is no institute email address. Valid domains are: charite.de, mdc-berlin.de"],
)

def test_form_valid_hpcadmin(self):
form = HpcUserCreateRequestForm(user=self.user_hpcadmin, data=self.data_valid)
Expand Down
7 changes: 6 additions & 1 deletion usersec/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4123,7 +4123,12 @@ def test_hpc_group_invitation_reject_view_post(self):
),
)
self.assert_permissions_on_url(
bad_users, url, "POST", 302, redirect_url=reverse("home"), not_authorized=True
bad_users,
url,
"POST",
302,
redirect_url=reverse("home"),
not_authorized=True,
)

def test_hpc_project_invitation_accept_view(self):
Expand Down
5 changes: 4 additions & 1 deletion usersec/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
)
from usersec.models import (
INVITATION_STATUS_ACCEPTED,
INVITATION_STATUS_PENDING,
INVITATION_STATUS_REJECTED,
OBJECT_STATUS_ACTIVE,
REQUEST_STATUS_ACTIVE,
Expand Down Expand Up @@ -152,7 +153,9 @@ def get(self, request, *args, **kwargs):
)

if rules.test_rule("usersec.has_group_invitation", request.user):
invitation = HpcGroupInvitation.objects.get(username=request.user.username)
invitation = HpcGroupInvitation.objects.get(
username=request.user.username, status=INVITATION_STATUS_PENDING
)
return redirect(
reverse(
"usersec:hpcgroupinvitation-detail",
Expand Down

0 comments on commit 0b72e85

Please sign in to comment.