Skip to content

Commit d477bf1

Browse files
authored
HBASE-28952 Add coprocessor hook to authorize user based on client SSL certificate chain (#6447)
Signed-off-by: Bryan Beaudreault <[email protected]>
1 parent e29fc04 commit d477bf1

9 files changed

Lines changed: 393 additions & 0 deletions

File tree

‎hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/CoprocessorHost.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public abstract class CoprocessorHost<C extends Coprocessor, E extends Coprocess
6262
"hbase.coprocessor.user.region.classes";
6363
public static final String MASTER_COPROCESSOR_CONF_KEY = "hbase.coprocessor.master.classes";
6464
public static final String WAL_COPROCESSOR_CONF_KEY = "hbase.coprocessor.wal.classes";
65+
public static final String RPC_COPROCESSOR_CONF_KEY = "hbase.coprocessor.rpc.classes";
6566
public static final String ABORT_ON_ERROR_KEY = "hbase.coprocessor.abortonerror";
6667
public static final boolean DEFAULT_ABORT_ON_ERROR = true;
6768
public static final String COPROCESSORS_ENABLED_CONF_KEY = "hbase.coprocessor.enabled";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.coprocessor;
19+
20+
import java.util.Optional;
21+
import org.apache.hadoop.hbase.Coprocessor;
22+
import org.apache.hadoop.hbase.HBaseInterfaceAudience;
23+
import org.apache.yetus.audience.InterfaceAudience;
24+
import org.apache.yetus.audience.InterfaceStability;
25+
26+
@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
27+
@InterfaceStability.Evolving
28+
public interface RpcCoprocessor extends Coprocessor {
29+
default Optional<RpcObserver> getRpcObserver() {
30+
return Optional.empty();
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.coprocessor;
19+
20+
import org.apache.hadoop.hbase.CoprocessorEnvironment;
21+
import org.apache.hadoop.hbase.HBaseInterfaceAudience;
22+
import org.apache.yetus.audience.InterfaceAudience;
23+
import org.apache.yetus.audience.InterfaceStability;
24+
25+
@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
26+
@InterfaceStability.Evolving
27+
public interface RpcCoprocessorEnvironment extends CoprocessorEnvironment<RpcCoprocessor> {
28+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.coprocessor;
19+
20+
import java.io.IOException;
21+
import java.net.InetAddress;
22+
import java.security.cert.X509Certificate;
23+
import org.apache.hadoop.hbase.HBaseInterfaceAudience;
24+
import org.apache.yetus.audience.InterfaceAudience;
25+
import org.apache.yetus.audience.InterfaceStability;
26+
27+
import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos;
28+
29+
/**
30+
* Coprocessors implement this interface to observe and mediate RPC events in Master and RS
31+
* instances.
32+
* <p>
33+
* Since most implementations will be interested in only a subset of hooks, this class uses
34+
* 'default' functions to avoid having to add unnecessary overrides. When the functions are
35+
* non-empty, it's simply to satisfy the compiler by returning value of expected (non-void) type. It
36+
* is done in a way that these default definitions act as no-op. So our suggestion to implementation
37+
* would be to not call these 'default' methods from overrides.
38+
* <p>
39+
* <h3>Exception Handling</h3><br>
40+
* For all functions, exception handling is done as follows:
41+
* <ul>
42+
* <li>Exceptions of type {@link IOException} are reported back to client.</li>
43+
* <li>For any other kind of exception:
44+
* <ul>
45+
* <li>Be aware that this coprocessor doesn't support abortion. If the configuration
46+
* {@link CoprocessorHost#ABORT_ON_ERROR_KEY} is set to true, the event will be logged, but the RPC
47+
* server won't be aborted.</li>
48+
* <li>Otherwise, coprocessor is removed from the server.</li>
49+
* </ul>
50+
* </li>
51+
* </ul>
52+
*/
53+
@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
54+
@InterfaceStability.Evolving
55+
public interface RpcObserver {
56+
57+
/**
58+
* Called before authorizing connection
59+
* @param ctx the coprocessor instance's environment
60+
*/
61+
default void preAuthorizeConnection(ObserverContext<RpcCoprocessorEnvironment> ctx,
62+
RPCProtos.ConnectionHeader connectionHeader, InetAddress remoteAddr) throws IOException {
63+
}
64+
65+
/**
66+
* Called after successfully authorizing connection
67+
* @param ctx the coprocessor instance's environment
68+
* @param userName the user name
69+
* @param clientCertificateChain list of peer certificates from SSL connection
70+
*/
71+
default void postAuthorizeConnection(ObserverContext<RpcCoprocessorEnvironment> ctx,
72+
String userName, X509Certificate[] clientCertificateChain) throws IOException {
73+
}
74+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

‎hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hadoop.hbase.HConstants;
4343
import org.apache.hadoop.hbase.Server;
4444
import org.apache.hadoop.hbase.conf.ConfigurationObserver;
45+
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
4546
import org.apache.hadoop.hbase.io.ByteBuffAllocator;
4647
import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandler;
4748
import org.apache.hadoop.hbase.monitoring.TaskMonitor;
@@ -54,6 +55,7 @@
5455
import org.apache.hadoop.hbase.security.User;
5556
import org.apache.hadoop.hbase.security.UserProvider;
5657
import org.apache.hadoop.hbase.security.token.AuthenticationTokenSecretManager;
58+
import org.apache.hadoop.hbase.util.CoprocessorConfigurationUtil;
5759
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
5860
import org.apache.hadoop.hbase.util.GsonUtil;
5961
import org.apache.hadoop.hbase.util.Pair;
@@ -220,6 +222,8 @@ public abstract class RpcServer implements RpcServerInterface, ConfigurationObse
220222

221223
protected volatile boolean allowFallbackToSimpleAuth;
222224

225+
volatile RpcCoprocessorHost cpHost;
226+
223227
/**
224228
* Used to get details for scan with a scanner_id<br/>
225229
* TODO try to figure out a better way and remove reference from regionserver package later.
@@ -312,6 +316,8 @@ public RpcServer(final Server server, final String name,
312316

313317
this.isOnlineLogProviderEnabled = getIsOnlineLogProviderEnabled(conf);
314318
this.scheduler = scheduler;
319+
320+
initializeCoprocessorHost(getConf());
315321
}
316322

317323
@Override
@@ -324,6 +330,13 @@ public void onConfigurationChange(Configuration newConf) {
324330
refreshAuthManager(newConf, new HBasePolicyProvider());
325331
}
326332
refreshSlowLogConfiguration(newConf);
333+
if (
334+
CoprocessorConfigurationUtil.checkConfigurationChange(getConf(), newConf,
335+
CoprocessorHost.RPC_COPROCESSOR_CONF_KEY)
336+
) {
337+
LOG.info("Update the RPC coprocessor(s) because the configuration has changed");
338+
initializeCoprocessorHost(newConf);
339+
}
327340
}
328341

329342
private void refreshSlowLogConfiguration(Configuration newConf) {
@@ -898,4 +911,13 @@ protected boolean needAuthorization() {
898911
public List<BlockingServiceAndInterface> getServices() {
899912
return services;
900913
}
914+
915+
private void initializeCoprocessorHost(Configuration conf) {
916+
this.cpHost = new RpcCoprocessorHost(conf);
917+
}
918+
919+
@Override
920+
public RpcCoprocessorHost getRpcCoprocessorHost() {
921+
return cpHost;
922+
}
901923
}

‎hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServerInterface.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,6 @@ Pair<Message, ExtendedCellScanner> call(RpcCall call, MonitoredRPCHandler status
8585
*/
8686
void setNamedQueueRecorder(final NamedQueueRecorder namedQueueRecorder);
8787

88+
/** Return RPC's instance of {@link RpcCoprocessorHost} */
89+
RpcCoprocessorHost getRpcCoprocessorHost();
8890
}

‎hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/ServerRpcConnection.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,16 @@ public void processOneRpc(ByteBuff buf) throws IOException, InterruptedException
366366
processConnectionHeader(buf);
367367
callCleanupIfNeeded();
368368
this.connectionHeaderRead = true;
369+
this.rpcServer.getRpcCoprocessorHost().preAuthorizeConnection(connectionHeader, addr);
369370
if (rpcServer.needAuthorization() && !authorizeConnection()) {
370371
// Throw FatalConnectionException wrapping ACE so client does right thing and closes
371372
// down the connection instead of trying to read non-existent retun.
372373
throw new AccessDeniedException("Connection from " + this + " for service "
373374
+ connectionHeader.getServiceName() + " is unauthorized for user: " + ugi);
374375
}
375376
this.user = this.rpcServer.userProvider.create(this.ugi);
377+
this.rpcServer.getRpcCoprocessorHost().postAuthorizeConnection(
378+
this.user != null ? this.user.getName() : null, this.clientCertificateChain);
376379
}
377380
}
378381

0 commit comments

Comments
 (0)