Skip to content

Commit

Permalink
feat: multiple-cors-allow-origin support (kubernetes#7134)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers authored May 23, 2021
1 parent f6dbd93 commit 8a55801
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/user-guide/nginx-configuration/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ CORS can be controlled with the following annotations:
- Example: `nginx.ingress.kubernetes.io/cors-expose-headers: "*, X-CustomResponseHeader"`

* `nginx.ingress.kubernetes.io/cors-allow-origin`
controls what's the accepted Origin for CORS.
This is a single field value, with the following format: `http(s)://origin-site.com` or `http(s)://origin-site.com:port`
controls which Origin(s) are allowed for CORS.
This is a multi-valued field, separated by ',', each value should obey the following format: `http(s)://origin-site.com` or `http(s)://origin-site.com:port`
- Default: `*`
- Example: `nginx.ingress.kubernetes.io/cors-allow-origin: "https://origin-site.com:4443"`
- Example: `nginx.ingress.kubernetes.io/cors-allow-origin: "https://origin-site.com:4443, https://origin-site-2.com"`

* `nginx.ingress.kubernetes.io/cors-allow-credentials`
controls if credentials can be passed during CORS operations.
Expand Down
10 changes: 9 additions & 1 deletion internal/ingress/annotations/cors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cors

import (
"regexp"
"strings"

networking "k8s.io/api/networking/v1beta1"

Expand Down Expand Up @@ -113,7 +114,14 @@ func (c cors) Parse(ing *networking.Ingress) (interface{}, error) {
}

config.CorsAllowOrigin, err = parser.GetStringAnnotation("cors-allow-origin", ing)
if err != nil || !corsOriginRegex.MatchString(config.CorsAllowOrigin) {
if err == nil {
for _, origin := range strings.Split(config.CorsAllowOrigin, ",") {
if !corsOriginRegex.MatchString(strings.TrimSpace(origin)) {
config.CorsAllowOrigin = "*"
break
}
}
} else {
config.CorsAllowOrigin = "*"
}

Expand Down
6 changes: 3 additions & 3 deletions internal/ingress/annotations/cors/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestIngressCorsConfigValid(t *testing.T) {
data[parser.GetAnnotationWithPrefix("cors-allow-headers")] = "DNT,X-CustomHeader, Keep-Alive,User-Agent"
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "false"
data[parser.GetAnnotationWithPrefix("cors-allow-methods")] = "GET, PATCH"
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "https://origin123.test.com:4443"
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "https://origin123.test.com:4443, https://origin456.test.com"
data[parser.GetAnnotationWithPrefix("cors-expose-headers")] = "*, X-CustomResponseHeader"
data[parser.GetAnnotationWithPrefix("cors-max-age")] = "600"
ing.SetAnnotations(data)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestIngressCorsConfigValid(t *testing.T) {
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-allow-methods")], nginxCors.CorsAllowMethods)
}

if nginxCors.CorsAllowOrigin != "https://origin123.test.com:4443" {
if nginxCors.CorsAllowOrigin != "https://origin123.test.com:4443, https://origin456.test.com" {
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-allow-origin")], nginxCors.CorsAllowOrigin)
}

Expand All @@ -126,7 +126,7 @@ func TestIngressCorsConfigInvalid(t *testing.T) {
data[parser.GetAnnotationWithPrefix("cors-allow-headers")] = "@alright, #ingress"
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "no"
data[parser.GetAnnotationWithPrefix("cors-allow-methods")] = "GET, PATCH, $nginx"
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "origin123.test.com:4443"
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "https://origin.test.com, origin123.test.com:4443"
data[parser.GetAnnotationWithPrefix("cors-expose-headers")] = "@alright, #ingress"
data[parser.GetAnnotationWithPrefix("cors-max-age")] = "abcd"
ing.SetAnnotations(data)
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/annotations/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ var _ = framework.DescribeAnnotation("cors-*", func() {
})
})

ginkgo.It("should allow multiple origins for cors", func() {
host := "cors.foo.com"
annotations := map[string]string{
"nginx.ingress.kubernetes.io/enable-cors": "true",
"nginx.ingress.kubernetes.io/cors-allow-origin": "https://origin.cors.com:8080, https://origin2.cors.com",
}

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, "more_set_headers 'Access-Control-Allow-Origin: https://origin.cors.com:8080, https://origin2.cors.com';")
})
})

ginkgo.It("should allow headers for cors", func() {
host := "cors.foo.com"
annotations := map[string]string{
Expand Down

0 comments on commit 8a55801

Please sign in to comment.