Search in sources :

Example 1 with VirtualHostUnavailableException

use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.

the class VirtualHostTest method testRegisterConnectionOnStoppedVirtualhost.

public void testRegisterConnectionOnStoppedVirtualhost() {
    QueueManagingVirtualHost<?> vhost = createVirtualHost("sdf");
    AMQPConnection<?> connection = getMockConnection();
    ((AbstractConfiguredObject<?>) vhost).stop();
    try {
        vhost.registerConnection(connection, new NoopConnectionEstablishmentPolicy());
        fail("exception not thrown");
    } catch (VirtualHostUnavailableException e) {
    // pass
    }
    assertEquals("unexpected number of connections", 0, vhost.getConnectionCount());
    ((AbstractConfiguredObject<?>) vhost).start();
    vhost.registerConnection(connection, new NoopConnectionEstablishmentPolicy());
    assertEquals("unexpected number of connections", 1, vhost.getConnectionCount());
}
Also used : VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) NoopConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.NoopConnectionEstablishmentPolicy)

Example 2 with VirtualHostUnavailableException

use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.

the class AbstractExchange method route.

@Override
public <M extends ServerMessage<? extends StorableMessageMetaData>> RoutingResult<M> route(final M message, final String routingAddress, final InstanceProperties instanceProperties) {
    if (_virtualHost.getState() != State.ACTIVE) {
        throw new VirtualHostUnavailableException(this._virtualHost);
    }
    final RoutingResult<M> routingResult = new RoutingResult<>(message);
    Map<AbstractExchange<?>, Set<String>> currentThreadMap = CURRENT_ROUTING.get();
    boolean topLevel = currentThreadMap == null;
    try {
        if (topLevel) {
            currentThreadMap = new HashMap<>();
            CURRENT_ROUTING.set(currentThreadMap);
        }
        Set<String> existingRoutes = currentThreadMap.get(this);
        if (existingRoutes == null) {
            currentThreadMap.put(this, Collections.singleton(routingAddress));
        } else if (existingRoutes.contains(routingAddress)) {
            return routingResult;
        } else {
            existingRoutes = new HashSet<>(existingRoutes);
            existingRoutes.add(routingAddress);
            currentThreadMap.put(this, existingRoutes);
        }
        _receivedMessageCount.incrementAndGet();
        _receivedMessageSize.addAndGet(message.getSizeIncludingHeader());
        doRoute(message, routingAddress, instanceProperties, routingResult);
        if (!routingResult.hasRoutes()) {
            MessageDestination alternateBindingDestination = getAlternateBindingDestination();
            if (alternateBindingDestination != null) {
                routingResult.add(alternateBindingDestination.route(message, routingAddress, instanceProperties));
            }
        }
        if (routingResult.hasRoutes()) {
            _routedMessageCount.incrementAndGet();
            _routedMessageSize.addAndGet(message.getSizeIncludingHeader());
        } else {
            _droppedMessageCount.incrementAndGet();
            _droppedMessageSize.addAndGet(message.getSizeIncludingHeader());
        }
        return routingResult;
    } finally {
        if (topLevel) {
            CURRENT_ROUTING.set(null);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MessageDestination(org.apache.qpid.server.message.MessageDestination) RoutingResult(org.apache.qpid.server.message.RoutingResult) VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) HashSet(java.util.HashSet)

Example 3 with VirtualHostUnavailableException

use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.

the class ServerConnectionDelegate method connectionOpen.

@Override
public void connectionOpen(ServerConnection sconn, ConnectionOpen open) {
    assertState(sconn, ConnectionState.AWAIT_OPEN);
    NamedAddressSpace addressSpace;
    String vhostName;
    if (open.hasVirtualHost()) {
        vhostName = open.getVirtualHost();
    } else {
        vhostName = "";
    }
    AmqpPort port = sconn.getPort();
    addressSpace = port.getAddressSpace(vhostName);
    if (addressSpace != null) {
        if (!addressSpace.isActive()) {
            sconn.setState(ServerConnection.State.CLOSING);
            final String redirectHost = addressSpace.getRedirectHost(port);
            if (redirectHost == null) {
                sconn.sendConnectionClose(ConnectionCloseCode.CONNECTION_FORCED, "Virtual host '" + vhostName + "' is not active");
            } else {
                sconn.invoke(new ConnectionRedirect(redirectHost, new ArrayList<Object>()));
            }
            return;
        }
        try {
            sconn.setVirtualHost(addressSpace);
            if (!addressSpace.authoriseCreateConnection(sconn.getAmqpConnection())) {
                sconn.setState(ServerConnection.State.CLOSING);
                sconn.sendConnectionClose(ConnectionCloseCode.CONNECTION_FORCED, "Connection not authorized");
                return;
            }
        } catch (AccessControlException | VirtualHostUnavailableException e) {
            sconn.setState(ServerConnection.State.CLOSING);
            sconn.sendConnectionClose(ConnectionCloseCode.CONNECTION_FORCED, e.getMessage());
            return;
        }
        sconn.setState(ServerConnection.State.OPEN);
        _state = ConnectionState.OPEN;
        sconn.invoke(new ConnectionOpenOk(Collections.emptyList()));
    } else {
        sconn.setState(ServerConnection.State.CLOSING);
        sconn.sendConnectionClose(ConnectionCloseCode.INVALID_PATH, "Unknown virtualhost '" + vhostName + "'");
    }
}
Also used : VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) ArrayList(java.util.ArrayList) AccessControlException(java.security.AccessControlException) AmqpPort(org.apache.qpid.server.model.port.AmqpPort)

Example 4 with VirtualHostUnavailableException

use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException 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());
            }
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) Arrays(java.util.Arrays) SoleConnectionDetectionPolicy(org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionDetectionPolicy) PeekingIterator(com.google.common.collect.PeekingIterator) AuthenticationResult(org.apache.qpid.server.security.auth.AuthenticationResult) ExternalAuthenticationManagerImpl(org.apache.qpid.server.security.auth.manager.ExternalAuthenticationManagerImpl) Map(java.util.Map) Disposition(org.apache.qpid.server.protocol.v1_0.type.transport.Disposition) ProtocolHandler(org.apache.qpid.server.protocol.v1_0.codec.ProtocolHandler) ConnectionError(org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError) End(org.apache.qpid.server.protocol.v1_0.type.transport.End) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) Set(java.util.Set) DescribedTypeConstructorRegistry(org.apache.qpid.server.protocol.v1_0.codec.DescribedTypeConstructorRegistry) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) SaslCode(org.apache.qpid.server.protocol.v1_0.type.security.SaslCode) VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) Transport(org.apache.qpid.server.model.Transport) SASLFrame(org.apache.qpid.server.protocol.v1_0.framing.SASLFrame) OversizeFrameException(org.apache.qpid.server.protocol.v1_0.framing.OversizeFrameException) AggregateTicker(org.apache.qpid.server.transport.AggregateTicker) ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) AccessController(java.security.AccessController) ValueWriter(org.apache.qpid.server.protocol.v1_0.codec.ValueWriter) ProtocolEngine(org.apache.qpid.server.transport.ProtocolEngine) FrameBody(org.apache.qpid.server.protocol.v1_0.type.FrameBody) FrameWriter(org.apache.qpid.server.protocol.v1_0.codec.FrameWriter) SOLE_CONNECTION_ENFORCEMENT_POLICY(org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionConnectionProperties.SOLE_CONNECTION_ENFORCEMENT_POLICY) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Close(org.apache.qpid.server.protocol.v1_0.type.transport.Close) ChannelFrameBody(org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody) SoleConnectionEnforcementPolicy(org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionEnforcementPolicy) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) StreamSupport(java.util.stream.StreamSupport) Flow(org.apache.qpid.server.protocol.v1_0.type.transport.Flow) ConnectionPropertyEnricher(org.apache.qpid.server.plugin.ConnectionPropertyEnricher) SaslFrameBody(org.apache.qpid.server.protocol.v1_0.type.SaslFrameBody) SoleConnectionConnectionProperties(org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionConnectionProperties) Connection(org.apache.qpid.server.model.Connection) ServerNetworkConnection(org.apache.qpid.server.transport.ServerNetworkConnection) UnsignedShort(org.apache.qpid.server.protocol.v1_0.type.UnsignedShort) AMQFrame(org.apache.qpid.server.protocol.v1_0.framing.AMQFrame) SaslChallenge(org.apache.qpid.server.protocol.v1_0.type.security.SaslChallenge) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) TransportFrame(org.apache.qpid.server.protocol.v1_0.framing.TransportFrame) AMQPDescribedTypeRegistry(org.apache.qpid.server.protocol.v1_0.type.codec.AMQPDescribedTypeRegistry) SocketAddress(java.net.SocketAddress) Open(org.apache.qpid.server.protocol.v1_0.type.transport.Open) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) ConnectionMessages(org.apache.qpid.server.logging.messages.ConnectionMessages) ErrorCondition(org.apache.qpid.server.protocol.v1_0.type.ErrorCondition) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) AmqpPort(org.apache.qpid.server.model.port.AmqpPort) ValueHandler(org.apache.qpid.server.protocol.v1_0.codec.ValueHandler) SectionDecoderRegistry(org.apache.qpid.server.protocol.v1_0.codec.SectionDecoderRegistry) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) StoreException(org.apache.qpid.server.store.StoreException) LocalTransaction(org.apache.qpid.server.txn.LocalTransaction) Detach(org.apache.qpid.server.protocol.v1_0.type.transport.Detach) Collection(java.util.Collection) AnonymousAuthenticationManager(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SaslOutcome(org.apache.qpid.server.protocol.v1_0.type.security.SaslOutcome) UUID(java.util.UUID) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) Functions(org.apache.qpid.server.transport.util.Functions) Sets(com.google.common.collect.Sets) Attach(org.apache.qpid.server.protocol.v1_0.type.transport.Attach) List(java.util.List) Principal(java.security.Principal) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) AccessControlException(java.security.AccessControlException) FrameHandler(org.apache.qpid.server.protocol.v1_0.framing.FrameHandler) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AuthenticationProvider(org.apache.qpid.server.model.AuthenticationProvider) Begin(org.apache.qpid.server.protocol.v1_0.type.transport.Begin) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SaslMechanisms(org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms) Iterators(com.google.common.collect.Iterators) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) SaslNegotiator(org.apache.qpid.server.security.auth.sasl.SaslNegotiator) SaslInit(org.apache.qpid.server.protocol.v1_0.type.security.SaslInit) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) AbstractAMQPConnection(org.apache.qpid.server.transport.AbstractAMQPConnection) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) SaslResponse(org.apache.qpid.server.protocol.v1_0.type.security.SaslResponse) NoSuchElementException(java.util.NoSuchElementException) SubjectAuthenticationResult(org.apache.qpid.server.security.auth.SubjectAuthenticationResult) ServerTransaction(org.apache.qpid.server.txn.ServerTransaction) Logger(org.slf4j.Logger) Action(org.apache.qpid.server.util.Action) Iterator(java.util.Iterator) Broker(org.apache.qpid.server.model.Broker) Protocol(org.apache.qpid.server.model.Protocol) SubjectCreator(org.apache.qpid.server.security.SubjectCreator) ConnectionClosingTicker(org.apache.qpid.server.protocol.ConnectionClosingTicker) AMQPSession(org.apache.qpid.server.session.AMQPSession) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ConnectionError(org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AccessControlException(java.security.AccessControlException) VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) SoleConnectionEnforcementPolicy(org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionEnforcementPolicy) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 5 with VirtualHostUnavailableException

