fix(kafka): 修复跳过证书校验未生效
This commit is contained in:
3 files changed
+131
-8
No files matched your search
+72
@@ -0,0 +1,72 @@
|
|||||||
|
package com.dbx.agent.kafka;
|
||||||
|
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.Provider;
|
||||||
|
import java.security.Security;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
import javax.net.ssl.ManagerFactoryParameters;
|
||||||
|
import javax.net.ssl.SSLEngine;
|
||||||
|
import javax.net.ssl.TrustManager;
|
||||||
|
import javax.net.ssl.TrustManagerFactorySpi;
|
||||||
|
import javax.net.ssl.X509ExtendedTrustManager;
|
||||||
|
|
||||||
|
/** Trust manager used only when a Kafka connection explicitly skips TLS verification. */
|
||||||
|
public final class DbxInsecureTrustManagerFactory {
|
||||||
|
static final String ALGORITHM = "DBX_TRUST_ALL";
|
||||||
|
private static final String PROVIDER_NAME = "DBX_KAFKA_INSECURE_TLS";
|
||||||
|
|
||||||
|
private DbxInsecureTrustManagerFactory() {}
|
||||||
|
|
||||||
|
static void ensureRegistered() {
|
||||||
|
if (Security.getProvider(PROVIDER_NAME) != null) return;
|
||||||
|
synchronized (DbxInsecureTrustManagerFactory.class) {
|
||||||
|
if (Security.getProvider(PROVIDER_NAME) != null) return;
|
||||||
|
Provider provider = new Provider(PROVIDER_NAME, "1.0", "DBX Kafka trust-all provider") {};
|
||||||
|
provider.put("TrustManagerFactory." + ALGORITHM, Spi.class.getName());
|
||||||
|
Security.addProvider(provider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class Spi extends TrustManagerFactorySpi {
|
||||||
|
private static final TrustManager[] TRUST_MANAGERS = { new TrustAllManager() };
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void engineInit(KeyStore keyStore) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void engineInit(ManagerFactoryParameters managerFactoryParameters) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected TrustManager[] engineGetTrustManagers() {
|
||||||
|
return TRUST_MANAGERS.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class TrustAllManager extends X509ExtendedTrustManager {
|
||||||
|
private static final X509Certificate[] NO_ISSUERS = new X509Certificate[0];
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return NO_ISSUERS.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -507,16 +507,9 @@ public final class KafkaAgent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TLS properties
|
||||||
JsonObject tls = conn.has("tls") && conn.get("tls").isJsonObject()
|
JsonObject tls = conn.has("tls") && conn.get("tls").isJsonObject()
|
||||||
? conn.getAsJsonObject("tls") : null;
|
? conn.getAsJsonObject("tls") : null;
|
||||||
boolean skipVerify = boolOrDefault(conn, "tls_skip_verify", false)
|
|
||||||
|| boolOrDefault(conn, "tlsSkipVerify", false)
|
|
||||||
|| (tls != null && boolOrDefault(tls, "skip_verify", false));
|
|
||||||
if (skipVerify) {
|
|
||||||
props.put("ssl.endpoint.identification.algorithm", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TLS properties
|
|
||||||
if (tls != null) {
|
if (tls != null) {
|
||||||
String truststorePath = stringOrEmpty(tls, "truststore_path");
|
String truststorePath = stringOrEmpty(tls, "truststore_path");
|
||||||
if (!truststorePath.isBlank()) {
|
if (!truststorePath.isBlank()) {
|
||||||
@@ -540,6 +533,20 @@ public final class KafkaAgent {
|
|||||||
static void applyConnectionProperties(JsonObject conn, Properties props) {
|
static void applyConnectionProperties(JsonObject conn, Properties props) {
|
||||||
applySecurityProperties(conn, props);
|
applySecurityProperties(conn, props);
|
||||||
applyExtraProperties(conn, props);
|
applyExtraProperties(conn, props);
|
||||||
|
applyTlsSkipVerification(conn, props);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void applyTlsSkipVerification(JsonObject conn, Properties props) {
|
||||||
|
JsonObject tls = conn.has("tls") && conn.get("tls").isJsonObject()
|
||||||
|
? conn.getAsJsonObject("tls") : null;
|
||||||
|
boolean skipVerify = boolOrDefault(conn, "tls_skip_verify", false)
|
||||||
|
|| boolOrDefault(conn, "tlsSkipVerify", false)
|
||||||
|
|| (tls != null && boolOrDefault(tls, "skip_verify", false));
|
||||||
|
if (!skipVerify) return;
|
||||||
|
|
||||||
|
DbxInsecureTrustManagerFactory.ensureRegistered();
|
||||||
|
props.put("ssl.endpoint.identification.algorithm", "");
|
||||||
|
props.put("ssl.trustmanager.algorithm", DbxInsecureTrustManagerFactory.ALGORITHM);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String jaasValue(String value) {
|
static String jaasValue(String value) {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ import java.net.InetSocketAddress;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -27,6 +29,8 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
|
import javax.net.ssl.X509TrustManager;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||||
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
|
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
|
||||||
@@ -1292,6 +1296,46 @@ class KafkaAgentTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void skipTlsVerificationDisablesHostnameAndCertificateChainValidation() throws Exception {
|
||||||
|
Properties props = new Properties();
|
||||||
|
KafkaAgent.applyConnectionProperties(JsonParser.parseString("""
|
||||||
|
{
|
||||||
|
"security_protocol": "SASL_SSL",
|
||||||
|
"tls_skip_verify": true,
|
||||||
|
"properties": {
|
||||||
|
"ssl.endpoint.identification.algorithm": "https",
|
||||||
|
"ssl.trustmanager.algorithm": "PKIX"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""").getAsJsonObject(), props);
|
||||||
|
|
||||||
|
assertEquals("", props.getProperty("ssl.endpoint.identification.algorithm"));
|
||||||
|
assertEquals(
|
||||||
|
DbxInsecureTrustManagerFactory.ALGORITHM,
|
||||||
|
props.getProperty("ssl.trustmanager.algorithm")
|
||||||
|
);
|
||||||
|
|
||||||
|
TrustManagerFactory factory = TrustManagerFactory.getInstance(DbxInsecureTrustManagerFactory.ALGORITHM);
|
||||||
|
factory.init((KeyStore) null);
|
||||||
|
X509TrustManager trustManager = (X509TrustManager) factory.getTrustManagers()[0];
|
||||||
|
assertDoesNotThrow(() -> trustManager.checkServerTrusted(new X509Certificate[0], "RSA"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void verifiedTlsKeepsKafkaDefaultTrustAndHostnameValidation() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
KafkaAgent.applyConnectionProperties(JsonParser.parseString("""
|
||||||
|
{
|
||||||
|
"security_protocol": "SASL_SSL",
|
||||||
|
"tls_skip_verify": false
|
||||||
|
}
|
||||||
|
""").getAsJsonObject(), props);
|
||||||
|
|
||||||
|
assertNull(props.getProperty("ssl.endpoint.identification.algorithm"));
|
||||||
|
assertNull(props.getProperty("ssl.trustmanager.algorithm"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void appliesAllowedKerberosSystemPropertiesFromConnectionProperties() {
|
void appliesAllowedKerberosSystemPropertiesFromConnectionProperties() {
|
||||||
Map<String, String> previous = KafkaAgent.applyKerberosSystemProperties(JsonParser.parseString("""
|
Map<String, String> previous = KafkaAgent.applyKerberosSystemProperties(JsonParser.parseString("""
|
||||||
|
|||||||
Reference in new issue
Block a user