Skip to content

Commit

Permalink
Fix grpc-web: move grpc-web filter in front of transcoder (#176)
Browse files Browse the repository at this point in the history
Signed-off-by: Wayne Zhang <[email protected]>
  • Loading branch information
qiwzhang authored Jun 2, 2020
1 parent fa11613 commit 7d7580b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
8 changes: 4 additions & 4 deletions examples/grpc_dynamic_routing/envoy_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,9 @@
]
}
},
{
"name": "envoy.filters.http.grpc_web"
},
{
"name": "envoy.filters.http.grpc_json_transcoder",
"typedConfig": {
Expand All @@ -1054,9 +1057,6 @@
]
}
},
{
"name": "envoy.filters.http.grpc_web"
},
{
"name": "envoy.filters.http.grpc_stats",
"typedConfig": {
Expand Down Expand Up @@ -1244,4 +1244,4 @@
}
]
}
}
}
15 changes: 10 additions & 5 deletions src/go/configgenerator/listener_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,23 @@ func makeListener(serviceInfo *sc.ServiceInfo) (*listenerpb.Listener, error) {

// Add gRPC Transcoder filter and gRPCWeb filter configs for gRPC backend.
if serviceInfo.GrpcSupportRequired {
// grpc-web filter should be before grpc transcoder filter.
// It converts content-type application/grpc-web to application/grpc and
// grpc transcoder will bypass requests with application/grpc content type.
// Otherwise grpc transcoder will try to transcode a grpc-web request which
// will fail.
grpcWebFilter := &hcmpb.HttpFilter{
Name: util.GRPCWeb,
}
httpFilters = append(httpFilters, grpcWebFilter)

transcoderFilter := makeTranscoderFilter(serviceInfo)
if transcoderFilter != nil {
httpFilters = append(httpFilters, transcoderFilter)
jsonStr, _ := util.ProtoToJson(transcoderFilter)
glog.Infof("adding Transcoder Filter config: %v", jsonStr)
}

grpcWebFilter := &hcmpb.HttpFilter{
Name: util.GRPCWeb,
}
httpFilters = append(httpFilters, grpcWebFilter)

// GrpcStats filter is used to count gRPC frames.
// The data is stored in filterState and used by ServiceControl
// filter in the final report call.
Expand Down
6 changes: 3 additions & 3 deletions src/go/configmanager/config_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func TestFetchListeners(t *testing.T) {
]
}
},
{
"name":"envoy.filters.http.grpc_web"
},
{
"name":"envoy.filters.http.grpc_json_transcoder",
"typedConfig":{
Expand All @@ -147,9 +150,6 @@ func TestFetchListeners(t *testing.T) {
]
}
},
{
"name":"envoy.filters.http.grpc_web"
},
{
"name":"envoy.filters.http.grpc_stats",
"typedConfig":{
Expand Down
Binary file modified tests/endpoints/bookstore_grpc/proto/api_descriptor.pb
Binary file not shown.
19 changes: 19 additions & 0 deletions tests/endpoints/bookstore_grpc/proto/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ service Bookstore {
// curl http://DOMAIN_NAME/v1/shelves
option (google.api.http) = {
get: "/v1/shelves"
// This is added to test grpc-web, to make sure the method is enabled
// for transcoder filter, and it should bypass this method.
// grpc-web client is using POST + /service_name/method_name.
// It could be auto-generated by setting auto_mapping. But auto_mapping
// will not apply if a method already has http.rules options defined.
additional_bindings {
post: "/endpoints.examples.bookstore.Bookstore/ListShelves"
body: "*"
}
};
}
// Creates a new shelf in the bookstore.
Expand All @@ -53,6 +62,11 @@ service Bookstore {
// curl http://DOMAIN_NAME/v1/shelves/1
option (google.api.http) = {
get: "/v1/shelves/{shelf}"
// This is added to test grpc-web, please see comment at ListShelves.
additional_bindings {
post: "/endpoints.examples.bookstore.Bookstore/GetShelf"
body: "*"
}
};
}
// Deletes a shelf, including all books that are stored on the shelf.
Expand All @@ -61,6 +75,11 @@ service Bookstore {
// curl -X DELETE http://DOMAIN_NAME/v1/shelves/2
option (google.api.http) = {
delete: "/v1/shelves/{shelf}"
// This is added to test grpc-web, please see comment at ListShelves.
additional_bindings {
post: "/endpoints.examples.bookstore.Bookstore/DeleteShelf"
body: "*"
}
};
}
// Returns a list of books on a shelf.
Expand Down

0 comments on commit 7d7580b

Please sign in to comment.