use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.

the class AMQPConnection_0_8Impl method receiveConnectionOpen.

@Override
public void receiveConnectionOpen(AMQShortString virtualHostName, AMQShortString capabilities, boolean insist) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV ConnectionOpen[" + " virtualHost: " + virtualHostName + " capabilities: " + capabilities + " insist: " + insist + " ]");
    }
    assertState(ConnectionState.AWAIT_OPEN);
    String virtualHostStr = AMQShortString.toString(virtualHostName);
    if ((virtualHostStr != null) && virtualHostStr.charAt(0) == '/') {
        virtualHostStr = virtualHostStr.substring(1);
    }
    NamedAddressSpace addressSpace = ((AmqpPort) getPort()).getAddressSpace(virtualHostStr);
    if (addressSpace == null) {
        sendConnectionClose(ErrorCodes.NOT_FOUND, "Unknown virtual host: '" + virtualHostName + "'", 0);
    } else {
        // Check virtualhost access
        if (!addressSpace.isActive()) {
            String redirectHost = addressSpace.getRedirectHost(getPort());
            if (redirectHost != null) {
                sendConnectionClose(0, new AMQFrame(0, new ConnectionRedirectBody(getProtocolVersion(), AMQShortString.valueOf(redirectHost), null)));
            } else {
                sendConnectionClose(ErrorCodes.CONNECTION_FORCED, "Virtual host '" + addressSpace.getName() + "' is not active", 0);
            }
        } else {
            try {
                addressSpace.registerConnection(this, new NoopConnectionEstablishmentPolicy());
                setAddressSpace(addressSpace);
                if (addressSpace.authoriseCreateConnection(this)) {
                    MethodRegistry methodRegistry = getMethodRegistry();
                    AMQMethodBody responseBody = methodRegistry.createConnectionOpenOkBody(virtualHostName);
                    writeFrame(responseBody.generateFrame(0));
                    _state = ConnectionState.OPEN;
                } else {
                    sendConnectionClose(ErrorCodes.ACCESS_REFUSED, "Connection refused", 0);
                }
            } catch (AccessControlException | VirtualHostUnavailableException e) {
                sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), 0);
            }
        }
    }
}
Also used : VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) NoopConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.NoopConnectionEstablishmentPolicy) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) AccessControlException(java.security.AccessControlException) AmqpPort(org.apache.qpid.server.model.port.AmqpPort)

Aggregations

VirtualHostUnavailableException (org.apache.qpid.server.virtualhost.VirtualHostUnavailableException)6 AccessControlException (java.security.AccessControlException)4 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)4 AmqpPort (org.apache.qpid.server.model.port.AmqpPort)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 MessageDestination (org.apache.qpid.server.message.MessageDestination)2 RoutingResult (org.apache.qpid.server.message.RoutingResult)2 Iterators (com.google.common.collect.Iterators)1 PeekingIterator (com.google.common.collect.PeekingIterator)1 Sets (com.google.common.collect.Sets)1 Futures.allAsList (com.google.common.util.concurrent.Futures.allAsList)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 AccessControlContext (java.security.AccessControlContext)1 AccessController (java.security.AccessController)1 Principal (java.security.Principal)1 PrivilegedAction (java.security.PrivilegedAction)1 Arrays (java.util.Arrays)1