Search in sources :

Example 6 with SaslMechanisms

use of org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method processProtocolHeader.

private void processProtocolHeader(final QpidByteBuffer msg) {
    if (msg.remaining() >= 8) {
        byte[] header = new byte[8];
        msg.get(header);
        final AuthenticationProvider<?> authenticationProvider = getPort().getAuthenticationProvider();
        if (Arrays.equals(header, SASL_HEADER)) {
            if (_saslComplete) {
                throw new ConnectionScopedRuntimeException("SASL Layer header received after SASL already established");
            }
            try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(SASL_HEADER)) {
                getSender().send(protocolHeader);
            }
            SaslMechanisms mechanisms = new SaslMechanisms();
            ArrayList<Symbol> mechanismsList = new ArrayList<>();
            for (String name : authenticationProvider.getAvailableMechanisms(getTransport().isSecure())) {
                mechanismsList.add(Symbol.valueOf(name));
            }
            mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()]));
            send(new SASLFrame(mechanisms), null);
            _connectionState = ConnectionState.AWAIT_SASL_INIT;
            _frameHandler = getFrameHandler(true);
        } else if (Arrays.equals(header, AMQP_HEADER)) {
            if (!_saslComplete) {
                final List<String> mechanisms = authenticationProvider.getAvailableMechanisms(getTransport().isSecure());
                if (mechanisms.contains(ExternalAuthenticationManagerImpl.MECHANISM_NAME) && getNetwork().getPeerPrincipal() != null) {
                    setUserPrincipal(new AuthenticatedPrincipal(getNetwork().getPeerPrincipal()));
                } else if (mechanisms.contains(AnonymousAuthenticationManager.MECHANISM_NAME)) {
                    setUserPrincipal(new AuthenticatedPrincipal(((AnonymousAuthenticationManager) authenticationProvider).getAnonymousPrincipal()));
                } else {
                    LOGGER.warn("{} : attempt to initiate AMQP connection without correctly authenticating", getLogSubject());
                    _connectionState = ConnectionState.CLOSED;
                    getNetwork().close();
                }
            }
            try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(AMQP_HEADER)) {
                getSender().send(protocolHeader);
            }
            _connectionState = ConnectionState.AWAIT_OPEN;
            _frameHandler = getFrameHandler(false);
        } else {
            LOGGER.warn("{} : unknown AMQP header {}", getLogSubject(), Functions.str(header));
            _connectionState = ConnectionState.CLOSED;
            getNetwork().close();
        }
    }
}
Also used : Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) SASLFrame(org.apache.qpid.server.protocol.v1_0.framing.SASLFrame) ArrayList(java.util.ArrayList) SaslMechanisms(org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) AnonymousAuthenticationManager(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with SaslMechanisms

use of org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms in project qpid-broker-j by apache.

the class SaslTest method saslSuccessfulAuthenticationWithPipelinedFrames.

@Test
@SpecificationTest(section = "2.4.2", description = "For applications that use many short-lived connections," + " it MAY be desirable to pipeline the connection negotiation process." + " A peer MAY do this by starting to send subsequent frames before receiving" + " the partner’s connection header or open frame")
public void saslSuccessfulAuthenticationWithPipelinedFrames() throws Exception {
    final InetSocketAddress addr = getBrokerAdmin().getBrokerAddress(BrokerAdmin.PortType.AMQP);
    try (FrameTransport transport = new FrameTransport(addr, true).connect()) {
        final Binary initialResponse = new Binary(String.format("\0%s\0%s", _username, _password).getBytes(StandardCharsets.US_ASCII));
        final Interaction interaction = transport.newInteraction();
        interaction.protocolHeader(SASL_AMQP_HEADER_BYTES).negotiateProtocol().saslMechanism(PLAIN).saslInitialResponse(initialResponse).saslInit().protocolHeader(AMQP_HEADER_BYTES).negotiateProtocol().openContainerId("testContainerId").open();
        final byte[] saslHeaderResponse = interaction.consumeResponse().getLatestResponse(byte[].class);
        assertThat(saslHeaderResponse, is(equalTo(SASL_AMQP_HEADER_BYTES)));
        SaslMechanisms saslMechanismsResponse = interaction.consumeResponse().getLatestResponse(SaslMechanisms.class);
        assertThat(Arrays.asList(saslMechanismsResponse.getSaslServerMechanisms()), hasItem(PLAIN));
        SaslOutcome saslOutcome = interaction.consumeResponse().getLatestResponse(SaslOutcome.class);
        assertThat(saslOutcome.getCode(), equalTo(SaslCode.OK));
        final byte[] headerResponse = interaction.consumeResponse().getLatestResponse(byte[].class);
        assertThat(headerResponse, is(equalTo(AMQP_HEADER_BYTES)));
        interaction.consumeResponse().getLatestResponse(Open.class);
        interaction.doCloseConnection();
    }
}
Also used : FrameTransport(org.apache.qpid.tests.protocol.v1_0.FrameTransport) SaslOutcome(org.apache.qpid.server.protocol.v1_0.type.security.SaslOutcome) InetSocketAddress(java.net.InetSocketAddress) Interaction(org.apache.qpid.tests.protocol.v1_0.Interaction) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) SaslMechanisms(org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms) Test(org.junit.Test) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest)

