|
| 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 | +} |
0 commit comments