Search in sources :

Example 6 with Broker

use of org.apache.qpid.server.model.Broker in project qpid-broker-j by apache.

the class OAuth2InteractiveAuthenticatorTest method createMockOAuth2AuthenticationProvider.

private OAuth2AuthenticationProvider<?> createMockOAuth2AuthenticationProvider(final HttpPort mockPort) throws URISyntaxException {
    OAuth2AuthenticationProvider authenticationProvider = mock(OAuth2AuthenticationProvider.class);
    Broker mockBroker = mock(Broker.class);
    SubjectCreator mockSubjectCreator = mock(SubjectCreator.class);
    when(_mockPort.getSubjectCreator(anyBoolean(), anyString())).thenReturn(mockSubjectCreator);
    SubjectAuthenticationResult mockSuccessfulSubjectAuthenticationResult = mock(SubjectAuthenticationResult.class);
    SubjectAuthenticationResult mockUnauthorizedSubjectAuthenticationResult = mock(SubjectAuthenticationResult.class);
    final Subject successfulSubject = new Subject(true, Collections.singleton(new AuthenticatedPrincipal(new UsernamePrincipal(TEST_AUTHORIZED_USER, null))), Collections.emptySet(), Collections.emptySet());
    final Subject unauthorizedSubject = new Subject(true, Collections.singleton(new AuthenticatedPrincipal(new UsernamePrincipal(TEST_UNAUTHORIZED_USER, null))), Collections.emptySet(), Collections.emptySet());
    AuthenticationResult mockSuccessfulAuthenticationResult = mock(AuthenticationResult.class);
    AuthenticationResult mockUnauthorizedAuthenticationResult = mock(AuthenticationResult.class);
    AuthenticationResult failedAuthenticationResult = new AuthenticationResult(AuthenticationResult.AuthenticationStatus.ERROR, new Exception("authentication failed"));
    SubjectAuthenticationResult failedSubjectAuthenticationResult = new SubjectAuthenticationResult(failedAuthenticationResult);
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
            final Subject subject = Subject.getSubject(AccessController.getContext());
            if (!subject.getPrincipals().iterator().next().getName().equals(TEST_AUTHORIZED_USER)) {
                throw new AccessControlException("access denied");
            }
            return null;
        }
    }).when(mockBroker).authorise(eq(Operation.PERFORM_ACTION("manage")));
    when(authenticationProvider.getAuthorizationEndpointURI(any())).thenReturn(new URI(TEST_AUTHORIZATION_ENDPOINT));
    when(authenticationProvider.getClientId()).thenReturn(TEST_CLIENT_ID);
    when(authenticationProvider.getScope()).thenReturn(TEST_OAUTH2_SCOPE);
    when(authenticationProvider.getParent()).thenReturn(mockBroker);
    when(authenticationProvider.authenticateViaAuthorizationCode(matches(TEST_VALID_AUTHORIZATION_CODE), matches(TEST_REQUEST_HOST), any())).thenReturn(mockSuccessfulAuthenticationResult);
    when(authenticationProvider.authenticateViaAuthorizationCode(matches(TEST_INVALID_AUTHORIZATION_CODE), matches(TEST_REQUEST_HOST), any())).thenReturn(failedAuthenticationResult);
    when(authenticationProvider.authenticateViaAuthorizationCode(matches(TEST_UNAUTHORIZED_AUTHORIZATION_CODE), matches(TEST_REQUEST_HOST), any())).thenReturn(mockUnauthorizedAuthenticationResult);
    when(mockSuccessfulSubjectAuthenticationResult.getSubject()).thenReturn(successfulSubject);
    when(mockUnauthorizedSubjectAuthenticationResult.getSubject()).thenReturn(unauthorizedSubject);
    when(mockSubjectCreator.createResultWithGroups(mockSuccessfulAuthenticationResult)).thenReturn(mockSuccessfulSubjectAuthenticationResult);
    when(mockSubjectCreator.createResultWithGroups(mockUnauthorizedAuthenticationResult)).thenReturn(mockUnauthorizedSubjectAuthenticationResult);
    when(mockSubjectCreator.createResultWithGroups(failedAuthenticationResult)).thenReturn(failedSubjectAuthenticationResult);
    return authenticationProvider;
}
Also used : Broker(org.apache.qpid.server.model.Broker) AccessControlException(java.security.AccessControlException) URI(java.net.URI) HttpURI(org.eclipse.jetty.http.HttpURI) SubjectAuthenticationResult(org.apache.qpid.server.security.auth.SubjectAuthenticationResult) Subject(javax.security.auth.Subject) URISyntaxException(java.net.URISyntaxException) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) AuthenticationResult(org.apache.qpid.server.security.auth.AuthenticationResult) SubjectAuthenticationResult(org.apache.qpid.server.security.auth.SubjectAuthenticationResult) UsernamePrincipal(org.apache.qpid.server.security.auth.UsernamePrincipal) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OAuth2AuthenticationProvider(org.apache.qpid.server.security.auth.manager.oauth2.OAuth2AuthenticationProvider) SubjectCreator(org.apache.qpid.server.security.SubjectCreator)

