From 3395bc456c091726041f6ccd7aa1a6233549d2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Segura=20Lucas?= Date: Mon, 22 Jun 2020 17:43:59 +0200 Subject: [PATCH 1/3] Removing CORS hack and adding a proper middleware for handling CORS headers --- go.mod | 3 ++- go.sum | 18 +++++++++++++++++ server/server.go | 45 ++++++++++++++++++------------------------- server/server_test.go | 32 ++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index c23a6169..83a82d49 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,12 @@ go 1.13 require ( github.com/BurntSushi/toml v0.3.1 github.com/RedHatInsights/insights-content-service v0.0.0-20200610101541-f0c10d6d451d - github.com/RedHatInsights/insights-operator-utils v1.0.2-0.20200610143236-c868b2f93d2a + github.com/RedHatInsights/insights-operator-utils v1.2.0 github.com/RedHatInsights/insights-results-aggregator v0.0.0-20200604090056-3534f6dd9c1c github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/google/uuid v1.1.1 + github.com/gorilla/handlers v1.4.2 github.com/gorilla/mux v1.7.4 github.com/prometheus/client_golang v1.6.0 github.com/rs/zerolog v1.19.0 diff --git a/go.sum b/go.sum index 4f438936..bb844f43 100644 --- a/go.sum +++ b/go.sum @@ -19,12 +19,17 @@ github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3 github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.7/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= +github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/RedHatInsights/cloudwatch v0.0.0-20200512151223-b0b55757a24b/go.mod h1:8l+HqU8iWM6hA9kSAHgY3ItSlpEsPr8fb2R0GBp9S0U= github.com/RedHatInsights/insights-content-service v0.0.0-20200610101541-f0c10d6d451d h1:0aXuHiiucADzC++1lOJs3u4k1UanSNX+wIcpnyi8Bi0= github.com/RedHatInsights/insights-content-service v0.0.0-20200610101541-f0c10d6d451d/go.mod h1:j3Mc+Xh2ospxJ+K2RIZjYlI6E6E4H2Qh3cgUzhstrfQ= github.com/RedHatInsights/insights-operator-utils v1.0.1/go.mod h1:gRzYBMY4csuOXgrxUuC10WUkz6STOm3mqVsQCb+AGOQ= github.com/RedHatInsights/insights-operator-utils v1.0.2-0.20200610143236-c868b2f93d2a h1:/aAv1hq0ZFLDIxfmdTR23cm/B6nHEOVlWxOreOMqr80= github.com/RedHatInsights/insights-operator-utils v1.0.2-0.20200610143236-c868b2f93d2a/go.mod h1:0rhk13kn0BB+yKdfZjpMoQy0lXdXY3i4NJ1xGwGMcII= +github.com/RedHatInsights/insights-operator-utils v1.1.0 h1:Bhoqqopchg4OgbDTx72PVQUuFlymyMyQPYEqN0YELP0= +github.com/RedHatInsights/insights-operator-utils v1.1.0/go.mod h1:PHoCR0njiPXVArw+ooowqZgBjozu1h+8mYDoRoPVh6g= +github.com/RedHatInsights/insights-operator-utils v1.2.0 h1:M1lEO5NSJnfwbs/IWyeAXoFcb0VE7XvdPa4bVJHOT74= +github.com/RedHatInsights/insights-operator-utils v1.2.0/go.mod h1:PHoCR0njiPXVArw+ooowqZgBjozu1h+8mYDoRoPVh6g= github.com/RedHatInsights/insights-results-aggregator v0.0.0-20200604090056-3534f6dd9c1c h1:/kb5AwNxizPf+0R4OWWRvWbLqAwf07Bu97R4WVHk2Uw= github.com/RedHatInsights/insights-results-aggregator v0.0.0-20200604090056-3534f6dd9c1c/go.mod h1:7Pc15NYXErx7BMJ4rF1Hacm+29G6atzjhwBpXNFMt+0= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= @@ -36,6 +41,7 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= @@ -164,6 +170,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= +github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg= +github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= @@ -176,6 +184,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/hashicorp/consul v1.4.5/go.mod h1:mFrjN1mfidgJfYP1xrJCF+AfRhr6Eaqhb2+sfyn/OOI= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= @@ -244,6 +254,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU= github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY= +github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= @@ -257,6 +269,7 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= @@ -286,6 +299,7 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -467,6 +481,7 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCc golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -487,6 +502,7 @@ golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM= golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -607,6 +623,8 @@ gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qS gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= +gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0= +gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo= diff --git a/server/server.go b/server/server.go index e7d8d017..26df5f27 100644 --- a/server/server.go +++ b/server/server.go @@ -39,6 +39,7 @@ import ( "github.com/RedHatInsights/insights-content-service/groups" "github.com/RedHatInsights/insights-operator-utils/responses" "github.com/RedHatInsights/insights-results-aggregator/types" + "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/rs/zerolog/log" @@ -97,30 +98,6 @@ func (server HTTPServer) serveAPISpecFile(writer http.ResponseWriter, request *h http.ServeFile(writer, request, absPath) } -// addCORSHeaders - middleware for adding headers that should be in any response -func (server *HTTPServer) addCORSHeaders(nextHandler http.Handler) http.Handler { - return http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Access-Control-Allow-Origin", "*") - w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") - w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") - w.Header().Set("Access-Control-Allow-Credentials", "true") - nextHandler.ServeHTTP(w, r) - }) -} - -// handleOptionsMethod - middleware for handling OPTIONS method -func (server *HTTPServer) handleOptionsMethod(nextHandler http.Handler) http.Handler { - return http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - if r.Method == "OPTIONS" { - w.WriteHeader(http.StatusOK) - } else { - nextHandler.ServeHTTP(w, r) - } - }) -} - // Initialize perform the server initialization func (server *HTTPServer) Initialize() http.Handler { log.Info().Msgf("Initializing HTTP server at '%s'", server.Config.Address) @@ -149,8 +126,24 @@ func (server *HTTPServer) Initialize() http.Handler { } if server.Config.EnableCORS { - router.Use(server.addCORSHeaders) - router.Use(server.handleOptionsMethod) + headersOK := handlers.AllowedHeaders([]string{ + "Content-Type", + "Content-Length", + "Accept-Encoding", + "X-CSRF-Token", + "Authorization", + }) + originsOK := handlers.AllowedOrigins([]string{"*"}) + methodsOK := handlers.AllowedMethods([]string{ + http.MethodPost, + http.MethodGet, + http.MethodOptions, + http.MethodPut, + http.MethodDelete, + }) + credsOK := handlers.AllowCredentials() + corsMiddleware := handlers.CORS(originsOK, headersOK, methodsOK, credsOK) + router.Use(corsMiddleware) } server.addEndpointsToRouter(router) diff --git a/server/server_test.go b/server/server_test.go index 0242181a..c23bddb4 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -17,8 +17,10 @@ limitations under the License. package server_test import ( + "net/http" "testing" + "github.com/RedHatInsights/insights-operator-utils/tests/helpers" "github.com/rs/zerolog" "github.com/stretchr/testify/assert" @@ -61,3 +63,33 @@ func TestServerStartError(t *testing.T) { err := testServer.Start() assert.EqualError(t, err, "listen tcp: address 99999: invalid port") } + +func TestAddCORSHeaders(t *testing.T) { + testServer := server.New( + config, + services.Configuration{ + AggregatorBaseEndpoint: "http://localhost:8081/api/v1/", + ContentBaseEndpoint: "http://localhost:8082/api/v1/", + }, + nil, + nil, + ) + + helpers.AssertAPIRequest(t, testServer, config.APIPrefix, &helpers.APIRequest{ + Method: http.MethodOptions, + Endpoint: server.RuleGroupsEndpoint, + ExtraHeaders: http.Header{ + "Origin": []string{"http://example.com"}, + "Access-Control-Request-Method": []string{http.MethodOptions}, + "Access-Control-Request-Headers": []string{"X-Csrf-Token,Content-Type,Content-Length"}, + }, + }, &helpers.APIResponse{ + StatusCode: http.StatusOK, + Headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Credentials": "true", + "Access-Control-Allow-Methods": http.MethodOptions, + "Access-Control-Allow-Headers": "X-Csrf-Token,Content-Type,Content-Length", + }, + }) +} From d7165aa5b519447664aa2d9f1e06c9b622332ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Segura=20Lucas?= Date: Tue, 23 Jun 2020 09:11:27 +0200 Subject: [PATCH 2/3] Fixing go.sum --- go.sum | 5 ----- 1 file changed, 5 deletions(-) diff --git a/go.sum b/go.sum index bb844f43..85a8ce93 100644 --- a/go.sum +++ b/go.sum @@ -24,10 +24,6 @@ github.com/RedHatInsights/cloudwatch v0.0.0-20200512151223-b0b55757a24b/go.mod h github.com/RedHatInsights/insights-content-service v0.0.0-20200610101541-f0c10d6d451d h1:0aXuHiiucADzC++1lOJs3u4k1UanSNX+wIcpnyi8Bi0= github.com/RedHatInsights/insights-content-service v0.0.0-20200610101541-f0c10d6d451d/go.mod h1:j3Mc+Xh2ospxJ+K2RIZjYlI6E6E4H2Qh3cgUzhstrfQ= github.com/RedHatInsights/insights-operator-utils v1.0.1/go.mod h1:gRzYBMY4csuOXgrxUuC10WUkz6STOm3mqVsQCb+AGOQ= -github.com/RedHatInsights/insights-operator-utils v1.0.2-0.20200610143236-c868b2f93d2a h1:/aAv1hq0ZFLDIxfmdTR23cm/B6nHEOVlWxOreOMqr80= -github.com/RedHatInsights/insights-operator-utils v1.0.2-0.20200610143236-c868b2f93d2a/go.mod h1:0rhk13kn0BB+yKdfZjpMoQy0lXdXY3i4NJ1xGwGMcII= -github.com/RedHatInsights/insights-operator-utils v1.1.0 h1:Bhoqqopchg4OgbDTx72PVQUuFlymyMyQPYEqN0YELP0= -github.com/RedHatInsights/insights-operator-utils v1.1.0/go.mod h1:PHoCR0njiPXVArw+ooowqZgBjozu1h+8mYDoRoPVh6g= github.com/RedHatInsights/insights-operator-utils v1.2.0 h1:M1lEO5NSJnfwbs/IWyeAXoFcb0VE7XvdPa4bVJHOT74= github.com/RedHatInsights/insights-operator-utils v1.2.0/go.mod h1:PHoCR0njiPXVArw+ooowqZgBjozu1h+8mYDoRoPVh6g= github.com/RedHatInsights/insights-results-aggregator v0.0.0-20200604090056-3534f6dd9c1c h1:/kb5AwNxizPf+0R4OWWRvWbLqAwf07Bu97R4WVHk2Uw= @@ -252,7 +248,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU= github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY= github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= From e41dc51ea6507b5c92368cd86bbfb70ad6c833f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Segura=20Lucas?= Date: Tue, 23 Jun 2020 10:07:38 +0200 Subject: [PATCH 3/3] Missing pieces --- server/server.go | 9 +-------- server/server_test.go | 13 +------------ tests/helpers/http.go | 12 ++++++++++++ 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/server/server.go b/server/server.go index d4e13480..da992acf 100644 --- a/server/server.go +++ b/server/server.go @@ -30,24 +30,17 @@ import ( "net/url" "strings" "time" - - "github.com/RedHatInsights/insights-content-service/groups" - "github.com/RedHatInsights/insights-operator-utils/responses" - "github.com/RedHatInsights/insights-operator-utils/types" - "github.com/gorilla/mux" - "github.com/rs/zerolog/log" // we just have to import this package in order to expose pprof interface in debug mode // disable "G108 (CWE-): Profiling endpoint is automatically exposed on /debug/pprof" // #nosec G108 _ "net/http/pprof" "path/filepath" - "github.com/RedHatInsights/insights-content-service/content" "github.com/RedHatInsights/insights-content-service/groups" httputils "github.com/RedHatInsights/insights-operator-utils/http" "github.com/RedHatInsights/insights-operator-utils/responses" + "github.com/RedHatInsights/insights-operator-utils/types" ira_server "github.com/RedHatInsights/insights-results-aggregator/server" - "github.com/RedHatInsights/insights-results-aggregator/types" "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/rs/zerolog/log" diff --git a/server/server_test.go b/server/server_test.go index 622e06c9..d05927c0 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -28,7 +28,6 @@ import ( "time" ics_server "github.com/RedHatInsights/insights-content-service/server" - "github.com/RedHatInsights/insights-operator-utils/tests/helpers" "github.com/RedHatInsights/insights-results-aggregator-data/testdata" ira_server "github.com/RedHatInsights/insights-results-aggregator/server" "github.com/rs/zerolog" @@ -134,17 +133,7 @@ func TestServerStartError(t *testing.T) { } func TestAddCORSHeaders(t *testing.T) { - testServer := server.New( - config, - services.Configuration{ - AggregatorBaseEndpoint: "http://localhost:8081/api/v1/", - ContentBaseEndpoint: "http://localhost:8082/api/v1/", - }, - nil, - nil, - ) - - helpers.AssertAPIRequest(t, testServer, config.APIPrefix, &helpers.APIRequest{ + helpers.AssertAPIRequest(t, &helpers.DefaultServerConfigCORS, &helpers.DefaultServicesConfig, nil, &helpers.APIRequest{ Method: http.MethodOptions, Endpoint: server.RuleGroupsEndpoint, ExtraHeaders: http.Header{ diff --git a/tests/helpers/http.go b/tests/helpers/http.go index 2cf8454f..8a8f52fc 100644 --- a/tests/helpers/http.go +++ b/tests/helpers/http.go @@ -55,6 +55,18 @@ var ( EnableCORS: false, } + // DefaultServerConfigCORS is a default server config with CORS enabled + DefaultServerConfigCORS = server.Configuration{ + Address: ":8081", + APIPrefix: "/api/v1/", + APISpecFile: "openapi.json", + Debug: true, + Auth: false, + AuthType: "", + UseHTTPS: false, + EnableCORS: true, + } + // DefaultServicesConfig is a default services config DefaultServicesConfig = services.Configuration{ AggregatorBaseEndpoint: "http://localhost:8080/",