Truststore and Google Cloud Dataflow

后端 未结 2 1638
挽巷
挽巷 2021-01-07 05:02

I need to use a trust store to make an SSL Kafka connection in Google Cloud Dataflow. Can I supply this from a bucket or is there a way to store this on the \"local file sys

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 05:13

    Thanks @jkff for the solution, here is an implementation example:

    Sample ConsumerFactoryFn implementation:

        private static class ConsumerFactoryFn
            implements SerializableFunction, Consumer> 
    {
    
    
    
        public Consumer apply(Map config) 
        {
            try 
            {
                Storage storage = StorageOptions.newBuilder()
                        .setProjectId("prj-id-of-your-bucket")
                        .setCredentials(GoogleCredentials.getApplicationDefault())
                        .build()
                        .getService();
                Blob blob = storage.get("your-bucket-name", "pth.to.your.kafka.client.truststore.jks");
                ReadChannel readChannel = blob.reader();
                FileOutputStream fileOuputStream;
                fileOuputStream = new FileOutputStream("/tmp/kafka.client.truststore.jks"); //path where the jks file will be stored
                fileOuputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
                fileOuputStream.close();
                File f = new File("/tmp/kafka.client.truststore.jks"); //assuring the store file exists
                if (f.exists())
                {
                    LOG.debug("key exists");
    
                }
                else
                {
                    LOG.error("key does not exist");
    
                }
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                LOG.error( e.getMessage());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                LOG.error( e.getMessage());
            }
    
    
            config.put("ssl.truststore.location",(Object) "/tmp/kafka.client.truststore.jks" );
    
            return new KafkaConsumer(config);
        }
    }
    

    and do not forget to use .withConsumerFactoryFn in your KafkaIO.read() call, should be something like:

    Map configMap = new HashMap();
    configMap.put("security.protocol", (Object) "SSL");
    configMap.put("ssl.truststore.password", (Object) "clientpass");
    
    p.apply("ReadFromKafka", KafkaIO.read()
                .withBootstrapServers("ip:9093")
                .withTopic("pageviews")
                .withKeyDeserializer(StringDeserializer.class)
                .withValueDeserializer(StringDeserializer.class)
                .updateConsumerProperties(configMap)
                .withConsumerFactoryFn(new ConsumerFactoryFn()) ... etc.
    

提交回复
热议问题