|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.omid.tools.hbase; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import javax.security.auth.login.AppConfigurationEntry; |
| 25 | +import javax.security.auth.login.Configuration; |
| 26 | + |
| 27 | +import org.apache.hadoop.hbase.security.User; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +/** |
| 32 | + * Programmatic Jaas configuration object, which adds a new app configuration called |
| 33 | + * _omid_kerberos_ticket_cache, and falls back to the Configuration in effect when this was called. |
| 34 | + * To be used for connecting to Zookeeper, to reuse the kerberos ticket cache set by Hadoop/HBase |
| 35 | + * Based on Hadoop's JaasConfiguration |
| 36 | + */ |
| 37 | +public class OmidKerberosTicketCacheConfiguration extends Configuration { |
| 38 | + |
| 39 | + private static final Logger LOG = |
| 40 | + LoggerFactory.getLogger(OmidKerberosTicketCacheConfiguration.class); |
| 41 | + |
| 42 | + private final javax.security.auth.login.Configuration baseConfig = |
| 43 | + javax.security.auth.login.Configuration.getConfiguration(); |
| 44 | + |
| 45 | + private final AppConfigurationEntry[] entry; |
| 46 | + |
| 47 | + public static String APP_NAME = "_omid_kerberos_ticket_cache"; |
| 48 | + |
| 49 | + public OmidKerberosTicketCacheConfiguration() { |
| 50 | + super(); |
| 51 | + Map<String, String> options = new HashMap<>(); |
| 52 | + options.put("useTicketCache", "true"); |
| 53 | + options.putAll(getPrincipalIfNeeded()); |
| 54 | + entry = |
| 55 | + new AppConfigurationEntry[] { new AppConfigurationEntry(getKrb5LoginModuleName(), |
| 56 | + AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options) }; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public AppConfigurationEntry[] getAppConfigurationEntry(String name) { |
| 61 | + return (APP_NAME.equals(name)) ? entry |
| 62 | + : ((baseConfig != null) ? baseConfig.getAppConfigurationEntry(name) : null); |
| 63 | + } |
| 64 | + |
| 65 | + private String getKrb5LoginModuleName() { |
| 66 | + String krb5LoginModuleName; |
| 67 | + if (System.getProperty("java.vendor").contains("IBM")) { |
| 68 | + krb5LoginModuleName = "com.ibm.security.auth.module.Krb5LoginModule"; |
| 69 | + } else { |
| 70 | + krb5LoginModuleName = "com.sun.security.auth.module.Krb5LoginModule"; |
| 71 | + } |
| 72 | + return krb5LoginModuleName; |
| 73 | + } |
| 74 | + |
| 75 | + private Map<String, String> getPrincipalIfNeeded() { |
| 76 | + HashMap<String, String> principalOptions = new HashMap<>(); |
| 77 | + if (System.getProperty("java.vendor").contains("IBM")) { |
| 78 | + // The IBM Kerberos implementation does not pick up the cache entry without the |
| 79 | + // principal |
| 80 | + String principalName; |
| 81 | + try { |
| 82 | + principalName = User.getCurrent().getName(); |
| 83 | + if (principalName == null || principalName.isEmpty()) { |
| 84 | + LOG.warn("Got empty principal name on IMB JVM"); |
| 85 | + } else { |
| 86 | + principalOptions.put("principal", principalName); |
| 87 | + } |
| 88 | + } catch (IOException e) { |
| 89 | + LOG.warn("Could not determine principal on IBM JVM.", e); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + return principalOptions; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments