From 774c0d30a7d60290e7bb013575d70adce3a0c6b0 Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Mon, 6 Apr 2020 16:01:42 +0100 Subject: [PATCH 1/2] Encode Endpoint in API auth wrapper --- api/server/auth/auth.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/api/server/auth/auth.go b/api/server/auth/auth.go index 13b3c999..49a9466c 100644 --- a/api/server/auth/auth.go +++ b/api/server/auth/auth.go @@ -1,12 +1,15 @@ package auth import ( + "encoding/json" "fmt" "net" "net/http" "net/url" "strings" + "github.com/micro/go-micro/v2/metadata" + "github.com/micro/go-micro/v2/api/resolver" "github.com/micro/go-micro/v2/api/resolver/path" "github.com/micro/go-micro/v2/auth" @@ -84,8 +87,16 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // a file not served by the resolver has been requested (e.g. favicon.ico) endpoint = &resolver.Endpoint{Path: req.URL.Path} } else if err != nil { + logger.Error(err) w.WriteHeader(http.StatusInternalServerError) return + } else if err == nil { + // set the endpoint in the context so it can be used to resolve + // the request later + if bytes, err := json.Marshal(endpoint); err == nil { + ctx := metadata.Set(req.Context(), "endpoint", string(bytes)) + *req = *req.WithContext(ctx) + } } // construct the resource name, e.g. home => go.micro.web.home @@ -132,6 +143,9 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } func namespaceFromRequest(req *http.Request) (string, error) { + // needed to tmp debug host in prod. will be removed. + logger.Infof("Host is '%v'; URL Host is '%v'; URL Hostname is '%v'", req.Host, req.URL.Host, req.URL.Hostname()) + // determine the host, e.g. dev.micro.mu:8080 host := req.URL.Hostname() if len(host) == 0 { @@ -139,8 +153,6 @@ func namespaceFromRequest(req *http.Request) (string, error) { host, _, _ = net.SplitHostPort(req.Host) } - logger.Infof("Host is %v", host) - // check for an ip address if net.ParseIP(host) != nil { return auth.DefaultNamespace, nil From 574bf5ac696f9af5f038653235400a69f6d0f611 Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Mon, 6 Apr 2020 16:10:08 +0100 Subject: [PATCH 2/2] Set value in context, not metadata --- api/server/auth/auth.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/api/server/auth/auth.go b/api/server/auth/auth.go index 49a9466c..f0e38562 100644 --- a/api/server/auth/auth.go +++ b/api/server/auth/auth.go @@ -1,15 +1,13 @@ package auth import ( - "encoding/json" + "context" "fmt" "net" "net/http" "net/url" "strings" - "github.com/micro/go-micro/v2/metadata" - "github.com/micro/go-micro/v2/api/resolver" "github.com/micro/go-micro/v2/api/resolver/path" "github.com/micro/go-micro/v2/auth" @@ -93,10 +91,8 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } else if err == nil { // set the endpoint in the context so it can be used to resolve // the request later - if bytes, err := json.Marshal(endpoint); err == nil { - ctx := metadata.Set(req.Context(), "endpoint", string(bytes)) - *req = *req.WithContext(ctx) - } + ctx := context.WithValue(req.Context(), resolver.Endpoint{}, endpoint) + *req = *req.WithContext(ctx) } // construct the resource name, e.g. home => go.micro.web.home