Add cors-max-age annotation

This commit is contained in:
Márk Sági-Kazár 2018-01-08 14:21:07 +01:00
parent 983b994305
commit 357499e7c5
No known key found for this signature in database
GPG key ID: 34CC109EB5ED1C2A
2 changed files with 15 additions and 0 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
}

View file

@ -71,6 +71,7 @@ func TestIngressCorsConfig(t *testing.T) {
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "false"
data[parser.GetAnnotationWithPrefix("cors-allow-methods")] = "PUT, GET,OPTIONS, PATCH, $nginx_version"
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "https://origin123.test.com:4443"
data[parser.GetAnnotationWithPrefix("cors-max-age")] = "600"
ing.SetAnnotations(data)
corst, _ := NewParser(&resolver.Mock{}).Parse(ing)
@ -95,4 +96,7 @@ func TestIngressCorsConfig(t *testing.T) {
t.Errorf("expected origin https://origin123.test.com:4443, but got %v", nginxCors.CorsAllowOrigin)
}
if nginxCors.CorsMaxAge != 600 {
t.Errorf("expected max age 600, but got %v", nginxCors.CorsMaxAge)
}
}