Skip to content

Commit f2b056b

Browse files
committed
HADOOP-13707. Skip authorization for anonymous user to access Hadoop
web interface in non-secure environment. (Yuanbo Liu via eyang)
1 parent a0da1ec commit f2b056b

File tree

7 files changed

+76
-12
lines changed

7 files changed

+76
-12
lines changed

hadoop-common-project/hadoop-common/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ Release 2.8.0 - UNRELEASED
175175

176176
BUG FIXES
177177

178+
HADOOP-13707. Skip authorization for anonymous user to access Hadoop
179+
web interface in non-secure environment. (Yuanbo Liu via eyang)
180+
178181
HADOOP-12124. Add HTrace support for FsShell (cmccabe)
179182

180183
HADOOP-12171. Shorten overly-long htrace span names for server (cmccabe)

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/ConfServlet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.Writer;
2222

23+
import javax.servlet.ServletContext;
2324
import javax.servlet.ServletException;
2425
import javax.servlet.http.HttpServlet;
2526
import javax.servlet.http.HttpServletRequest;
@@ -56,7 +57,12 @@ private Configuration getConfFromContext() {
5657
public void doGet(HttpServletRequest request, HttpServletResponse response)
5758
throws ServletException, IOException {
5859

59-
if (!HttpServer2.isInstrumentationAccessAllowed(getServletContext(),
60+
// If user is a static user and auth Type is null, that means
61+
// there is a non-security environment and no need authorization,
62+
// otherwise, do the authorization.
63+
final ServletContext servletContext = getServletContext();
64+
if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) &&
65+
!HttpServer2.isInstrumentationAccessAllowed(servletContext,
6066
request, response)) {
6167
return;
6268
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/AdminAuthorizedServlet.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121

22+
import javax.servlet.ServletContext;
2223
import javax.servlet.ServletException;
2324
import javax.servlet.http.HttpServletRequest;
2425
import javax.servlet.http.HttpServletResponse;
@@ -35,9 +36,13 @@ public class AdminAuthorizedServlet extends DefaultServlet {
3536

3637
@Override
3738
protected void doGet(HttpServletRequest request, HttpServletResponse response)
38-
throws ServletException, IOException {
39-
// Do the authorization
40-
if (HttpServer2.hasAdministratorAccess(getServletContext(), request,
39+
throws ServletException, IOException {
40+
// If user is a static user and auth Type is null, that means
41+
// there is a non-security environment and no need authorization,
42+
// otherwise, do the authorization.
43+
final ServletContext servletContext = getServletContext();
44+
if (HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) ||
45+
HttpServer2.hasAdministratorAccess(servletContext, request,
4146
response)) {
4247
// Authorization is done. Just call super.
4348
super.doGet(request, response);

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
import com.google.common.base.Preconditions;
9797
import com.google.common.collect.Lists;
9898
import com.sun.jersey.spi.container.servlet.ServletContainer;
99+
import static org.apache.hadoop.fs.CommonConfigurationKeys.DEFAULT_HADOOP_HTTP_STATIC_USER;
100+
import static org.apache.hadoop.fs.CommonConfigurationKeys.HADOOP_HTTP_STATIC_USER;
99101

100102
import static org.apache.hadoop.security.authentication.server
101103
.AuthenticationFilter.*;
@@ -1004,6 +1006,24 @@ public String toString() {
10041006
return sb.toString();
10051007
}
10061008

1009+
/**
1010+
* check whether user is static and unauthenticated, if the
1011+
* answer is TRUE, that means http sever is in non-security
1012+
* environment.
1013+
* @param servletContext the servlet context.
1014+
* @param request the servlet request.
1015+
* @return TRUE/FALSE based on the logic described above.
1016+
*/
1017+
public static boolean isStaticUserAndNoneAuthType(
1018+
ServletContext servletContext, HttpServletRequest request) {
1019+
Configuration conf =
1020+
(Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE);
1021+
final String authType = request.getAuthType();
1022+
final String staticUser = conf.get(HADOOP_HTTP_STATIC_USER,
1023+
DEFAULT_HADOOP_HTTP_STATIC_USER);
1024+
return authType == null && staticUser.equals(request.getRemoteUser());
1025+
}
1026+
10071027
/**
10081028
* Checks the user has privileges to access to instrumentation servlets.
10091029
* <p/>
@@ -1101,9 +1121,14 @@ public static class StackServlet extends HttpServlet {
11011121

11021122
@Override
11031123
public void doGet(HttpServletRequest request, HttpServletResponse response)
1104-
throws ServletException, IOException {
1105-
if (!HttpServer2.isInstrumentationAccessAllowed(getServletContext(),
1106-
request, response)) {
1124+
throws ServletException, IOException {
1125+
// If user is a static user and auth Type is null, that means
1126+
// there is a non-security environment and no need authorization,
1127+
// otherwise, do the authorization.
1128+
final ServletContext servletContext = getServletContext();
1129+
if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) &&
1130+
!HttpServer2.isInstrumentationAccessAllowed(servletContext,
1131+
request, response)) {
11071132
return;
11081133
}
11091134
response.setContentType("text/plain; charset=UTF-8");

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.management.openmbean.CompositeData;
3939
import javax.management.openmbean.CompositeType;
4040
import javax.management.openmbean.TabularData;
41+
import javax.servlet.ServletContext;
4142
import javax.servlet.ServletException;
4243
import javax.servlet.http.HttpServlet;
4344
import javax.servlet.http.HttpServletRequest;
@@ -157,7 +158,12 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) {
157158
String jsonpcb = null;
158159
PrintWriter writer = null;
159160
try {
160-
if (!isInstrumentationAccessAllowed(request, response)) {
161+
// If user is a static user and auth Type is null, that means
162+
// there is a non-security environment and no need authorization,
163+
// otherwise, do the authorization.
164+
final ServletContext servletContext = getServletContext();
165+
if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) &&
166+
!isInstrumentationAccessAllowed(request, response)) {
161167
return;
162168
}
163169

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ public static class Servlet extends HttpServlet {
9393
public void doGet(HttpServletRequest request, HttpServletResponse response
9494
) throws ServletException, IOException {
9595

96-
// Do the authorization
97-
if (!HttpServer2.hasAdministratorAccess(getServletContext(), request,
98-
response)) {
96+
// If user is a static user and auth Type is null, that means
97+
// there is a non-security environment and no need authorization,
98+
// otherwise, do the authorization.
99+
final ServletContext servletContext = getServletContext();
100+
if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) &&
101+
!HttpServer2.hasAdministratorAccess(servletContext,
102+
request, response)) {
99103
return;
100104
}
101105

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/http/TestHttpServer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
import java.util.concurrent.Executor;
6565
import java.util.concurrent.Executors;
6666

67+
import static org.apache.hadoop.fs.CommonConfigurationKeys.DEFAULT_HADOOP_HTTP_STATIC_USER;
68+
import static org.apache.hadoop.fs.CommonConfigurationKeys.HADOOP_HTTP_STATIC_USER;
69+
6770
public class TestHttpServer extends HttpServerFunctionalTest {
6871
static final Log LOG = LogFactory.getLog(TestHttpServer.class);
6972
private static HttpServer2 server;
@@ -383,14 +386,26 @@ public void testAuthorizationOfDefaultServlets() throws Exception {
383386
String serverURL = "http://"
384387
+ NetUtils.getHostPortString(myServer.getConnectorAddress(0)) + "/";
385388
for (String servlet : new String[] { "conf", "logs", "stacks",
386-
"logLevel", "metrics" }) {
389+
"logLevel", "metrics", "jmx" }) {
387390
for (String user : new String[] { "userA", "userB", "userC", "userD" }) {
388391
assertEquals(HttpURLConnection.HTTP_OK, getHttpStatusCode(serverURL
389392
+ servlet, user));
390393
}
391394
assertEquals(HttpURLConnection.HTTP_FORBIDDEN, getHttpStatusCode(
392395
serverURL + servlet, "userE"));
393396
}
397+
398+
// hadoop.security.authorization is set as true while
399+
// hadoop.http.authentication.type's value is `simple`(default value)
400+
// in this case, static user has administrator access
401+
final String staticUser = conf.get(HADOOP_HTTP_STATIC_USER,
402+
DEFAULT_HADOOP_HTTP_STATIC_USER);
403+
for (String servlet : new String[] {"conf", "logs", "stacks",
404+
"logLevel", "jmx"}) {
405+
assertEquals(HttpURLConnection.HTTP_OK, getHttpStatusCode(
406+
serverURL + servlet, staticUser));
407+
}
408+
394409
myServer.stop();
395410
}
396411

0 commit comments

Comments
 (0)