use of javax.security.auth.kerberos.KerberosPrincipal in project zookeeper by apache.
the class MiniKdcTest method testKerberosLogin.
@Test
@Timeout(value = 60)
public void testKerberosLogin() throws Exception {
MiniKdc kdc = getKdc();
File workDir = getWorkDir();
LoginContext loginContext = null;
try {
String principal = "foo";
File keytab = new File(workDir, "foo.keytab");
kdc.createPrincipal(keytab, principal);
Set<Principal> principals = new HashSet<Principal>();
principals.add(new KerberosPrincipal(principal));
// client login
Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
loginContext = new LoginContext("", subject, null, KerberosConfiguration.createClientConfig(principal, keytab));
loginContext.login();
subject = loginContext.getSubject();
assertEquals(1, subject.getPrincipals().size());
assertEquals(KerberosPrincipal.class, subject.getPrincipals().iterator().next().getClass());
assertEquals(principal + "@" + kdc.getRealm(), subject.getPrincipals().iterator().next().getName());
loginContext.logout();
// server login
subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
loginContext = new LoginContext("", subject, null, KerberosConfiguration.createServerConfig(principal, keytab));
loginContext.login();
subject = loginContext.getSubject();
assertEquals(1, subject.getPrincipals().size());
assertEquals(KerberosPrincipal.class, subject.getPrincipals().iterator().next().getClass());
assertEquals(principal + "@" + kdc.getRealm(), subject.getPrincipals().iterator().next().getName());
loginContext.logout();
} finally {
if (loginContext != null && loginContext.getSubject() != null && !loginContext.getSubject().getPrincipals().isEmpty()) {
loginContext.logout();
}
}
}
use of javax.security.auth.kerberos.KerberosPrincipal in project zookeeper by apache.
the class Login method getTGT.
private synchronized KerberosTicket getTGT() {
Set<KerberosTicket> tickets = subject.getPrivateCredentials(KerberosTicket.class);
for (KerberosTicket ticket : tickets) {
KerberosPrincipal server = ticket.getServer();
if (server.getName().equals("krbtgt/" + server.getRealm() + "@" + server.getRealm())) {
LOG.debug("Client principal is \"{}\".", ticket.getClient().getName());
LOG.debug("Server principal is \"{}\".", ticket.getServer().getName());
return ticket;
}
}
return null;
}
use of javax.security.auth.kerberos.KerberosPrincipal in project SSM by Intel-bigdata.
the class SecurityUtil method loginUsingKeytab.
public static Subject loginUsingKeytab(String principal, File keytabFile) throws IOException {
Set<Principal> principals = new HashSet<Principal>();
principals.add(new KerberosPrincipal(principal));
Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
Configuration conf = useKeytab(principal, keytabFile);
String confName = "KeytabConf";
LoginContext loginContext = null;
try {
loginContext = new LoginContext(confName, subject, null, conf);
LOG.info("Login successful for user " + subject.getPrincipals().iterator().next().getName());
} catch (LoginException e) {
throw new IOException("Faill to create LoginContext for " + e);
}
try {
loginContext.login();
} catch (LoginException e) {
throw new IOException("Login failure for " + e);
}
return loginContext.getSubject();
}
use of javax.security.auth.kerberos.KerberosPrincipal in project ddf by codice.
the class SubjectUtils method getName.
/**
* Retrieves the user name from a given subject.
*
* @param subject Subject to get the user name from.
* @param defaultName Name to send back if no user name was found.
* @param returnDisplayName return formatted user name for displaying
* @return String representation of the user name if available or defaultName if no user name
* could be found or incoming subject was null.
*/
@Override
public String getName(Subject subject, String defaultName, boolean returnDisplayName) {
String name = defaultName;
if (subject != null) {
PrincipalCollection principals = subject.getPrincipals();
if (principals != null) {
Collection<SecurityAssertion> assertions = principals.byType(SecurityAssertion.class);
if (!assertions.isEmpty()) {
List<SecurityAssertion> assertionList = new ArrayList<>(assertions);
assertionList.sort(new SecurityAssertionComparator());
for (SecurityAssertion assertion : assertionList) {
Principal principal = assertion.getPrincipal();
if (principal instanceof KerberosPrincipal) {
StringTokenizer st = new StringTokenizer(principal.getName(), "@");
st = new StringTokenizer(st.nextToken(), "/");
name = st.nextToken();
} else {
name = principal.getName();
}
if (returnDisplayName) {
name = getDisplayName(principal, name);
}
if (StringUtils.isNotEmpty(name)) {
break;
}
}
} else {
// send back the primary principal as a string
name = principals.getPrimaryPrincipal().toString();
}
} else {
LOGGER.debug("No principals located in the incoming subject, cannot look up user name. Using default name of {}.", defaultName);
}
} else {
LOGGER.debug("Incoming subject was null, cannot look up user name. Using default name of {}.", defaultName);
}
LOGGER.debug("Sending back name {}.", name);
return name;
}
use of javax.security.auth.kerberos.KerberosPrincipal in project ddf by codice.
the class PropertyFileClaimsHandlerTest method testGetUser.
@Test
public void testGetUser() {
PropertyFileClaimsHandler propertyFileClaimsHandler = new PropertyFileClaimsHandler();
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn("mydude");
String user = propertyFileClaimsHandler.getUser(principal);
assertEquals("mydude", user);
principal = new X500Principal("cn=myxman,ou=someunit,o=someorg");
user = propertyFileClaimsHandler.getUser(principal);
assertEquals("myxman", user);
principal = new KerberosPrincipal("mykman@SOMEDOMAIN.COM");
user = propertyFileClaimsHandler.getUser(principal);
assertEquals("mykman", user);
}
Aggregations