Commit da7a6a88 by Jon Maron

BUG-32834 login processor

parent 403c04b3
......@@ -78,6 +78,10 @@ public class GuiceServletConfig extends GuiceServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
super.contextInitialized(servletContextEvent);
// perform login operations
LoginProcessor loginProcessor = new LoginProcessor();
loginProcessor.login();
restoreTypeSystem();
}
......
......@@ -25,34 +25,26 @@ import org.apache.hadoop.util.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.net.InetAddress;
/**
* A listener capable of performing a simple or kerberos login.
* A class capable of performing a simple or kerberos login.
*/
public class LoginListener implements ServletContextListener {
public class LoginProcessor {
private static final Logger LOG = LoggerFactory
.getLogger(LoginListener.class);
public static final String AUTHENTICATION_METHOD = "authentication.method";
public static final String AUTHENTICATION_PRINCIPAL = "authentication.principal";
public static final String AUTHENTICATION_KEYTAB = "authentication.keytab";
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
.getLogger(LoginProcessor.class);
public static final String METADATA_AUTHENTICATION_PREFIX = "metadata.authentication.";
public static final String AUTHENTICATION_METHOD = METADATA_AUTHENTICATION_PREFIX + "method";
public static final String AUTHENTICATION_PRINCIPAL = METADATA_AUTHENTICATION_PREFIX + "principal";
public static final String AUTHENTICATION_KEYTAB = METADATA_AUTHENTICATION_PREFIX + "keytab";
/**
* Perform a SIMPLE login based on established OS identity or a kerberos based login using the configured
* principal and keytab (via application.properties).
* @param servletContextEvent
*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
public void login() {
// first, let's see if we're running in a hadoop cluster and have the env configured
boolean isHadoopCluster = isHadoopCluster();
Configuration hadoopConfig = isHadoopCluster ? getHadoopConfiguration() : new Configuration(false);
......@@ -64,17 +56,12 @@ public class LoginListener implements ServletContextListener {
}
if (!isHadoopCluster) {
// need to read the configured authentication choice and create the UGI configuration
String authMethod;
authMethod = configuration != null ? configuration.getString(AUTHENTICATION_METHOD) : null;
// getString may return null, and would like to log the nature of the default setting
if (authMethod == null) {
LOG.info("No authentication method configured. Defaulting to simple authentication");
authMethod = "simple";
}
SecurityUtil.setAuthenticationMethod(
UserGroupInformation.AuthenticationMethod.valueOf(authMethod.toUpperCase()),
hadoopConfig);
setupHadoopConfiguration(hadoopConfig, configuration);
}
doServiceLogin(hadoopConfig, configuration);
}
protected void doServiceLogin(Configuration hadoopConfig, PropertiesConfiguration configuration) {
UserGroupInformation.setConfiguration(hadoopConfig);
UserGroupInformation ugi = null;
......@@ -95,6 +82,19 @@ public class LoginListener implements ServletContextListener {
}
}
protected void setupHadoopConfiguration(Configuration hadoopConfig, PropertiesConfiguration configuration) {
String authMethod;
authMethod = configuration != null ? configuration.getString(AUTHENTICATION_METHOD) : null;
// getString may return null, and would like to log the nature of the default setting
if (authMethod == null) {
LOG.info("No authentication method configured. Defaulting to simple authentication");
authMethod = "simple";
}
SecurityUtil.setAuthenticationMethod(
UserGroupInformation.AuthenticationMethod.valueOf(authMethod.toUpperCase()),
hadoopConfig);
}
/**
* Return a server (service) principal. The token "_HOST" in the principal will be replaced with the local host
* name (e.g. dgi/_HOST will be changed to dgi/localHostName)
......
......@@ -48,9 +48,6 @@
</filter-mapping>
<listener>
<listener-class>org.apache.hadoop.metadata.web.listeners.LoginListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.hadoop.metadata.web.listeners.GuiceServletConfig</listener-class>
</listener>
</web-app>
......@@ -35,7 +35,7 @@ import java.util.Properties;
/**
*
*/
public class LoginListenerIT {
public class LoginProcessorIT {
private static final String JAAS_ENTRY =
"%s { \n"
......@@ -56,13 +56,13 @@ public class LoginListenerIT {
@Test
public void testDefaultSimpleLogin() throws Exception {
LoginListener listener = new LoginListener() {
LoginProcessor processor = new LoginProcessor() {
@Override
protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException {
return new PropertiesConfiguration();
}
};
listener.contextInitialized(null);
processor.login();
assert UserGroupInformation.getCurrentUser() != null;
assert !UserGroupInformation.isLoginKeytabBased();
......@@ -73,13 +73,13 @@ public class LoginListenerIT {
public void testKerberosLogin() throws Exception {
final File keytab = setupKDCAndPrincipals();
LoginListener listener = new LoginListener() {
LoginProcessor processor = new LoginProcessor() {
@Override
protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException {
PropertiesConfiguration config = new PropertiesConfiguration();
config.setProperty("authentication.method", "kerberos");
config.setProperty("authentication.principal", "dgi@EXAMPLE.COM");
config.setProperty("authentication.keytab", keytab.getAbsolutePath());
config.setProperty("metadata.authentication.method", "kerberos");
config.setProperty("metadata.authentication.principal", "dgi@EXAMPLE.COM");
config.setProperty("metadata.authentication.keytab", keytab.getAbsolutePath());
return config;
}
......@@ -98,7 +98,7 @@ public class LoginListenerIT {
return true;
}
};
listener.contextInitialized(null);
processor.login();
assert UserGroupInformation.getLoginUser().getShortUserName().endsWith("dgi");
assert UserGroupInformation.getCurrentUser() != null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment