use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.
the class ConnectionAndUserPredicateTest method testEvaluateRemoteContainerIdAndUsername.
public void testEvaluateRemoteContainerIdAndUsername() {
AMQPConnection connection = mock(AMQPConnection.class);
when(connection.getRemoteAddressString()).thenReturn("foo:1234");
when(connection.getRemoteContainerName()).thenReturn("TestClientId");
_subject.getPrincipals().add(new ConnectionPrincipal(connection));
_predicate.setRemoteContainerIdPattern(".*Client.*");
_predicate.setUsernamePattern("testUser.*");
Subject.doAs(_subject, new PrivilegedAction<Void>() {
@Override
public Void run() {
assertTrue("predicate unexpectedly did not match", _predicate.evaluate(mock(ILoggingEvent.class)));
return null;
}
});
_predicate.setRemoteContainerIdPattern(".*noMatchingClient.*");
Subject.doAs(_subject, new PrivilegedAction<Void>() {
@Override
public Void run() {
assertFalse("predicate unexpectedly matched", _predicate.evaluate(mock(ILoggingEvent.class)));
return null;
}
});
_predicate.setRemoteContainerIdPattern(".*Client.*");
_predicate.setUsernamePattern("noMatchingUsername.*");
Subject.doAs(_subject, new PrivilegedAction<Void>() {
@Override
public Void run() {
assertFalse("predicate unexpectedly matched", _predicate.evaluate(mock(ILoggingEvent.class)));
return null;
}
});
}
use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.
the class AMQPConnection_1_0Impl method receiveOpenInternal.
private void receiveOpenInternal(final NamedAddressSpace addressSpace) {
if (!addressSpace.isActive()) {
final Error err = new Error();
err.setCondition(AmqpError.NOT_FOUND);
populateConnectionRedirect(addressSpace, err);
closeConnection(err);
} else {
if (AuthenticatedPrincipal.getOptionalAuthenticatedPrincipalFromSubject(getSubject()) == null) {
closeConnection(AmqpError.NOT_ALLOWED, "Connection has not been authenticated");
} else {
try {
boolean registerSucceeded = addressSpace.registerConnection(this, (existingConnections, newConnection) -> {
boolean proceedWithRegistration = true;
if (newConnection instanceof AMQPConnection_1_0Impl && !newConnection.isClosing()) {
List<ListenableFuture<Void>> rescheduleFutures = new ArrayList<>();
for (AMQPConnection<?> existingConnection : StreamSupport.stream(existingConnections.spliterator(), false).filter(con -> con instanceof AMQPConnection_1_0).filter(con -> !con.isClosing()).filter(con -> con.getRemoteContainerName().equals(newConnection.getRemoteContainerName())).collect(Collectors.toList())) {
SoleConnectionEnforcementPolicy soleConnectionEnforcementPolicy = null;
if (((AMQPConnection_1_0Impl) existingConnection)._soleConnectionEnforcementPolicy != null) {
soleConnectionEnforcementPolicy = ((AMQPConnection_1_0Impl) existingConnection)._soleConnectionEnforcementPolicy;
} else if (((AMQPConnection_1_0Impl) newConnection)._soleConnectionEnforcementPolicy != null) {
soleConnectionEnforcementPolicy = ((AMQPConnection_1_0Impl) newConnection)._soleConnectionEnforcementPolicy;
}
if (SoleConnectionEnforcementPolicy.REFUSE_CONNECTION.equals(soleConnectionEnforcementPolicy)) {
_properties.put(Symbol.valueOf("amqp:connection-establishment-failed"), true);
Error error = new Error(AmqpError.INVALID_FIELD, String.format("Connection closed due to sole-connection-enforcement-policy '%s'", soleConnectionEnforcementPolicy.toString()));
error.setInfo(Collections.singletonMap(Symbol.valueOf("invalid-field"), Symbol.valueOf("container-id")));
newConnection.doOnIOThreadAsync(() -> ((AMQPConnection_1_0Impl) newConnection).closeConnection(error));
proceedWithRegistration = false;
break;
} else if (SoleConnectionEnforcementPolicy.CLOSE_EXISTING.equals(soleConnectionEnforcementPolicy)) {
final Error error = new Error(AmqpError.RESOURCE_LOCKED, String.format("Connection closed due to sole-connection-enforcement-policy '%s'", soleConnectionEnforcementPolicy.toString()));
error.setInfo(Collections.singletonMap(Symbol.valueOf("sole-connection-enforcement"), true));
rescheduleFutures.add(existingConnection.doOnIOThreadAsync(() -> ((AMQPConnection_1_0Impl) existingConnection).closeConnection(error)));
proceedWithRegistration = false;
}
}
if (!rescheduleFutures.isEmpty()) {
doAfter(allAsList(rescheduleFutures), () -> newConnection.doOnIOThreadAsync(() -> receiveOpenInternal(addressSpace)));
}
}
return proceedWithRegistration;
});
if (registerSucceeded) {
setAddressSpace(addressSpace);
if (!addressSpace.authoriseCreateConnection(this)) {
closeConnection(AmqpError.NOT_ALLOWED, "Connection refused");
} else {
switch(_connectionState) {
case AWAIT_OPEN:
sendOpen(_channelMax, _maxFrameSize);
_connectionState = ConnectionState.OPENED;
break;
case CLOSE_SENT:
case CLOSED:
// already sent our close - probably due to an error
break;
default:
throw new ConnectionScopedRuntimeException(String.format("Unexpected state %s during connection open.", _connectionState));
}
}
}
} catch (VirtualHostUnavailableException | AccessControlException e) {
closeConnection(AmqpError.NOT_ALLOWED, e.getMessage());
}
}
}
}
use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.
the class ServerConnectionDelegate method isSessionNameUnique.
private boolean isSessionNameUnique(final byte[] name, final ServerConnection conn) {
final Principal authorizedPrincipal = conn.getAuthorizedPrincipal();
final String userId = authorizedPrincipal == null ? "" : authorizedPrincipal.getName();
final Iterator<? extends org.apache.qpid.server.model.Connection<?>> connections = conn.getAddressSpace().getConnections().iterator();
while (connections.hasNext()) {
final AMQPConnection<?> amqConnectionModel = (AMQPConnection<?>) connections.next();
final String userName = amqConnectionModel.getAuthorizedPrincipal() == null ? "" : amqConnectionModel.getAuthorizedPrincipal().getName();
if (userId.equals(userName) && amqConnectionModel.hasSessionWithName(name)) {
return false;
}
}
return true;
}
use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.
the class ChannelLogSubject method updateSessionDetails.
public void updateSessionDetails() {
/**
* LOG FORMAT used by the AMQPConnectorActor follows
* ChannelLogSubject.CHANNEL_FORMAT : con:{0}({1}@{2}/{3})/ch:{4}.
*
* Uses a MessageFormat call to insert the required values according to
* these indices:
*
* 0 - Connection ID
* 1 - User ID
* 2 - IP
* 3 - Virtualhost
* 4 - Channel ID
*/
AMQPConnection connection = _sessionModel.getAMQPConnection();
setLogStringWithFormat(CHANNEL_FORMAT, connection == null ? -1L : connection.getConnectionId(), (connection == null || connection.getAuthorizedPrincipal() == null) ? "?" : connection.getAuthorizedPrincipal().getName(), (connection == null || connection.getRemoteAddressString() == null) ? "?" : connection.getRemoteAddressString(), (connection == null || connection.getAddressSpaceName() == null) ? "?" : connection.getAddressSpaceName(), _sessionModel.getChannelId());
}
use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.
the class VirtualHostTest method testDeleteVirtualHost_ClosesConnections.
public void testDeleteVirtualHost_ClosesConnections() {
String virtualHostName = getName();
QueueManagingVirtualHost<?> virtualHost = createVirtualHost(virtualHostName);
assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
AbstractAMQPConnection connection = createMockProtocolConnection(virtualHost);
assertEquals("Unexpected number of connections before connection registered", 0, virtualHost.getConnectionCount());
AMQPConnection modelConnection = mock(AMQPConnection.class);
when(modelConnection.closeAsync()).thenReturn(Futures.immediateFuture(null));
virtualHost.registerConnection(modelConnection, new NoopConnectionEstablishmentPolicy());
assertEquals("Unexpected number of connections after connection registered", 1, virtualHost.getConnectionCount());
virtualHost.delete();
assertEquals("Unexpected state", State.DELETED, virtualHost.getState());
assertEquals("Unexpected number of connections after virtualhost deleted", 0, virtualHost.getConnectionCount());
verify(modelConnection).closeAsync();
}
Aggregations