org.apache.hive.service.auth.TSetIpAddressProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hive-apache-jdbc Show documentation
Show all versions of hive-apache-jdbc Show documentation
Shaded version of Apache Hive JDBC driver for Presto
/**
* 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.hive.service.auth;
import com.facebook.presto.hive.jdbc.$internal.org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Processor;
import org.apache.hive.service.cli.thrift.TCLIService;
import org.apache.hive.service.cli.thrift.TCLIService.Iface;
import com.facebook.presto.hive.jdbc.$internal.org.apache.thrift.TException;
import com.facebook.presto.hive.jdbc.$internal.org.apache.thrift.protocol.TProtocol;
import com.facebook.presto.hive.jdbc.$internal.org.apache.thrift.transport.TSaslClientTransport;
import com.facebook.presto.hive.jdbc.$internal.org.apache.thrift.transport.TSaslServerTransport;
import com.facebook.presto.hive.jdbc.$internal.org.apache.thrift.transport.TSocket;
import com.facebook.presto.hive.jdbc.$internal.org.apache.thrift.transport.TTransport;
import com.facebook.presto.hive.jdbc.$internal.org.slf4j.Logger;
import com.facebook.presto.hive.jdbc.$internal.org.slf4j.LoggerFactory;
/**
* This class is responsible for setting the ipAddress for operations executed via HiveServer2.
*
*
* - Ipaddress is only set for operations that calls listeners with hookContext @see ExecuteWithHookContext.
* - Ipaddress is only set if the underlying transport mechanism is socket.
*
*
*/
public class TSetIpAddressProcessor extends TCLIService.Processor {
private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());
public TSetIpAddressProcessor(Iface iface) {
super(iface);
}
@Override
public boolean process(final TProtocol in, final TProtocol out) throws TException {
setIpAddress(in);
setUserName(in);
try {
return super.process(in, out);
} finally {
threadLocalUserName.remove();
threadLocalIpAddress.remove();
}
}
private void setUserName(final TProtocol in) {
TTransport transport = in.getTransport();
if (transport instanceof TSaslServerTransport) {
String userName = ((TSaslServerTransport)transport).getSaslServer().getAuthorizationID();
threadLocalUserName.set(userName);
}
}
protected void setIpAddress(final TProtocol in) {
TTransport transport = in.getTransport();
TSocket tSocket = getUnderlyingSocketFromTransport(transport);
if (tSocket != null) {
threadLocalIpAddress.set(tSocket.getSocket().getInetAddress().toString());
} else {
LOGGER.warn("Unknown Transport, cannot determine ipAddress");
}
}
private TSocket getUnderlyingSocketFromTransport(TTransport transport) {
while (transport != null) {
if (transport instanceof TSaslServerTransport) {
transport = ((TSaslServerTransport) transport).getUnderlyingTransport();
}
if (transport instanceof TSaslClientTransport) {
transport = ((TSaslClientTransport) transport).getUnderlyingTransport();
}
if (transport instanceof TSocket) {
return (TSocket) transport;
}
}
return null;
}
private static ThreadLocal threadLocalIpAddress = new ThreadLocal() {
@Override
protected synchronized String initialValue() {
return null;
}
};
private static ThreadLocal threadLocalUserName = new ThreadLocal(){
@Override
protected synchronized String initialValue() {
return null;
}
};
public static String getUserIpAddress() {
return threadLocalIpAddress.get();
}
public static String getUserName() {
return threadLocalUserName.get();
}
}