2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.components.kafka;
22 import java.util.Properties;
23 import java.util.UUID;
24 import org.apache.kafka.clients.CommonClientConfigs;
25 import org.apache.kafka.clients.consumer.ConsumerConfig;
26 import org.apache.kafka.clients.producer.ProducerConfig;
27 import org.apache.kafka.common.KafkaException;
28 import org.apache.kafka.common.config.SaslConfigs;
29 import org.apache.kafka.common.config.SslConfigs;
30 import org.onap.config.api.JettySSLUtils;
31 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
33 public class KafkaCommonConfig {
35 private final DistributionEngineConfiguration deConfiguration;
37 public KafkaCommonConfig(DistributionEngineConfiguration config){
38 this.deConfiguration = config;
41 public Properties getConsumerProperties(){
42 Properties props = new Properties();
43 setCommonProperties(props);
44 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
45 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
46 props.put(ConsumerConfig.CLIENT_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerId() + "-consumer-" + UUID.randomUUID());
47 props.put(ConsumerConfig.GROUP_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerGroup());
48 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
52 public Properties getProducerProperties(){
53 Properties props = new Properties();
54 setCommonProperties(props);
55 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
56 props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
57 props.put(ProducerConfig.CLIENT_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerId() + "-producer-" + UUID.randomUUID());
62 private void setCommonProperties(Properties props) {
63 String securityProtocolConfig = System.getenv().getOrDefault("SECURITY_PROTOCOL", "SASL_PLAINTEXT");
64 props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, securityProtocolConfig);
65 props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, deConfiguration.getKafkaBootStrapServers());
67 if("SSL".equals(securityProtocolConfig)) {
68 final JettySSLUtils.JettySslConfig sslConfig = JettySSLUtils.getSSLConfig();
69 props.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, sslConfig.getKeystorePath());
70 props.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, sslConfig.getKeystorePass());
71 props.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, sslConfig.getKeyStoreManager());
72 props.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "");
73 props.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, sslConfig.getTruststorePath());
74 props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, sslConfig.getTruststorePass());
76 props.put(SaslConfigs.SASL_JAAS_CONFIG, getKafkaSaslJaasConfig());
77 props.put(SaslConfigs.SASL_MECHANISM, "SCRAM-SHA-512");
82 * @return The Sasl Jaas Configuration
84 private String getKafkaSaslJaasConfig() throws KafkaException {
85 String saslJaasConfFromEnv = System.getenv("SASL_JAAS_CONFIG");
86 if(saslJaasConfFromEnv != null) {
87 return saslJaasConfFromEnv;
89 throw new KafkaException("sasl.jaas.config not set for Kafka Consumer");