From 3e2b36adc4416c43c22faa70a0df2f0f2dce8b28 Mon Sep 17 00:00:00 2001 From: Ricardo Pchevuzinske Katz Date: Thu, 19 Oct 2017 18:03:02 -0200 Subject: [PATCH] Adds support for other Cors directives --- pkg/ingress/annotations/cors/main.go | 49 ++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/pkg/ingress/annotations/cors/main.go b/pkg/ingress/annotations/cors/main.go index 8ea55bb1f..50ff7fda2 100644 --- a/pkg/ingress/annotations/cors/main.go +++ b/pkg/ingress/annotations/cors/main.go @@ -23,12 +23,25 @@ import ( ) 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 { } +// 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 func NewParser() parser.IngressAnnotation { return cors{} @@ -37,5 +50,37 @@ func NewParser() parser.IngressAnnotation { // Parse parses the annotations contained in the ingress // rule used to indicate if the location/s should allows CORS 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 + }