|
@@ -39,15 +39,18 @@ import javax.ws.rs.core.MediaType;
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
|
|
+import com.google.common.collect.ImmutableSet;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.http.JettyUtils;
|
|
|
+import org.apache.hadoop.security.UserGroupInformation;
|
|
|
import org.apache.hadoop.service.Service.STATE;
|
|
|
import org.apache.hadoop.util.VersionInfo;
|
|
|
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest;
|
|
|
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse;
|
|
|
import org.apache.hadoop.yarn.api.records.ApplicationId;
|
|
|
import org.apache.hadoop.yarn.api.records.ApplicationReport;
|
|
|
+import org.apache.hadoop.yarn.api.records.QueueACL;
|
|
|
import org.apache.hadoop.yarn.api.records.QueueState;
|
|
|
import org.apache.hadoop.yarn.conf.YarnConfiguration;
|
|
|
import org.apache.hadoop.yarn.server.resourcemanager.ClientRMService;
|
|
@@ -72,6 +75,8 @@ import org.apache.hadoop.yarn.webapp.JerseyTestBase;
|
|
|
import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
|
|
|
import org.codehaus.jettison.json.JSONException;
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
|
|
+import org.eclipse.jetty.server.Response;
|
|
|
+import org.junit.Assert;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.BeforeClass;
|
|
|
import org.junit.Test;
|
|
@@ -752,4 +757,83 @@ public class TestRMWebServices extends JerseyTestBase {
|
|
|
tickcount--;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCheckUserAccessToQueue() throws Exception {
|
|
|
+
|
|
|
+ ResourceManager mockRM = mock(ResourceManager.class);
|
|
|
+ Configuration conf = new YarnConfiguration();
|
|
|
+
|
|
|
+ // Inject a mock scheduler implementation.
|
|
|
+ // Only admin user has ADMINISTER_QUEUE access.
|
|
|
+ // For SUBMIT_APPLICATION ACL, both of admin/yarn user have acess
|
|
|
+ ResourceScheduler mockScheduler = new FifoScheduler() {
|
|
|
+ @Override
|
|
|
+ public synchronized boolean checkAccess(UserGroupInformation callerUGI,
|
|
|
+ QueueACL acl, String queueName) {
|
|
|
+ if (acl == QueueACL.ADMINISTER_QUEUE) {
|
|
|
+ if (callerUGI.getUserName().equals("admin")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (ImmutableSet.of("admin", "yarn").contains(callerUGI.getUserName())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ when(mockRM.getResourceScheduler()).thenReturn(mockScheduler);
|
|
|
+
|
|
|
+ RMWebServices webSvc =
|
|
|
+ new RMWebServices(mockRM, conf, mock(HttpServletResponse.class));
|
|
|
+
|
|
|
+ // Case 1: Only queue admin user can access other user's information
|
|
|
+ HttpServletRequest mockHsr = mockHttpServletRequestByUserName("non-admin");
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "jack",
|
|
|
+ QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_FORBIDDEN);
|
|
|
+
|
|
|
+ // Case 2: request an unknown ACL causes BAD_REQUEST
|
|
|
+ mockHsr = mockHttpServletRequestByUserName("admin");
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "jack",
|
|
|
+ "XYZ_ACL", mockHsr).getStatus(), Response.SC_BAD_REQUEST);
|
|
|
+
|
|
|
+ // Case 3: get FORBIDDEN for rejected ACL
|
|
|
+ mockHsr = mockHttpServletRequestByUserName("admin");
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "jack",
|
|
|
+ QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_FORBIDDEN);
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "jack",
|
|
|
+ QueueACL.ADMINISTER_QUEUE.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_FORBIDDEN);
|
|
|
+
|
|
|
+ // Case 4: get OK for listed ACLs
|
|
|
+ mockHsr = mockHttpServletRequestByUserName("admin");
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "admin",
|
|
|
+ QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_OK);
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "admin",
|
|
|
+ QueueACL.ADMINISTER_QUEUE.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_OK);
|
|
|
+
|
|
|
+ // Case 5: get OK only for SUBMIT_APP acl for "yarn" user
|
|
|
+ mockHsr = mockHttpServletRequestByUserName("admin");
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "yarn",
|
|
|
+ QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_OK);
|
|
|
+ Assert.assertEquals(webSvc.checkUserAccessToQueue("queue", "yarn",
|
|
|
+ QueueACL.ADMINISTER_QUEUE.name(), mockHsr).getStatus(),
|
|
|
+ Response.SC_FORBIDDEN);
|
|
|
+ }
|
|
|
}
|