Commit da7a6a88 by Jon Maron

BUG-32834 login processor

parent 403c04b3
...@@ -78,6 +78,10 @@ public class GuiceServletConfig extends GuiceServletContextListener { ...@@ -78,6 +78,10 @@ public class GuiceServletConfig extends GuiceServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) { public void contextInitialized(ServletContextEvent servletContextEvent) {
super.contextInitialized(servletContextEvent); super.contextInitialized(servletContextEvent);
// perform login operations
LoginProcessor loginProcessor = new LoginProcessor();
loginProcessor.login();
restoreTypeSystem(); restoreTypeSystem();
} }
......
...@@ -25,34 +25,26 @@ import org.apache.hadoop.util.Shell; ...@@ -25,34 +25,26 @@ import org.apache.hadoop.util.Shell;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; 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 private static final Logger LOG = LoggerFactory
.getLogger(LoginListener.class); .getLogger(LoginProcessor.class);
public static final String AUTHENTICATION_METHOD = "authentication.method"; public static final String METADATA_AUTHENTICATION_PREFIX = "metadata.authentication.";
public static final String AUTHENTICATION_PRINCIPAL = "authentication.principal"; public static final String AUTHENTICATION_METHOD = METADATA_AUTHENTICATION_PREFIX + "method";
public static final String AUTHENTICATION_KEYTAB = "authentication.keytab"; public static final String AUTHENTICATION_PRINCIPAL = METADATA_AUTHENTICATION_PREFIX + "principal";
public static final String AUTHENTICATION_KEYTAB = METADATA_AUTHENTICATION_PREFIX + "keytab";
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
/** /**
* Perform a SIMPLE login based on established OS identity or a kerberos based login using the configured * Perform a SIMPLE login based on established OS identity or a kerberos based login using the configured
* principal and keytab (via application.properties). * principal and keytab (via application.properties).
* @param servletContextEvent
*/ */
@Override public void login() {
public void contextInitialized(ServletContextEvent servletContextEvent) {
// first, let's see if we're running in a hadoop cluster and have the env configured // first, let's see if we're running in a hadoop cluster and have the env configured
boolean isHadoopCluster = isHadoopCluster(); boolean isHadoopCluster = isHadoopCluster();
Configuration hadoopConfig = isHadoopCluster ? getHadoopConfiguration() : new Configuration(false); Configuration hadoopConfig = isHadoopCluster ? getHadoopConfiguration() : new Configuration(false);
...@@ -64,17 +56,12 @@ public class LoginListener implements ServletContextListener { ...@@ -64,17 +56,12 @@ public class LoginListener implements ServletContextListener {
} }
if (!isHadoopCluster) { if (!isHadoopCluster) {
// need to read the configured authentication choice and create the UGI configuration // need to read the configured authentication choice and create the UGI configuration
String authMethod; setupHadoopConfiguration(hadoopConfig, configuration);
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);
} }
doServiceLogin(hadoopConfig, configuration);
}
protected void doServiceLogin(Configuration hadoopConfig, PropertiesConfiguration configuration) {
UserGroupInformation.setConfiguration(hadoopConfig); UserGroupInformation.setConfiguration(hadoopConfig);
UserGroupInformation ugi = null; UserGroupInformation ugi = null;
...@@ -95,6 +82,19 @@ public class LoginListener implements ServletContextListener { ...@@ -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 * 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) * name (e.g. dgi/_HOST will be changed to dgi/localHostName)
......
...@@ -48,9 +48,6 @@ ...@@ -48,9 +48,6 @@
</filter-mapping> </filter-mapping>
<listener> <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-class>org.apache.hadoop.metadata.web.listeners.GuiceServletConfig</listener-class>
</listener> </listener>
</web-app> </web-app>
...@@ -35,7 +35,7 @@ import java.util.Properties; ...@@ -35,7 +35,7 @@ import java.util.Properties;
/** /**
* *
*/ */
public class LoginListenerIT { public class LoginProcessorIT {
private static final String JAAS_ENTRY = private static final String JAAS_ENTRY =
"%s { \n" "%s { \n"
...@@ -56,13 +56,13 @@ public class LoginListenerIT { ...@@ -56,13 +56,13 @@ public class LoginListenerIT {
@Test @Test
public void testDefaultSimpleLogin() throws Exception { public void testDefaultSimpleLogin() throws Exception {
LoginListener listener = new LoginListener() { LoginProcessor processor = new LoginProcessor() {
@Override @Override
protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException { protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException {
return new PropertiesConfiguration(); return new PropertiesConfiguration();
} }
}; };
listener.contextInitialized(null); processor.login();
assert UserGroupInformation.getCurrentUser() != null; assert UserGroupInformation.getCurrentUser() != null;
assert !UserGroupInformation.isLoginKeytabBased(); assert !UserGroupInformation.isLoginKeytabBased();
...@@ -73,13 +73,13 @@ public class LoginListenerIT { ...@@ -73,13 +73,13 @@ public class LoginListenerIT {
public void testKerberosLogin() throws Exception { public void testKerberosLogin() throws Exception {
final File keytab = setupKDCAndPrincipals(); final File keytab = setupKDCAndPrincipals();
LoginListener listener = new LoginListener() { LoginProcessor processor = new LoginProcessor() {
@Override @Override
protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException { protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException {
PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfiguration config = new PropertiesConfiguration();
config.setProperty("authentication.method", "kerberos"); config.setProperty("metadata.authentication.method", "kerberos");
config.setProperty("authentication.principal", "dgi@EXAMPLE.COM"); config.setProperty("metadata.authentication.principal", "dgi@EXAMPLE.COM");
config.setProperty("authentication.keytab", keytab.getAbsolutePath()); config.setProperty("metadata.authentication.keytab", keytab.getAbsolutePath());
return config; return config;
} }
...@@ -98,7 +98,7 @@ public class LoginListenerIT { ...@@ -98,7 +98,7 @@ public class LoginListenerIT {
return true; return true;
} }
}; };
listener.contextInitialized(null); processor.login();
assert UserGroupInformation.getLoginUser().getShortUserName().endsWith("dgi"); assert UserGroupInformation.getLoginUser().getShortUserName().endsWith("dgi");
assert UserGroupInformation.getCurrentUser() != null; 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