From 8a235ab0700877a6f4d7f9271f661cb448b9219a Mon Sep 17 00:00:00 2001 From: Yi Hu Date: Fri, 10 Nov 2023 14:47:53 -0500 Subject: [PATCH] Fix MongoDBIO ignore SSL KeyStore not initialized --- .../apache/beam/sdk/io/mongodb/SSLUtils.java | 2 +- .../beam/sdk/io/mongodb/SSLUtilsTest.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/SSLUtilsTest.java diff --git a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/SSLUtils.java b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/SSLUtils.java index 1c4697289311..f68aaaea95dd 100644 --- a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/SSLUtils.java +++ b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/SSLUtils.java @@ -68,12 +68,12 @@ static SSLContext ignoreSSLCertificate() { InputStream inputStream = classLoader.getResourceAsStream("resources/.keystore"); if (inputStream != null) { LOG.info("Found keystore in classpath 'resources/.keystore'. Loading..."); - ks.load(inputStream, "changeit".toCharArray()); } else { LOG.info( "Unable to find keystore under 'resources/.keystore' in the classpath. " + "Continuing with an empty keystore."); } + ks.load(inputStream, "changeit".toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, "changeit".toCharArray()); diff --git a/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/SSLUtilsTest.java b/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/SSLUtilsTest.java new file mode 100644 index 000000000000..978163129f87 --- /dev/null +++ b/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/SSLUtilsTest.java @@ -0,0 +1,32 @@ +/* + * 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.beam.sdk.io.mongodb; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Test on the MongoDbIO SSLUtils. */ +@RunWith(JUnit4.class) +public class SSLUtilsTest { + @Test + public void testIgnoreSSLCertificate() { + // smoke test + SSLUtils.ignoreSSLCertificate(); + } +}