Skip to content

Commit 5c1b562

Browse files
Dedeepya-Talex-rufous
authored andcommitted
QPID-8529:[Broker-J] Make sure that subject is set for all http requests
This closes #89
1 parent f10cbb6 commit 5c1b562

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacher.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,27 @@ private String digestCredentials(final String... content)
115115
MessageDigest md = MessageDigest.getInstance("SHA-256");
116116

117117
Subject subject = Subject.getSubject(AccessController.getContext());
118-
Set<SocketConnectionPrincipal> connectionPrincipals = subject.getPrincipals(SocketConnectionPrincipal.class);
119-
if (connectionPrincipals != null && !connectionPrincipals.isEmpty())
118+
if (subject != null)
120119
{
121-
SocketConnectionPrincipal connectionPrincipal = connectionPrincipals.iterator().next();
122-
SocketAddress remoteAddress = connectionPrincipal.getRemoteAddress();
123-
String address;
124-
if (remoteAddress instanceof InetSocketAddress)
120+
Set<SocketConnectionPrincipal> connectionPrincipals =
121+
subject.getPrincipals(SocketConnectionPrincipal.class);
122+
if (!connectionPrincipals.isEmpty())
125123
{
126-
address = ((InetSocketAddress) remoteAddress).getHostString();
127-
}
128-
else
129-
{
130-
address = remoteAddress.toString();
131-
}
132-
if (address != null)
133-
{
134-
md.update(address.getBytes(UTF8));
124+
SocketConnectionPrincipal connectionPrincipal = connectionPrincipals.iterator().next();
125+
SocketAddress remoteAddress = connectionPrincipal.getRemoteAddress();
126+
String address;
127+
if (remoteAddress instanceof InetSocketAddress)
128+
{
129+
address = ((InetSocketAddress) remoteAddress).getHostString();
130+
}
131+
else
132+
{
133+
address = remoteAddress.toString();
134+
}
135+
if (address != null)
136+
{
137+
md.update(address.getBytes(UTF8));
138+
}
135139
}
136140
}
137141

broker-core/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacherTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ public void testCacheHitDifferentRemoteAddressPorts() throws Exception
135135
assertGetOrLoad(credentials, expectedResult, expectedHitCount);
136136
}
137137

138+
@Test
139+
public void testCacheHitNoSubject()
140+
{
141+
final String credentials = "credentials";
142+
final AuthenticationResult result1 = _authenticationResultCacher.getOrLoad(new String[]{credentials}, _loader);
143+
assertEquals("Unexpected AuthenticationResult", _successfulAuthenticationResult, result1);
144+
assertEquals("Unexpected number of loads before cache hit", 1, _loadCallCount);
145+
146+
final AuthenticationResult result2 = _authenticationResultCacher.getOrLoad(new String[]{credentials}, _loader);
147+
assertEquals("Unexpected AuthenticationResult", _successfulAuthenticationResult, result2);
148+
assertEquals("Unexpected number of loads before cache hit", 1, _loadCallCount);
149+
}
150+
138151
private void assertGetOrLoad(final String credentials,
139152
final AuthenticationResult expectedResult,
140153
final int expectedHitCount)

broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/filter/InteractiveAuthenticationFilter.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
package org.apache.qpid.server.management.plugin.filter;
2222

2323
import java.io.IOException;
24+
import java.security.Principal;
25+
import java.security.PrivilegedActionException;
26+
import java.security.PrivilegedExceptionAction;
2427
import java.util.ArrayList;
2528
import java.util.Collection;
2629
import java.util.Collections;
@@ -40,6 +43,7 @@
4043
import org.apache.qpid.server.management.plugin.HttpManagementConfiguration;
4144
import org.apache.qpid.server.management.plugin.HttpManagementUtil;
4245
import org.apache.qpid.server.management.plugin.HttpRequestInteractiveAuthenticator;
46+
import org.apache.qpid.server.management.plugin.servlet.ServletConnectionPrincipal;
4347
import org.apache.qpid.server.plugin.QpidServiceLoader;
4448
import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
4549

@@ -96,7 +100,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
96100

97101
if(handler != null)
98102
{
99-
handler.handleAuthentication(httpResponse);
103+
invokeAuthenticationHandler(httpRequest, httpResponse, handler);
100104
}
101105
else
102106
{
@@ -105,4 +109,25 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
105109
}
106110
}
107111

112+
private void invokeAuthenticationHandler(final HttpServletRequest httpRequest,
113+
final HttpServletResponse httpResponse,
114+
final HttpRequestInteractiveAuthenticator.AuthenticationHandler handler)
115+
throws ServletException
116+
{
117+
final Subject tempSubject = new Subject(true,
118+
Collections.<Principal>singleton(new ServletConnectionPrincipal(httpRequest)),
119+
Collections.emptySet(),
120+
Collections.emptySet());
121+
try
122+
{
123+
Subject.doAs(tempSubject, (PrivilegedExceptionAction<Void>) () -> {
124+
handler.handleAuthentication(httpResponse);
125+
return null;
126+
});
127+
}
128+
catch (PrivilegedActionException e)
129+
{
130+
throw new ServletException(e);
131+
}
132+
}
108133
}

0 commit comments

Comments
 (0)