Search in sources :

Example 1 with ConnectionLimitException

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);
}
Also used : ConnectionLimitException(org.apache.qpid.server.security.limit.ConnectionLimitException) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) Principal(java.security.Principal)

Example 2 with ConnectionLimitException

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());
    }
}
Also used : ConnectionLimitException(org.apache.qpid.server.security.limit.ConnectionLimitException) Builder(org.apache.qpid.server.user.connection.limits.config.RuleSet.Builder)

Example 3 with ConnectionLimitException

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();
}
Also used : ConnectionSlot(org.apache.qpid.server.security.limit.ConnectionSlot) ConnectionLimitException(org.apache.qpid.server.security.limit.ConnectionLimitException)

Example 4 with ConnectionLimitException

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();
}
Also used : ConnectionSlot(org.apache.qpid.server.security.limit.ConnectionSlot) ConnectionLimitException(org.apache.qpid.server.security.limit.ConnectionLimitException) Builder(org.apache.qpid.server.user.connection.limits.config.RuleSet.Builder)

Example 5 with ConnectionLimitException

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);
}
Also used : ConnectionLimitException(org.apache.qpid.server.security.limit.ConnectionLimitException) Builder(org.apache.qpid.server.user.connection.limits.config.RuleSet.Builder) Instant(java.time.Instant) Duration(java.time.Duration) Test(org.junit.Test)

Aggregations

ConnectionLimitException (org.apache.qpid.server.security.limit.ConnectionLimitException)12 ConnectionSlot (org.apache.qpid.server.security.limit.ConnectionSlot)4 Builder (org.apache.qpid.server.user.connection.limits.config.RuleSet.Builder)4 AccessControlException (java.security.AccessControlException)3 VirtualHostUnavailableException (org.apache.qpid.server.virtualhost.VirtualHostUnavailableException)3 Principal (java.security.Principal)2 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)2 AmqpPort (org.apache.qpid.server.model.port.AmqpPort)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SoleConnectionEnforcementPolicyException (org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionEnforcementPolicyException)1 AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)1 ConnectionError (org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError)1 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)1 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)1 GroupPrincipal (org.apache.qpid.server.security.group.GroupPrincipal)1 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)1 Test (org.junit.Test)1