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

Adding x-forwarded-host support #39

Open
wants to merge 2 commits into
base: master
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
15 changes: 14 additions & 1 deletion proxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

var (
Expand Down Expand Up @@ -70,7 +72,18 @@ func (s *handler) handler(srv interface{}, serverStream grpc.ServerStream) error
}

clientCtx, clientCancel := context.WithCancel(outgoingCtx)
// TODO(mwitkow): Add a `forwarded` header to metadata, https://en.wikipedia.org/wiki/X-Forwarded-For.
md, ok := metadata.FromIncomingContext(clientCtx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

extract to function, link to relevant docs (i.e. wikipedia)

if !ok {
return status.Error(codes.Internal, ":authority header has not been set")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this now an error? seems like a breaking change.

}
authority, ok := md[":authority"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there any way of connecting these keys back to the upstream definition?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems incorrect. x-forwarded-for is the list of peers forwarding the request. the authority is the intended destination of the request.

Copy link

Choose a reason for hiding this comment

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

This looks strange to me as well - I'm not clear on how x-forwarded-host and authority are related.

If you want to record the caller, then x-forwarded-for seems like a good choice. X-forwarded-for is implemented in my fork: https://github.com/vgough/grpc-proxy/blob/master/proxy/handler.go

if !ok {
return status.Error(codes.Internal, ":authority header has not been set")
}
md = md.Copy() // metadata is immutable, copy.
md["x-forwarded-host"] = authority

clientCtx = metadata.NewOutgoingContext(clientCtx, md)
clientStream, err := grpc.NewClientStream(clientCtx, clientStreamDescForProxying, backendConn, fullMethodName)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions proxy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package proxy_test

import (
"fmt"
"io"
"log"
"net"
Expand All @@ -13,6 +14,7 @@ import (
"time"

"github.com/mwitkow/grpc-proxy/proxy"
pb "github.com/mwitkow/grpc-proxy/testservice"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand All @@ -21,10 +23,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"

"fmt"

pb "github.com/mwitkow/grpc-proxy/testservice"
)

const (
Expand Down