Skip to content

Commit 46a774c

Browse files
gtullyjbonofre
authored andcommitted
AMQ-8035 - ensure propagated credentials are visible for bind and removed for subsequent mapping operations
(cherry picked from commit 73e2916)
1 parent abd531f commit 46a774c

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

activemq-jaas/src/main/java/org/apache/activemq/jaas/LDAPLoginModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ protected boolean bindUser(DirContext context, String dn, String password) throw
440440
if (log.isDebugEnabled()) {
441441
log.debug("Binding the user.");
442442
}
443+
context.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
443444
context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
444445
context.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
445446
try {
@@ -465,7 +466,7 @@ protected boolean bindUser(DirContext context, String dn, String password) throw
465466
} else {
466467
context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
467468
}
468-
469+
context.addToEnvironment(Context.SECURITY_AUTHENTICATION, getLDAPPropertyValue(AUTHENTICATION));
469470
return isValid;
470471
}
471472

activemq-jaas/src/test/java/org/apache/activemq/jaas/LDAPLoginModuleTest.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
2020
import org.apache.directory.server.core.integ.FrameworkRunner;
21-
import org.apache.directory.server.integ.ServerIntegrationUtils;
2221
import org.apache.directory.server.ldap.LdapServer;
2322
import org.apache.directory.server.annotations.CreateLdapServer;
2423
import org.apache.directory.server.annotations.CreateTransport;
@@ -34,11 +33,11 @@
3433
import javax.naming.directory.DirContext;
3534
import javax.naming.directory.InitialDirContext;
3635
import javax.security.auth.callback.*;
36+
import javax.security.auth.login.FailedLoginException;
3737
import javax.security.auth.login.LoginContext;
3838
import javax.security.auth.login.LoginException;
3939

4040
import java.io.IOException;
41-
import java.net.URL;
4241
import java.util.HashSet;
4342
import java.util.Hashtable;
4443

@@ -47,7 +46,7 @@
4746
import static org.junit.Assert.fail;
4847

4948
@RunWith ( FrameworkRunner.class )
50-
@CreateLdapServer(transports = {@CreateTransport(protocol = "LDAP", port=1024)})
49+
@CreateLdapServer(transports = {@CreateTransport(protocol = "LDAP", port=1024)}, allowAnonymousAccess = true)
5150
@ApplyLdifFiles(
5251
"test.ldif"
5352
)
@@ -172,4 +171,47 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
172171
}
173172

174173

174+
@Test
175+
public void testAuthenticatedViaBindOnAnonConnection() throws Exception {
176+
LoginContext context = new LoginContext("AnonBindCheckUserLDAPLogin", new CallbackHandler() {
177+
@Override
178+
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
179+
for (int i = 0; i < callbacks.length; i++) {
180+
if (callbacks[i] instanceof NameCallback) {
181+
((NameCallback) callbacks[i]).setName("first");
182+
} else if (callbacks[i] instanceof PasswordCallback) {
183+
((PasswordCallback) callbacks[i]).setPassword("wrongSecret".toCharArray());
184+
} else {
185+
throw new UnsupportedCallbackException(callbacks[i]);
186+
}
187+
}
188+
}
189+
});
190+
try {
191+
context.login();
192+
fail("Should have failed authenticating");
193+
} catch (FailedLoginException expected) {
194+
}
195+
}
196+
197+
@Test
198+
public void testAuthenticatedOkViaBindOnAnonConnection() throws Exception {
199+
LoginContext context = new LoginContext("AnonBindCheckUserLDAPLogin", new CallbackHandler() {
200+
@Override
201+
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
202+
for (int i = 0; i < callbacks.length; i++) {
203+
if (callbacks[i] instanceof NameCallback) {
204+
((NameCallback) callbacks[i]).setName("first");
205+
} else if (callbacks[i] instanceof PasswordCallback) {
206+
((PasswordCallback) callbacks[i]).setPassword("secret".toCharArray());
207+
} else {
208+
throw new UnsupportedCallbackException(callbacks[i]);
209+
}
210+
}
211+
}
212+
});
213+
context.login();
214+
context.logout();
215+
}
216+
175217
}

activemq-jaas/src/test/resources/login.config

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ UnAuthenticatedLDAPLogin {
8888
;
8989
};
9090

91+
AnonBindCheckUserLDAPLogin {
92+
org.apache.activemq.jaas.LDAPLoginModule required
93+
debug=true
94+
initialContextFactory=com.sun.jndi.ldap.LdapCtxFactory
95+
connectionURL="ldap://localhost:1024"
96+
connectionUsername=none
97+
connectionPassword=none
98+
connectionProtocol=s
99+
authentication=none
100+
userBase="ou=system"
101+
userSearchMatching="(uid={0})"
102+
userSearchSubtree=false
103+
roleBase="ou=system"
104+
roleName=cn
105+
roleSearchMatching="(member=uid={1},ou=system)"
106+
roleSearchSubtree=false
107+
;
108+
};
109+
91110
ExpandedLDAPLogin {
92111
org.apache.activemq.jaas.LDAPLoginModule required
93112
debug=true

0 commit comments

Comments
 (0)