2017-01-20 09:06:44 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2017-04-01 14:39:42 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-11-23 16:46:23 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2017-11-08 20:58:57 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2017-01-20 09:06:44 +00:00
|
|
|
)
|
|
|
|
|
2019-06-09 22:49:59 +00:00
|
|
|
func buildIngress() *networking.Ingress {
|
|
|
|
defaultBackend := networking.IngressBackend{
|
2021-08-21 20:42:00 +00:00
|
|
|
Service: &networking.IngressServiceBackend{
|
|
|
|
Name: "default-backend",
|
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Number: 80,
|
|
|
|
},
|
|
|
|
},
|
2017-01-20 09:06:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 22:49:59 +00:00
|
|
|
return &networking.Ingress{
|
2017-04-01 14:39:42 +00:00
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
2017-01-20 09:06:44 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
2019-06-09 22:49:59 +00:00
|
|
|
Spec: networking.IngressSpec{
|
2021-08-21 20:42:00 +00:00
|
|
|
DefaultBackend: &networking.IngressBackend{
|
|
|
|
Service: &networking.IngressServiceBackend{
|
|
|
|
Name: "default-backend",
|
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Number: 80,
|
|
|
|
},
|
|
|
|
},
|
2017-10-19 20:03:02 +00:00
|
|
|
},
|
2019-06-09 22:49:59 +00:00
|
|
|
Rules: []networking.IngressRule{
|
2017-10-19 20:03:02 +00:00
|
|
|
{
|
|
|
|
Host: "foo.bar.com",
|
2019-06-09 22:49:59 +00:00
|
|
|
IngressRuleValue: networking.IngressRuleValue{
|
|
|
|
HTTP: &networking.HTTPIngressRuleValue{
|
|
|
|
Paths: []networking.HTTPIngressPath{
|
2017-10-19 20:03:02 +00:00
|
|
|
{
|
|
|
|
Path: "/foo",
|
|
|
|
Backend: defaultBackend,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-01-20 09:06:44 +00:00
|
|
|
}
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 02:58:40 +00:00
|
|
|
func TestIngressCorsConfigValid(t *testing.T) {
|
2017-10-19 20:03:02 +00:00
|
|
|
ing := buildIngress()
|
2017-01-20 09:06:44 +00:00
|
|
|
|
2017-10-19 20:03:02 +00:00
|
|
|
data := map[string]string{}
|
2018-08-30 02:58:40 +00:00
|
|
|
|
|
|
|
// Valid
|
2017-11-23 16:46:23 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("enable-cors")] = "true"
|
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-headers")] = "DNT,X-CustomHeader, Keep-Alive,User-Agent"
|
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "false"
|
2018-08-30 02:58:40 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-methods")] = "GET, PATCH"
|
2021-05-27 12:38:24 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "https://origin123.test.com:4443"
|
2020-09-23 15:41:52 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-expose-headers")] = "*, X-CustomResponseHeader"
|
2018-01-09 11:19:42 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-max-age")] = "600"
|
2017-10-19 20:03:02 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2018-08-30 02:58:40 +00:00
|
|
|
corst, err := NewParser(&resolver.Mock{}).Parse(ing)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error parsing annotations: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
nginxCors, ok := corst.(*Config)
|
2017-10-19 20:03:02 +00:00
|
|
|
if !ok {
|
2018-08-30 02:58:40 +00:00
|
|
|
t.Errorf("expected a Config type but returned %t", corst)
|
2017-01-20 09:06:44 +00:00
|
|
|
}
|
2017-10-19 20:03:02 +00:00
|
|
|
|
2017-11-05 01:18:28 +00:00
|
|
|
if !nginxCors.CorsEnabled {
|
2018-08-30 02:58:40 +00:00
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("enable-cors")], nginxCors.CorsEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nginxCors.CorsAllowCredentials {
|
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-allow-credentials")], nginxCors.CorsAllowCredentials)
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if nginxCors.CorsAllowHeaders != "DNT,X-CustomHeader, Keep-Alive,User-Agent" {
|
2018-08-30 02:58:40 +00:00
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-allow-headers")], nginxCors.CorsAllowHeaders)
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 02:58:40 +00:00
|
|
|
if nginxCors.CorsAllowMethods != "GET, PATCH" {
|
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-allow-methods")], nginxCors.CorsAllowMethods)
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 19:31:42 +00:00
|
|
|
if nginxCors.CorsAllowOrigin[0] != "https://origin123.test.com:4443" {
|
2018-08-30 02:58:40 +00:00
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-allow-origin")], nginxCors.CorsAllowOrigin)
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 15:41:52 +00:00
|
|
|
if nginxCors.CorsExposeHeaders != "*, X-CustomResponseHeader" {
|
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-expose-headers")], nginxCors.CorsExposeHeaders)
|
|
|
|
}
|
|
|
|
|
2018-01-09 11:19:42 +00:00
|
|
|
if nginxCors.CorsMaxAge != 600 {
|
2018-08-30 02:58:40 +00:00
|
|
|
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix("cors-max-age")], nginxCors.CorsMaxAge)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIngressCorsConfigInvalid(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
data := map[string]string{}
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
data[parser.GetAnnotationWithPrefix("enable-cors")] = "yes"
|
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-headers")] = "@alright, #ingress"
|
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-credentials")] = "no"
|
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-methods")] = "GET, PATCH, $nginx"
|
2021-05-27 12:38:24 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-allow-origin")] = "origin123.test.com:4443"
|
2020-09-23 15:41:52 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-expose-headers")] = "@alright, #ingress"
|
2018-08-30 02:58:40 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("cors-max-age")] = "abcd"
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
|
|
|
corst, err := NewParser(&resolver.Mock{}).Parse(ing)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error parsing annotations: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nginxCors, ok := corst.(*Config)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected a Config type but returned %t", corst)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nginxCors.CorsEnabled {
|
|
|
|
t.Errorf("expected %v but returned %v", false, nginxCors.CorsEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nginxCors.CorsAllowCredentials {
|
|
|
|
t.Errorf("expected %v but returned %v", true, nginxCors.CorsAllowCredentials)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nginxCors.CorsAllowHeaders != defaultCorsHeaders {
|
|
|
|
t.Errorf("expected %v but returned %v", defaultCorsHeaders, nginxCors.CorsAllowHeaders)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nginxCors.CorsAllowMethods != defaultCorsMethods {
|
|
|
|
t.Errorf("expected %v but returned %v", defaultCorsHeaders, nginxCors.CorsAllowMethods)
|
|
|
|
}
|
|
|
|
|
2020-09-23 15:41:52 +00:00
|
|
|
if nginxCors.CorsExposeHeaders != "" {
|
|
|
|
t.Errorf("expected %v but returned %v", "", nginxCors.CorsExposeHeaders)
|
|
|
|
}
|
|
|
|
|
2018-08-30 02:58:40 +00:00
|
|
|
if nginxCors.CorsMaxAge != defaultCorsMaxAge {
|
|
|
|
t.Errorf("expected %v but returned %v", defaultCorsMaxAge, nginxCors.CorsMaxAge)
|
2018-01-09 11:19:42 +00:00
|
|
|
}
|
2017-01-20 09:06:44 +00:00
|
|
|
}
|