use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project ddf by codice.
the class LogoutRequestService method logout.
private void logout() {
HttpSession session = sessionFactory.getOrCreateSession(request);
SecurityTokenHolder tokenHolder = ((SecurityTokenHolder) session.getAttribute(SecurityConstants.SAML_ASSERTION));
SecurityAssertion securityAssertion = new SecurityAssertionImpl(tokenHolder.getSecurityToken("idp"));
boolean hasSecurityAuditRole = Arrays.stream(System.getProperty("security.audit.roles").split(",")).filter(role -> securityAssertion.getPrincipals().contains(new RolePrincipal(role))).findFirst().isPresent();
if (hasSecurityAuditRole) {
SecurityLogger.audit("Subject with admin privileges has logged out: {}", securityAssertion.getPrincipal().getName());
}
tokenHolder.remove("idp");
}
use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project opennms by OpenNMS.
the class OpenNMSLoginModule method createPrincipals.
@Override
public Set<Principal> createPrincipals(final GrantedAuthority authority) {
final String role = authority.getAuthority().replaceFirst("^[Rr][Oo][Ll][Ee]_", "");
final Set<Principal> principals = new HashSet<>();
principals.add(new RolePrincipal(role));
principals.add(new RolePrincipal(role.toLowerCase()));
principals.add(new RolePrincipal(authority.getAuthority()));
LOG.debug("created principals from authority {}: {}", authority, principals);
return principals;
}
use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project opennms by OpenNMS.
the class KarafTestCase method executeCommand.
/**
* Executes a shell command and returns output as a String.
* Commands have a default timeout of 10 seconds.
*
* @param command
* @return
*/
protected String executeCommand(final String command) {
try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
final PrintStream errStream = new PrintStream(byteArrayOutputStream)) {
final ExecutorService executor = Executors.newCachedThreadPool();
Subject subject = new Subject();
subject.getPrincipals().add(new RolePrincipal("admin"));
return Subject.doAs(subject, new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
final Session session = sessionFactory.create(System.in, printStream, errStream);
LOG.info("Command: {}", command);
FutureTask<String> commandFuture = new FutureTask<String>(new Callable<String>() {
public String call() {
try {
session.execute(command);
} catch (Exception e) {
e.printStackTrace(System.err);
}
printStream.flush();
errStream.flush();
return byteArrayOutputStream.toString();
}
});
try {
executor.submit(commandFuture);
String response = commandFuture.get(10, TimeUnit.SECONDS);
LOG.info("Response: {}", response);
return response;
} catch (Exception e) {
e.printStackTrace(System.err);
return "SHELL COMMAND TIMED OUT: " + command;
}
}
});
} catch (Exception e) {
LOG.error("Error while executing command", e);
throw new RuntimeException(e);
}
}
use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.
the class JaasAuthenticator method authenticate.
public Subject authenticate(Object credentials) throws SecurityException {
if (!(credentials instanceof String[])) {
throw new IllegalArgumentException("Expected String[2], got " + (credentials != null ? credentials.getClass().getName() : null));
}
final String[] params = (String[]) credentials;
if (params.length != 2) {
throw new IllegalArgumentException("Expected String[2] but length was " + params.length);
}
try {
Subject subject = new Subject();
try {
subject.getPrincipals().add(new ClientPrincipal("jmx", RemoteServer.getClientHost()));
} catch (Throwable t) {
// Ignore
}
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName(params[0]);
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword((params[1].toCharArray()));
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
});
loginContext.login();
int roleCount = 0;
for (Principal principal : subject.getPrincipals()) {
if (principal instanceof RolePrincipal) {
roleCount++;
}
}
if (roleCount == 0) {
throw new FailedLoginException("User doesn't have role defined");
}
return subject;
} catch (LoginException e) {
throw new SecurityException("Authentication failed", e);
}
}
use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.
the class LocalConsoleManager method createLocalKarafSubject.
private Subject createLocalKarafSubject() {
String userName = System.getProperty(KARAF_LOCAL_USER);
if (userName == null) {
userName = "karaf";
}
final Subject subject = new Subject();
subject.getPrincipals().add(new UserPrincipal(userName));
subject.getPrincipals().add(new ClientPrincipal("local", "localhost"));
String roles = System.getProperty(KARAF_LOCAL_ROLES, KARAF_LOCAL_ROLES_DEFAULT);
if (roles != null) {
for (String role : roles.split("[,]")) {
subject.getPrincipals().add(new RolePrincipal(role.trim()));
}
}
return subject;
}
Aggregations