Search in sources :

Example 16 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project apache-kafka-on-k8s by banzaicloud.

the class SslFactory method createSSLContext.

// package access for testing
SSLContext createSSLContext(SecurityStore keystore) throws GeneralSecurityException, IOException {
    SSLContext sslContext;
    if (provider != null)
        sslContext = SSLContext.getInstance(protocol, provider);
    else
        sslContext = SSLContext.getInstance(protocol);
    KeyManager[] keyManagers = null;
    if (keystore != null) {
        String kmfAlgorithm = this.kmfAlgorithm != null ? this.kmfAlgorithm : KeyManagerFactory.getDefaultAlgorithm();
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
        KeyStore ks = keystore.load();
        Password keyPassword = keystore.keyPassword != null ? keystore.keyPassword : keystore.password;
        kmf.init(ks, keyPassword.value().toCharArray());
        keyManagers = kmf.getKeyManagers();
    }
    String tmfAlgorithm = this.tmfAlgorithm != null ? this.tmfAlgorithm : TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    KeyStore ts = truststore == null ? null : truststore.load();
    tmf.init(ts);
    sslContext.init(keyManagers, tmf.getTrustManagers(), this.secureRandomImplementation);
    if (keystore != null && keystore != this.keystore) {
        if (this.keystore == null)
            throw new ConfigException("Cannot add SSL keystore to an existing listener for which no keystore was configured.");
        if (keystoreVerifiableUsingTruststore)
            SSLConfigValidatorEngine.validate(this, sslContext);
        if (!CertificateEntries.create(this.keystore.load()).equals(CertificateEntries.create(keystore.load()))) {
            throw new ConfigException("Keystore DistinguishedName or SubjectAltNames do not match");
        }
    }
    return sslContext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ConfigException(org.apache.kafka.common.config.ConfigException) SSLContext(javax.net.ssl.SSLContext) KeyManager(javax.net.ssl.KeyManager) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) Password(org.apache.kafka.common.config.types.Password)

Example 17 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project debezium by debezium.

the class Field method validate.

/**
 * Validate this field in the supplied configuration, updating the {@link ConfigValue} for the field with the results.
 *
 * @param config the configuration to be validated; may not be null
 * @param fieldSupplier the supplier for dependent fields by name; may not be null
 * @param results the set of configuration results keyed by field name; may not be null
 */
protected void validate(Configuration config, Function<String, Field> fieldSupplier, Map<String, ConfigValue> results) {
    // First, merge any new recommended values ...
    ConfigValue value = results.computeIfAbsent(this.name(), n -> new ConfigValue(n));
    // Apply the validator ...
    validate(config, (f, v, problem) -> {
        value.addErrorMessage(problem);
    });
    // Apply the recommender ..
    if (recommender != null) {
        try {
            // Set the visibility ...
            value.visible(recommender.visible(this, config));
            // Compute and set the new recommendations ...
            List<Object> newRecommendations = recommender.validValues(this, config);
            List<Object> previousRecommendations = value.recommendedValues();
            if (!previousRecommendations.isEmpty()) {
                // Don't use any newly recommended values if they were not previously recommended ...
                newRecommendations.retainAll(previousRecommendations);
            }
            value.recommendedValues(newRecommendations);
        } catch (ConfigException e) {
            value.addErrorMessage(e.getMessage());
        }
    }
    // Do the same for any dependents ...
    dependents.forEach(name -> {
        Field dependentField = fieldSupplier.apply(name);
        if (dependentField != null) {
            dependentField.validate(config, fieldSupplier, results);
        }
    });
}
Also used : ConfigValue(org.apache.kafka.common.config.ConfigValue) ConfigException(org.apache.kafka.common.config.ConfigException)

Example 18 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducer method configureTransactionState.

private static TransactionManager configureTransactionState(ProducerConfig config, LogContext logContext, Logger log) {
    TransactionManager transactionManager = null;
    boolean userConfiguredIdempotence = false;
    if (config.originals().containsKey(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG))
        userConfiguredIdempotence = true;
    boolean userConfiguredTransactions = false;
    if (config.originals().containsKey(ProducerConfig.TRANSACTIONAL_ID_CONFIG))
        userConfiguredTransactions = true;
    boolean idempotenceEnabled = config.getBoolean(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG);
    if (!idempotenceEnabled && userConfiguredIdempotence && userConfiguredTransactions)
        throw new ConfigException("Cannot set a " + ProducerConfig.TRANSACTIONAL_ID_CONFIG + " without also enabling idempotence.");
    if (userConfiguredTransactions)
        idempotenceEnabled = true;
    if (idempotenceEnabled) {
        String transactionalId = config.getString(ProducerConfig.TRANSACTIONAL_ID_CONFIG);
        int transactionTimeoutMs = config.getInt(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG);
        long retryBackoffMs = config.getLong(ProducerConfig.RETRY_BACKOFF_MS_CONFIG);
        transactionManager = new TransactionManager(logContext, transactionalId, transactionTimeoutMs, retryBackoffMs);
        if (transactionManager.isTransactional())
            log.info("Instantiated a transactional producer.");
        else
            log.info("Instantiated an idempotent producer.");
    }
    return transactionManager;
}
Also used : TransactionManager(org.apache.kafka.clients.producer.internals.TransactionManager) ConfigException(org.apache.kafka.common.config.ConfigException)

