package config import ( "errors" "fmt" "math" "net" "regexp" "strconv" "strings" ) const ( defaultKafkaBrokers = "localhost:9092" kafkaBrokersSeparator = "," // defaultKafkaEventsTopic receives every domain event. One topic keeps // the setup small; the record key keeps each aggregate's events ordered // within one partition. defaultKafkaEventsTopic = "lincodex.domain.events" // defaultKafkaEventsTopicPartitions lets up to three consumers of one // group share the topic. Partitions can be added later but not removed, // and adding them moves keys to other partitions. defaultKafkaEventsTopicPartitions = 3 // maxKafkaEventsTopicPartitions bounds a mistyped value; one node does // not benefit from more partitions. maxKafkaEventsTopicPartitions = 1000 // KafkaBrokerDefaultReplicationFactor asks the broker to apply its own // default.replication.factor when it creates the topic, so one setting on // the broker covers both the single-node stand and a replicated cluster. KafkaBrokerDefaultReplicationFactor = -1 // KafkaBrokerDefaultMinInsyncReplicas leaves min.insync.replicas of a // created topic to the broker default. KafkaBrokerDefaultMinInsyncReplicas = 0 // Topic provisioning is on by default because the broker never creates // topics on demand. defaultKafkaProvisionTopics = true // maxKafkaTopicNameLength is the Kafka limit for topic names. maxKafkaTopicNameLength = 249 ) // kafkaTopicNamePattern holds the characters Kafka accepts in a topic name. var kafkaTopicNamePattern = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`) // parseKafkaBrokers splits a comma-separated broker list. Empty entries are // kept so that validation can reject values such as "a:9092,,b:9092". func parseKafkaBrokers(raw string) []string { if strings.TrimSpace(raw) == "" { return nil } entries := strings.Split(raw, kafkaBrokersSeparator) brokers := make([]string, 0, len(entries)) for _, entry := range entries { brokers = append(brokers, strings.TrimSpace(entry)) } return brokers } // loadKafkaTopics reads the events topic settings. An unset value takes its // default; an empty or malformed value is an error. func (config *Config) loadKafkaTopics(lookup LookupEnv) error { config.KafkaEventsTopic = strings.TrimSpace(valueOrDefault(lookup, "KAFKA_EVENTS_TOPIC", defaultKafkaEventsTopic)) partitions, err := intOrDefault(lookup, "KAFKA_EVENTS_TOPIC_PARTITIONS", defaultKafkaEventsTopicPartitions) if err != nil { return err } if partitions < 1 || partitions > maxKafkaEventsTopicPartitions { return fmt.Errorf("Config.loadKafkaTopics - KAFKA_EVENTS_TOPIC_PARTITIONS must be between 1 and %d", maxKafkaEventsTopicPartitions) } config.KafkaEventsTopicPartitions = int32(partitions) replicationFactor, err := intOrDefault(lookup, "KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR", KafkaBrokerDefaultReplicationFactor) if err != nil { return err } if replicationFactor < KafkaBrokerDefaultReplicationFactor || replicationFactor == 0 || replicationFactor > math.MaxInt16 { return fmt.Errorf("Config.loadKafkaTopics - KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR must be -1 for the broker default or between 1 and %d", math.MaxInt16) } config.KafkaEventsTopicReplicationFactor = int16(replicationFactor) minInsyncReplicas, err := intOrDefault(lookup, "KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS", KafkaBrokerDefaultMinInsyncReplicas) if err != nil { return err } // An explicit 0 would read as the broker default, so only an unset // variable selects it. _, set := lookup("KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS") if minInsyncReplicas < KafkaBrokerDefaultMinInsyncReplicas || minInsyncReplicas > math.MaxInt16 || (set && minInsyncReplicas == KafkaBrokerDefaultMinInsyncReplicas) { return fmt.Errorf("Config.loadKafkaTopics - KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS must be between 1 and %d, or unset for the broker default", math.MaxInt16) } config.KafkaEventsTopicMinInsyncReplicas = int16(minInsyncReplicas) config.KafkaProvisionTopics, err = boolOrDefault(lookup, "KAFKA_PROVISION_TOPICS", defaultKafkaProvisionTopics) return err } func (config Config) ValidateKafka() error { if len(config.KafkaBrokers) == 0 { return errors.New("Config.ValidateKafka - KAFKA_BROKERS must not be empty") } for index, broker := range config.KafkaBrokers { // Report the position instead of the value so a mistyped entry with // embedded credentials does not reach logs. if !validKafkaBroker(broker) { return fmt.Errorf("Config.ValidateKafka - KAFKA_BROKERS entry %d must be a host:port address with a nonzero port", index+1) } } if !validKafkaTopicName(config.KafkaEventsTopic) { return fmt.Errorf("Config.ValidateKafka - KAFKA_EVENTS_TOPIC must be 1 to %d letters, digits, dots, underscores, or hyphens other than a single or double dot", maxKafkaTopicNameLength) } if config.KafkaEventsTopicPartitions < 1 || config.KafkaEventsTopicPartitions > maxKafkaEventsTopicPartitions { return fmt.Errorf("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_PARTITIONS must be between 1 and %d", maxKafkaEventsTopicPartitions) } if config.KafkaEventsTopicReplicationFactor != KafkaBrokerDefaultReplicationFactor && config.KafkaEventsTopicReplicationFactor < 1 { return errors.New("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR must be -1 for the broker default or positive") } if config.KafkaEventsTopicMinInsyncReplicas < KafkaBrokerDefaultMinInsyncReplicas { return errors.New("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS must be positive or unset for the broker default") } // With an explicit replication factor, a larger min.insync.replicas would // make every acks=all write fail. if config.KafkaEventsTopicReplicationFactor > 0 && config.KafkaEventsTopicMinInsyncReplicas > config.KafkaEventsTopicReplicationFactor { return errors.New("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS must not exceed KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR") } return nil } // validKafkaTopicName applies the broker's topic name rules. func validKafkaTopicName(topic string) bool { return len(topic) <= maxKafkaTopicNameLength && topic != "." && topic != ".." && kafkaTopicNamePattern.MatchString(topic) } // validKafkaBroker accepts a hostname or IP address with a port in 1-65535. // URL schemes, user info, and paths are rejected because Kafka clients expect // plain bootstrap addresses. func validKafkaBroker(broker string) bool { host, port, err := net.SplitHostPort(broker) if err != nil { return false } number, err := strconv.Atoi(port) if err != nil || number < 1 || number > maxPortNumber { return false } return net.ParseIP(host) != nil || validHostname(host) } func validHostname(host string) bool { if host == "" { return false } for _, label := range strings.Split(host, ".") { if !validHostnameLabel(label) { return false } } return true } func validHostnameLabel(label string) bool { if label == "" || strings.HasPrefix(label, "-") || strings.HasSuffix(label, "-") { return false } for _, character := range label { isLetter := (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') isDigit := character >= '0' && character <= '9' if !isLetter && !isDigit && character != '-' && character != '_' { return false } } return true }