feat: support raw Avro values without Schema Registry - #338
Closed
YingqiDuan wants to merge 7 commits into
Closed
YingqiDuan wants to merge 7 commits into
YingqiDuan wants to merge 7 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
kafkactlcurrently expects Avro messages to use Confluent framing, with each value prefixed by a magic byte and a Schema Registry ID. Some producers instead encode an Avro datum directly as the Kafka record value and provide the schema separately. CloudEvents messages in binary content mode are one example: the Kafka binding carries the schema reference in thece_dataschemaheader.This PR adds opt-in support for producing and consuming these raw Avro values. A standalone schema can be loaded from a local file or an HTTP(S) URL. When consuming,
kafkactlcan also read the schema URL from a Kafka header on each message.No new dependencies or configuration keys are added.
Fixes #337
What changed
produce --avro-schema-file <path-or-url>encodes the record value as raw Avro binary, without Confluent framing.consume --avro-schema-file <path-or-url>decodes record values with one fixed schema.consume --avro-schema-header[=<name>]reads the schema URL from a Kafka message header. Without an explicit name, it usesce_dataschema; pass--avro-schema-header=<name>to select a different header.--avro-schema-cache=falseto fetch and compile the schema again for each non-tombstone message that contains the header.--print-schemaworks with standalone schemas, and the existingavro.jsonCodecsetting continues to control whether standard JSON or Avro JSON is used.Headers such as
ce_dataschemaandcontent-typecan still be added with the existing-H/--headeroption. Raw Avro mode does not add them automatically.Compatibility and scope
Raw Avro mode applies only to record values and is enabled only by the new raw Avro flags. When no raw Avro flag is set, codec selection follows the existing Schema Registry and default paths.
Raw Avro mode does not initialize the configured Schema Registry client. Schema HTTP requests are made only for URLs supplied through
--avro-schema-fileor the selected message header.Plain keys continue to work, as do Protobuf keys backed by a local
.protofile or protoset description. Registry-backed key codecs are not used while raw Avro mode is active.Incompatible combinations are rejected before schema loading or Kafka client creation. These include raw Avro with
--value-proto-typeand, onproduce, non-default--key-schema-versionor--value-schema-versionsettings.This PR does not inspect
content-type, detect raw Avro automatically, add CloudEvents headers, perform full CloudEvents validation, or support CloudEvents structured content mode.Remote schema safety
Header-based lookup is opt-in because the target URL comes from Kafka message data. Remote schema loading:
Localhost and private-network addresses are allowed so local and in-cluster schema services can be used. A message header can therefore trigger requests to services reachable by
kafkactl. Enable header-based lookup only when every producer that can write to the topic is trusted.The header URL cache has no entry limit. It can be disabled for topics that use mutable URLs or many distinct schema URLs.
Related hardening
str32andvbin32lengths safely on 32-bit systems and handles valid zero-length values correctly. The fix also applies to schema URL headers received through Kafka-compatible services such as Azure Event Hubs.Testing
The relevant package tests passed without cached results:
go test -count=1 -short \ ./internal/helpers/avro \ ./internal/consume \ ./internal/producer \ ./cmd/consume \ ./cmd/producego test -racealso passed for the same packages in a Linux Docker environment.go vet,go tool golangci-lint run,go build ./..., and the AMQP tests withGOARCH=386also passed.The following integration tests passed against ZooKeeper and three Kafka brokers without a running Schema Registry:
TestConsumeRawAvroIntegrationTestConsumeRawAvroProtobufKeyIntegrationTestProduceRawAvroNullIntegrationTestProduceRawAvroProtobufKeyIntegrationThese tests cover per-message schemas, fixed-schema fallback, caching on and off, tombstones, zero-byte Avro values, and local Protobuf keys. They also verify that raw Avro values can be produced and consumed without Schema Registry.
Type of change
Documentation
## [Unreleased]section ofCHANGELOG.mdREADME.adoc