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

Add Host property for Metadata #4736

Merged
merged 8 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- create `configcompression` package to manage compression methods in `confighttp` and `configgrpc`
- Add support for cgroupv2 memory limit (#4654)
- Enable end users to provide multiple files for config location (#4727)
- Add Host property for Metadata (#4736)
orloffv marked this conversation as resolved.
Show resolved Hide resolved

## 🧰 Bug fixes 🧰

Expand Down
6 changes: 5 additions & 1 deletion config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,11 @@ func contextWithClient(ctx context.Context, includeMetadata bool) context.Contex
}
if includeMetadata {
if md, ok := metadata.FromIncomingContext(ctx); ok {
cl.Metadata = client.NewMetadata(md.Copy())
copiedMD := md.Copy()
if len(md["Host"]) == 0 && len(md[":authority"]) > 0 {
copiedMD["Host"] = md[":authority"]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove the ":authority" in this case? (Not sure, but want to make sure somebody thinks about this).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no, because it's real header

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, at least as part of this PR here. We might want to start a discussion for something like "semantic conventions" for the headers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpkrohling Is this comment related to host const?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was agreeing in keeping :authority here, as it's the key used by gRPC, and would expect to see it there. The second part of my comment was about having a single value where downstream components would expect the "host" value to exist, but this is being addressed in this PR already. Sorry about the noise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"semantic conventions" for the headers.

This also may include us having the constant values for things like "Host", that's why I got confused. So you are ok with having that constant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, at least for the moment. If we keep finding ourselves doing it often, we might want to split that out.

cl.Metadata = client.NewMetadata(copiedMD)
}
}
return client.NewContext(ctx, cl)
Expand Down
11 changes: 11 additions & 0 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,17 @@ func TestContextWithClient(t *testing.T) {
),
expected: client.Info{},
},
{
desc: "existing client with Host and metadata",
input: metadata.NewIncomingContext(
client.NewContext(context.Background(), client.Info{}),
metadata.Pairs("test-metadata-key", "test-value", ":authority", "localhost:55443"),
),
doMetadata: true,
expected: client.Info{
Metadata: client.NewMetadata(map[string][]string{"test-metadata-key": {"test-value"}, ":authority": {"localhost:55443"}, "Host": {"localhost:55443"}}),
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion config/confighttp/clientinfohandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func contextWithClient(req *http.Request, includeMetadata bool) context.Context
}

if includeMetadata {
cl.Metadata = client.NewMetadata(req.Header.Clone())
md := req.Header.Clone()
if len(md.Get("Host")) == 0 && req.Host != "" {
md.Add("Host", req.Host)
orloffv marked this conversation as resolved.
Show resolved Hide resolved
}

cl.Metadata = client.NewMetadata(md)
}

ctx := client.NewContext(req.Context(), cl)
Expand Down
11 changes: 11 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,17 @@ func TestContextWithClient(t *testing.T) {
Metadata: client.NewMetadata(map[string][]string{"x-test-header": {"test-value"}}),
},
},
{
desc: "request with Host and client headers",
input: &http.Request{
Header: map[string][]string{"x-test-header": {"test-value"}},
Host: "localhost:55443",
},
doMetadata: true,
expected: client.Info{
Metadata: client.NewMetadata(map[string][]string{"x-test-header": {"test-value"}, "Host": {"localhost:55443"}}),
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
Expand Down