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;
}
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;
}
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);
}
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);
}
}
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));
}
}
});
}
Aggregations