Skip to content

Commit f3e8be3

Browse files
committed
HADOOP-6603. Provide workaround for issue with Kerberos not resolving cross-realm principal. Contributed by Kan Zhang and Jitendra Pandey.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@953517 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3360bff commit f3e8be3

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ Trunk (unreleased changes)
8181

8282
HADOOP-6796. FileStatus allows null srcPath but crashes if that's done. (Rodrigo Schmidt via eli)
8383

84+
HADOOP-6603. Provide workaround for issue with Kerberos not resolving
85+
cross-realm principal (Kan Zhang and Jitendra Pandey via jghoman)
86+
8487
Release 0.21.0 - Unreleased
8588

8689
INCOMPATIBLE CHANGES
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
package org.apache.hadoop.security;
18+
19+
import java.io.IOException;
20+
import java.net.URL;
21+
import java.security.AccessController;
22+
import java.util.Set;
23+
24+
import javax.security.auth.Subject;
25+
import javax.security.auth.kerberos.KerberosTicket;
26+
27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
29+
import org.apache.hadoop.security.UserGroupInformation;
30+
31+
import sun.security.jgss.krb5.Krb5Util;
32+
import sun.security.krb5.Credentials;
33+
import sun.security.krb5.PrincipalName;
34+
35+
public class SecurityUtil {
36+
private static final Log LOG = LogFactory.getLog(SecurityUtil.class);
37+
38+
/**
39+
* Find the original TGT within the current subject's credentials. Cross-realm
40+
* TGT's of the form "krbtgt/[email protected]" may be present.
41+
*
42+
* @return The TGT from the current subject
43+
* @throws IOException
44+
* if TGT can't be found
45+
*/
46+
private static KerberosTicket getTgtFromSubject() throws IOException {
47+
Set<KerberosTicket> tickets = Subject.getSubject(
48+
AccessController.getContext()).getPrivateCredentials(
49+
KerberosTicket.class);
50+
for (KerberosTicket t : tickets) {
51+
if (isOriginalTGT(t.getServer().getName()))
52+
return t;
53+
}
54+
throw new IOException("Failed to find TGT from current Subject");
55+
}
56+
57+
// Original TGT must be of form "krbtgt/FOO@FOO". Verify this
58+
protected static boolean isOriginalTGT(String name) {
59+
if(name == null) return false;
60+
61+
String [] components = name.split("[/@]");
62+
63+
return components.length == 3 &&
64+
"krbtgt".equals(components[0]) &&
65+
components[1].equals(components[2]);
66+
}
67+
68+
/**
69+
* Explicitly pull the service ticket for the specified host. This solves a
70+
* problem with Java's Kerberos SSL problem where the client cannot
71+
* authenticate against a cross-realm service. It is necessary for clients
72+
* making kerberized https requests to call this method on the target URL
73+
* to ensure that in a cross-realm environment the remote host will be
74+
* successfully authenticated.
75+
*
76+
* This method is internal to Hadoop and should not be used by other
77+
* applications. This method should not be considered stable or open:
78+
* it will be removed when the Java behavior is changed.
79+
*
80+
* @param remoteHost Target URL the krb-https client will access
81+
* @throws IOException
82+
*/
83+
public static void fetchServiceTicket(URL remoteHost) throws IOException {
84+
if(!UserGroupInformation.isSecurityEnabled())
85+
return;
86+
87+
String serviceName = "host/" + remoteHost.getHost();
88+
LOG.debug("Fetching service ticket for host at: " + serviceName);
89+
Credentials serviceCred = null;
90+
try {
91+
PrincipalName principal = new PrincipalName(serviceName,
92+
PrincipalName.KRB_NT_SRV_HST);
93+
serviceCred = Credentials.acquireServiceCreds(principal
94+
.toString(), Krb5Util.ticketToCreds(getTgtFromSubject()));
95+
} catch (Exception e) {
96+
throw new IOException("Invalid service principal name: "
97+
+ serviceName, e);
98+
}
99+
if (serviceCred == null) {
100+
throw new IOException("Can't get service ticket for " + serviceName);
101+
}
102+
Subject.getSubject(AccessController.getContext()).getPrivateCredentials()
103+
.add(Krb5Util.credsToTicket(serviceCred));
104+
}
105+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
package org.apache.hadoop.security;
18+
19+
import static org.junit.Assert.*;
20+
import org.junit.Test;
21+
22+
public class TestSecurityUtil {
23+
@Test
24+
public void isOriginalTGTReturnsCorrectValues() {
25+
assertTrue(SecurityUtil.isOriginalTGT("krbtgt/foo@foo"));
26+
assertTrue(SecurityUtil.isOriginalTGT("krbtgt/[email protected]"));
27+
assertFalse(SecurityUtil.isOriginalTGT(null));
28+
assertFalse(SecurityUtil.isOriginalTGT("blah"));
29+
assertFalse(SecurityUtil.isOriginalTGT(""));
30+
assertFalse(SecurityUtil.isOriginalTGT("krbtgt/hello"));
31+
assertFalse(SecurityUtil.isOriginalTGT("/@"));
32+
assertFalse(SecurityUtil.isOriginalTGT("this@is/notright"));
33+
assertFalse(SecurityUtil.isOriginalTGT("krbtgt/foo@FOO"));
34+
}
35+
}

0 commit comments

Comments
 (0)