Skip to content

Commit 9304b37

Browse files
committed
test: add in-memory ldap server to test app
1 parent d8bcc63 commit 9304b37

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

plugin-ldap/examples/retrieve-db-roles/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ dependencies {
3737
implementation 'org.webjars:bootstrap:4.1.3'
3838
implementation 'org.webjars:jquery:3.3.1'
3939

40+
// in-memory ldap server for testing
41+
implementation "com.unboundid:unboundid-ldapsdk:$unboundidLdapSdk"
42+
4043
runtimeOnly 'cloud.wondrify:asset-pipeline-grails'
4144
runtimeOnly 'com.h2database:h2'
4245
runtimeOnly 'com.zaxxer:HikariCP'

plugin-ldap/examples/retrieve-db-roles/grails-app/conf/application.groovy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ grails {
4747
authorityJoinClassName = 'com.test.UserRole'
4848
}
4949

50-
// http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
5150
ldap {
5251
context {
53-
managerDn = 'cn=read-only-admin,dc=example,dc=com'
54-
managerPassword = 'password'
55-
server = 'ldap://ldap.forumsys.com:389/' //'ldap://[ip]:[port]/'
52+
managerDn = 'cn=admin,dc=example,dc=com'
53+
managerPassword = 'secret'
54+
server = System.getProperty('grails.test.ldap.url')
5655
}
5756
authorities {
5857
ignorePartialResultException = true

plugin-ldap/examples/retrieve-db-roles/grails-app/init/com/test/Application.groovy

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,135 @@
1919

2020
package com.test
2121

22+
import com.unboundid.ldap.listener.InMemoryDirectoryServer
23+
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig
24+
import com.unboundid.ldap.listener.InMemoryListenerConfig
25+
import com.unboundid.ldap.sdk.Attribute
26+
import com.unboundid.ldap.sdk.Entry
27+
2228
import grails.boot.GrailsApp
2329
import grails.boot.config.GrailsAutoConfiguration
2430

2531
import groovy.transform.CompileStatic
2632

2733
@CompileStatic
2834
class Application extends GrailsAutoConfiguration {
35+
36+
static InMemoryDirectoryServer directoryServer
37+
static private Entry scientistsUnit
38+
2939
static void main(String[] args) {
40+
def config = new InMemoryDirectoryServerConfig('dc=example,dc=com')
41+
config.addAdditionalBindCredentials('cn=admin,dc=example,dc=com', 'secret')
42+
config.setListenerConfigs(
43+
InMemoryListenerConfig.createLDAPConfig(
44+
'default',
45+
null,
46+
0,
47+
null,
48+
false,
49+
false
50+
)
51+
)
52+
53+
directoryServer = new InMemoryDirectoryServer(config)
54+
def base = new Entry(
55+
'dc=example,dc=com',
56+
new Attribute('objectClass', 'top', 'domain'),
57+
new Attribute('dc', 'example')
58+
)
59+
directoryServer.add(base)
60+
61+
def people = new Entry(
62+
'ou=people,dc=example,dc=com',
63+
new Attribute('objectClass', 'top', 'organizationalUnit'),
64+
new Attribute('ou', 'people')
65+
)
66+
directoryServer.add(people)
67+
68+
def mathematiciansUnit = new Entry(
69+
'ou=mathematicians,dc=example,dc=com',
70+
new Attribute('objectClass', 'top', 'organizationalUnit'),
71+
new Attribute('ou', 'mathematicians')
72+
)
73+
directoryServer.add(mathematiciansUnit)
74+
75+
scientistsUnit = new Entry(
76+
'ou=scientists,dc=example,dc=com',
77+
new Attribute('objectClass', 'top', 'organizationalUnit'),
78+
new Attribute('ou', 'scientists')
79+
)
80+
directoryServer.add(scientistsUnit)
81+
82+
def jane = new Entry(
83+
'uid=jane,ou=people,dc=example,dc=com',
84+
new Attribute('objectClass', 'inetOrgPerson'),
85+
new Attribute('uid', 'jane'),
86+
new Attribute('cn', 'Jane Doe'),
87+
new Attribute('sn', 'Doe'),
88+
new Attribute('mail', '[email protected]'),
89+
new Attribute('telephoneNumber', '+1 555 111 2222'),
90+
new Attribute('userPassword', 'password')
91+
)
92+
directoryServer.add(jane)
93+
94+
['riemann', 'gauss', 'euler', 'euclid'].each { uid ->
95+
directoryServer.add(new Entry(
96+
"uid=$uid,ou=mathematicians,dc=example,dc=com" as String,
97+
new Attribute('objectClass', 'inetOrgPerson'),
98+
new Attribute('uid', uid),
99+
new Attribute('cn', uid.capitalize()),
100+
new Attribute('sn', uid.capitalize()),
101+
new Attribute('userPassword', 'password')
102+
))
103+
}
104+
105+
def mathGroup = new Entry(
106+
'cn=mathematicians,ou=mathematicians,dc=example,dc=com',
107+
new Attribute('objectClass', 'top', 'groupOfUniqueNames'),
108+
new Attribute('cn', 'mathematicians'),
109+
new Attribute('uniqueMember',
110+
'uid=riemann,ou=mathematicians,dc=example,dc=com',
111+
'uid=gauss,ou=mathematicians,dc=example,dc=com',
112+
'uid=euler,ou=mathematicians,dc=example,dc=com',
113+
'uid=euclid,ou=mathematicians,dc=example,dc=com'
114+
)
115+
)
116+
directoryServer.add(mathGroup)
117+
118+
['einstein', 'newton', 'galieleo', 'tesla'].each { uid ->
119+
directoryServer.add(new Entry(
120+
"uid=$uid,ou=scientists,dc=example,dc=com" as String,
121+
new Attribute('objectClass', 'inetOrgPerson'),
122+
new Attribute('uid', uid),
123+
new Attribute('cn', uid.capitalize()),
124+
new Attribute('sn', uid.capitalize()),
125+
new Attribute('userPassword', 'password')
126+
))
127+
}
128+
def scientistGroup = new Entry(
129+
'cn=scientists,ou=scientists,dc=example,dc=com',
130+
new Attribute('objectClass', 'top', 'groupOfUniqueNames'),
131+
new Attribute('cn', 'scientists'),
132+
new Attribute('uniqueMember',
133+
'uid=einstein,ou=scientists,dc=example,dc=com',
134+
'uid=newton,ou=scientists,dc=example,dc=com',
135+
'uid=galieleo,ou=scientists,dc=example,dc=com',
136+
'uid=tesla,ou=scientists,dc=example,dc=com'
137+
)
138+
)
139+
directoryServer.add(scientistGroup)
140+
directoryServer.startListening()
141+
System.setProperty('grails.test.ldap.url', "ldap://localhost:$directoryServer.listenPort")
142+
30143
GrailsApp.run(Application, args)
31144
}
145+
146+
@Override
147+
void onShutdown(Map<String, Object> event) {
148+
if (directoryServer) {
149+
directoryServer.close()
150+
directoryServer = null
151+
}
152+
}
32153
}

0 commit comments

Comments
 (0)