Skip to content

Commit d6a61af

Browse files
author
Gauvain Pocentek
committed
Add support for LDAP groups
1 parent a6512f9 commit d6a61af

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

docs/gl_objects/groups.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,18 @@ Remove a member from the group::
171171
group.members.delete(member_id)
172172
# or
173173
member.delete()
174+
175+
LDAP group links
176+
================
177+
178+
Add an LDAP group link to an existing GitLab group::
179+
180+
group.add_ldap_group_link(ldap_group_cn, gitlab.DEVELOPER_ACCESS, 'main')
181+
182+
Remove a link::
183+
184+
group.delete_ldap_group_link(ldap_group_cn, 'main')
185+
186+
Sync the LDAP groups::
187+
188+
group.ldap_sync()

gitlab/v4/objects.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,61 @@ def search(self, scope, search, **kwargs):
807807
path = '/groups/%d/search' % self.get_id()
808808
return self.manager.gitlab.http_list(path, query_data=data, **kwargs)
809809

810+
@cli.register_custom_action('Group', ('cn', 'group_access', 'provider'))
811+
@exc.on_http_error(exc.GitlabCreateError)
812+
def add_ldap_group_link(self, cn, group_access, provider, **kwargs):
813+
"""Add an LDAP group link.
814+
815+
Args:
816+
cn (str): CN of the LDAP group
817+
group_access (int): Minimum access level for members of the LDAP
818+
group
819+
provider (str): LDAP provider for the LDAP group
820+
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
821+
822+
Raises:
823+
GitlabAuthenticationError: If authentication is not correct
824+
GitlabCreateError: If the server cannot perform the request
825+
"""
826+
path = '/groups/%d/ldap_group_links' % self.get_id()
827+
data = {'cn': cn, 'group_access': group_access, 'provider': provider}
828+
self.manager.gitlab.http_post(path, post_data=data, **kwargs)
829+
830+
@cli.register_custom_action('Group', ('cn',), ('provider',))
831+
@exc.on_http_error(exc.GitlabDeleteError)
832+
def delete_ldap_group_link(self, cn, provider=None, **kwargs):
833+
"""Delete an LDAP group link.
834+
835+
Args:
836+
cn (str): CN of the LDAP group
837+
provider (str): LDAP provider for the LDAP group
838+
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
839+
840+
Raises:
841+
GitlabAuthenticationError: If authentication is not correct
842+
GitlabDeleteError: If the server cannot perform the request
843+
"""
844+
path = '/groups/%d/ldap_group_links' % self.get_id()
845+
if provider is not None:
846+
path += '/%s' % provider
847+
path += '/%s' % cn
848+
self.manager.gitlab.http_delete(path)
849+
850+
@cli.register_custom_action('Group')
851+
@exc.on_http_error(exc.GitlabCreateError)
852+
def ldap_sync(self, **kwargs):
853+
"""Sync LDAP groups.
854+
855+
Args:
856+
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
857+
858+
Raises:
859+
GitlabAuthenticationError: If authentication is not correct
860+
GitlabCreateError: If the server cannot perform the request
861+
"""
862+
path = '/groups/%d/ldap_sync' % self.get_id()
863+
self.manager.gitlab.http_post(path, **kwargs)
864+
810865

811866
class GroupManager(CRUDMixin, RESTManager):
812867
_path = '/groups'

tools/ee-test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
P2 = 'root/project2'
88
I_P1 = 1
99
I_P2 = 1
10+
G1 = 'group1'
11+
LDAP_CN = 'app1'
12+
LDAP_PROVIDER = 'ldapmain'
1013

1114

1215
def start_log(message):
@@ -22,6 +25,7 @@ def end_log():
2225
project2 = gl.projects.get(P2)
2326
issue_p1 = project1.issues.get(I_P1)
2427
issue_p2 = project2.issues.get(I_P2)
28+
group1 = gl.groups.get(G1)
2529

2630
start_log('MR approvals')
2731
approval = project1.approvals.get()
@@ -52,3 +56,13 @@ def end_log():
5256
link_id = links[0].issue_link_id
5357
issue_p1.links.delete(link_id)
5458
end_log()
59+
60+
start_log('LDAP links')
61+
# bit of cleanup just in case
62+
if hasattr(group1, 'ldap_group_links'):
63+
for link in group1.ldap_group_links:
64+
group1.delete_ldap_group_link(link['cn'], link['provider'])
65+
group1.add_ldap_group_link(LDAP_CN, 30, LDAP_PROVIDER)
66+
group1.ldap_sync()
67+
group1.delete_ldap_group_link(LDAP_CN)
68+
end_log()

0 commit comments

Comments
 (0)