Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.apache.pulsar.common.sasl.SaslConstants.SASL_STATE_SERVER_CHECK_TOKEN;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.annotations.VisibleForTesting;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
Expand Down Expand Up @@ -334,6 +335,16 @@ public boolean authenticateHttpRequest(HttpServletRequest request, HttpServletRe
}
}

@VisibleForTesting
Cache<Long, AuthenticationState> getAuthStates() {
return authStates;
}

@VisibleForTesting
void setAuthStates(Cache<Long, AuthenticationState> authStates) {
this.authStates = authStates;
}

private String sanitizeHeaderValue(String value) {
if (value == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.collect.ImmutableSet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -42,6 +43,9 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.security.auth.login.Configuration;
import lombok.Cleanup;
Expand Down Expand Up @@ -356,12 +360,11 @@ public void testSaslOnlyAuthFirstStage() throws Exception {
}

@Test
@SuppressWarnings("unchecked")
public void testMaxInflightContext() throws Exception {
@Cleanup
AuthenticationProviderSasl saslServer = new AuthenticationProviderSasl();
HttpServletRequest servletRequest = mock(HttpServletRequest.class);
doReturn("Init").when(servletRequest).getHeader("State");
doReturn(SaslConstants.SASL_STATE_CLIENT_INIT).when(servletRequest).getHeader(SaslConstants.SASL_HEADER_STATE);
conf.setInflightSaslContextExpiryMs(Integer.MAX_VALUE);
conf.setMaxInflightSaslContext(1);
saslServer.initialize(AuthenticationProvider.Context.builder().config(conf).build());
Expand All @@ -370,14 +373,65 @@ public void testMaxInflightContext() throws Exception {
AuthenticationDataProvider dataProvider = authSasl.getAuthData("localhost");
AuthData initData1 = dataProvider.authenticate(AuthData.INIT_AUTH_DATA);
doReturn(Base64.getEncoder().encodeToString(initData1.getBytes())).when(
servletRequest).getHeader("SASL-Token");
doReturn(String.valueOf(i)).when(servletRequest).getHeader("SASL-Server-ID");
servletRequest).getHeader(SaslConstants.SASL_AUTH_TOKEN);
doReturn(String.valueOf(i)).when(servletRequest).getHeader(SaslConstants.SASL_STATE_SERVER);
saslServer.authenticateHttpRequest(servletRequest, mock(HttpServletResponse.class));
}
Field field = AuthenticationProviderSasl.class.getDeclaredField("authStates");
field.setAccessible(true);
Cache<Long, AuthenticationState> cache = (Cache<Long, AuthenticationState>) field.get(saslServer);
Cache<Long, AuthenticationState> cache = saslServer.getAuthStates();
//only 1 context was left in the memory
assertEquals(cache.asMap().size(), 1);
// Caffeine may perform size-based eviction asynchronously, so force maintenance before asserting.
cache.cleanUp();
assertEquals(cache.asMap().size(), conf.getMaxInflightSaslContext());
}

@Test
public void testMaxInflightContextWithDelayedCaffeineMaintenance() throws Exception {
@Cleanup
AuthenticationProviderSasl saslServer = new AuthenticationProviderSasl();
HttpServletRequest servletRequest = mock(HttpServletRequest.class);
doReturn(SaslConstants.SASL_STATE_CLIENT_INIT).when(servletRequest).getHeader(SaslConstants.SASL_HEADER_STATE);
conf.setInflightSaslContextExpiryMs(Integer.MAX_VALUE);
conf.setMaxInflightSaslContext(1);
saslServer.initialize(AuthenticationProvider.Context.builder().config(conf).build());

CountDownLatch maintenanceStarted = new CountDownLatch(1);
CountDownLatch allowMaintenance = new CountDownLatch(1);
@Cleanup("shutdownNow")
ExecutorService maintenanceExecutor = Executors.newSingleThreadExecutor();
Cache<Long, AuthenticationState> delayedMaintenanceCache = Caffeine.newBuilder()
.maximumSize(conf.getMaxInflightSaslContext())
.expireAfterWrite(conf.getInflightSaslContextExpiryMs(), TimeUnit.MILLISECONDS)
.executor(command -> maintenanceExecutor.execute(() -> {
maintenanceStarted.countDown();
try {
allowMaintenance.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
command.run();
}))
.build();
saslServer.setAuthStates(delayedMaintenanceCache);

try {
for (int i = 0; i < 10; i++) {
AuthenticationDataProvider dataProvider = authSasl.getAuthData("localhost");
AuthData initData1 = dataProvider.authenticate(AuthData.INIT_AUTH_DATA);
doReturn(Base64.getEncoder().encodeToString(initData1.getBytes())).when(
servletRequest).getHeader(SaslConstants.SASL_AUTH_TOKEN);
doReturn(String.valueOf(i)).when(servletRequest).getHeader(SaslConstants.SASL_STATE_SERVER);
saslServer.authenticateHttpRequest(servletRequest, mock(HttpServletResponse.class));
}
assertTrue(maintenanceStarted.await(5, TimeUnit.SECONDS));

Cache<Long, AuthenticationState> cache = saslServer.getAuthStates();
assertTrue(cache.asMap().size() > conf.getMaxInflightSaslContext());

// Caffeine may perform size-based eviction asynchronously, so force maintenance before asserting.
cache.cleanUp();
assertEquals(cache.asMap().size(), conf.getMaxInflightSaslContext());
} finally {
allowMaintenance.countDown();
}
}
}
Loading