Example 7 with Broker

use of org.apache.qpid.server.model.Broker in project qpid-broker-j by apache.

the class LatestManagementControllerTest method createVirtualHostWithQueue.

private QueueManagingVirtualHost<?> createVirtualHostWithQueue(final String hostName, String... queueName) throws Exception {
    final QueueManagingVirtualHost<?> virtualHost = BrokerTestHelper.createVirtualHost(hostName, this);
    final Broker root = virtualHost.getBroker();
    final ConfiguredObject<?> virtualHostNode = virtualHost.getParent();
    when(root.getChildren(VirtualHostNode.class)).thenReturn(Collections.singletonList(virtualHostNode));
    when(virtualHostNode.getChildren(VirtualHost.class)).thenReturn(Collections.singletonList(virtualHost));
    when(virtualHostNode.getChildByName(VirtualHost.class, hostName)).thenReturn(virtualHost);
    Stream.of(queueName).forEach(n -> virtualHost.createChild(Queue.class, Collections.singletonMap(Queue.NAME, n)));
    return virtualHost;
}
Also used : Broker(org.apache.qpid.server.model.Broker) Queue(org.apache.qpid.server.model.Queue)

Example 8 with Broker

use of org.apache.qpid.server.model.Broker in project qpid-broker-j by apache.

the class SaslServlet method evaluateSaslResponse.

private void evaluateSaslResponse(final HttpServletRequest request, final HttpServletResponse response, final HttpSession session, final String saslResponse, final SaslNegotiator saslNegotiator, SubjectCreator subjectCreator) throws IOException {
    byte[] saslResponseBytes = saslResponse == null ? new byte[0] : Strings.decodeBase64(saslResponse);
    SubjectAuthenticationResult authenticationResult = subjectCreator.authenticate(saslNegotiator, saslResponseBytes);
    byte[] challenge = authenticationResult.getChallenge();
    Map<String, Object> outputObject = new LinkedHashMap<>();
    int responseStatus = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    if (authenticationResult.getStatus() == AuthenticationResult.AuthenticationStatus.SUCCESS) {
        Subject original = authenticationResult.getSubject();
        Broker broker = getBroker();
        try {
            HttpManagementUtil.createServletConnectionSubjectAssertManagementAccessAndSave(broker, request, original);
            if (challenge != null && challenge.length != 0) {
                outputObject.put("additionalData", Base64.getEncoder().encodeToString(challenge));
            }
            responseStatus = HttpServletResponse.SC_OK;
        } catch (SecurityException e) {
            responseStatus = HttpServletResponse.SC_FORBIDDEN;
        } finally {
            cleanup(request, session);
        }
    } else if (authenticationResult.getStatus() == AuthenticationResult.AuthenticationStatus.CONTINUE) {
        Random rand = getRandom(request);
        String id = String.valueOf(rand.nextLong());
        HttpManagementUtil.setSessionAttribute(ATTR_ID, id, session, request);
        HttpManagementUtil.setSessionAttribute(ATTR_SASL_NEGOTIATOR, saslNegotiator, session, request);
        long saslExchangeExpiry = getManagementConfiguration().getSaslExchangeExpiry();
        HttpManagementUtil.setSessionAttribute(ATTR_EXPIRY, System.currentTimeMillis() + saslExchangeExpiry, session, request);
        outputObject.put("id", id);
        outputObject.put("challenge", Base64.getEncoder().encodeToString(challenge));
        responseStatus = HttpServletResponse.SC_OK;
    } else {
        responseStatus = HttpServletResponse.SC_UNAUTHORIZED;
        cleanup(request, session);
    }
    sendJsonResponse(outputObject, request, response, responseStatus, false);
}
Also used : Broker(org.apache.qpid.server.model.Broker) Random(java.util.Random) SecureRandom(java.security.SecureRandom) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) SubjectAuthenticationResult(org.apache.qpid.server.security.auth.SubjectAuthenticationResult) Subject(javax.security.auth.Subject) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with Broker

use of org.apache.qpid.server.model.Broker in project qpid-broker-j by apache.

the class BrokerQueryServlet method getAllObjects.

@Override
protected List<ConfiguredObject<?>> getAllObjects(final Broker<?> broker, final Class<? extends ConfiguredObject> category, final HttpServletRequest request) {
    if (category == Broker.class) {
        return Collections.<ConfiguredObject<?>>singletonList(broker);
    } else {
        final Model brokerModel = broker.getModel();
        List<Class<? extends ConfiguredObject>> hierarchy = new ArrayList<>();
        Class<? extends ConfiguredObject> element = category;
        while (element != null && element != Broker.class) {
            hierarchy.add(element);
            Class<? extends ConfiguredObject> parentType = brokerModel.getParentType(element);
            if (parentType == null) {
                break;
            } else {
                element = parentType;
            }
        }
        Collections.reverse(hierarchy);
        Collection<ConfiguredObject<?>> parents = Collections.<ConfiguredObject<?>>singletonList(broker);
        Collection<ConfiguredObject<?>> children = Collections.emptyList();
        for (Class<? extends ConfiguredObject> childClass : hierarchy) {
            children = new HashSet<>();
            for (ConfiguredObject<?> parent : parents) {
                children.addAll((Collection<? extends ConfiguredObject<?>>) parent.getChildren(childClass));
            }
            parents = children;
        }
        return new ArrayList<>(children);
    }
}
Also used : Broker(org.apache.qpid.server.model.Broker) Model(org.apache.qpid.server.model.Model) ArrayList(java.util.ArrayList) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Example 10 with Broker

