Skip to content

Commit e2c55cb

Browse files
committed
Add the ability to configure CORS headers
1 parent 85dd91f commit e2c55cb

5 files changed

Lines changed: 38 additions & 6 deletions

File tree

cmd/filter-proxy/main.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/golang-jwt/jwt/v4"
1717
"github.com/gorilla/mux"
1818
"github.com/itchyny/gojq"
19+
"github.com/rs/cors"
1920

2021
"github.com/delta10/filter-proxy/internal/config"
2122
"github.com/delta10/filter-proxy/internal/route"
@@ -78,10 +79,10 @@ func main() {
7879

7980
resp, err := client.Do(r)
8081
if err != nil {
81-
log.Printf("%+v", r.URL)
82-
http.Error(w, "Server Error", http.StatusInternalServerError)
83-
log.Fatal("ServeHTTP:", err)
82+
writeError(w, http.StatusBadGateway, fmt.Sprintf("could not fetch backend response: %s", err))
83+
return
8484
}
85+
8586
defer resp.Body.Close()
8687

8788
utils.DelHopHeaders(resp.Header)
@@ -239,7 +240,7 @@ func main() {
239240

240241
proxyResp, err := client.Do(backendRequest)
241242
if err != nil {
242-
writeError(w, http.StatusInternalServerError, fmt.Sprintf("could not fetch backend response: %s", err))
243+
writeError(w, http.StatusBadGateway, fmt.Sprintf("could not fetch backend response: %s", err))
243244
return
244245
}
245246

@@ -295,9 +296,23 @@ func main() {
295296
}
296297
}
297298

299+
var httpHandler http.Handler
300+
if len(config.Cors.AllowedOrigins) > 0 {
301+
c := cors.New(cors.Options{
302+
AllowedOrigins: config.Cors.AllowedOrigins,
303+
AllowedMethods: config.Cors.AllowedMethods,
304+
AllowedHeaders: config.Cors.AllowedHeaders,
305+
AllowCredentials: config.Cors.AllowCredentials,
306+
})
307+
308+
httpHandler = c.Handler(router)
309+
} else {
310+
httpHandler = router
311+
}
312+
298313
s := &http.Server{
299314
Addr: config.ListenAddress,
300-
Handler: router,
315+
Handler: httpHandler,
301316
ReadTimeout: 10 * time.Second,
302317
WriteTimeout: 10 * time.Second,
303318
MaxHeaderBytes: 1 << 20,
@@ -403,7 +418,7 @@ func authorizeRequestWithService(config *config.Config, backend config.Backend,
403418
request.Header.Set("X-Forwarded-For", utils.ReadUserIP(r))
404419

405420
client := &http.Client{
406-
Timeout: 10 * time.Second,
421+
Timeout: 25 * time.Second,
407422
}
408423

409424
resp, err := client.Do(request)

config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ listenAddress: localhost:8050
66

77
authorizationServiceUrl: http://localhost:8000/atlas/api/v1/authorize
88

9+
cors:
10+
allowedOrigins: []
11+
allowedMethods: []
12+
allowedHeaders: []
13+
allowCredentials: true
14+
915
paths:
1016
- path: /api/ows
1117
backend:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ require (
1313
github.com/itchyny/timefmt-go v0.1.5 // indirect
1414
github.com/kr/pretty v0.3.0 // indirect
1515
github.com/rogpeppe/go-internal v1.9.0 // indirect
16+
github.com/rs/cors v1.10.1 // indirect
1617
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
1718
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1818
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
1919
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
2020
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
21+
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
22+
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
2123
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2224
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2325
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

internal/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ type Path struct {
3636
ResponseRewrite string `yaml:"responseRewrite"`
3737
}
3838

39+
type Cors struct {
40+
AllowedOrigins []string `yaml:"allowedOrigins"`
41+
AllowedMethods []string `yaml:"allowedMethods"`
42+
AllowedHeaders []string `yaml:"allowedHeaders"`
43+
AllowCredentials bool `yaml:"allowCredentials"`
44+
}
45+
3946
type Config struct {
4047
ListenAddress string `yaml:"listenAddress"`
4148
ListenTLS struct {
@@ -46,6 +53,7 @@ type Config struct {
4653
JwksURL string `yaml:"jwksUrl"`
4754
Paths []Path `yaml:"paths"`
4855
Backends map[string]Backend `yaml:"backends"`
56+
Cors Cors `yaml:"cors"`
4957
}
5058

5159
// NewConfig returns a new decoded Config struct

0 commit comments

Comments
 (0)