use of org.apache.qpid.server.security.limit.ConnectionLimitException in project qpid-broker-j by apache.
the class PortConnectionCounter method register.
public AcceptRegistration register(AMQPConnection<?> connection, ConnectionLimiter subLimiter) {
final Principal principal = connection.getAuthorizedPrincipal();
if (principal == null) {
throw new ConnectionLimitException("Unauthorized connection is forbidden");
}
final String userId = principal.getName();
return _connectionCounters.computeIfAbsent(userId, _connectionCounterFactory).registerConnection(userId, collectGroupPrincipals(connection.getSubject()), connection, subLimiter);
}
use of org.apache.qpid.server.security.limit.ConnectionLimitException in project qpid-broker-j by apache.
the class RuleSetTest method testRegisterNullSubject.
private void testRegisterNullSubject(Duration duration) {
final Builder builder = RuleSet.newBuilder(LIMITER_NAME, duration);
builder.addRule(Rule.newNonBlockingRule(RulePredicates.ALL_PORTS, TEST_GROUP1, 1000, 1000, duration));
builder.addRule(Rule.newNonBlockingRule(RulePredicates.ALL_PORTS, TEST_GROUP2, 1000, 1000, duration));
builder.addRule(Rule.newBlockingRule(RulePredicates.ALL_PORTS, RulePredicates.ALL_USERS));
final RuleSet ruleSet = builder.build();
assertNotNull(ruleSet);
final AMQPConnection<?> connection = Mockito.mock(AMQPConnection.class);
Mockito.doReturn(_port).when(connection).getPort();
Mockito.doReturn(_principal).when(connection).getAuthorizedPrincipal();
Mockito.doReturn(_eventLogger).when(connection).getEventLogger();
try {
ruleSet.register(connection);
fail("An exception is expected");
} catch (ConnectionLimitException e) {
assertEquals("User user is blocked on port amqp", e.getMessage());
}
}
use of org.apache.qpid.server.security.limit.ConnectionLimitException in project qpid-broker-j by apache.
the class RuleSetTest method testConnectionCountLimit2.
private void testConnectionCountLimit2(RuleSet ruleSet) {
assertNotNull(ruleSet);
ConnectionSlot connection1 = null;
ConnectionSlot connection2 = null;
try {
connection1 = ruleSet.register(newConnection());
connection2 = ruleSet.register(newConnection());
} catch (ConnectionLimitException e) {
fail("No exception is expected: " + e.getMessage());
}
assertNotNull(connection1);
assertNotNull(connection2);
try {
ruleSet.register(newConnection());
fail("An exception is expected");
} catch (ConnectionLimitException e) {
assertEquals("User user breaks connection count limit 2 on port amqp", e.getMessage());
}
connection1.free();
ConnectionSlot connection3 = null;
try {
connection3 = ruleSet.register(newConnection());
} catch (ConnectionLimitException e) {
fail("No exception is expected: " + e.getMessage());
}
assertNotNull(connection3);
connection2.free();
connection3.free();
}
use of org.apache.qpid.server.security.limit.ConnectionLimitException in project qpid-broker-j by apache.
the class RuleSetTest method testNoLimits.
private void testNoLimits(Duration duration) {
final Builder builder = RuleSet.newBuilder(LIMITER_NAME, duration);
builder.addRule(Rule.newBlockingRule(TEST_PORT, OTHER_USER));
builder.addRule(Rule.newBlockingRule(RulePredicates.ALL_PORTS, OTHER_GROUP));
final RuleSet ruleSet = builder.build();
assertNotNull(ruleSet);
ConnectionSlot connection1 = null;
ConnectionSlot connection2 = null;
ConnectionSlot connection3 = null;
try {
connection1 = ruleSet.register(newConnection());
connection2 = ruleSet.register(newConnection());
connection3 = ruleSet.register(newConnection());
} catch (ConnectionLimitException e) {
fail("An exception is not expected here");
}
assertNotNull(connection1);
assertNotNull(connection2);
assertNotNull(connection3);
connection1.free();
connection2.free();
connection3.free();
}
use of org.apache.qpid.server.security.limit.ConnectionLimitException in project qpid-broker-j by apache.
the class RuleSetTest method testFrequencyLimit_multiplePeriods.
@Test
public void testFrequencyLimit_multiplePeriods() {
final Duration frequencyPeriod1 = Duration.ofSeconds(1L);
final Duration frequencyPeriod2 = Duration.ofSeconds(2L);
final Builder builder = RuleSet.newBuilder(LIMITER_NAME, Duration.ofMinutes(1L));
builder.addRule(Rule.newNonBlockingRule(TEST_PORT, TEST_USER, null, 3, frequencyPeriod1));
builder.addRule(Rule.newNonBlockingRule(TEST_PORT, TEST_USER, null, 2, frequencyPeriod2));
builder.addRule(Rule.newBlockingRule(RulePredicates.ALL_PORTS, RulePredicates.ALL_USERS));
final RuleSet ruleSet = builder.build();
assertNotNull(ruleSet);
final Instant registrationStart = Instant.now();
try {
ruleSet.register(newConnection()).free();
ruleSet.register(newConnection()).free();
} catch (ConnectionLimitException e) {
fail("An exception is not expected here");
}
final Instant registrationEnd = Instant.now();
Instant before = Instant.now();
do {
try {
before = Instant.now();
ruleSet.register(newConnection()).free();
assertTrue(Duration.between(registrationStart, Instant.now()).compareTo(frequencyPeriod2) >= 0);
break;
} catch (ConnectionLimitException e) {
assertTrue(Duration.between(registrationEnd, before).compareTo(frequencyPeriod2) <= 0);
}
} while (Duration.between(registrationEnd, Instant.now()).compareTo(Duration.ofSeconds(3L)) < 0);
}
Aggregations