use of org.apache.qpid.server.model.Broker in project qpid-broker-j by apache.

the class TrustStoreMessageSourceCreator method register.

@Override
public void register(final SystemNodeRegistry registry) {
    final VirtualHost<?> vhost = registry.getVirtualHost();
    VirtualHostNode<?> virtualHostNode = (VirtualHostNode<?>) vhost.getParent();
    final Broker<?> broker = (Broker<?>) virtualHostNode.getParent();
    final Collection<TrustStore> trustStores = broker.getChildren(TrustStore.class);
    final TrustStoreChangeListener trustStoreChangeListener = new TrustStoreChangeListener(registry);
    for (final TrustStore trustStore : trustStores) {
        updateTrustStoreSourceRegistration(registry, trustStore);
        trustStore.addChangeListener(trustStoreChangeListener);
    }
    AbstractConfigurationChangeListener brokerListener = new AbstractConfigurationChangeListener() {

        @Override
        public void childAdded(final ConfiguredObject<?> object, final ConfiguredObject<?> child) {
            if (child instanceof TrustStore) {
                TrustStore<?> trustStore = (TrustStore<?>) child;
                updateTrustStoreSourceRegistration(registry, trustStore);
                trustStore.addChangeListener(trustStoreChangeListener);
            }
        }

        @Override
        public void childRemoved(final ConfiguredObject<?> object, final ConfiguredObject<?> child) {
            if (child instanceof TrustStore) {
                TrustStore<?> trustStore = (TrustStore<?>) child;
                trustStore.removeChangeListener(trustStoreChangeListener);
                registry.removeSystemNode(TrustStoreMessageSource.getSourceNameFromTrustStore(trustStore));
            } else if (child == virtualHostNode) {
                object.removeChangeListener(this);
                broker.getChildren(TrustStore.class).forEach(t -> t.removeChangeListener(trustStoreChangeListener));
            }
        }
    };
    broker.addChangeListener(brokerListener);
    virtualHostNode.addChangeListener(new AbstractConfigurationChangeListener() {

        @Override
        public void childRemoved(final ConfiguredObject<?> object, final ConfiguredObject<?> child) {
            if (child == vhost) {
                broker.removeChangeListener(brokerListener);
                object.removeChangeListener(this);
                broker.getChildren(TrustStore.class).forEach(t -> t.removeChangeListener(trustStoreChangeListener));
            }
        }
    });
}
Also used : TrustStore(org.apache.qpid.server.model.TrustStore) VirtualHost(org.apache.qpid.server.model.VirtualHost) PluggableService(org.apache.qpid.server.plugin.PluggableService) Collection(java.util.Collection) Broker(org.apache.qpid.server.model.Broker) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) State(org.apache.qpid.server.model.State) SystemNodeCreator(org.apache.qpid.server.plugin.SystemNodeCreator) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener) VirtualHostNode(org.apache.qpid.server.model.VirtualHostNode) Broker(org.apache.qpid.server.model.Broker) TrustStore(org.apache.qpid.server.model.TrustStore) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) VirtualHostNode(org.apache.qpid.server.model.VirtualHostNode)

Aggregations

Broker (org.apache.qpid.server.model.Broker)38 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)15 EventLogger (org.apache.qpid.server.logging.EventLogger)13 Before (org.junit.Before)11 Test (org.junit.Test)8 Subject (javax.security.auth.Subject)7 HashMap (java.util.HashMap)6 TaskExecutor (org.apache.qpid.server.configuration.updater.TaskExecutor)6 VirtualHost (org.apache.qpid.server.model.VirtualHost)6 VirtualHostNode (org.apache.qpid.server.model.VirtualHostNode)6 CurrentThreadTaskExecutor (org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor)5 Model (org.apache.qpid.server.model.Model)5 TaskExecutorImpl (org.apache.qpid.server.configuration.updater.TaskExecutorImpl)4 SubjectAuthenticationResult (org.apache.qpid.server.security.auth.SubjectAuthenticationResult)4 DurableConfigurationStore (org.apache.qpid.server.store.DurableConfigurationStore)4 AccessControlException (java.security.AccessControlException)3 Principal (java.security.Principal)3 SSLContext (javax.net.ssl.SSLContext)3 BrokerModel (org.apache.qpid.server.model.BrokerModel)3 Queue (org.apache.qpid.server.model.Queue)3