From c33f1e112e521cc703540709dd92b84e01890b3b Mon Sep 17 00:00:00 2001 From: Ta-Ching Chen Date: Mon, 4 Nov 2019 21:59:40 +0800 Subject: [PATCH] Fix reverse proxy shows 404 not found when Istio enabled (#1377) Istio sidecar proxy blocks all requests sent through the reverse proxy to the target service if the request.Host is not properly set to the internal target service host. This PR sets the target service hosts before establishing the proxy for the client in order to pass the Istio sidecar proxy check. --- environments/tensorflow-serving/server.go | 1 + pkg/controller/functionApi.go | 8 ++++++-- pkg/controller/storagesvc.go | 1 + pkg/controller/workflowApiProxy.go | 1 + pkg/fission-cli/logdb/influxdb.go | 3 ++- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/environments/tensorflow-serving/server.go b/environments/tensorflow-serving/server.go index 060f711d..163fcd2d 100644 --- a/environments/tensorflow-serving/server.go +++ b/environments/tensorflow-serving/server.go @@ -212,6 +212,7 @@ func main() { req.URL.Scheme = "http" req.URL.Host = "localhost:8501" req.URL.Path = fmt.Sprintf("/v1/models/%v:%v", MODEL_NAME, API_TYPE) + req.Host = "localhost:8501" } proxy := &httputil.ReverseProxy{ diff --git a/pkg/controller/functionApi.go b/pkg/controller/functionApi.go index a886b882..b78244d4 100644 --- a/pkg/controller/functionApi.go +++ b/pkg/controller/functionApi.go @@ -18,7 +18,6 @@ package controller import ( "encoding/json" - "errors" "io" "io/ioutil" "net/http" @@ -30,6 +29,7 @@ import ( restfulspec "github.com/emicklei/go-restful-openapi" "github.com/go-openapi/spec" "github.com/gorilla/mux" + "github.com/pkg/errors" "go.uber.org/zap" apiv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -258,9 +258,12 @@ func (a *API) FunctionLogsApiPost(w http.ResponseWriter, r *http.Request) { svcUrl, err := url.Parse(dbCnf.httpURL) if err != nil { - a.logger.Error("failed parse url to establish proxy to database for function logs", + msg := "failed parse url to establish proxy to database for function logs" + a.logger.Error(msg, zap.Error(err), zap.String("database_url", dbCnf.httpURL)) + a.respondWithError(w, errors.Wrap(err, msg)) + return } // set up proxy server director director := func(req *http.Request) { @@ -269,6 +272,7 @@ func (a *API) FunctionLogsApiPost(w http.ResponseWriter, r *http.Request) { req.URL.Scheme = svcUrl.Scheme req.URL.Host = svcUrl.Host req.URL.Path = svcUrl.Path + req.Host = svcUrl.Host // set up http basic auth for database authentication req.SetBasicAuth(dbCnf.username, dbCnf.password) } diff --git a/pkg/controller/storagesvc.go b/pkg/controller/storagesvc.go index a587fd25..292a9bd0 100644 --- a/pkg/controller/storagesvc.go +++ b/pkg/controller/storagesvc.go @@ -69,6 +69,7 @@ func (api *API) StorageServiceProxy(w http.ResponseWriter, r *http.Request) { req.URL.Scheme = ssUrl.Scheme req.URL.Host = ssUrl.Host req.URL.Path = "/v1/archive" + req.Host = ssUrl.Host } proxy := &httputil.ReverseProxy{ Director: director, diff --git a/pkg/controller/workflowApiProxy.go b/pkg/controller/workflowApiProxy.go index d99c18f8..3c49be6d 100644 --- a/pkg/controller/workflowApiProxy.go +++ b/pkg/controller/workflowApiProxy.go @@ -24,6 +24,7 @@ func (api *API) WorkflowApiserverProxy(w http.ResponseWriter, r *http.Request) { req.URL.Scheme = ssUrl.Scheme req.URL.Host = ssUrl.Host req.URL.Path = path + req.Host = ssUrl.Host } proxy := &httputil.ReverseProxy{ Director: director, diff --git a/pkg/fission-cli/logdb/influxdb.go b/pkg/fission-cli/logdb/influxdb.go index ca7ee56f..156e398b 100644 --- a/pkg/fission-cli/logdb/influxdb.go +++ b/pkg/fission-cli/logdb/influxdb.go @@ -27,6 +27,7 @@ import ( "time" influxdbClient "github.com/influxdata/influxdb/client/v2" + "github.com/pkg/errors" ferror "github.com/fission/fission/pkg/error" "github.com/fission/fission/pkg/fission-cli/log" @@ -143,7 +144,7 @@ func (influx InfluxDB) query(query influxdbClient.Query) (*influxdbClient.Respon queryURL.Path = path.Clean(fmt.Sprintf("%s/proxy/%s", queryURL.Path, INFLUXDB)) req, err := http.NewRequest(http.MethodPost, queryURL.String(), nil) if err != nil { - return nil, err + return nil, errors.Wrap(err, "error creating request for log proxy") } parametersBytes, err := json.Marshal(query.Parameters)