Search in sources :

Example 1 with SaslInit

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

the class SaslTest method saslUnsuccessfulAuthentication.

@Test
@SpecificationTest(section = "5.3.2", description = "SASL Negotiation")
public void saslUnsuccessfulAuthentication() 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(PLAIN));
        final Binary initialResponse = new Binary(String.format("\0%s\0badpassword", _username).getBytes(StandardCharsets.US_ASCII));
        SaslOutcome saslOutcome = interaction.saslMechanism(PLAIN).saslInitialResponse(initialResponse).saslInit().consumeResponse().getLatestResponse(SaslOutcome.class);
        assertThat(saslOutcome.getCode(), equalTo(SaslCode.AUTH));
        transport.assertNoMoreResponsesAndChannelClosed();
    }
}
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 2 with SaslInit

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

the class SaslTest method saslSuccessfulAuthentication.

@Test
@SpecificationTest(section = "5.3.2", description = "SASL Negotiation [...] challenge/response step occurs zero times")
public void saslSuccessfulAuthentication() 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(PLAIN));
        final Binary initialResponse = new Binary(String.format("\0%s\0%s", _username, _password).getBytes(StandardCharsets.US_ASCII));
        SaslOutcome saslOutcome = interaction.saslMechanism(PLAIN).saslInitialResponse(initialResponse).saslInit().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) 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 3 with SaslInit

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

the class AMQPConnection_1_0Impl method receiveSaslInit.

@Override
public void receiveSaslInit(final SaslInit saslInit) {
    assertState(ConnectionState.AWAIT_SASL_INIT);
    if (saslInit.getHostname() != null && !"".equals(saslInit.getHostname().trim())) {
        _localHostname = saslInit.getHostname();
    } else if (getNetwork().getSelectedHost() != null) {
        _localHostname = getNetwork().getSelectedHost();
    }
    String mechanism = saslInit.getMechanism().toString();
    final Binary initialResponse = saslInit.getInitialResponse();
    byte[] response = initialResponse == null ? new byte[0] : initialResponse.getArray();
    List<String> availableMechanisms = _subjectCreator.getAuthenticationProvider().getAvailableMechanisms(getTransport().isSecure());
    if (!availableMechanisms.contains(mechanism)) {
        handleSaslError();
    } else {
        _saslNegotiator = _subjectCreator.createSaslNegotiator(mechanism, this);
        processSaslResponse(response);
    }
}
Also used : Binary(org.apache.qpid.server.protocol.v1_0.type.Binary)

Example 4 with SaslInit

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

the class SaslInit method toString.

@Override
public String toString() {
    StringBuilder builder = new StringBuilder("SaslInit{");
    final int origLength = builder.length();
    if (_mechanism != null) {
        if (builder.length() != origLength) {
            builder.append(',');
        }
        builder.append("mechanism=").append(_mechanism);
    }
    if (_initialResponse != null) {
        if (builder.length() != origLength) {
            builder.append(',');
        }
        builder.append("initialResponse=").append(_initialResponse);
    }
    if (_hostname != null) {
        if (builder.length() != origLength) {
            builder.append(',');
        }
        builder.append("hostname=").append(_hostname);
    }
    builder.append('}');
    return builder.toString();
}
Also used : SASLEndpoint(org.apache.qpid.server.protocol.v1_0.SASLEndpoint)

Example 5 with SaslInit

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

the class ProtocolEngine_1_0_0Test method testProtocolEngineWithSaslNonTLSandAnon.

public void testProtocolEngineWithSaslNonTLSandAnon() throws Exception {
    final Map<String, Object> attrs = Collections.singletonMap(ConfiguredObject.NAME, getTestName());
    final AnonymousAuthenticationManager anonymousAuthenticationManager = (new AnonymousAuthenticationManagerFactory()).create(null, attrs, _broker);
    when(_port.getAuthenticationProvider()).thenReturn(anonymousAuthenticationManager);
    when(_port.getSubjectCreator(anyBoolean(), anyString())).thenReturn(new SubjectCreator(anonymousAuthenticationManager, Collections.emptyList(), null));
    allowMechanisms(AnonymousAuthenticationManager.MECHANISM_NAME);
    createEngine(Transport.TCP);
    _protocolEngine_1_0_0.received(QpidByteBuffer.wrap(ProtocolEngineCreator_1_0_0_SASL.getInstance().getHeaderIdentifier()));
    SaslInit init = new SaslInit();
    init.setMechanism(Symbol.valueOf("ANONYMOUS"));
    _frameWriter.send(new SASLFrame(init));
    _protocolEngine_1_0_0.received(QpidByteBuffer.wrap(ProtocolEngineCreator_1_0_0.getInstance().getHeaderIdentifier()));
    Open open = new Open();
    open.setContainerId("testContainerId");
    _frameWriter.send(AMQFrame.createAMQFrame((short) 0, open));
    verify(_virtualHost).registerConnection(any(AMQPConnection.class), any(ConnectionEstablishmentPolicy.class));
    AuthenticatedPrincipal principal = (AuthenticatedPrincipal) _connection.getAuthorizedPrincipal();
    assertNotNull(principal);
    assertEquals(principal, new AuthenticatedPrincipal(anonymousAuthenticationManager.getAnonymousPrincipal()));
}
Also used : ConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.ConnectionEstablishmentPolicy) AnonymousAuthenticationManagerFactory(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManagerFactory) AnonymousAuthenticationManager(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) SaslInit(org.apache.qpid.server.protocol.v1_0.type.security.SaslInit) SASLFrame(org.apache.qpid.server.protocol.v1_0.framing.SASLFrame) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Matchers.anyString(org.mockito.Matchers.anyString) SubjectCreator(org.apache.qpid.server.security.SubjectCreator) Open(org.apache.qpid.server.protocol.v1_0.type.transport.Open) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)5 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)5 SaslOutcome (org.apache.qpid.server.protocol.v1_0.type.security.SaslOutcome)5 SpecificationTest (org.apache.qpid.tests.protocol.SpecificationTest)5 FrameTransport (org.apache.qpid.tests.protocol.v1_0.FrameTransport)5 Interaction (org.apache.qpid.tests.protocol.v1_0.Interaction)5 Test (org.junit.Test)5 SaslMechanisms (org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms)4 SaslInit (org.apache.qpid.server.protocol.v1_0.type.security.SaslInit)2 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)1 SASLEndpoint (org.apache.qpid.server.protocol.v1_0.SASLEndpoint)1 SASLFrame (org.apache.qpid.server.protocol.v1_0.framing.SASLFrame)1 SaslChallenge (org.apache.qpid.server.protocol.v1_0.type.security.SaslChallenge)1 Open (org.apache.qpid.server.protocol.v1_0.type.transport.Open)1 SubjectCreator (org.apache.qpid.server.security.SubjectCreator)1 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)1 AnonymousAuthenticationManager (org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager)1 AnonymousAuthenticationManagerFactory (org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManagerFactory)1 AMQPConnection (org.apache.qpid.server.transport.AMQPConnection)1 ConnectionEstablishmentPolicy (org.apache.qpid.server.virtualhost.ConnectionEstablishmentPolicy)1