|
27 | 27 | import com.github.shyiko.mysql.binlog.event.EventData; |
28 | 28 | import com.github.shyiko.mysql.binlog.event.EventHeaderV4; |
29 | 29 | import com.github.shyiko.mysql.binlog.event.RotateEventData; |
| 30 | +import com.github.shyiko.mysql.binlog.network.DefaultSSLSocketFactory; |
30 | 31 | import com.github.shyiko.mysql.binlog.network.SSLMode; |
| 32 | +import com.github.shyiko.mysql.binlog.network.SSLSocketFactory; |
31 | 33 | import io.debezium.config.Configuration; |
32 | 34 | import io.debezium.connector.mysql.MySqlConnection; |
33 | 35 | import io.debezium.connector.mysql.MySqlConnectorConfig; |
|
47 | 49 | import org.slf4j.Logger; |
48 | 50 | import org.slf4j.LoggerFactory; |
49 | 51 |
|
| 52 | +import javax.net.ssl.KeyManager; |
| 53 | +import javax.net.ssl.KeyManagerFactory; |
| 54 | +import javax.net.ssl.SSLContext; |
| 55 | +import javax.net.ssl.TrustManager; |
| 56 | +import javax.net.ssl.TrustManagerFactory; |
| 57 | +import javax.net.ssl.X509TrustManager; |
| 58 | + |
50 | 59 | import java.io.IOException; |
| 60 | +import java.security.GeneralSecurityException; |
| 61 | +import java.security.KeyStore; |
| 62 | +import java.security.KeyStoreException; |
| 63 | +import java.security.NoSuchAlgorithmException; |
| 64 | +import java.security.UnrecoverableKeyException; |
| 65 | +import java.security.cert.X509Certificate; |
51 | 66 | import java.sql.SQLException; |
52 | 67 | import java.util.ArrayList; |
53 | 68 | import java.util.HashMap; |
|
57 | 72 | import java.util.concurrent.ArrayBlockingQueue; |
58 | 73 | import java.util.function.Predicate; |
59 | 74 |
|
| 75 | +import static io.debezium.util.Strings.isNullOrEmpty; |
| 76 | + |
60 | 77 | /** Utilities related to Debezium. */ |
61 | 78 | public class DebeziumUtils { |
62 | 79 | private static final String QUOTED_CHARACTER = "`"; |
@@ -94,13 +111,27 @@ public static MySqlConnection createMySqlConnection( |
94 | 111 | } |
95 | 112 |
|
96 | 113 | /** Creates a new {@link BinaryLogClient} for consuming mysql binlog. */ |
97 | | - public static BinaryLogClient createBinaryClient(Configuration dbzConfiguration) { |
| 114 | + public static BinaryLogClient createBinaryClient( |
| 115 | + Configuration dbzConfiguration, MySqlConnection connection) { |
98 | 116 | final MySqlConnectorConfig connectorConfig = new MySqlConnectorConfig(dbzConfiguration); |
99 | | - return new BinaryLogClient( |
100 | | - connectorConfig.hostname(), |
101 | | - connectorConfig.port(), |
102 | | - connectorConfig.username(), |
103 | | - connectorConfig.password()); |
| 117 | + BinaryLogClient client = |
| 118 | + new BinaryLogClient( |
| 119 | + connectorConfig.hostname(), |
| 120 | + connectorConfig.port(), |
| 121 | + connectorConfig.username(), |
| 122 | + connectorConfig.password()); |
| 123 | + SSLMode sslMode = sslModeFor(connectorConfig.sslMode()); |
| 124 | + if (sslMode != null) { |
| 125 | + client.setSSLMode(sslMode); |
| 126 | + } |
| 127 | + if (connectorConfig.sslModeEnabled()) { |
| 128 | + SSLSocketFactory sslSocketFactory = |
| 129 | + getBinlogSslSocketFactory(connectorConfig, connection); |
| 130 | + if (sslSocketFactory != null) { |
| 131 | + client.setSslSocketFactory(sslSocketFactory); |
| 132 | + } |
| 133 | + } |
| 134 | + return client; |
104 | 135 | } |
105 | 136 |
|
106 | 137 | /** Creates a new {@link MySqlDatabaseSchema} to monitor the latest MySql database schemas. */ |
@@ -252,17 +283,92 @@ static SSLMode sslModeFor(MySqlConnectorConfig.SecureConnectionMode mode) { |
252 | 283 | } |
253 | 284 | } |
254 | 285 |
|
| 286 | + // see |
| 287 | + // flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/io/debezium/connector/mysql/MySqlStreamingChangeEventSource#getBinlogSslSocketFactory |
| 288 | + static SSLSocketFactory getBinlogSslSocketFactory( |
| 289 | + MySqlConnectorConfig connectorConfig, MySqlConnection connection) { |
| 290 | + String acceptedTlsVersion = connection.getSessionVariableForSslVersion(); |
| 291 | + if (!isNullOrEmpty(acceptedTlsVersion)) { |
| 292 | + SSLMode sslMode = sslModeFor(connectorConfig.sslMode()); |
| 293 | + LOG.info( |
| 294 | + "Enable ssl {} mode for connector {}", |
| 295 | + sslMode, |
| 296 | + connectorConfig.getLogicalName()); |
| 297 | + |
| 298 | + final char[] keyPasswordArray = connection.connectionConfig().sslKeyStorePassword(); |
| 299 | + final String keyFilename = connection.connectionConfig().sslKeyStore(); |
| 300 | + final char[] trustPasswordArray = connection.connectionConfig().sslTrustStorePassword(); |
| 301 | + final String trustFilename = connection.connectionConfig().sslTrustStore(); |
| 302 | + KeyManager[] keyManagers = null; |
| 303 | + if (keyFilename != null) { |
| 304 | + try { |
| 305 | + KeyStore ks = connection.loadKeyStore(keyFilename, keyPasswordArray); |
| 306 | + |
| 307 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509"); |
| 308 | + kmf.init(ks, keyPasswordArray); |
| 309 | + |
| 310 | + keyManagers = kmf.getKeyManagers(); |
| 311 | + } catch (KeyStoreException |
| 312 | + | NoSuchAlgorithmException |
| 313 | + | UnrecoverableKeyException e) { |
| 314 | + throw new FlinkRuntimeException("Could not load keystore", e); |
| 315 | + } |
| 316 | + } |
| 317 | + TrustManager[] trustManagers; |
| 318 | + try { |
| 319 | + KeyStore ks = null; |
| 320 | + if (trustFilename != null) { |
| 321 | + ks = connection.loadKeyStore(trustFilename, trustPasswordArray); |
| 322 | + } |
| 323 | + |
| 324 | + if (ks == null && (sslMode == SSLMode.PREFERRED || sslMode == SSLMode.REQUIRED)) { |
| 325 | + trustManagers = |
| 326 | + new TrustManager[] { |
| 327 | + new X509TrustManager() { |
| 328 | + |
| 329 | + @Override |
| 330 | + public void checkClientTrusted( |
| 331 | + X509Certificate[] x509Certificates, String s) {} |
| 332 | + |
| 333 | + @Override |
| 334 | + public void checkServerTrusted( |
| 335 | + X509Certificate[] x509Certificates, String s) {} |
| 336 | + |
| 337 | + @Override |
| 338 | + public X509Certificate[] getAcceptedIssuers() { |
| 339 | + return new X509Certificate[0]; |
| 340 | + } |
| 341 | + } |
| 342 | + }; |
| 343 | + } else { |
| 344 | + TrustManagerFactory tmf = |
| 345 | + TrustManagerFactory.getInstance( |
| 346 | + TrustManagerFactory.getDefaultAlgorithm()); |
| 347 | + tmf.init(ks); |
| 348 | + trustManagers = tmf.getTrustManagers(); |
| 349 | + } |
| 350 | + } catch (KeyStoreException | NoSuchAlgorithmException e) { |
| 351 | + throw new FlinkRuntimeException("Could not load truststore", e); |
| 352 | + } |
| 353 | + // DBZ-1208 Resembles the logic from the upstream BinaryLogClient, only that |
| 354 | + // the accepted TLS version is passed to the constructed factory |
| 355 | + final KeyManager[] finalKMS = keyManagers; |
| 356 | + return new DefaultSSLSocketFactory(acceptedTlsVersion) { |
| 357 | + |
| 358 | + @Override |
| 359 | + protected void initSSLContext(SSLContext sc) throws GeneralSecurityException { |
| 360 | + sc.init(finalKMS, trustManagers, null); |
| 361 | + } |
| 362 | + }; |
| 363 | + } |
| 364 | + |
| 365 | + return null; |
| 366 | + } |
| 367 | + |
255 | 368 | public static BinlogOffset findBinlogOffset( |
256 | 369 | long targetMs, MySqlConnection connection, MySqlSourceConfig mySqlSourceConfig) { |
257 | | - MySqlConnection.MySqlConnectionConfiguration config = connection.connectionConfig(); |
258 | 370 | BinaryLogClient client = |
259 | | - new BinaryLogClient( |
260 | | - config.hostname(), config.port(), config.username(), config.password()); |
261 | | - SSLMode sslMode = sslModeFor(config.sslMode()); |
262 | | - if (sslMode != null) { |
263 | | - client.setSSLMode(sslMode); |
264 | | - } |
265 | | - |
| 371 | + createBinaryClient(mySqlSourceConfig.getDbzConfiguration(), connection); |
266 | 372 | if (mySqlSourceConfig.getServerIdRange() != null) { |
267 | 373 | client.setServerId(mySqlSourceConfig.getServerIdRange().getStartServerId()); |
268 | 374 | } |
|
0 commit comments