You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/12/01 15:48:07 UTC

[GitHub] [beam] ilya-kozyrev commented on a change in pull request #13112: [BEAM-11065] Apache Beam Template to ingest from Apache Kafka to Google Pub/Sub

ilya-kozyrev commented on a change in pull request #13112:
URL: https://github.com/apache/beam/pull/13112#discussion_r533517106



##########
File path: examples/templates/java/kafka-to-pubsub/src/main/java/org/apache/beam/templates/kafka/consumer/SslConsumerFactoryFn.java
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.templates.kafka.consumer;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.apache.beam.sdk.io.FileSystems;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.clients.consumer.Consumer;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.config.SslConfigs;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Class to create Kafka Consumer with configured SSL. */
+public class SslConsumerFactoryFn
+    implements SerializableFunction<Map<String, Object>, Consumer<byte[], byte[]>> {
+  private final Map<String, String> sslConfig;
+  private static final String TRUSTSTORE_LOCAL_PATH = "/tmp/kafka.truststore.jks";
+  private static final String KEYSTORE_LOCAL_PATH = "/tmp/kafka.keystore.jks";
+
+  /* Logger for class.*/
+  private static final Logger LOG = LoggerFactory.getLogger(SslConsumerFactoryFn.class);
+
+  public SslConsumerFactoryFn(Map<String, String> sslConfig) {
+    this.sslConfig = sslConfig;
+  }
+
+  @SuppressWarnings("nullness")
+  @Override
+  public Consumer<byte[], byte[]> apply(Map<String, Object> config) {
+    try {
+      String truststoreLocation = sslConfig.get(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG);
+      if (truststoreLocation.startsWith("gs://")) {
+        getGcsFileAsLocal(truststoreLocation, TRUSTSTORE_LOCAL_PATH);
+      } else {
+        checkFileExists(truststoreLocation);

Review comment:
       Thank you for the great idea to use different filesystems. We will add it to future plans. 
   
   I added a new message to mention what we cant use local paths when we using distribute runners.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org