How to use SslTest class of ssl package

Best Karate code snippet using ssl.SslTest

Source:TestSSL.java Github

copy

Full Screen

1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.hive.jdbc;19import static org.junit.Assert.assertEquals;20import static org.junit.Assert.assertTrue;21import static org.junit.Assert.fail;22import java.io.File;23import java.sql.Connection;24import java.sql.DriverManager;25import java.sql.ResultSet;26import java.sql.SQLException;27import java.sql.Statement;28import java.util.HashMap;29import java.util.Map;30import org.apache.hadoop.fs.Path;31import org.apache.hadoop.util.Shell;32import org.apache.hadoop.hive.conf.HiveConf;33import org.apache.hadoop.hive.conf.HiveConf.ConfVars;34import org.apache.hive.jdbc.miniHS2.MiniHS2;35import org.hadoop.hive.jdbc.SSLTestUtils;36import org.junit.After;37import org.junit.AfterClass;38import org.junit.Assert;39import org.junit.Assume;40import org.junit.Before;41import org.junit.BeforeClass;42import org.junit.Ignore;43import org.junit.Test;44import org.slf4j.Logger;45import org.slf4j.LoggerFactory;46public class TestSSL {47 private static final Logger LOG = LoggerFactory.getLogger(TestSSL.class);48 private static final String LOCALHOST_KEY_STORE_NAME = "keystore.jks";49 private static final String EXAMPLEDOTCOM_KEY_STORE_NAME = "keystore_exampledotcom.jks";50 private static final String TRUST_STORE_NAME = "truststore.jks";51 private static final String KEY_STORE_TRUST_STORE_PASSWORD = "HiveJdbc";52 private static final String JAVA_TRUST_STORE_PROP = "javax.net.ssl.trustStore";53 private static final String JAVA_TRUST_STORE_PASS_PROP = "javax.net.ssl.trustStorePassword";54 private MiniHS2 miniHS2 = null;55 private static HiveConf conf = new HiveConf();56 private Connection hs2Conn = null;57 private String dataFileDir = SSLTestUtils.getDataFileDir();58 private Map<String, String> confOverlay;59 @BeforeClass60 public static void beforeTest() throws Exception {61 MiniHS2.cleanupLocalDir();62 Class.forName(MiniHS2.getJdbcDriverName());63 }64 @AfterClass65 public static void afterClass() throws Exception {66 MiniHS2.cleanupLocalDir();67 }68 @Before69 public void setUp() throws Exception {70 DriverManager.setLoginTimeout(0);71 miniHS2 = new MiniHS2.Builder().withConf(conf).cleanupLocalDirOnStartup(false).build();72 confOverlay = new HashMap<String, String>();73 }74 @After75 public void tearDown() throws Exception {76 if (hs2Conn != null) {77 hs2Conn.close();78 }79 if (miniHS2 != null && miniHS2.isStarted()) {80 miniHS2.stop();81 }82 System.clearProperty(JAVA_TRUST_STORE_PROP);83 System.clearProperty(JAVA_TRUST_STORE_PASS_PROP);84 }85 private int execCommand(String cmd) throws Exception {86 int exitCode;87 try {88 String output = Shell.execCommand("bash", "-c", cmd);89 LOG.info("Output from '" + cmd + "': " + output) ;90 exitCode = 0;91 } catch (Shell.ExitCodeException e) {92 exitCode = e.getExitCode();93 LOG.info("Error executing '" + cmd + "', exitCode = " + exitCode, e);94 }95 return exitCode;96 }97 /***98 * Tests to ensure SSLv2 and SSLv3 are disabled99 */100 @Test101 @Ignore("Temporarily disable until fixed")102 public void testSSLVersion() throws Exception {103 // we need openssl104 Assume.assumeTrue(execCommand("which openssl") == 0);105 // we depend on linux openssl exit codes106 Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("linux"));107 SSLTestUtils.setSslConfOverlay(confOverlay);108 // Test in binary mode109 SSLTestUtils.setBinaryConfOverlay(confOverlay);110 // Start HS2 with SSL111 miniHS2.start(confOverlay);112 // make SSL connection113 hs2Conn =114 DriverManager.getConnection(miniHS2.getJdbcURL() + ";ssl=true;sslTrustStore=" + dataFileDir115 + File.separator + TRUST_STORE_NAME + ";trustStorePassword=" + KEY_STORE_TRUST_STORE_PASSWORD,116 System.getProperty("user.name"), "bar");117 hs2Conn.close();118 Assert.assertEquals("Expected exit code of 1", 1, execCommand("openssl s_client -connect "119 + miniHS2.getHost() + ":" + miniHS2.getBinaryPort() + " -ssl2 < /dev/null"));120 Assert.assertEquals("Expected exit code of 1", 1, execCommand("openssl s_client -connect "121 + miniHS2.getHost() + ":" + miniHS2.getBinaryPort() + " -ssl3 < /dev/null"));122 miniHS2.stop();123 // Test in http mode124 SSLTestUtils.setHttpConfOverlay(confOverlay);125 miniHS2.start(confOverlay);126 // make SSL connection127 try {128 hs2Conn =129 DriverManager.getConnection(miniHS2.getJdbcURL() + ";ssl=true;sslTrustStore="130 + dataFileDir + File.separator + TRUST_STORE_NAME + ";trustStorePassword="131 + KEY_STORE_TRUST_STORE_PASSWORD, System.getProperty("user.name"), "bar");132 Assert.fail("Expected SQLException during connect");133 } catch (SQLException e) {134 LOG.info("Expected exception: " + e, e);135 Assert.assertEquals("08S01", e.getSQLState().trim());136 Throwable cause = e.getCause();137 Assert.assertNotNull(cause);138 while (cause.getCause() != null) {139 cause = cause.getCause();140 }141 Assert.assertEquals("org.apache.http.NoHttpResponseException", cause.getClass().getName());142 Assert.assertTrue(cause.getMessage().contains("failed to respond"));143 }144 miniHS2.stop();145 }146 /***147 * Test SSL client with non-SSL server fails148 * @throws Exception149 */150 @Test151 public void testInvalidConfig() throws Exception {152 SSLTestUtils.clearSslConfOverlay(confOverlay);153 // Test in binary mode154 SSLTestUtils.setBinaryConfOverlay(confOverlay);155 miniHS2.start(confOverlay);156 DriverManager.setLoginTimeout(4);157 try {158 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),159 System.getProperty("user.name"), "bar");160 fail("SSL connection should fail with NON-SSL server");161 } catch (SQLException e) {162 // expected error163 assertEquals("08S01", e.getSQLState().trim());164 }165 System.setProperty(JAVA_TRUST_STORE_PROP, dataFileDir + File.separator + TRUST_STORE_NAME );166 System.setProperty(JAVA_TRUST_STORE_PASS_PROP, KEY_STORE_TRUST_STORE_PASSWORD);167 try {168 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL() + ";ssl=true",169 System.getProperty("user.name"), "bar");170 fail("SSL connection should fail with NON-SSL server");171 } catch (SQLException e) {172 // expected error173 assertEquals("08S01", e.getSQLState().trim());174 }175 miniHS2.stop();176 // Test in http mode with ssl properties specified in url177 System.clearProperty(JAVA_TRUST_STORE_PROP);178 System.clearProperty(JAVA_TRUST_STORE_PASS_PROP);179 SSLTestUtils.setHttpConfOverlay(confOverlay);180 miniHS2.start(confOverlay);181 try {182 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),183 System.getProperty("user.name"), "bar");184 fail("SSL connection should fail with NON-SSL server");185 } catch (SQLException e) {186 // expected error187 assertEquals("08S01", e.getSQLState().trim());188 }189 }190 /***191 * Test non-SSL client with SSL server fails192 * @throws Exception193 */194 @Test195 public void testConnectionMismatch() throws Exception {196 SSLTestUtils.setSslConfOverlay(confOverlay);197 // Test in binary mode198 SSLTestUtils.setBinaryConfOverlay(confOverlay);199 miniHS2.start(confOverlay);200 // Start HS2 with SSL201 try {202 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), System.getProperty("user.name"), "bar");203 fail("NON SSL connection should fail with SSL server");204 } catch (SQLException e) {205 // expected error206 assertEquals("08S01", e.getSQLState().trim());207 }208 try {209 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL()+ ";ssl=false",210 System.getProperty("user.name"), "bar");211 fail("NON SSL connection should fail with SSL server");212 } catch (SQLException e) {213 // expected error214 assertEquals("08S01", e.getSQLState().trim());215 }216 miniHS2.stop();217 // Test in http mode218 SSLTestUtils.setHttpConfOverlay(confOverlay);219 miniHS2.start(confOverlay);220 try {221 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", ";ssl=false"),222 System.getProperty("user.name"), "bar");223 fail("NON SSL connection should fail with SSL server");224 } catch (SQLException e) {225 // expected error226 assertEquals("08S01", e.getSQLState().trim());227 }228 }229 /***230 * Test SSL client connection to SSL server231 * @throws Exception232 */233 @Test234 public void testSSLConnectionWithURL() throws Exception {235 SSLTestUtils.setSslConfOverlay(confOverlay);236 // Test in binary mode237 SSLTestUtils.setBinaryConfOverlay(confOverlay);238 // Start HS2 with SSL239 miniHS2.start(confOverlay);240 // make SSL connection241 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),242 System.getProperty("user.name"), "bar");243 hs2Conn.close();244 miniHS2.stop();245 // Test in http mode246 SSLTestUtils.setHttpConfOverlay(confOverlay);247 miniHS2.start(confOverlay);248 // make SSL connection249 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),250 System.getProperty("user.name"), "bar");251 hs2Conn.close();252 }253 /***254 * Test SSL client connection to SSL server255 * @throws Exception256 */257 @Test258 public void testSSLConnectionWithProperty() throws Exception {259 SSLTestUtils.setSslConfOverlay(confOverlay);260 // Test in binary mode261 SSLTestUtils.setBinaryConfOverlay(confOverlay);262 // Start HS2 with SSL263 miniHS2.start(confOverlay);264 System.setProperty(JAVA_TRUST_STORE_PROP, dataFileDir + File.separator + TRUST_STORE_NAME );265 System.setProperty(JAVA_TRUST_STORE_PASS_PROP, KEY_STORE_TRUST_STORE_PASSWORD);266 // make SSL connection267 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL() + ";ssl=true",268 System.getProperty("user.name"), "bar");269 hs2Conn.close();270 miniHS2.stop();271 // Test in http mode272 SSLTestUtils.setHttpConfOverlay(confOverlay);273 miniHS2.start(confOverlay);274 // make SSL connection275 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default",SSLTestUtils.SSL_CONN_PARAMS),276 System.getProperty("user.name"), "bar");277 hs2Conn.close();278 }279 /**280 * Start HS2 in SSL mode, open a SSL connection and fetch data281 * @throws Exception282 */283 @Test284 public void testSSLFetch() throws Exception {285 SSLTestUtils.setSslConfOverlay(confOverlay);286 // Test in binary mode287 SSLTestUtils.setBinaryConfOverlay(confOverlay);288 // Start HS2 with SSL289 miniHS2.start(confOverlay);290 String tableName = "sslTab";291 Path dataFilePath = new Path(dataFileDir, "kv1.txt");292 // make SSL connection293 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),294 System.getProperty("user.name"), "bar");295 // Set up test data296 SSLTestUtils.setupTestTableWithData(tableName, dataFilePath, hs2Conn);297 Statement stmt = hs2Conn.createStatement();298 ResultSet res = stmt.executeQuery("SELECT * FROM " + tableName);299 int rowCount = 0;300 while (res.next()) {301 ++rowCount;302 assertEquals("val_" + res.getInt(1), res.getString(2));303 }304 // read result over SSL305 assertEquals(500, rowCount);306 hs2Conn.close();307 }308 /**309 * Start HS2 in Http mode with SSL enabled, open a SSL connection and fetch data310 * @throws Exception311 */312 @Ignore("HIVE-19509: Disable tests that are failing continuously")313 @Test314 public void testSSLFetchHttp() throws Exception {315 SSLTestUtils.setSslConfOverlay(confOverlay);316 // Test in http mode317 SSLTestUtils.setHttpConfOverlay(confOverlay);318 miniHS2.start(confOverlay);319 String tableName = "sslTab";320 Path dataFilePath = new Path(dataFileDir, "kv1.txt");321 // make SSL connection322 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),323 System.getProperty("user.name"), "bar");324 // Set up test data325 SSLTestUtils.setupTestTableWithData(tableName, dataFilePath, hs2Conn);326 Statement stmt = hs2Conn.createStatement();327 ResultSet res = stmt.executeQuery("SELECT * FROM " + tableName);328 int rowCount = 0;329 while (res.next()) {330 ++rowCount;331 assertEquals("val_" + res.getInt(1), res.getString(2));332 }333 // read result over SSL334 assertEquals(500, rowCount);335 hs2Conn.close();336 }337 /***338 * Test a new connection when server sends a certificate with wrong CN339 * (sends a certificate for www.example.com instead of localhost)340 * Opening a new connection with this wrong certificate should fail341 * @throws Exception342 */343 @Test344 public void testConnectionWrongCertCN() throws Exception {345 // This call sets the default ssl params including the correct keystore in the server config346 SSLTestUtils.setSslConfOverlay(confOverlay);347 // Replace default keystore with keystore for www.example.com348 confOverlay.put(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname, dataFileDir + File.separator349 + EXAMPLEDOTCOM_KEY_STORE_NAME);350 // Binary (TCP) mode351 SSLTestUtils.setBinaryConfOverlay(confOverlay);352 miniHS2.start(confOverlay);353 try {354 hs2Conn =355 DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),356 System.getProperty("user.name"), "bar");357 fail("SSL connection, with the server providing wrong certifcate (with CN www.example.com, "358 + "instead of localhost), should fail");359 } catch (SQLException e) {360 // Expected error: should throw java.security.cert.CertificateException361 assertEquals("08S01", e.getSQLState().trim());362 assertTrue(e.toString().contains("java.security.cert.CertificateException"));363 }364 miniHS2.stop();365 // Http mode366 SSLTestUtils.setHttpConfOverlay(confOverlay);367 miniHS2.start(confOverlay);368 try {369 hs2Conn =370 DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),371 System.getProperty("user.name"), "bar");372 fail("SSL connection, with the server providing wrong certifcate (with CN www.example.com, "373 + "instead of localhost), should fail");374 } catch (SQLException e) {375 // Expected error: should throw javax.net.ssl.SSLPeerUnverifiedException376 assertEquals("08S01", e.getSQLState().trim());377 assertTrue(e.toString().contains("javax.net.ssl.SSLPeerUnverifiedException"));378 }379 // Revert to default keystore path380 confOverlay.put(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname, dataFileDir + File.separator381 + LOCALHOST_KEY_STORE_NAME);382 }383 /**384 * Test HMS server with SSL385 * @throws Exception386 */387 @Test388 public void testMetastoreWithSSL() throws Exception {389 SSLTestUtils.setMetastoreSslConf(conf);390 SSLTestUtils.setSslConfOverlay(confOverlay);391 // Test in http mode392 SSLTestUtils.setHttpConfOverlay(confOverlay);393 miniHS2 = new MiniHS2.Builder().withRemoteMetastore().withConf(conf).cleanupLocalDirOnStartup(false).build();394 miniHS2.start(confOverlay);395 String tableName = "sslTab";396 Path dataFilePath = new Path(dataFileDir, "kv1.txt");397 // make SSL connection398 hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL("default", SSLTestUtils.SSL_CONN_PARAMS),399 System.getProperty("user.name"), "bar");400 // Set up test data401 SSLTestUtils.setupTestTableWithData(tableName, dataFilePath, hs2Conn);402 Statement stmt = hs2Conn.createStatement();403 ResultSet res = stmt.executeQuery("SELECT * FROM " + tableName);404 int rowCount = 0;405 while (res.next()) {406 ++rowCount;407 assertEquals("val_" + res.getInt(1), res.getString(2));408 }409 // read result over SSL410 assertEquals(500, rowCount);411 hs2Conn.close();412 }413 /**414 * Verify the HS2 can't connect to HMS if the certificate doesn't match415 * @throws Exception416 */417 @Test418 public void testMetastoreConnectionWrongCertCN() throws Exception {419 SSLTestUtils.setMetastoreSslConf(conf);420 conf.setVar(ConfVars.HIVE_METASTORE_SSL_KEYSTORE_PATH,421 dataFileDir + File.separator + EXAMPLEDOTCOM_KEY_STORE_NAME);422 miniHS2 = new MiniHS2.Builder().withRemoteMetastore().withConf(conf).cleanupLocalDirOnStartup(false).build();423 try {424 miniHS2.start(confOverlay);425 } catch (java.net.ConnectException e) {426 assertTrue(e.toString().contains("Connection refused"));427 }428 miniHS2.stop();429 }430}...

Full Screen

Full Screen

Source:JdbcThinConnectionSSLTest.java Github

copy

Full Screen

1/**2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17package org.apache.ignite.jdbc.thin;18import java.security.NoSuchAlgorithmException;19import java.sql.Connection;20import java.sql.DriverManager;21import java.sql.SQLException;22import java.util.concurrent.Callable;23import javax.cache.configuration.Factory;24import javax.net.ssl.SSLContext;25import javax.net.ssl.SSLSocketFactory;26import org.apache.ignite.IgniteException;27import org.apache.ignite.internal.util.typedef.internal.U;28import org.apache.ignite.testframework.GridTestUtils;29import org.junit.Test;30/**31 * SSL connection test.32 */33@SuppressWarnings("ThrowableNotThrown")34public class JdbcThinConnectionSSLTest extends JdbcThinAbstractSelfTest {35 /**36 * Client key store path.37 */38 private static final String CLI_KEY_STORE_PATH = (U.getIgniteHome()) + "/modules/clients/src/test/keystore/client.jks";39 /**40 * Server key store path.41 */42 private static final String SRV_KEY_STORE_PATH = (U.getIgniteHome()) + "/modules/clients/src/test/keystore/server.jks";43 /**44 * Trust key store path.45 */46 private static final String TRUST_KEY_STORE_PATH = (U.getIgniteHome()) + "/modules/clients/src/test/keystore/trust.jks";47 /**48 * SSL context factory.49 */50 private static Factory<SSLContext> sslCtxFactory;51 /**52 * Set SSL context factory to client listener.53 */54 private static boolean setSslCtxFactoryToCli;55 /**56 * Set SSL context factory to ignite.57 */58 private static boolean setSslCtxFactoryToIgnite;59 /**60 *61 *62 * @throws Exception63 * If failed.64 */65 @Test66 public void testConnection() throws Exception {67 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;68 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();69 startGrids(1);70 try {71 try (Connection conn = DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"))) {72 checkConnection(conn);73 }74 } finally {75 stopAllGrids();76 }77 }78 /**79 *80 *81 * @throws Exception82 * If failed.83 */84 @Test85 public void testConnectionTrustAll() throws Exception {86 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;87 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();88 startGrids(1);89 try {90 try (Connection conn = DriverManager.getConnection((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustAll=true"))) {91 checkConnection(conn);92 }93 } finally {94 stopAllGrids();95 }96 }97 /**98 *99 *100 * @throws Exception101 * If failed.102 */103 @Test104 public void testConnectionUseIgniteFactory() throws Exception {105 JdbcThinConnectionSSLTest.setSslCtxFactoryToIgnite = true;106 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();107 startGrids(1);108 try {109 try (Connection conn = DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"))) {110 checkConnection(conn);111 }112 } finally {113 stopAllGrids();114 }115 }116 /**117 *118 *119 * @throws Exception120 * If failed.121 */122 @Test123 public void testDefaultContext() throws Exception {124 // Store exists default SSL context to restore after test.125 final SSLContext dfltSslCtx = SSLContext.getDefault();126 // Setup default context127 SSLContext.setDefault(JdbcThinConnectionSSLTest.getTestSslContextFactory().create());128 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;129 // Factory return default SSL context130 JdbcThinConnectionSSLTest.sslCtxFactory = new Factory<SSLContext>() {131 @Override132 public SSLContext create() {133 try {134 return SSLContext.getDefault();135 } catch (NoSuchAlgorithmException e) {136 throw new IgniteException(e);137 }138 }139 };140 startGrids(1);141 try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/?sslMode=require")) {142 checkConnection(conn);143 } finally {144 stopAllGrids();145 // Restore SSL context.146 SSLContext.setDefault(dfltSslCtx);147 }148 }149 /**150 *151 *152 * @throws Exception153 * If failed.154 */155 @Test156 public void testContextFactory() throws Exception {157 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;158 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();159 startGrids(1);160 try (Connection conn = DriverManager.getConnection((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslFactory=") + (JdbcThinConnectionSSLTest.TestSSLFactory.class.getName())))) {161 checkConnection(conn);162 } finally {163 stopAllGrids();164 }165 }166 /**167 *168 *169 * @throws Exception170 * If failed.171 */172 @Test173 public void testSslServerAndPlainClient() throws Exception {174 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;175 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();176 startGrids(1);177 try {178 GridTestUtils.assertThrows(log, new Callable<Object>() {179 @Override180 public Object call() throws Exception {181 DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1");182 return null;183 }184 }, SQLException.class, "Failed to connect to Ignite cluster");185 } finally {186 stopAllGrids();187 }188 }189 /**190 *191 *192 * @throws Exception193 * If failed.194 */195 @Test196 public void testInvalidKeystoreConfig() throws Exception {197 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;198 startGrids(1);199 try {200 GridTestUtils.assertThrows(log, new Callable<Object>() {201 @Override202 public Object call() throws Exception {203 DriverManager.getConnection(((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + (("&sslClientCertificateKeyStoreUrl=invalid_client_keystore_path" + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=")) + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));204 return null;205 }206 }, SQLException.class, "Could not open client key store");207 GridTestUtils.assertThrows(log, new Callable<Object>() {208 @Override209 public Object call() throws Exception {210 DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=invalid_cli_passwd") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));211 return null;212 }213 }, SQLException.class, "Could not open client key store");214 GridTestUtils.assertThrows(log, new Callable<Object>() {215 @Override216 public Object call() throws Exception {217 DriverManager.getConnection(((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=invalid_trust_keystore_path") + "&sslTrustCertificateKeyStorePassword=123456"));218 return null;219 }220 }, SQLException.class, "Could not open trusted key store");221 GridTestUtils.assertThrows(log, new Callable<Object>() {222 @Override223 public Object call() throws Exception {224 DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=invalid_trust_passwd"));225 return null;226 }227 }, SQLException.class, "Could not open trusted key store");228 GridTestUtils.assertThrows(log, new Callable<Object>() {229 @Override230 public Object call() throws Exception {231 DriverManager.getConnection(((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslClientCertificateKeyStoreType=INVALID") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));232 return null;233 }234 }, SQLException.class, "Could not create client KeyStore instance");235 GridTestUtils.assertThrows(log, new Callable<Object>() {236 @Override237 public Object call() throws Exception {238 DriverManager.getConnection(((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStoreType=INVALID") + "&sslTrustCertificateKeyStorePassword=123456"));239 return null;240 }241 }, SQLException.class, "Could not create trust KeyStore instance");242 } finally {243 stopAllGrids();244 }245 }246 /**247 *248 *249 * @throws Exception250 * If failed.251 */252 @Test253 public void testUnknownClientCertificate() throws Exception {254 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;255 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();256 startGrids(1);257 try {258 GridTestUtils.assertThrows(log, new Callable<Object>() {259 @Override260 public Object call() throws Exception {261 Connection c = DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + "&sslClientCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));262 return null;263 }264 }, SQLException.class, "Failed to SSL connect to server");265 } finally {266 stopAllGrids();267 }268 }269 /**270 *271 *272 * @throws Exception273 * If failed.274 */275 @Test276 public void testUnsupportedSslProtocol() throws Exception {277 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;278 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();279 startGrids(1);280 try {281 GridTestUtils.assertThrows(log, new Callable<Object>() {282 @Override283 public Object call() throws Exception {284 Connection c = DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + ("&sslProtocol=TLSv1.3" + "&sslClientCertificateKeyStoreUrl=")) + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));285 return null;286 }287 }, SQLException.class, "TLSv1.3 is not a valid SSL protocol");288 } finally {289 stopAllGrids();290 }291 }292 /**293 *294 *295 * @throws Exception296 * If failed.297 */298 @Test299 public void testInvalidKeyAlgorithm() throws Exception {300 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;301 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();302 startGrids(1);303 try {304 GridTestUtils.assertThrows(log, new Callable<Object>() {305 @Override306 public Object call() throws Exception {307 Connection c = DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + ("&sslKeyAlgorithm=INVALID" + "&sslClientCertificateKeyStoreUrl=")) + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));308 return null;309 }310 }, SQLException.class, "Default algorithm definitions for TrustManager and/or KeyManager are invalid");311 } finally {312 stopAllGrids();313 }314 }315 /**316 *317 *318 * @throws Exception319 * If failed.320 */321 @Test322 public void testInvalidKeyStoreType() throws Exception {323 JdbcThinConnectionSSLTest.setSslCtxFactoryToCli = true;324 JdbcThinConnectionSSLTest.sslCtxFactory = JdbcThinConnectionSSLTest.getTestSslContextFactory();325 startGrids(1);326 try {327 GridTestUtils.assertThrows(log, new Callable<Object>() {328 @Override329 public Object call() throws Exception {330 Connection c = DriverManager.getConnection((((((("jdbc:ignite:thin://127.0.0.1/?sslMode=require" + ("&sslClientCertificateKeyStoreType=PKCS12" + "&sslClientCertificateKeyStoreUrl=")) + (JdbcThinConnectionSSLTest.CLI_KEY_STORE_PATH)) + "&sslClientCertificateKeyStorePassword=123456") + "&sslTrustCertificateKeyStoreUrl=") + (JdbcThinConnectionSSLTest.TRUST_KEY_STORE_PATH)) + "&sslTrustCertificateKeyStorePassword=123456"));331 return null;332 }333 }, SQLException.class, "Could not open client key store");334 } finally {335 stopAllGrids();336 }337 }338 /**339 *340 */341 public static class TestSSLFactory implements Factory<SSLSocketFactory> {342 /**343 * {@inheritDoc }344 */345 @Override346 public SSLSocketFactory create() {347 return JdbcThinConnectionSSLTest.getTestSslContextFactory().create().getSocketFactory();348 }349 }350}...

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2{3public static void main(String args[])4{5SslTest obj=new SslTest();6obj.display();7}8}9package ssl;10{11public void display()12{13System.out.println("display method of ssl package");14}15}

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2public class 4 {3 public static void main(String args[]) {4 SslTest s = new SslTest();5 s.test();6 }7}8package ssl;9import java.io.*;10import java.net.*;11import java.security.*;12import javax.net.ssl.*;13public class SslTest {14 public void test() {15 try {16 SSLContext ctx;17 KeyManagerFactory kmf;18 KeyStore ks;19 char[] passphrase = "passphrase".toCharArray();20 ctx = SSLContext.getInstance("TLS");21 kmf = KeyManagerFactory.getInstance("SunX509");22 ks = KeyStore.getInstance("JKS");23 ks.load(new FileInputStream("testkeys"), passphrase);24 kmf.init(ks, passphrase);25 ctx.init(kmf.getKeyManagers(), null, null);26 SSLServerSocketFactory ssf = ctx.getServerSocketFactory();27 SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(4433);28 SSLSocket c = (SSLSocket) s.accept();29 c.startHandshake();30 SSLSession session = c.getSession();31 System.out.println("SSLSession :");32 System.out.println("\tProtocol : " + session.getProtocol());33 System.out.println("\tCipher suite : " + session.getCipherSuite());34 c.close();35 } catch (Exception e) {36 e.printStackTrace();37 }38 }39}

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2public class Main {3 public static void main(String[] args) {4 SslTest sslTest = new SslTest();5 sslTest.test();6 }7}8package ssl;9public class SslTest {10 public void test() {11 System.out.println("Hello from ssl package");12 }13}14import ssl.*;15public class Main {16 public static void main(String[] args) {17 SslTest sslTest = new SslTest();18 sslTest.test();19 }20}21package ssl;22public class SslTest {23 public void test() {24 System.out.println("Hello from ssl package");25 }26}27package ssl;28class Main {29 public static void main(String[] args) {30 SslTest sslTest = new SslTest();31 sslTest.test();32 }33}34package ssl;35public class SslTest {36 void test() {37 System.out.println("Hello from ssl package");38 }39}40package ssl;41class Main {42 public static void main(String[] args) {43 SslTest sslTest = new SslTest();44 sslTest.test();45 }46}47package ssl;48public class SslTest {49 void test() {50 System.out.println("Hello from ssl package");51 }52}

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2{3public static void main(String args[])4{5SslTest st=new SslTest();6st.display();7}8}9package ssl;10{11public void display()12{13System.out.println("Hello");14}15}

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2{3public static void main(String[] args)4{5SslTest st = new SslTest();6st.test();7}8}

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2{3public static void main(String[] args)4{5SslTest s = new SslTest();6s.display();7}8}

Full Screen

Full Screen

SslTest

Using AI Code Generation

copy

Full Screen

1import ssl.SslTest;2{3public static void main(String args[])4{5SslTest obj = new SslTest();6obj.display();7}8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Karate automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in SslTest

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful