Return-Path: X-Original-To: apmail-hadoop-common-commits-archive@www.apache.org Delivered-To: apmail-hadoop-common-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 52E98D1F7 for ; Sun, 16 Dec 2012 01:02:17 +0000 (UTC) Received: (qmail 41341 invoked by uid 500); 16 Dec 2012 01:02:17 -0000 Delivered-To: apmail-hadoop-common-commits-archive@hadoop.apache.org Received: (qmail 41298 invoked by uid 500); 16 Dec 2012 01:02:17 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: common-dev@hadoop.apache.org Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 41290 invoked by uid 99); 16 Dec 2012 01:02:17 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Dec 2012 01:02:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Dec 2012 01:02:15 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id BB6C82388980 for ; Sun, 16 Dec 2012 01:01:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1422443 - in /hadoop/common/branches/branch-1.1/src: core/org/apache/hadoop/security/UserGroupInformation.java test/org/apache/hadoop/security/TestProxyUserFromEnv.java Date: Sun, 16 Dec 2012 01:01:55 -0000 To: common-commits@hadoop.apache.org From: llu@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121216010155.BB6C82388980@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: llu Date: Sun Dec 16 01:01:54 2012 New Revision: 1422443 URL: http://svn.apache.org/viewvc?rev=1422443&view=rev Log: HADOOP-8561. Introduce HADOOP_PROXY_USER for secure impersonation in child hadoop client processes. (Yu Gao via llu) Added: hadoop/common/branches/branch-1.1/src/test/org/apache/hadoop/security/TestProxyUserFromEnv.java Modified: hadoop/common/branches/branch-1.1/src/core/org/apache/hadoop/security/UserGroupInformation.java Modified: hadoop/common/branches/branch-1.1/src/core/org/apache/hadoop/security/UserGroupInformation.java URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1.1/src/core/org/apache/hadoop/security/UserGroupInformation.java?rev=1422443&r1=1422442&r2=1422443&view=diff ============================================================================== --- hadoop/common/branches/branch-1.1/src/core/org/apache/hadoop/security/UserGroupInformation.java (original) +++ hadoop/common/branches/branch-1.1/src/core/org/apache/hadoop/security/UserGroupInformation.java Sun Dec 16 01:01:54 2012 @@ -69,6 +69,7 @@ public class UserGroupInformation { */ private static final float TICKET_RENEW_WINDOW = 0.80f; static final String HADOOP_USER_NAME = "HADOOP_USER_NAME"; + static final String HADOOP_PROXY_USER = "HADOOP_PROXY_USER"; /** * A login module that looks at the Kerberos, Unix, or Windows principal and @@ -484,12 +485,20 @@ public class UserGroupInformation { login = newLoginContext(HadoopConfiguration.SIMPLE_CONFIG_NAME, subject); } login.login(); - loginUser = new UserGroupInformation(subject); - loginUser.setLogin(login); - loginUser.setAuthenticationMethod(isSecurityEnabled() ? + UserGroupInformation realUser = new UserGroupInformation(subject); + realUser.setLogin(login); + realUser.setAuthenticationMethod(isSecurityEnabled() ? AuthenticationMethod.KERBEROS : AuthenticationMethod.SIMPLE); - loginUser = new UserGroupInformation(login.getSubject()); + realUser = new UserGroupInformation(login.getSubject()); + // If the HADOOP_PROXY_USER environment variable or property + // is specified, create a proxy user as the logged in user. + String proxyUser = System.getenv(HADOOP_PROXY_USER); + if (proxyUser == null) { + proxyUser = System.getProperty(HADOOP_PROXY_USER); + } + loginUser = proxyUser == null ? realUser : createProxyUser(proxyUser, realUser); + String fileLocation = System.getenv(HADOOP_TOKEN_FILE_LOCATION); if (fileLocation != null && isSecurityEnabled()) { // load the token storage file and put all of the tokens into the Added: hadoop/common/branches/branch-1.1/src/test/org/apache/hadoop/security/TestProxyUserFromEnv.java URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1.1/src/test/org/apache/hadoop/security/TestProxyUserFromEnv.java?rev=1422443&view=auto ============================================================================== --- hadoop/common/branches/branch-1.1/src/test/org/apache/hadoop/security/TestProxyUserFromEnv.java (added) +++ hadoop/common/branches/branch-1.1/src/test/org/apache/hadoop/security/TestProxyUserFromEnv.java Sun Dec 16 01:01:54 2012 @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.hadoop.security; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import org.junit.Test; + +public class TestProxyUserFromEnv { + /** Test HADOOP_PROXY_USER for impersonation */ + @Test + public void testProxyUserFromEnvironment() throws IOException { + String proxyUser = "foo.bar"; + System.setProperty(UserGroupInformation.HADOOP_PROXY_USER, proxyUser); + UserGroupInformation ugi = UserGroupInformation.getLoginUser(); + assertEquals(proxyUser, ugi.getUserName()); + + UserGroupInformation realUgi = ugi.getRealUser(); + assertNotNull(realUgi); + // get the expected real user name + Process pp = Runtime.getRuntime().exec("whoami"); + BufferedReader br = new BufferedReader + (new InputStreamReader(pp.getInputStream())); + String realUser = br.readLine().trim(); + assertEquals(realUser, realUgi.getUserName()); + } +}