Add cors-max-age annotation
This commit is contained in:
parent
983b994305
commit
357499e7c5
2 changed files with 15 additions and 0 deletions
|
@ -29,6 +29,7 @@ const (
|
||||||
// Default values
|
// Default values
|
||||||
defaultCorsMethods = "GET, PUT, POST, DELETE, PATCH, OPTIONS"
|
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"
|
defaultCorsHeaders = "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"
|
||||||
|
defaultCorsMaxAge = 1728000
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -55,6 +56,7 @@ type Config struct {
|
||||||
CorsAllowMethods string `json:"corsAllowMethods"`
|
CorsAllowMethods string `json:"corsAllowMethods"`
|
||||||
CorsAllowHeaders string `json:"corsAllowHeaders"`
|
CorsAllowHeaders string `json:"corsAllowHeaders"`
|
||||||
CorsAllowCredentials bool `json:"corsAllowCredentials"`
|
CorsAllowCredentials bool `json:"corsAllowCredentials"`
|
||||||
|
CorsMaxAge int `json:"corsMaxAge"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewParser creates a new CORS annotation parser
|
// NewParser creates a new CORS annotation parser
|
||||||
|
@ -70,6 +72,9 @@ func (c1 *Config) Equal(c2 *Config) bool {
|
||||||
if c1 == nil || c2 == nil {
|
if c1 == nil || c2 == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if c1.CorsMaxAge != c2.CorsMaxAge {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if c1.CorsAllowCredentials != c2.CorsAllowCredentials {
|
if c1.CorsAllowCredentials != c2.CorsAllowCredentials {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -117,12 +122,18 @@ func (c cors) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||||
corsallowcredentials = true
|
corsallowcredentials = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
corsmaxage, err := parser.GetIntAnnotation("cors-max-age", ing)
|
||||||
|
if err != nil {
|
||||||
|
corsmaxage = defaultCorsMaxAge
|
||||||
|
}
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
CorsEnabled: corsenabled,
|
CorsEnabled: corsenabled,
|
||||||
CorsAllowOrigin: corsalloworigin,
|
CorsAllowOrigin: corsalloworigin,
|
||||||
CorsAllowHeaders: corsallowheaders,
|
CorsAllowHeaders: corsallowheaders,
|
||||||
CorsAllowMethods: corsallowmethods,
|
CorsAllowMethods: corsallowmethods,
|
||||||
CorsAllowCredentials: corsallowcredentials,
|
CorsAllowCredentials: corsallowcredentials,
|
||||||
|
CorsMaxAge: corsmaxage,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,6 +71,7 @@ func TestIngressCorsConfig(t *testing.T) {
|
||||||
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "false"
|
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "false"
|
||||||
data[parser.GetAnnotationWithPrefix("cors-allow-methods")] = "PUT, GET,OPTIONS, PATCH, $nginx_version"
|
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-allow-origin")] = "https://origin123.test.com:4443"
|
||||||
|
data[parser.GetAnnotationWithPrefix("cors-max-age")] = "600"
|
||||||
ing.SetAnnotations(data)
|
ing.SetAnnotations(data)
|
||||||
|
|
||||||
corst, _ := NewParser(&resolver.Mock{}).Parse(ing)
|
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)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue