Merge pull request #1493 from micro/auth-encode-endpoint

Encode Endpoint in API auth wrapper
This commit is contained in:
ben-toogood 2020-04-06 16:21:14 +01:00 committed by GitHub
commit 3a378eb7d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package auth package auth
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -84,8 +85,14 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// a file not served by the resolver has been requested (e.g. favicon.ico) // a file not served by the resolver has been requested (e.g. favicon.ico)
endpoint = &resolver.Endpoint{Path: req.URL.Path} endpoint = &resolver.Endpoint{Path: req.URL.Path}
} else if err != nil { } else if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} else if err == nil {
// set the endpoint in the context so it can be used to resolve
// the request later
ctx := context.WithValue(req.Context(), resolver.Endpoint{}, endpoint)
*req = *req.WithContext(ctx)
} }
// construct the resource name, e.g. home => go.micro.web.home // construct the resource name, e.g. home => go.micro.web.home
@ -132,6 +139,9 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
func namespaceFromRequest(req *http.Request) (string, error) { 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 // determine the host, e.g. dev.micro.mu:8080
host := req.URL.Hostname() host := req.URL.Hostname()
if len(host) == 0 { if len(host) == 0 {
@ -139,8 +149,6 @@ func namespaceFromRequest(req *http.Request) (string, error) {
host, _, _ = net.SplitHostPort(req.Host) host, _, _ = net.SplitHostPort(req.Host)
} }
logger.Infof("Host is %v", host)
// check for an ip address // check for an ip address
if net.ParseIP(host) != nil { if net.ParseIP(host) != nil {
return auth.DefaultNamespace, nil return auth.DefaultNamespace, nil