Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GCP Pubsub Utf8 checks #3004

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/impl/gcp/integration_pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,25 @@ input:
name: "invalid key",
key: "\xc0\x80",
value: "bar",
expectedErr: "failed to build message attributes: metadata field \xc0\x80 contains non-UTF-8 characters",
expectedErr: "rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8: metadata field \xc0\x80 contains non-UTF-8 characters",
},
{
name: "invalid control",
key: "foo",
value: "\xc0\x80",
expectedErr: "failed to build message attributes: metadata field foo contains non-UTF-8 data: \xc0\x80",
expectedErr: "rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8: metadata field foo contains non-UTF-8 data: \xc0\x80",
},
{
name: "invalid high",
key: "foo",
value: "\xed\xa0\x80",
expectedErr: "failed to build message attributes: metadata field foo contains non-UTF-8 data: \xed\xa0\x80",
expectedErr: "rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8: metadata field foo contains non-UTF-8 data: \xed\xa0\x80",
},
{
name: "invalid low",
key: "foo",
value: "\xed\xbf\xbf",
expectedErr: "failed to build message attributes: metadata field foo contains non-UTF-8 data: \xed\xbf\xbf",
expectedErr: "rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8: metadata field foo contains non-UTF-8 data: \xed\xbf\xbf",
},
}

Expand Down
25 changes: 15 additions & 10 deletions internal/impl/gcp/output_pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,21 @@ func (out *pubsubOutput) WriteBatch(ctx context.Context, batch service.MessageBa
p.Go(func(ctx context.Context) (*serverResult, error) {
_, err := res.Get(ctx)
if err != nil {
errMeta := out.metaFilter.Walk(msg, func(key, value string) error {
// Checking attributes explicitly for UTF-8 validity makes the user experience way better. We can point out
// which key is non-compatible.
// The UTF-8 requirement comes from internal Protocol Buffer/GRPC conversions happening in the PubSub client.
if !utf8.ValidString(key) {
return fmt.Errorf("metadata field %s contains non-UTF-8 characters", key)
}
if !utf8.ValidString(value) {
return fmt.Errorf("metadata field %s contains non-UTF-8 data: %s", key, value)
}
return nil
})
if errMeta != nil {
err = fmt.Errorf("%w: %w", err, errMeta)
}
return &serverResult{batchIndex: i, err: err}, nil
}
return nil, nil
Expand Down Expand Up @@ -325,16 +340,6 @@ func (out *pubsubOutput) writeMessage(ctx context.Context, cachedTopics map[stri

attr := make(map[string]string)
if err := out.metaFilter.Walk(msg, func(key, value string) error {
// Checking attributes explicitly for UTF-8 validity makes the user experience way better. We can point out
// which key is non-compatible.
// The UTF-8 requirement comes from internal Protocol Buffer/GRPC conversions happening in the PubSub client.
if !utf8.ValidString(key) {
return fmt.Errorf("metadata field %s contains non-UTF-8 characters", key)
}
if !utf8.ValidString(value) {
return fmt.Errorf("metadata field %s contains non-UTF-8 data: %s", key, value)
}

attr[key] = value
return nil
}); err != nil {
Expand Down