Repository: qpid-broker-j Updated Branches: refs/heads/7.0.x b3ca3855e -> 6fd5156b6
QPID-8219: [Broker-J] Cache authentication results for the same remote hosts and credentials Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/6fd5156b Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/6fd5156b Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/6fd5156b Branch: refs/heads/7.0.x Commit: 6fd5156b6a16381839ac86fce1c7ce2aabe542dc Parents: b3ca385 Author: Alex Rudyy <[email protected]> Authored: Tue Jul 17 13:05:44 2018 +0100 Committer: Alex Rudyy <[email protected]> Committed: Thu Jul 26 12:23:03 2018 +0100 ---------------------------------------------------------------------- .../manager/AuthenticationResultCacher.java | 17 ++++++- .../manager/AuthenticationResultCacherTest.java | 50 ++++++++++---------- 2 files changed, 41 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6fd5156b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacher.java ---------------------------------------------------------------------- diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacher.java b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacher.java index c5cb157..b034916 100644 --- a/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacher.java +++ b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacher.java @@ -19,6 +19,8 @@ package org.apache.qpid.server.security.auth.manager; +import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.AccessController; @@ -117,7 +119,20 @@ public class AuthenticationResultCacher if (connectionPrincipals != null && !connectionPrincipals.isEmpty()) { SocketConnectionPrincipal connectionPrincipal = connectionPrincipals.iterator().next(); - md.update(connectionPrincipal.getRemoteAddress().toString().getBytes(UTF8)); + SocketAddress remoteAddress = connectionPrincipal.getRemoteAddress(); + String address; + if (remoteAddress instanceof InetSocketAddress) + { + address = ((InetSocketAddress) remoteAddress).getHostString(); + } + else + { + address = remoteAddress.toString(); + } + if (address != null) + { + md.update(address.getBytes(UTF8)); + } } for (String part : content) http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6fd5156b/broker-core/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacherTest.java ---------------------------------------------------------------------- diff --git a/broker-core/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacherTest.java b/broker-core/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacherTest.java index 8844fe1..7512bf6 100644 --- a/broker-core/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacherTest.java +++ b/broker-core/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationResultCacherTest.java @@ -108,34 +108,34 @@ public class AuthenticationResultCacherTest extends QpidTestCase }); } - - public void testCacheMissDifferentAddress() throws Exception + public void testCacheMissDifferentRemoteAddressHosts() throws Exception { - Subject.doAs(_subject, new PrivilegedAction<Void>() - { - @Override - public Void run() - { - AuthenticationResult result; - result = _authenticationResultCacher.getOrLoad(new String[]{"credentials"}, _loader); - assertEquals("Unexpected AuthenticationResult", _successfulAuthenticationResult, result); - assertEquals("Unexpected number of loads before cache hit", 1, _loadCallCount); - return null; - } - }); + final String credentials = "credentials"; + assertGetOrLoad(credentials, _successfulAuthenticationResult, 1); + when(_connection.getRemoteSocketAddress()).thenReturn(new InetSocketAddress("example2.com", 8888)); + assertGetOrLoad(credentials, _successfulAuthenticationResult, 2); + } + public void testCacheHitDifferentRemoteAddressPorts() throws Exception + { + final int expectedHitCount = 1; + final AuthenticationResult expectedResult = _successfulAuthenticationResult; + final String credentials = "credentials"; + assertGetOrLoad(credentials, expectedResult, expectedHitCount); when(_connection.getRemoteSocketAddress()).thenReturn(new InetSocketAddress("example.com", 8888)); - Subject.doAs(_subject, new PrivilegedAction<Void>() - { - @Override - public Void run() - { - AuthenticationResult result; - result = _authenticationResultCacher.getOrLoad(new String[]{"credentials"}, _loader); - assertEquals("Unexpected AuthenticationResult", _successfulAuthenticationResult, result); - assertEquals("Unexpected number of loads before cache hit", 2, _loadCallCount); - return null; - } + assertGetOrLoad(credentials, expectedResult, expectedHitCount); + } + + private void assertGetOrLoad(final String credentials, + final AuthenticationResult expectedResult, + final int expectedHitCount) + { + Subject.doAs(_subject, (PrivilegedAction<Void>) () -> { + AuthenticationResult result; + result = _authenticationResultCacher.getOrLoad(new String[]{credentials}, _loader); + assertEquals("Unexpected AuthenticationResult", expectedResult, result); + assertEquals("Unexpected number of loads before cache hit", (long)expectedHitCount, (long) _loadCallCount); + return null; }); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
