|
| 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.hadoop.hbase.ipc; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.net.InetAddress; |
| 23 | +import java.security.cert.X509Certificate; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.hadoop.hbase.coprocessor.BaseEnvironment; |
| 26 | +import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; |
| 27 | +import org.apache.hadoop.hbase.coprocessor.RpcCoprocessor; |
| 28 | +import org.apache.hadoop.hbase.coprocessor.RpcCoprocessorEnvironment; |
| 29 | +import org.apache.hadoop.hbase.coprocessor.RpcObserver; |
| 30 | +import org.apache.hadoop.hbase.security.User; |
| 31 | +import org.apache.yetus.audience.InterfaceAudience; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos; |
| 36 | + |
| 37 | +@InterfaceAudience.Private |
| 38 | +public class RpcCoprocessorHost extends CoprocessorHost<RpcCoprocessor, RpcCoprocessorEnvironment> { |
| 39 | + |
| 40 | + private static final Logger LOG = LoggerFactory.getLogger(RpcCoprocessorHost.class); |
| 41 | + |
| 42 | + private static class RpcEnvironment extends BaseEnvironment<RpcCoprocessor> |
| 43 | + implements RpcCoprocessorEnvironment { |
| 44 | + |
| 45 | + public RpcEnvironment(RpcCoprocessor impl, int priority, int seq, Configuration conf) { |
| 46 | + super(impl, priority, seq, conf); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public RpcCoprocessorHost(final Configuration conf) { |
| 51 | + // RPCServer cannot be aborted, so we don't pass Abortable down here. |
| 52 | + super(null); |
| 53 | + this.conf = conf; |
| 54 | + boolean coprocessorsEnabled = |
| 55 | + conf.getBoolean(COPROCESSORS_ENABLED_CONF_KEY, DEFAULT_COPROCESSORS_ENABLED); |
| 56 | + LOG.trace("System coprocessor loading is {}", (coprocessorsEnabled ? "enabled" : "disabled")); |
| 57 | + loadSystemCoprocessors(conf, RPC_COPROCESSOR_CONF_KEY); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public RpcCoprocessorEnvironment createEnvironment(RpcCoprocessor instance, int priority, |
| 62 | + int sequence, Configuration conf) { |
| 63 | + return new RpcEnvironment(instance, priority, sequence, conf); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public RpcCoprocessor checkAndGetInstance(Class<?> implClass) |
| 68 | + throws InstantiationException, IllegalAccessException { |
| 69 | + try { |
| 70 | + if (RpcCoprocessor.class.isAssignableFrom(implClass)) { |
| 71 | + return implClass.asSubclass(RpcCoprocessor.class).getDeclaredConstructor().newInstance(); |
| 72 | + } else { |
| 73 | + LOG.error("{} is not of type RpcCoprocessor. Check the configuration of {}", |
| 74 | + implClass.getName(), CoprocessorHost.RPC_COPROCESSOR_CONF_KEY); |
| 75 | + return null; |
| 76 | + } |
| 77 | + } catch (NoSuchMethodException | InvocationTargetException e) { |
| 78 | + throw (InstantiationException) new InstantiationException(implClass.getName()).initCause(e); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private final ObserverGetter<RpcCoprocessor, RpcObserver> rpcObserverGetter = |
| 83 | + RpcCoprocessor::getRpcObserver; |
| 84 | + |
| 85 | + abstract class RpcObserverOperation extends ObserverOperationWithoutResult<RpcObserver> { |
| 86 | + public RpcObserverOperation() { |
| 87 | + super(rpcObserverGetter); |
| 88 | + } |
| 89 | + |
| 90 | + public RpcObserverOperation(boolean bypassable) { |
| 91 | + this(null, bypassable); |
| 92 | + } |
| 93 | + |
| 94 | + public RpcObserverOperation(User user) { |
| 95 | + super(rpcObserverGetter, user); |
| 96 | + } |
| 97 | + |
| 98 | + public RpcObserverOperation(User user, boolean bypassable) { |
| 99 | + super(rpcObserverGetter, user, bypassable); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void preAuthorizeConnection(RPCProtos.ConnectionHeader connectionHeader, |
| 104 | + InetAddress remoteAddr) throws IOException { |
| 105 | + execOperation(coprocEnvironments.isEmpty() ? null : new RpcObserverOperation() { |
| 106 | + @Override |
| 107 | + protected void call(RpcObserver observer) throws IOException { |
| 108 | + observer.preAuthorizeConnection(this, connectionHeader, remoteAddr); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + public void postAuthorizeConnection(final String userName, |
| 114 | + final X509Certificate[] clientCertificates) throws IOException { |
| 115 | + execOperation(coprocEnvironments.isEmpty() ? null : new RpcObserverOperation() { |
| 116 | + @Override |
| 117 | + protected void call(RpcObserver observer) throws IOException { |
| 118 | + observer.postAuthorizeConnection(this, userName, clientCertificates); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
0 commit comments