Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions external-service-impl/rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.iotdb</groupId>
<artifactId>node-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.iotdb.rest;

import org.apache.iotdb.db.audit.DNAuditLogger;
import org.apache.iotdb.db.conf.rest.IoTDBRestServiceConfig;
import org.apache.iotdb.db.conf.rest.IoTDBRestServiceDescriptor;
import org.apache.iotdb.externalservice.api.IExternalService;
Expand Down Expand Up @@ -79,6 +80,9 @@ private void startSSL(
new HttpConnectionFactory(httpsConfig));
httpsConnector.setPort(port);
httpsConnector.setIdleTimeout(idleTime);
httpsConnector.addBean(
new TrustedChannelAuditHandshakeListener(
DNAuditLogger.getInstance()::recordTrustedChannelFailureAuditLogIfNecessary));
server.addConnector(httpsConnector);

server.setHandler(constructServletContextHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.iotdb.rest;

Check warning on line 19 in external-service-impl/rest/src/main/java/org/apache/iotdb/rest/TrustedChannelAuditHandshakeListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'package' should be separated from previous line.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ-S5bX_NRP0su2tLJ4F&open=AZ-S5bX_NRP0su2tLJ4F&pullRequest=18299

import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.commons.audit.TrustedChannelFailureHandler;

import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.ssl.SslHandshakeListener;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Objects;

final class TrustedChannelAuditHandshakeListener implements SslHandshakeListener {

private final TrustedChannelFailureHandler failureHandler;

TrustedChannelAuditHandshakeListener(TrustedChannelFailureHandler failureHandler) {
this.failureHandler = Objects.requireNonNull(failureHandler);
}

@Override
public void handshakeFailed(Event event, Throwable failure) {
recordHandshakeFailure(event.getEndPoint(), failure);
}

void recordHandshakeFailure(EndPoint endPoint, Throwable failure) {
if (endPoint == null) {
return;
}

TEndPoint initiator = toEndPoint(endPoint.getRemoteSocketAddress());
TEndPoint target = toEndPoint(endPoint.getLocalSocketAddress());
if (initiator == null || target == null) {
return;
}

try {
failureHandler.onFailure(failure, initiator, target);
} catch (RuntimeException auditFailure) {
if (auditFailure != failure) {
failure.addSuppressed(auditFailure);
}
}
Comment thread
Copilot marked this conversation as resolved.
}
Comment thread
HTHou marked this conversation as resolved.

private static TEndPoint toEndPoint(SocketAddress socketAddress) {
if (!(socketAddress instanceof InetSocketAddress)) {
return null;
}
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
String host =
inetSocketAddress.getAddress() == null
? inetSocketAddress.getHostString()
: inetSocketAddress.getAddress().getHostAddress();
return new TEndPoint(host, inetSocketAddress.getPort());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.iotdb.rest;

import org.apache.iotdb.common.rpc.thrift.TEndPoint;

import org.eclipse.jetty.io.EndPoint;
import org.junit.Test;

import javax.net.ssl.SSLHandshakeException;

import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

public class TrustedChannelAuditHandshakeListenerTest {

@Test
public void testRecordSocketEndpointsOnHandshakeFailure() {
AtomicReference<Throwable> actualFailure = new AtomicReference<>();
AtomicReference<TEndPoint> actualInitiator = new AtomicReference<>();
AtomicReference<TEndPoint> actualTarget = new AtomicReference<>();
TrustedChannelAuditHandshakeListener listener =
new TrustedChannelAuditHandshakeListener(
(failure, initiator, target) -> {
actualFailure.set(failure);
actualInitiator.set(initiator);
actualTarget.set(target);
});
SSLHandshakeException failure = new SSLHandshakeException("test");
EndPoint endPoint =
newEndPoint(
new InetSocketAddress("192.0.2.10", 45123), new InetSocketAddress("192.0.2.20", 18080));

listener.recordHandshakeFailure(endPoint, failure);

assertSame(failure, actualFailure.get());
assertEquals(new TEndPoint("192.0.2.10", 45123), actualInitiator.get());
assertEquals(new TEndPoint("192.0.2.20", 18080), actualTarget.get());
}

@Test
public void testReportingFailureDoesNotSelfSuppressHandshakeFailure() {
RuntimeException failure = new RuntimeException("test");
TrustedChannelAuditHandshakeListener listener =
new TrustedChannelAuditHandshakeListener(
(ignoredFailure, initiator, target) -> {
throw failure;
});
EndPoint endPoint =
newEndPoint(
new InetSocketAddress("192.0.2.10", 45123), new InetSocketAddress("192.0.2.20", 18080));

listener.recordHandshakeFailure(endPoint, failure);

assertEquals(0, failure.getSuppressed().length);
}

private static EndPoint newEndPoint(
InetSocketAddress remoteAddress, InetSocketAddress localAddress) {
return (EndPoint)
Proxy.newProxyInstance(
EndPoint.class.getClassLoader(),
new Class<?>[] {EndPoint.class},
(proxy, method, args) -> {
switch (method.getName()) {
case "getRemoteAddress":
case "getRemoteSocketAddress":
return remoteAddress;
case "getLocalAddress":
case "getLocalSocketAddress":
return localAddress;
default:
return null;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.iotdb.confignode.audit;

import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.commons.audit.AbstractAuditLogger;
import org.apache.iotdb.commons.audit.IAuditEntity;
import org.apache.iotdb.confignode.conf.ConfigNodeConfig;
Expand All @@ -43,4 +44,9 @@ public CNAuditLogger(ConfigManager configManager) {

@Override
public void log(IAuditEntity auditLogFields, Supplier<String> log) {}

public void recordTrustedChannelFailureAuditLogIfNecessary(Throwable failure, TEndPoint target) {
recordTrustedChannelFailureAuditLogIfNecessary(
failure, new TEndPoint(CONF.getInternalAddress(), CONF.getInternalPort()), target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.iotdb.confignode.client.async.handlers.audit.DataNodeWriteAuditLogHandler;
import org.apache.iotdb.confignode.client.async.handlers.heartbeat.DataNodeHeartbeatHandler;
import org.apache.iotdb.confignode.conf.ConfigNodeDescriptor;
import org.apache.iotdb.confignode.service.ConfigNode;
import org.apache.iotdb.mpp.rpc.thrift.TAuditLogReq;
import org.apache.iotdb.mpp.rpc.thrift.TDataNodeHeartbeatReq;

Expand Down Expand Up @@ -57,6 +58,7 @@ public void getDataNodeHeartBeat(
client.getDataNodeHeartBeat(req, handler);
dispatched = true;
} catch (Exception e) {
recordTrustedChannelFailureAuditLogIfNecessary(e, endPoint);
handleError(handler, e);
} finally {
returnClientIfNotDispatched(endPoint, client, dispatched);
Expand Down Expand Up @@ -105,6 +107,17 @@ private void handleError(final DataNodeWriteAuditLogHandler handler, final Excep
}
}

private void recordTrustedChannelFailureAuditLogIfNecessary(
Throwable failure, TEndPoint targetEndPoint) {
if (ConfigNode.getInstance() == null || ConfigNode.getInstance().getConfigManager() == null) {
return;
}
ConfigNode.getInstance()
.getConfigManager()
.getAuditLogger()
.recordTrustedChannelFailureAuditLogIfNecessary(failure, targetEndPoint);
}

private static class AsyncDataNodeHeartbeatClientPoolHolder {

private static final AsyncDataNodeHeartbeatClientPool INSTANCE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.iotdb.confignode.client.async;

import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.common.rpc.thrift.TFlushReq;
import org.apache.iotdb.common.rpc.thrift.TNodeLocations;
import org.apache.iotdb.common.rpc.thrift.TSetConfigurationReq;
Expand Down Expand Up @@ -50,6 +51,7 @@
import org.apache.iotdb.confignode.client.async.handlers.rpc.subscription.TopicPushMetaRPCHandler;
import org.apache.iotdb.confignode.conf.ConfigNodeDescriptor;
import org.apache.iotdb.confignode.i18n.ConfigNodeMessages;
import org.apache.iotdb.confignode.service.ConfigNode;
import org.apache.iotdb.mpp.rpc.thrift.TActiveTriggerInstanceReq;
import org.apache.iotdb.mpp.rpc.thrift.TAlterEncodingCompressorReq;
import org.apache.iotdb.mpp.rpc.thrift.TAlterTimeSeriesReq;
Expand Down Expand Up @@ -557,6 +559,17 @@ protected void adjustClientTimeoutIfNecessary(
}
}

@Override
protected void onRequestFailure(Exception failure, TEndPoint targetEndPoint) {
if (ConfigNode.getInstance() == null || ConfigNode.getInstance().getConfigManager() == null) {
return;
}
ConfigNode.getInstance()
.getConfigManager()
.getAuditLogger()
.recordTrustedChannelFailureAuditLogIfNecessary(failure, targetEndPoint);
}

private static class ClientPoolHolder {

private static final CnToDnInternalServiceAsyncRequestManager INSTANCE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.iotdb.commons.client.sync.SyncDataNodeInternalServiceClient;
import org.apache.iotdb.commons.exception.UncheckedStartupException;
import org.apache.iotdb.confignode.i18n.ConfigNodeMessages;
import org.apache.iotdb.confignode.service.ConfigNode;
import org.apache.iotdb.mpp.rpc.thrift.TCleanDataNodeCacheReq;
import org.apache.iotdb.mpp.rpc.thrift.TCreateDataRegionReq;
import org.apache.iotdb.mpp.rpc.thrift.TCreatePeerReq;
Expand Down Expand Up @@ -177,6 +178,7 @@ public Object sendSyncRequestToDataNodeWithRetry(
return executeSyncRequest(requestType, client, req);
} catch (Exception e) {
lastException = e;
recordTrustedChannelFailureAuditLogIfNecessary(e, endPoint);
if (retry != DEFAULT_RETRY_NUM - 1) {
LOGGER.warn(
ConfigNodeMessages.FAILED_ON_DATANODE_RETRYING, requestType, endPoint, retry + 1);
Expand All @@ -197,6 +199,7 @@ public Object sendSyncRequestToDataNodeWithGivenRetry(
return executeSyncRequest(requestType, client, req);
} catch (Exception e) {
lastException = e;
recordTrustedChannelFailureAuditLogIfNecessary(e, endPoint);
if (retry != retryNum - 1) {
LOGGER.warn(
ConfigNodeMessages.FAILED_ON_DATANODE_RETRYING, requestType, endPoint, retry + 1);
Expand Down Expand Up @@ -248,16 +251,29 @@ public TRegionLeaderChangeResp changeRegionLeader(
return client.changeRegionLeader(req);
} catch (ClientManagerException e) {
LOGGER.error(ConfigNodeMessages.CAN_T_CONNECT_TO_DATA_NODE, dataNode, e);
recordTrustedChannelFailureAuditLogIfNecessary(e, dataNode);
status = new TSStatus(TSStatusCode.CAN_NOT_CONNECT_DATANODE.getStatusCode());
status.setMessage(e.getMessage());
} catch (TException e) {
LOGGER.error(ConfigNodeMessages.CHANGE_REGIONS_LEADER_ERROR_ON_DATE_NODE, dataNode, e);
recordTrustedChannelFailureAuditLogIfNecessary(e, dataNode);
status = new TSStatus(TSStatusCode.REGION_LEADER_CHANGE_ERROR.getStatusCode());
status.setMessage(e.getMessage());
}
return new TRegionLeaderChangeResp(status, -1L);
}

private void recordTrustedChannelFailureAuditLogIfNecessary(
Throwable failure, TEndPoint targetEndPoint) {
if (ConfigNode.getInstance() == null || ConfigNode.getInstance().getConfigManager() == null) {
return;
}
ConfigNode.getInstance()
.getConfigManager()
.getAuditLogger()
.recordTrustedChannelFailureAuditLogIfNecessary(failure, targetEndPoint);
}

private static class ClientPoolHolder {

private static final SyncDataNodeClientPool INSTANCE = new SyncDataNodeClientPool();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ protected PipeTransferHandshakeV2Req buildHandshakeV2Req(Map<String, String> par
protected String getClusterId() {
return ConfigNode.getInstance().getConfigManager().getClusterManager().getClusterId();
}

@Override
protected void onClientConnectionFailure(
final Exception failure, final TEndPoint targetEndPoint) {
if (ConfigNode.getInstance() == null || ConfigNode.getInstance().getConfigManager() == null) {
return;
}
ConfigNode.getInstance()
.getConfigManager()
.getAuditLogger()
.recordTrustedChannelFailureAuditLogIfNecessary(failure, targetEndPoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package org.apache.iotdb.confignode.service.thrift;

import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.commons.concurrent.ThreadName;
import org.apache.iotdb.commons.conf.CommonConfig;
import org.apache.iotdb.commons.conf.CommonDescriptor;
import org.apache.iotdb.commons.exception.runtime.RPCServiceException;
import org.apache.iotdb.commons.service.ServiceType;
import org.apache.iotdb.commons.service.ThriftService;
import org.apache.iotdb.commons.service.ThriftServiceThread;
import org.apache.iotdb.commons.service.TrustedChannelAuditServerEventHandler;
import org.apache.iotdb.commons.service.metric.MetricService;
import org.apache.iotdb.confignode.conf.ConfigNodeConfig;
import org.apache.iotdb.confignode.conf.ConfigNodeDescriptor;
Expand Down Expand Up @@ -70,7 +72,11 @@ public void initThriftServiceThread() throws IllegalAccessException {
getBindPort(),
configConf.getCnRpcMaxConcurrentClientNum(),
configConf.getThriftServerAwaitTimeForStopService(),
new ConfigNodeRPCServiceHandler(),
new TrustedChannelAuditServerEventHandler(
new ConfigNodeRPCServiceHandler(),
new TEndPoint(getBindIP(), getBindPort()),
configNodeRPCServiceProcessor.configManager.getAuditLogger()
::recordTrustedChannelFailureAuditLogIfNecessary),
commonConfig.isRpcThriftCompressionEnabled(),
commonConfig.getKeyStorePath(),
commonConfig.getKeyStorePwd(),
Expand Down
Loading
Loading