Add CORS max age annotation (#1888)

Add cors-max-age annotation
This commit is contained in:
Márk Sági-Kazár 2018-01-09 12:19:42 +01:00 committed by Manuel Alejandro de Brito Fontes
parent 5973bec599
commit 313fdd2d1a
5 changed files with 22 additions and 1 deletions

View file

@ -29,6 +29,7 @@ const (
// Default values
defaultCorsMethods = "GET, PUT, POST, DELETE, PATCH, OPTIONS"
defaultCorsHeaders = "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"
defaultCorsMaxAge = 1728000
)
var (
@ -55,6 +56,7 @@ type Config struct {
CorsAllowMethods string `json:"corsAllowMethods"`
CorsAllowHeaders string `json:"corsAllowHeaders"`
CorsAllowCredentials bool `json:"corsAllowCredentials"`
CorsMaxAge int `json:"corsMaxAge"`
}
// NewParser creates a new CORS annotation parser
@ -70,6 +72,9 @@ func (c1 *Config) Equal(c2 *Config) bool {
if c1 == nil || c2 == nil {
return false
}
if c1.CorsMaxAge != c2.CorsMaxAge {
return false
}
if c1.CorsAllowCredentials != c2.CorsAllowCredentials {
return false
}
@ -117,12 +122,18 @@ func (c cors) Parse(ing *extensions.Ingress) (interface{}, error) {
corsallowcredentials = true
}
corsmaxage, err := parser.GetIntAnnotation("cors-max-age", ing)
if err != nil {
corsmaxage = defaultCorsMaxAge
}
return &Config{
CorsEnabled: corsenabled,
CorsAllowOrigin: corsalloworigin,
CorsAllowHeaders: corsallowheaders,
CorsAllowMethods: corsallowmethods,
CorsAllowCredentials: corsallowcredentials,
CorsMaxAge: corsmaxage,
}, nil
}