Example 8 with SaslMechanisms

use of org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms in project qpid-broker-j by apache.

the class SaslTest method saslSuccessfulAuthenticationWithChallengeResponse.

@Test
@SpecificationTest(section = "5.3.2", description = "SASL Negotiation [...] challenge/response step occurs once")
public void saslSuccessfulAuthenticationWithChallengeResponse() throws Exception {
    final InetSocketAddress addr = getBrokerAdmin().getBrokerAddress(BrokerAdmin.PortType.AMQP);
    try (FrameTransport transport = new FrameTransport(addr, true).connect()) {
        final Interaction interaction = transport.newInteraction();
        final byte[] saslHeaderResponse = interaction.protocolHeader(SASL_AMQP_HEADER_BYTES).negotiateProtocol().consumeResponse().getLatestResponse(byte[].class);
        assertThat(saslHeaderResponse, is(equalTo(SASL_AMQP_HEADER_BYTES)));
        SaslMechanisms saslMechanismsResponse = interaction.consumeResponse().getLatestResponse(SaslMechanisms.class);
        assertThat(Arrays.asList(saslMechanismsResponse.getSaslServerMechanisms()), hasItem(CRAM_MD5));
        SaslChallenge saslChallenge = interaction.saslMechanism(CRAM_MD5).saslInit().consumeResponse().getLatestResponse(SaslChallenge.class);
        assertThat(saslChallenge.getChallenge(), is(notNullValue()));
        byte[] response = generateCramMD5ClientResponse(_username, _password, saslChallenge.getChallenge().getArray());
        final SaslOutcome saslOutcome = interaction.saslResponseResponse(new Binary(response)).saslResponse().consumeResponse().getLatestResponse(SaslOutcome.class);
        assertThat(saslOutcome.getCode(), equalTo(SaslCode.OK));
        final byte[] headerResponse = interaction.protocolHeader(AMQP_HEADER_BYTES).negotiateProtocol().consumeResponse().getLatestResponse(byte[].class);
        assertThat(headerResponse, is(equalTo(AMQP_HEADER_BYTES)));
        transport.assertNoMoreResponses();
    }
}
Also used : FrameTransport(org.apache.qpid.tests.protocol.v1_0.FrameTransport) SaslOutcome(org.apache.qpid.server.protocol.v1_0.type.security.SaslOutcome) SaslChallenge(org.apache.qpid.server.protocol.v1_0.type.security.SaslChallenge) InetSocketAddress(java.net.InetSocketAddress) Interaction(org.apache.qpid.tests.protocol.v1_0.Interaction) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) SaslMechanisms(org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms) Test(org.junit.Test) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest)

Aggregations

SaslMechanisms (org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms)7 InetSocketAddress (java.net.InetSocketAddress)6 FrameTransport (org.apache.qpid.tests.protocol.v1_0.FrameTransport)6 Test (org.junit.Test)6 SpecificationTest (org.apache.qpid.tests.protocol.SpecificationTest)5 Interaction (org.apache.qpid.tests.protocol.v1_0.Interaction)5 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)4 SaslOutcome (org.apache.qpid.server.protocol.v1_0.type.security.SaslOutcome)4 Futures.allAsList (com.google.common.util.concurrent.Futures.allAsList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)1 SASLEndpoint (org.apache.qpid.server.protocol.v1_0.SASLEndpoint)1 SASLFrame (org.apache.qpid.server.protocol.v1_0.framing.SASLFrame)1 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)1 SaslChallenge (org.apache.qpid.server.protocol.v1_0.type.security.SaslChallenge)1 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)1 AnonymousAuthenticationManager (org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager)1 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)1