|
@@ -22,12 +22,17 @@ import static org.apache.hadoop.yarn.webapp.WebServicesTestUtils.assertResponseS
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
import static org.junit.Assert.fail;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.StringReader;
|
|
|
+import java.security.Principal;
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
@@ -44,11 +49,13 @@ import org.apache.hadoop.yarn.server.nodemanager.Context;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.LocalDirsHandlerService;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.NodeHealthCheckerService;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.NodeManager;
|
|
|
+import org.apache.hadoop.yarn.server.nodemanager.NodeManager.NMContext;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.ResourceView;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.containermanager.application.ApplicationState;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.webapp.WebServer.NMWebApp;
|
|
|
+import org.apache.hadoop.yarn.server.nodemanager.webapp.dao.AppsInfo;
|
|
|
import org.apache.hadoop.yarn.server.security.ApplicationACLsManager;
|
|
|
import org.apache.hadoop.yarn.server.utils.BuilderUtils;
|
|
|
import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
|
|
@@ -192,25 +199,28 @@ public class TestNMWebServicesApps extends JerseyTestBase {
|
|
|
|
|
|
private HashMap<String, String> addAppContainers(Application app)
|
|
|
throws IOException {
|
|
|
+ return addAppContainers(app, nmContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ private HashMap<String, String> addAppContainers(Application app,
|
|
|
+ Context context) throws IOException {
|
|
|
Dispatcher dispatcher = new AsyncDispatcher();
|
|
|
- ApplicationAttemptId appAttemptId = BuilderUtils.newApplicationAttemptId(
|
|
|
- app.getAppId(), 1);
|
|
|
+ ApplicationAttemptId appAttemptId = BuilderUtils
|
|
|
+ .newApplicationAttemptId(app.getAppId(), 1);
|
|
|
Container container1 = new MockContainer(appAttemptId, dispatcher, conf,
|
|
|
app.getUser(), app.getAppId(), 1);
|
|
|
Container container2 = new MockContainer(appAttemptId, dispatcher, conf,
|
|
|
app.getUser(), app.getAppId(), 2);
|
|
|
- nmContext.getContainers()
|
|
|
- .put(container1.getContainerId(), container1);
|
|
|
- nmContext.getContainers()
|
|
|
- .put(container2.getContainerId(), container2);
|
|
|
+ context.getContainers().put(container1.getContainerId(), container1);
|
|
|
+ context.getContainers().put(container2.getContainerId(), container2);
|
|
|
|
|
|
app.getContainers().put(container1.getContainerId(), container1);
|
|
|
app.getContainers().put(container2.getContainerId(), container2);
|
|
|
HashMap<String, String> hash = new HashMap<String, String>();
|
|
|
- hash.put(container1.getContainerId().toString(), container1
|
|
|
- .getContainerId().toString());
|
|
|
- hash.put(container2.getContainerId().toString(), container2
|
|
|
- .getContainerId().toString());
|
|
|
+ hash.put(container1.getContainerId().toString(),
|
|
|
+ container1.getContainerId().toString());
|
|
|
+ hash.put(container2.getContainerId().toString(),
|
|
|
+ container2.getContainerId().toString());
|
|
|
return hash;
|
|
|
}
|
|
|
|
|
@@ -721,4 +731,42 @@ public class TestNMWebServicesApps extends JerseyTestBase {
|
|
|
user);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testNodeAppsUserFiltering() throws JSONException, Exception {
|
|
|
+ Configuration yarnConf = new Configuration();
|
|
|
+ yarnConf.setBoolean(YarnConfiguration.FILTER_ENTITY_LIST_BY_USER, true);
|
|
|
+ yarnConf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, true);
|
|
|
+ yarnConf.setStrings(YarnConfiguration.YARN_ADMIN_ACL, "admin");
|
|
|
+ ApplicationACLsManager aclManager = new ApplicationACLsManager(yarnConf);
|
|
|
+
|
|
|
+ NMContext context = new NodeManager.NMContext(null, null, dirsHandler,
|
|
|
+ aclManager, null, false, yarnConf);
|
|
|
+ Application app = new MockApp(1);
|
|
|
+ context.getApplications().put(app.getAppId(), app);
|
|
|
+ addAppContainers(app, context);
|
|
|
+ Application app2 = new MockApp("foo", 1234, 2);
|
|
|
+ context.getApplications().put(app2.getAppId(), app2);
|
|
|
+ addAppContainers(app2, context);
|
|
|
+
|
|
|
+ // User "foo" could only see its own apps/containers.
|
|
|
+ NMWebServices webSvc = new NMWebServices(context, null, nmWebApp,
|
|
|
+ mock(HttpServletResponse.class));
|
|
|
+ HttpServletRequest mockHsr = mockHttpServletRequestByUserName("foo");
|
|
|
+ AppsInfo appsInfo = webSvc.getNodeApps(mockHsr, null, null);
|
|
|
+ assertEquals(1, appsInfo.getApps().size());
|
|
|
+
|
|
|
+ // Admin could see all apps and containers.
|
|
|
+ HttpServletRequest mockHsrAdmin = mockHttpServletRequestByUserName("admin");
|
|
|
+ AppsInfo appsInfo2 = webSvc.getNodeApps(mockHsrAdmin, null, null);
|
|
|
+ assertEquals(2, appsInfo2.getApps().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpServletRequest mockHttpServletRequestByUserName(String username) {
|
|
|
+ HttpServletRequest mockHsr = mock(HttpServletRequest.class);
|
|
|
+ when(mockHsr.getRemoteUser()).thenReturn(username);
|
|
|
+ Principal principal = mock(Principal.class);
|
|
|
+ when(principal.getName()).thenReturn(username);
|
|
|
+ when(mockHsr.getUserPrincipal()).thenReturn(principal);
|
|
|
+ return mockHsr;
|
|
|
+ }
|
|
|
}
|