@Test public void testHasAdministratorAccess() throws Exception { Configuration conf = new Configuration(); conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false); ServletContext context = Mockito.mock(ServletContext.class); Mockito.when(context.getAttribute(HttpServer.CONF_CONTEXT_ATTRIBUTE)).thenReturn(conf); Mockito.when(context.getAttribute(HttpServer.ADMINS_ACL)).thenReturn(null); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getRemoteUser()).thenReturn(null); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Assert.assertTrue(HttpServer.hasAdministratorAccess(context, request, response)); response = Mockito.mock(HttpServletResponse.class); conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, true); Assert.assertFalse(HttpServer.hasAdministratorAccess(context, request, response)); Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED), Mockito.anyString()); response = Mockito.mock(HttpServletResponse.class); Mockito.when(request.getRemoteUser()).thenReturn("foo"); Assert.assertTrue(HttpServer.hasAdministratorAccess(context, request, response)); response = Mockito.mock(HttpServletResponse.class); AccessControlList acls = Mockito.mock(AccessControlList.class); Mockito.when(acls.isUserAllowed(Mockito.<UserGroupInformation>any())).thenReturn(false); Mockito.when(context.getAttribute(HttpServer.ADMINS_ACL)).thenReturn(acls); Assert.assertFalse(HttpServer.hasAdministratorAccess(context, request, response)); Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED), Mockito.anyString()); response = Mockito.mock(HttpServletResponse.class); Mockito.when(acls.isUserAllowed(Mockito.<UserGroupInformation>any())).thenReturn(true); Mockito.when(context.getAttribute(HttpServer.ADMINS_ACL)).thenReturn(acls); Assert.assertTrue(HttpServer.hasAdministratorAccess(context, request, response)); }
public static boolean hasAdministratorAccess( ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws IOException { Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE); if (!conf.getBoolean( CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) { return true; } String remoteUser = request.getRemoteUser(); if (remoteUser == null) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthenticated users are not " + "authorized to access this page."); return false; } if (servletContext.getAttribute(ADMINS_ACL) != null && !userHasAdministratorAccess(servletContext, remoteUser)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User " + remoteUser + " is unauthorized to access this page."); return false; } return true; }
HttpServer implements FilterContainer { public static boolean hasAdministratorAccess( ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws IOException { Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE); if (!conf.getBoolean( CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) { return true; } String remoteUser = request.getRemoteUser(); if (remoteUser == null) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthenticated users are not " + "authorized to access this page."); return false; } if (servletContext.getAttribute(ADMINS_ACL) != null && !userHasAdministratorAccess(servletContext, remoteUser)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User " + remoteUser + " is unauthorized to access this page."); return false; } return true; } }
HttpServer implements FilterContainer { public static boolean hasAdministratorAccess( ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws IOException { Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE); if (!conf.getBoolean( CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) { return true; } String remoteUser = request.getRemoteUser(); if (remoteUser == null) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthenticated users are not " + "authorized to access this page."); return false; } if (servletContext.getAttribute(ADMINS_ACL) != null && !userHasAdministratorAccess(servletContext, remoteUser)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User " + remoteUser + " is unauthorized to access this page."); return false; } return true; } @Deprecated  HttpServer(String name, String bindAddress, int port, boolean findPort
      ); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, Connector connector); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, String[] pathSpecs); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl, 
      Connector connector); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl, 
      Connector connector, String[] pathSpecs); private  HttpServer(final Builder b); }
