|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.common.security.authentication; |
| 19 | + |
| 20 | +import com.google.common.base.Preconditions; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import com.google.common.collect.ImmutableSet; |
| 23 | +import io.trino.plugin.base.authentication.KerberosTicketUtils; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.hadoop.fs.CommonConfigurationKeysPublic; |
| 26 | +import org.apache.hadoop.security.UserGroupInformation; |
| 27 | +import org.apache.logging.log4j.LogManager; |
| 28 | +import org.apache.logging.log4j.Logger; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.Date; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.Set; |
| 36 | +import javax.security.auth.Subject; |
| 37 | +import javax.security.auth.kerberos.KerberosPrincipal; |
| 38 | +import javax.security.auth.kerberos.KerberosTicket; |
| 39 | +import javax.security.auth.login.AppConfigurationEntry; |
| 40 | +import javax.security.auth.login.LoginContext; |
| 41 | +import javax.security.auth.login.LoginException; |
| 42 | + |
| 43 | +public class HadoopKerberosAuthenticator implements HadoopAuthenticator { |
| 44 | + private static final Logger LOG = LogManager.getLogger(HadoopKerberosAuthenticator.class); |
| 45 | + private final KerberosAuthenticationConfig config; |
| 46 | + private Subject subject; |
| 47 | + private long nextRefreshTime; |
| 48 | + private UserGroupInformation ugi; |
| 49 | + |
| 50 | + public HadoopKerberosAuthenticator(KerberosAuthenticationConfig config) { |
| 51 | + this.config = config; |
| 52 | + } |
| 53 | + |
| 54 | + public static void initializeAuthConfig(Configuration hadoopConf) { |
| 55 | + hadoopConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHORIZATION, "true"); |
| 56 | + synchronized (HadoopKerberosAuthenticator.class) { |
| 57 | + // avoid other catalog set conf at the same time |
| 58 | + UserGroupInformation.setConfiguration(hadoopConf); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public synchronized UserGroupInformation getUGI() throws IOException { |
| 64 | + if (ugi == null) { |
| 65 | + subject = getSubject(config.getKerberosKeytab(), config.getKerberosPrincipal(), config.isPrintDebugLog()); |
| 66 | + ugi = Objects.requireNonNull(login(subject), "login result is null"); |
| 67 | + return ugi; |
| 68 | + } |
| 69 | + if (nextRefreshTime < System.currentTimeMillis()) { |
| 70 | + long lastRefreshTime = nextRefreshTime; |
| 71 | + Subject existingSubject = subject; |
| 72 | + if (LOG.isDebugEnabled()) { |
| 73 | + Date lastTicketEndTime = getTicketEndTime(subject); |
| 74 | + LOG.debug("Current ticket expired time is {}", lastTicketEndTime); |
| 75 | + } |
| 76 | + // renew subject |
| 77 | + Subject newSubject = getSubject(config.getKerberosKeytab(), config.getKerberosPrincipal(), |
| 78 | + config.isPrintDebugLog()); |
| 79 | + Objects.requireNonNull(login(newSubject), "re-login result is null"); |
| 80 | + // modify UGI instead of returning new UGI |
| 81 | + existingSubject.getPrincipals().addAll(newSubject.getPrincipals()); |
| 82 | + Set<Object> privateCredentials = existingSubject.getPrivateCredentials(); |
| 83 | + // clear the old credentials |
| 84 | + synchronized (privateCredentials) { |
| 85 | + privateCredentials.clear(); |
| 86 | + privateCredentials.addAll(newSubject.getPrivateCredentials()); |
| 87 | + } |
| 88 | + Set<Object> publicCredentials = existingSubject.getPublicCredentials(); |
| 89 | + synchronized (publicCredentials) { |
| 90 | + publicCredentials.clear(); |
| 91 | + publicCredentials.addAll(newSubject.getPublicCredentials()); |
| 92 | + } |
| 93 | + nextRefreshTime = calculateNextRefreshTime(newSubject); |
| 94 | + if (LOG.isDebugEnabled()) { |
| 95 | + Date lastTicketEndTime = getTicketEndTime(newSubject); |
| 96 | + LOG.debug("Next ticket expired time is {}", lastTicketEndTime); |
| 97 | + LOG.debug("Refresh kerberos ticket succeeded, last time is {}, next time is {}", |
| 98 | + lastRefreshTime, nextRefreshTime); |
| 99 | + } |
| 100 | + } |
| 101 | + return ugi; |
| 102 | + } |
| 103 | + |
| 104 | + private UserGroupInformation login(Subject subject) throws IOException { |
| 105 | + // login and get ugi when catalog is initialized |
| 106 | + initializeAuthConfig(config.getConf()); |
| 107 | + String principal = config.getKerberosPrincipal(); |
| 108 | + if (LOG.isDebugEnabled()) { |
| 109 | + LOG.debug("Login by kerberos authentication with principal: {}", principal); |
| 110 | + } |
| 111 | + return UserGroupInformation.getUGIFromSubject(subject); |
| 112 | + } |
| 113 | + |
| 114 | + private static long calculateNextRefreshTime(Subject subject) { |
| 115 | + Preconditions.checkArgument(subject != null, "subject must be present in kerberos based UGI"); |
| 116 | + KerberosTicket tgtTicket = KerberosTicketUtils.getTicketGrantingTicket(subject); |
| 117 | + return KerberosTicketUtils.getRefreshTime(tgtTicket); |
| 118 | + } |
| 119 | + |
| 120 | + private static Date getTicketEndTime(Subject subject) { |
| 121 | + Preconditions.checkArgument(subject != null, "subject must be present in kerberos based UGI"); |
| 122 | + KerberosTicket tgtTicket = KerberosTicketUtils.getTicketGrantingTicket(subject); |
| 123 | + return tgtTicket.getEndTime(); |
| 124 | + } |
| 125 | + |
| 126 | + private static Subject getSubject(String keytab, String principal, boolean printDebugLog) { |
| 127 | + Subject subject = new Subject(false, ImmutableSet.of(new KerberosPrincipal(principal)), |
| 128 | + Collections.emptySet(), Collections.emptySet()); |
| 129 | + javax.security.auth.login.Configuration conf = getConfiguration(keytab, principal, printDebugLog); |
| 130 | + try { |
| 131 | + LoginContext loginContext = new LoginContext("", subject, null, conf); |
| 132 | + loginContext.login(); |
| 133 | + return loginContext.getSubject(); |
| 134 | + } catch (LoginException e) { |
| 135 | + throw new RuntimeException(e); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static javax.security.auth.login.Configuration getConfiguration(String keytab, String principal, |
| 140 | + boolean printDebugLog) { |
| 141 | + return new javax.security.auth.login.Configuration() { |
| 142 | + @Override |
| 143 | + public AppConfigurationEntry[] getAppConfigurationEntry(String name) { |
| 144 | + ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder() |
| 145 | + .put("doNotPrompt", "true") |
| 146 | + .put("isInitiator", "true") |
| 147 | + .put("useKeyTab", "true") |
| 148 | + .put("storeKey", "true") |
| 149 | + .put("keyTab", keytab) |
| 150 | + .put("principal", principal); |
| 151 | + if (printDebugLog) { |
| 152 | + builder.put("debug", "true"); |
| 153 | + } |
| 154 | + Map<String, String> options = builder.build(); |
| 155 | + return new AppConfigurationEntry[]{ |
| 156 | + new AppConfigurationEntry( |
| 157 | + "com.sun.security.auth.module.Krb5LoginModule", |
| 158 | + AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, |
| 159 | + options)}; |
| 160 | + } |
| 161 | + }; |
| 162 | + } |
| 163 | +} |
0 commit comments