Skip to content

Commit 46a7e02

Browse files
committed
HADOOP-8561. Introduce HADOOP_PROXY_USER for secure impersonation in child hadoop client processes. (Yu Gao via llu)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1422429 13f79535-47bb-0310-9956-ffa450edef68
1 parent a6f3fe8 commit 46a7e02

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class UserGroupInformation {
8181
*/
8282
private static final float TICKET_RENEW_WINDOW = 0.80f;
8383
static final String HADOOP_USER_NAME = "HADOOP_USER_NAME";
84+
static final String HADOOP_PROXY_USER = "HADOOP_PROXY_USER";
8485

8586
/**
8687
* UgiMetrics maintains UGI activity statistics
@@ -641,10 +642,18 @@ static UserGroupInformation getLoginUser() throws IOException {
641642
newLoginContext(authenticationMethod.getLoginAppName(),
642643
subject, new HadoopConfiguration());
643644
login.login();
644-
loginUser = new UserGroupInformation(subject);
645-
loginUser.setLogin(login);
646-
loginUser.setAuthenticationMethod(authenticationMethod);
647-
loginUser = new UserGroupInformation(login.getSubject());
645+
UserGroupInformation realUser = new UserGroupInformation(subject);
646+
realUser.setLogin(login);
647+
realUser.setAuthenticationMethod(authenticationMethod);
648+
realUser = new UserGroupInformation(login.getSubject());
649+
// If the HADOOP_PROXY_USER environment variable or property
650+
// is specified, create a proxy user as the logged in user.
651+
String proxyUser = System.getenv(HADOOP_PROXY_USER);
652+
if (proxyUser == null) {
653+
proxyUser = System.getProperty(HADOOP_PROXY_USER);
654+
}
655+
loginUser = proxyUser == null ? realUser : createProxyUser(proxyUser, realUser);
656+
648657
String fileLocation = System.getenv(HADOOP_TOKEN_FILE_LOCATION);
649658
if (fileLocation != null) {
650659
// load the token storage file and put all of the tokens into the
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package org.apache.hadoop.security;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
23+
import java.io.BufferedReader;
24+
import java.io.IOException;
25+
import java.io.InputStreamReader;
26+
27+
import org.junit.Test;
28+
29+
public class TestProxyUserFromEnv {
30+
/** Test HADOOP_PROXY_USER for impersonation */
31+
@Test
32+
public void testProxyUserFromEnvironment() throws IOException {
33+
String proxyUser = "foo.bar";
34+
System.setProperty(UserGroupInformation.HADOOP_PROXY_USER, proxyUser);
35+
UserGroupInformation ugi = UserGroupInformation.getLoginUser();
36+
assertEquals(proxyUser, ugi.getUserName());
37+
38+
UserGroupInformation realUgi = ugi.getRealUser();
39+
assertNotNull(realUgi);
40+
// get the expected real user name
41+
Process pp = Runtime.getRuntime().exec("whoami");
42+
BufferedReader br = new BufferedReader
43+
(new InputStreamReader(pp.getInputStream()));
44+
String realUser = br.readLine().trim();
45+
assertEquals(realUser, realUgi.getUserName());
46+
}
47+
}

0 commit comments

Comments
 (0)