feat: verify RpcPath on startup (#2159)

* feat: verify RpcPath on startup

* feat: support http header Grpc-Timeout
This commit is contained in:
Kevin Wan
2022-07-17 12:37:23 +08:00
committed by GitHub
parent b206dd28a3
commit 557383fbbf
11 changed files with 155 additions and 31 deletions

View File

@@ -0,0 +1,34 @@
package internal
import (
"fmt"
"github.com/fullstorydev/grpcurl"
"github.com/jhump/protoreflect/desc"
)
// GetMethods returns all methods of the given grpcurl.DescriptorSource.
func GetMethods(source grpcurl.DescriptorSource) ([]string, error) {
svcs, err := source.ListServices()
if err != nil {
return nil, err
}
var methods []string
for _, svc := range svcs {
d, err := source.FindSymbol(svc)
if err != nil {
return nil, err
}
switch val := d.(type) {
case *desc.ServiceDescriptor:
svcMethods := val.GetMethods()
for _, method := range svcMethods {
methods = append(methods, fmt.Sprintf("%s/%s", svc, method.GetName()))
}
}
}
return methods, nil
}

View File

@@ -0,0 +1,29 @@
package internal
import (
"encoding/base64"
"io/ioutil"
"os"
"testing"
"github.com/fullstorydev/grpcurl"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/hash"
)
const b64pb = `CpgBCgtoZWxsby5wcm90bxIFaGVsbG8iHQoHUmVxdWVzdBISCgRwaW5nGAEgASgJUgRwaW5nIh4KCFJlc3BvbnNlEhIKBHBvbmcYASABKAlSBHBvbmcyMAoFSGVsbG8SJwoEUGluZxIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZUIJWgcuL2hlbGxvYgZwcm90bzM=`
func TestGetMethods(t *testing.T) {
tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(b64pb)))
assert.Nil(t, err)
b, err := base64.StdEncoding.DecodeString(b64pb)
assert.Nil(t, err)
assert.Nil(t, ioutil.WriteFile(tmpfile.Name(), b, os.ModeTemporary))
defer os.Remove(tmpfile.Name())
source, err := grpcurl.DescriptorSourceFromProtoSets(tmpfile.Name())
assert.Nil(t, err)
methods, err := GetMethods(source)
assert.Nil(t, err)
assert.EqualValues(t, []string{"hello.Hello/Ping"}, methods)
}

View File

@@ -0,0 +1,30 @@
package internal
import (
"fmt"
"net/http"
"strings"
)
const (
metadataHeaderPrefix = "Grpc-Metadata-"
metadataPrefix = "gateway-"
)
// BuildHeaders builds the headers for the gateway from HTTP headers.
func BuildHeaders(header http.Header) []string {
var headers []string
for k, v := range header {
if !strings.HasPrefix(k, metadataHeaderPrefix) {
continue
}
key := fmt.Sprintf("%s%s", metadataPrefix, strings.TrimPrefix(k, metadataHeaderPrefix))
for _, vv := range v {
headers = append(headers, key+":"+vv)
}
}
return headers
}

View File

@@ -0,0 +1,21 @@
package internal
import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildHeadersNoValue(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Add("a", "b")
assert.Nil(t, BuildHeaders(req.Header))
}
func TestBuildHeadersWithValues(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Add("grpc-metadata-a", "b")
req.Header.Add("grpc-metadata-b", "b")
assert.EqualValues(t, []string{"gateway-A:b", "gateway-B:b"}, BuildHeaders(req.Header))
}

View File

@@ -0,0 +1,53 @@
package internal
import (
"bytes"
"encoding/json"
"net/http"
"github.com/fullstorydev/grpcurl"
"github.com/golang/protobuf/jsonpb"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/pathvar"
)
// NewRequestParser creates a new request parser from the given http.Request and resolver.
func NewRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.RequestParser, error) {
vars := pathvar.Vars(r)
params, err := httpx.GetFormValues(r)
if err != nil {
return nil, err
}
for k, v := range vars {
params[k] = v
}
if len(params) == 0 {
return grpcurl.NewJSONRequestParser(r.Body, resolver), nil
}
if r.ContentLength == 0 {
return buildJsonRequestParser(params, resolver)
}
m := make(map[string]interface{})
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
return nil, err
}
for k, v := range params {
m[k] = v
}
return buildJsonRequestParser(m, resolver)
}
func buildJsonRequestParser(m map[string]interface{}, resolver jsonpb.AnyResolver) (
grpcurl.RequestParser, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(m); err != nil {
return nil, err
}
return grpcurl.NewJSONRequestParser(&buf, resolver), nil
}

View File

@@ -0,0 +1,55 @@
package internal
import (
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/pathvar"
)
func TestNewRequestParserNoVar(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}
func TestNewRequestParserWithVars(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req = pathvar.WithVars(req, map[string]string{"a": "b"})
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}
func TestNewRequestParserNoVarWithBody(t *testing.T) {
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}
func TestNewRequestParserWithVarsWithBody(t *testing.T) {
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
req = pathvar.WithVars(req, map[string]string{"c": "d"})
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}
func TestNewRequestParserWithVarsWithWrongBody(t *testing.T) {
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"`))
req = pathvar.WithVars(req, map[string]string{"c": "d"})
parser, err := NewRequestParser(req, nil)
assert.NotNil(t, err)
assert.Nil(t, parser)
}
func TestNewRequestParserWithForm(t *testing.T) {
req := httptest.NewRequest("GET", "/val?a=b", nil)
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}

View File

@@ -0,0 +1,19 @@
package internal
import (
"net/http"
"time"
)
const grpcTimeoutHeader = "Grpc-Timeout"
// GetTimeout returns the timeout from the header, if not set, returns the default timeout.
func GetTimeout(header http.Header, defaultTimeout time.Duration) time.Duration {
if timeout := header.Get(grpcTimeoutHeader); len(timeout) > 0 {
if t, err := time.ParseDuration(timeout); err == nil {
return t
}
}
return defaultTimeout
}

View File

@@ -0,0 +1,22 @@
package internal
import (
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetTimeout(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(grpcTimeoutHeader, "1s")
timeout := GetTimeout(req.Header, time.Second*5)
assert.Equal(t, time.Second, timeout)
}
func TestGetTimeoutDefault(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
timeout := GetTimeout(req.Header, time.Second*5)
assert.Equal(t, time.Second*5, timeout)
}