Example 19 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class SourceConnectorConfig method enrich.

/**
 * Returns an enriched {@link ConfigDef} building upon the {@code ConfigDef}, using the current configuration specified in {@code props} as an input.
 *
 * @param baseConfigDef the base configuration definition to be enriched
 * @param props the non parsed configuration properties
 * @return the enriched configuration definition
 */
public static ConfigDef enrich(ConfigDef baseConfigDef, Map<String, String> props, AbstractConfig defaultGroupConfig) {
    List<Object> topicCreationGroups = new ArrayList<>();
    Object aliases = ConfigDef.parseType(TOPIC_CREATION_GROUPS_CONFIG, props.get(TOPIC_CREATION_GROUPS_CONFIG), ConfigDef.Type.LIST);
    if (aliases instanceof List) {
        topicCreationGroups.addAll((List<?>) aliases);
    }
    ConfigDef newDef = new ConfigDef(baseConfigDef);
    String defaultGroupPrefix = TOPIC_CREATION_PREFIX + DEFAULT_TOPIC_CREATION_GROUP + ".";
    short defaultGroupReplicationFactor = defaultGroupConfig.getShort(defaultGroupPrefix + REPLICATION_FACTOR_CONFIG);
    int defaultGroupPartitions = defaultGroupConfig.getInt(defaultGroupPrefix + PARTITIONS_CONFIG);
    topicCreationGroups.stream().distinct().forEach(group -> {
        if (!(group instanceof String)) {
            throw new ConfigException("Item in " + TOPIC_CREATION_GROUPS_CONFIG + " property is not of type String");
        }
        String alias = (String) group;
        String prefix = TOPIC_CREATION_PREFIX + alias + ".";
        String configGroup = TOPIC_CREATION_GROUP + ": " + alias;
        newDef.embed(prefix, configGroup, 0, TopicCreationConfig.configDef(configGroup, defaultGroupReplicationFactor, defaultGroupPartitions));
    });
    return newDef;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConfigException(org.apache.kafka.common.config.ConfigException) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 20 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class ClientUtils method parseAndValidateAddresses.

public static List<InetSocketAddress> parseAndValidateAddresses(List<String> urls, ClientDnsLookup clientDnsLookup) {
    List<InetSocketAddress> addresses = new ArrayList<>();
    for (String url : urls) {
        if (url != null && !url.isEmpty()) {
            try {
                String host = getHost(url);
                Integer port = getPort(url);
                if (host == null || port == null)
                    throw new ConfigException("Invalid url in " + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG + ": " + url);
                if (clientDnsLookup == ClientDnsLookup.RESOLVE_CANONICAL_BOOTSTRAP_SERVERS_ONLY) {
                    InetAddress[] inetAddresses = InetAddress.getAllByName(host);
                    for (InetAddress inetAddress : inetAddresses) {
                        String resolvedCanonicalName = inetAddress.getCanonicalHostName();
                        InetSocketAddress address = new InetSocketAddress(resolvedCanonicalName, port);
                        if (address.isUnresolved()) {
                            log.warn("Couldn't resolve server {} from {} as DNS resolution of the canonical hostname {} failed for {}", url, CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, resolvedCanonicalName, host);
                        } else {
                            addresses.add(address);
                        }
                    }
                } else {
                    InetSocketAddress address = new InetSocketAddress(host, port);
                    if (address.isUnresolved()) {
                        log.warn("Couldn't resolve server {} from {} as DNS resolution failed for {}", url, CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, host);
                    } else {
                        addresses.add(address);
                    }
                }
            } catch (IllegalArgumentException e) {
                throw new ConfigException("Invalid port in " + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG + ": " + url);
            } catch (UnknownHostException e) {
                throw new ConfigException("Unknown host in " + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG + ": " + url);
            }
        }
    }
    if (addresses.isEmpty())
        throw new ConfigException("No resolvable bootstrap urls given in " + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG);
    return addresses;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ConfigException(org.apache.kafka.common.config.ConfigException) InetAddress(java.net.InetAddress)

Aggregations

ConfigException (org.apache.kafka.common.config.ConfigException)136 HashMap (java.util.HashMap)29 Test (org.junit.jupiter.api.Test)28 Test (org.junit.Test)20 Properties (java.util.Properties)10 KafkaException (org.apache.kafka.common.KafkaException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Pattern (java.util.regex.Pattern)9 Serde (org.apache.kafka.common.serialization.Serde)8 SimpleConfig (org.apache.kafka.connect.transforms.util.SimpleConfig)8 File (java.io.File)7 SSLContext (javax.net.ssl.SSLContext)7 Map (java.util.Map)6 ByteArraySerializer (org.apache.kafka.common.serialization.ByteArraySerializer)6 KeyStore (java.security.KeyStore)5 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)5 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)5 ConfigDef (org.apache.kafka.common.config.ConfigDef)5 IOException (java.io.IOException)4