Skip to content

Commit

Permalink
Implement CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
harture committed Apr 16, 2019
1 parent ef5b748 commit c8fd438
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions cmd/keycloakb/keycloak_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
gokit_influx "github.com/go-kit/kit/metrics/influx"
"github.com/go-kit/kit/ratelimit"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
influx "github.com/influxdata/influxdb/client/v2"
_ "github.com/lib/pq"
opentracing "github.com/opentracing/opentracing-go"
"github.com/rs/cors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
jaeger "github.com/uber/jaeger-client-go/config"
Expand Down Expand Up @@ -135,7 +135,13 @@ func main() {
"management": c.GetInt("rate-management"),
}

corsAllowedOrigin = c.GetStringSlice("cors-allowed-origin")
corsOptions = cors.Options{
AllowedOrigins: c.GetStringSlice("cors-allowed-origins"),
AllowedMethods: c.GetStringSlice("cors-allowed-methods"),
AllowCredentials: c.GetBool("cors-allow-credential"),
AllowedHeaders: c.GetStringSlice("cors-allowed-headers"),
Debug: c.GetBool("cors-debug"),
}
)

// Unique ID generator
Expand Down Expand Up @@ -662,7 +668,9 @@ func main() {
debugSubroute.HandleFunc("/pprof/trace", http.HandlerFunc(pprof.Trace))
}

errc <- http.ListenAndServe(httpAddr, handlers.CORS(handlers.AllowedOrigins(corsAllowedOrigin))(route))
c := cors.New(corsOptions)
errc <- http.ListenAndServe(httpAddr, c.Handler(route))

}()

// Influx writing.
Expand Down Expand Up @@ -714,7 +722,11 @@ func config(logger log.Logger) *viper.Viper {
v.SetDefault("component-http-host-port", "0.0.0.0:8888")

// CORS configuration
v.SetDefault("cors-allowed-origin", []string{})
v.SetDefault("cors-allowed-origins", []string{})
v.SetDefault("cors-allowed-methods", []string{})
v.SetDefault("cors-allow-credentials", true)
v.SetDefault("cors-allowed-headers", []string{})
v.SetDefault("cors-debug", false)

// Keycloak default.
v.SetDefault("keycloak", true)
Expand Down
13 changes: 11 additions & 2 deletions configs/keycloak_bridge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
component-http-host-port: 0.0.0.0:8888

# CORS
cors-allowed-origin:
- "toto"
cors-allowed-origins:
- "http://localhost"
cors-allowed-methods:
- "GET"
- "POST"
- "PUT"
- "DELETE"
cors-allow-credentials: true
cors-allowed-headers:
- "Authorization"
cors-debug: true

# Keycloak configs
keycloak: false
Expand Down

0 comments on commit c8fd438

Please sign in to comment.