use of org.apache.qpid.server.connection.ConnectionPrincipal in project qpid-broker-j by apache.
the class AbstractMessageLogger method getActor.
protected String getActor() {
Subject subject = Subject.getSubject(AccessController.getContext());
SessionPrincipal sessionPrincipal = getPrincipal(subject, SessionPrincipal.class);
String message;
if (sessionPrincipal != null) {
message = generateSessionMessage(sessionPrincipal.getSession());
} else {
ConnectionPrincipal connPrincipal = getPrincipal(subject, ConnectionPrincipal.class);
if (connPrincipal != null) {
message = generateConnectionMessage(connPrincipal.getConnection());
} else {
TaskPrincipal taskPrincipal = getPrincipal(subject, TaskPrincipal.class);
if (taskPrincipal != null) {
message = generateTaskMessage(taskPrincipal);
} else {
ManagementConnectionPrincipal managementConnection = getPrincipal(subject, ManagementConnectionPrincipal.class);
if (managementConnection != null) {
message = generateManagementConnectionMessage(managementConnection, getPrincipal(subject, AuthenticatedPrincipal.class));
} else {
message = "<<UNKNOWN>> ";
}
}
}
}
return message;
}
use of org.apache.qpid.server.connection.ConnectionPrincipal in project qpid-broker-j by apache.
the class AMQPConnectionActorTest method sendLogMessage.
private String sendLogMessage() {
final String message = "test logging";
Subject subject = new Subject(false, Collections.singleton(new ConnectionPrincipal(getConnection())), Collections.emptySet(), Collections.emptySet());
Subject.doAs(subject, new PrivilegedAction<Object>() {
@Override
public Object run() {
getEventLogger().message(new LogSubject() {
@Override
public String toLogString() {
return "[AMQPActorTest]";
}
}, new LogMessage() {
@Override
public String toString() {
return message;
}
@Override
public String getLogHierarchy() {
return "test.hierarchy";
}
});
return null;
}
});
return message;
}
use of org.apache.qpid.server.connection.ConnectionPrincipal in project qpid-broker-j by apache.
the class RuleBasedAccessControl method authorise.
/**
* Check if an operation is authorised by asking the configuration object about the access
* control rules granted to the current thread's {@link Subject}. If there is no current
* user the plugin will abstain.
*/
@Override
public Result authorise(LegacyOperation operation, ObjectType objectType, ObjectProperties properties) {
InetAddress addressOfClient = null;
final Subject subject = Subject.getSubject(AccessController.getContext());
// Abstain if there is no subject/principal associated with this thread
if (subject == null || subject.getPrincipals().size() == 0) {
return Result.DEFER;
}
Set<ConnectionPrincipal> principals = subject.getPrincipals(ConnectionPrincipal.class);
if (!principals.isEmpty()) {
SocketAddress address = principals.iterator().next().getConnection().getRemoteSocketAddress();
if (address instanceof InetSocketAddress) {
addressOfClient = ((InetSocketAddress) address).getAddress();
}
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Checking " + operation + " " + objectType + " " + (addressOfClient == null ? "" : addressOfClient));
}
try {
return _ruleSet.check(subject, operation, objectType, properties, addressOfClient);
} catch (Exception e) {
LOGGER.error("Unable to check " + operation + " " + objectType + " " + (addressOfClient == null ? "" : addressOfClient), e);
return Result.DENIED;
}
}
use of org.apache.qpid.server.connection.ConnectionPrincipal in project qpid-broker-j by apache.
the class RuleBasedAccessControlTest method testAccess.
public void testAccess() throws Exception {
final Subject subject = TestPrincipalUtils.createTestSubject("user1");
final String testVirtualHost = getName();
final InetAddress inetAddress = InetAddress.getLocalHost();
final InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 1);
AMQPConnection connectionModel = mock(AMQPConnection.class);
when(connectionModel.getRemoteSocketAddress()).thenReturn(inetSocketAddress);
subject.getPrincipals().add(new ConnectionPrincipal(connectionModel));
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
RuleSet mockRuleSet = mock(RuleSet.class);
RuleBasedAccessControl accessControl = new RuleBasedAccessControl(mockRuleSet, BrokerModel.getInstance());
ObjectProperties properties = new ObjectProperties(testVirtualHost);
accessControl.authorise(LegacyOperation.ACCESS, ObjectType.VIRTUALHOST, properties);
verify(mockRuleSet).check(subject, LegacyOperation.ACCESS, ObjectType.VIRTUALHOST, properties, inetAddress);
return null;
}
});
}
use of org.apache.qpid.server.connection.ConnectionPrincipal in project qpid-broker-j by apache.
the class RuleBasedAccessControlTest method testAccessIsDeniedIfRuleThrowsException.
public void testAccessIsDeniedIfRuleThrowsException() throws Exception {
final Subject subject = TestPrincipalUtils.createTestSubject("user1");
final InetAddress inetAddress = InetAddress.getLocalHost();
final InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 1);
AMQPConnection connectionModel = mock(AMQPConnection.class);
when(connectionModel.getRemoteSocketAddress()).thenReturn(inetSocketAddress);
subject.getPrincipals().add(new ConnectionPrincipal(connectionModel));
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
RuleSet mockRuleSet = mock(RuleSet.class);
when(mockRuleSet.check(subject, LegacyOperation.ACCESS, ObjectType.VIRTUALHOST, ObjectProperties.EMPTY, inetAddress)).thenThrow(new RuntimeException());
RuleBasedAccessControl accessControl = new RuleBasedAccessControl(mockRuleSet, BrokerModel.getInstance());
Result result = accessControl.authorise(LegacyOperation.ACCESS, ObjectType.VIRTUALHOST, ObjectProperties.EMPTY);
assertEquals(Result.DENIED, result);
return null;
}
});
}
Aggregations