HttpServer implements FilterContainer { public static boolean hasAdministratorAccess( ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws IOException { Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE); if (!conf.getBoolean( CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) { return true; } String remoteUser = request.getRemoteUser(); if (remoteUser == null) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthenticated users are not " + "authorized to access this page."); return false; } if (servletContext.getAttribute(ADMINS_ACL) != null && !userHasAdministratorAccess(servletContext, remoteUser)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User " + remoteUser + " is unauthorized to access this page."); return false; } return true; } @Deprecated  HttpServer(String name, String bindAddress, int port, boolean findPort
      ); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, Connector connector); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, String[] pathSpecs); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl, 
      Connector connector); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl, 
      Connector connector, String[] pathSpecs); private  HttpServer(final Builder b); Connector createBaseListener(Configuration conf); @InterfaceAudience.Private static Connector createDefaultChannelConnector(); void addContext(Context ctxt, boolean isFiltered); void setAttribute(String name, Object value); void addJerseyResourcePackage(final String packageName,
      final String pathSpec); void addServlet(String name, String pathSpec,
      Class<? extends HttpServlet> clazz); void addInternalServlet(String name, String pathSpec,
      Class<? extends HttpServlet> clazz); void addInternalServlet(String name, String pathSpec, 
      Class<? extends HttpServlet> clazz, boolean requireAuth); @Override void addFilter(String name, String classname,
      Map<String, String> parameters); @Override void addGlobalFilter(String name, String classname,
      Map<String, String> parameters); static void defineFilter(Context ctx, String name,
      String classname, Map<String,String> parameters, String[] urls); Object getAttribute(String name); WebAppContext getWebAppContext(); String getWebAppsPath(String appName); @Deprecated int getPort(); InetSocketAddress getConnectorAddress(int index); void setThreads(int min, int max); void start(); void stop(); void join(); boolean isAlive(); @Override String toString(); static boolean isInstrumentationAccessAllowed(
    ServletContext servletContext, HttpServletRequest request,
    HttpServletResponse response); static boolean hasAdministratorAccess(
      ServletContext servletContext, HttpServletRequest request,
      HttpServletResponse response); static boolean userHasAdministratorAccess(ServletContext servletContext,
      String remoteUser); }
HttpServer implements FilterContainer { public static boolean hasAdministratorAccess( ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws IOException { Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE); if (!conf.getBoolean( CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) { return true; } String remoteUser = request.getRemoteUser(); if (remoteUser == null) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthenticated users are not " + "authorized to access this page."); return false; } if (servletContext.getAttribute(ADMINS_ACL) != null && !userHasAdministratorAccess(servletContext, remoteUser)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User " + remoteUser + " is unauthorized to access this page."); return false; } return true; } @Deprecated  HttpServer(String name, String bindAddress, int port, boolean findPort
      ); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, Connector connector); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, String[] pathSpecs); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl, 
      Connector connector); @Deprecated  HttpServer(String name, String bindAddress, int port,
      boolean findPort, Configuration conf, AccessControlList adminsAcl, 
      Connector connector, String[] pathSpecs); private  HttpServer(final Builder b); Connector createBaseListener(Configuration conf); @InterfaceAudience.Private static Connector createDefaultChannelConnector(); void addContext(Context ctxt, boolean isFiltered); void setAttribute(String name, Object value); void addJerseyResourcePackage(final String packageName,
      final String pathSpec); void addServlet(String name, String pathSpec,
      Class<? extends HttpServlet> clazz); void addInternalServlet(String name, String pathSpec,
      Class<? extends HttpServlet> clazz); void addInternalServlet(String name, String pathSpec, 
      Class<? extends HttpServlet> clazz, boolean requireAuth); @Override void addFilter(String name, String classname,
      Map<String, String> parameters); @Override void addGlobalFilter(String name, String classname,
      Map<String, String> parameters); static void defineFilter(Context ctx, String name,
      String classname, Map<String,String> parameters, String[] urls); Object getAttribute(String name); WebAppContext getWebAppContext(); String getWebAppsPath(String appName); @Deprecated int getPort(); InetSocketAddress getConnectorAddress(int index); void setThreads(int min, int max); void start(); void stop(); void join(); boolean isAlive(); @Override String toString(); static boolean isInstrumentationAccessAllowed(
    ServletContext servletContext, HttpServletRequest request,
    HttpServletResponse response); static boolean hasAdministratorAccess(
      ServletContext servletContext, HttpServletRequest request,
      HttpServletResponse response); static boolean userHasAdministratorAccess(ServletContext servletContext,
      String remoteUser); static final Log LOG; static final String CONF_CONTEXT_ATTRIBUTE; static final String ADMINS_ACL; static final String BIND_ADDRESS; static final String SPNEGO_FILTER; static final String NO_CACHE_FILTER; static final String APP_DIR; }