Adds support for other Cors directives
This commit is contained in:
parent
99a355f25d
commit
3e2b36adc4
1 changed files with 47 additions and 2 deletions
|
@ -23,12 +23,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
annotation = "ingress.kubernetes.io/enable-cors"
|
annotationCorsEnabled = "ingress.kubernetes.io/enable-cors"
|
||||||
|
annotationCorsAllowOrigin = "ingress.kubernetes.io/cors-allow-origin"
|
||||||
|
annotationCorsAllowMethods = "ingress.kubernetes.io/cors-allow-methods"
|
||||||
|
annotationCorsAllowHeaders = "ingress.kubernetes.io/cors-allow-headers"
|
||||||
|
annotationCorsAllowCredentials = "ingress.kubernetes.io/cors-allow-credentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cors struct {
|
type cors struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CorsConfig contains the Cors configuration to be used in the Ingress
|
||||||
|
type CorsConfig struct {
|
||||||
|
CorsEnabled bool `json:"corsEnabled"`
|
||||||
|
CorsAllowOrigin string `json:"corsAllowOrigin"`
|
||||||
|
CorsAllowMethods string `json:"corsAllowMethods"`
|
||||||
|
CorsAllowHeaders string `json:"corsAllowHeaders"`
|
||||||
|
CorsAllowCredentials bool `json:"corsAllowCredentials"`
|
||||||
|
}
|
||||||
|
|
||||||
// NewParser creates a new CORS annotation parser
|
// NewParser creates a new CORS annotation parser
|
||||||
func NewParser() parser.IngressAnnotation {
|
func NewParser() parser.IngressAnnotation {
|
||||||
return cors{}
|
return cors{}
|
||||||
|
@ -37,5 +50,37 @@ func NewParser() parser.IngressAnnotation {
|
||||||
// Parse parses the annotations contained in the ingress
|
// Parse parses the annotations contained in the ingress
|
||||||
// rule used to indicate if the location/s should allows CORS
|
// rule used to indicate if the location/s should allows CORS
|
||||||
func (a cors) Parse(ing *extensions.Ingress) (interface{}, error) {
|
func (a cors) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||||
return parser.GetBoolAnnotation(annotation, ing)
|
corsenabled, err := parser.GetBoolAnnotation(annotationCorsEnabled, ing)
|
||||||
|
if err != nil {
|
||||||
|
corsenabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
corsalloworigin, err := parser.GetStringAnnotation(annotationCorsAllowOrigin, ing)
|
||||||
|
if err != nil || corsalloworigin == "" {
|
||||||
|
corsalloworigin = "*"
|
||||||
|
}
|
||||||
|
|
||||||
|
corsallowheaders, err := parser.GetStringAnnotation(annotationCorsAllowHeaders, ing)
|
||||||
|
if err != nil || corsallowheaders == "" {
|
||||||
|
corsallowheaders = "'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"
|
||||||
|
}
|
||||||
|
|
||||||
|
corsallowmethods, err := parser.GetStringAnnotation(annotationCorsAllowMethods, ing)
|
||||||
|
if err != nil || corsallowmethods == "" {
|
||||||
|
corsallowheaders = "GET, PUT, POST, DELETE, PATCH, OPTIONS"
|
||||||
|
}
|
||||||
|
|
||||||
|
corsallowcredentials, err := parser.GetBoolAnnotation(annotationCorsAllowCredentials, ing)
|
||||||
|
if err != nil {
|
||||||
|
corsallowcredentials = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return &CorsConfig{
|
||||||
|
CorsEnabled: corsenabled,
|
||||||
|
CorsAllowOrigin: corsalloworigin,
|
||||||
|
CorsAllowHeaders: corsallowheaders,
|
||||||
|
CorsAllowMethods: corsallowmethods,
|
||||||
|
CorsAllowCredentials: corsallowcredentials,
|
||||||
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue