update CORS middleware and make it configurable
This commit is contained in:
@@ -33,6 +33,15 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
if !cfg.Supervised {
|
||||
return ParseConfig(ctx, cfg)
|
||||
}
|
||||
if origins := ctx.StringSlice("cors-allowed-origins"); len(origins) != 0 {
|
||||
cfg.HTTP.CORS.AllowedOrigins = origins
|
||||
}
|
||||
if methods := ctx.StringSlice("cors-allowed-methods"); len(methods) != 0 {
|
||||
cfg.HTTP.CORS.AllowedMethods = methods
|
||||
}
|
||||
if headers := ctx.StringSlice("cors-allowed-headers"); len(headers) != 0 {
|
||||
cfg.HTTP.CORS.AllowedOrigins = headers
|
||||
}
|
||||
logger.Debug().Str("service", "settings").Msg("ignoring config file parsing when running supervised")
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -18,12 +18,21 @@ type Debug struct {
|
||||
Zpages bool
|
||||
}
|
||||
|
||||
// CORS defines the available cors configuration.
|
||||
type CORS struct {
|
||||
AllowedOrigins []string
|
||||
AllowedMethods []string
|
||||
AllowedHeaders []string
|
||||
AllowCredentials bool
|
||||
}
|
||||
|
||||
// HTTP defines the available http configuration.
|
||||
type HTTP struct {
|
||||
Addr string
|
||||
Namespace string
|
||||
Root string
|
||||
CacheTTL int
|
||||
CORS CORS
|
||||
}
|
||||
|
||||
// GRPC defines the available grpc configuration.
|
||||
|
||||
@@ -144,6 +144,30 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
||||
EnvVars: []string{"SETTINGS_CACHE_TTL"},
|
||||
Destination: &cfg.HTTP.CacheTTL,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "cors-allowed-origins",
|
||||
Value: cli.NewStringSlice("*"),
|
||||
Usage: "Set the allowed CORS origins",
|
||||
EnvVars: []string{"SETTINGS_CORS_ALLOW_ORIGINS", "OCIS_CORS_ALLOW_ORIGINS"},
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "cors-allowed-methods",
|
||||
Value: cli.NewStringSlice("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"),
|
||||
Usage: "Set the allowed CORS origins",
|
||||
EnvVars: []string{"SETTINGS_CORS_ALLOW_METHODS", "OCIS_CORS_ALLOW_METHODS"},
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "cors-allowed-headers",
|
||||
Value: cli.NewStringSlice("Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"),
|
||||
Usage: "Set the allowed CORS origins",
|
||||
EnvVars: []string{"SETTINGS_CORS_ALLOW_HEADERS", "OCIS_CORS_ALLOW_HEADERS"},
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "cors-allow-credentials",
|
||||
Value: flags.OverrideDefaultBool(cfg.HTTP.CORS.AllowCredentials, true),
|
||||
Usage: "Allow credentials for CORS",
|
||||
EnvVars: []string{"SETTINGS_CORS_ALLOW_CREDENTIALS", "OCIS_CORS_ALLOW_CREDENTIALS"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "grpc-addr",
|
||||
Value: flags.OverrideDefaultString(cfg.GRPC.Addr, "127.0.0.1:9191"),
|
||||
|
||||
@@ -23,6 +23,10 @@ func Server(opts ...Option) (*http.Server, error) {
|
||||
debug.Zpages(options.Config.Debug.Zpages),
|
||||
debug.Health(health(options.Config)),
|
||||
debug.Ready(ready(options.Config)),
|
||||
debug.CorsAllowedOrigins(options.Config.HTTP.CORS.AllowedOrigins),
|
||||
debug.CorsAllowedMethods(options.Config.HTTP.CORS.AllowedMethods),
|
||||
debug.CorsAllowedHeaders(options.Config.HTTP.CORS.AllowedHeaders),
|
||||
debug.CorsAllowCredentials(options.Config.HTTP.CORS.AllowCredentials),
|
||||
), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/owncloud/ocis/ocis-pkg/account"
|
||||
"github.com/owncloud/ocis/ocis-pkg/cors"
|
||||
"github.com/owncloud/ocis/ocis-pkg/middleware"
|
||||
"github.com/owncloud/ocis/ocis-pkg/service/http"
|
||||
"github.com/owncloud/ocis/ocis-pkg/version"
|
||||
@@ -40,7 +41,13 @@ func Server(opts ...Option) http.Service {
|
||||
mux.Use(chimiddleware.RealIP)
|
||||
mux.Use(chimiddleware.RequestID)
|
||||
mux.Use(middleware.NoCache)
|
||||
mux.Use(middleware.Cors)
|
||||
mux.Use(middleware.Cors(
|
||||
cors.Logger(options.Logger),
|
||||
cors.AllowedOrigins(options.Config.HTTP.CORS.AllowedOrigins),
|
||||
cors.AllowedMethods(options.Config.HTTP.CORS.AllowedMethods),
|
||||
cors.AllowedHeaders(options.Config.HTTP.CORS.AllowedHeaders),
|
||||
cors.AllowCredentials(options.Config.HTTP.CORS.AllowCredentials),
|
||||
))
|
||||
mux.Use(middleware.Secure)
|
||||
mux.Use(middleware.ExtractAccountUUID(
|
||||
account.Logger(options.Logger),
|
||||
|
||||
Reference in New Issue
Block a user