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 1 commit
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
Prev Previous commit
Next Next commit
Add Host to metadata
  • Loading branch information
orloffv committed Jan 24, 2022
commit 0abf2fab41f9a59120b215b00fc79ddc80ac0adf
3 changes: 0 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ type Info struct {
// this connection.
Auth AuthData

// Host from the incoming request (http.Request.Host)
Host string

// Metadata is the request metadata from the client connecting to this connector.
// Experimental: *NOTE* this structure is subject to change or removal in the future.
Metadata Metadata
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
9 changes: 6 additions & 3 deletions config/confighttp/clientinfohandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ 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.Host = req.Host
cl.Metadata = client.NewMetadata(md)
}

ctx := client.NewContext(req.Context(), cl)
return ctx
Expand Down
8 changes: 5 additions & 3 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,12 +781,14 @@ func TestContextWithClient(t *testing.T) {
},
},
{
desc: "request with Host",
desc: "request with Host and client headers",
input: &http.Request{
Host: "localhost:55443",
Header: map[string][]string{"x-test-header": {"test-value"}},
Host: "localhost:55443",
},
doMetadata: true,
expected: client.Info{
Host: "localhost:55443",
Metadata: client.NewMetadata(map[string][]string{"x-test-header": {"test-value"}, "Host": {"localhost:55443"}}),
},
},
}
Expand Down