2016-06-01 18:47:37 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-06-01 18:47:37 +00:00
|
|
|
|
|
|
|
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 secureupstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2017-04-01 14:39:42 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2017-05-14 22:14:27 +00:00
|
|
|
"fmt"
|
2017-08-25 15:50:08 +00:00
|
|
|
|
2017-04-01 14:39:42 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2017-10-06 20:33:32 +00:00
|
|
|
"k8s.io/ingress-nginx/pkg/ingress/resolver"
|
2016-06-01 18:47:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildIngress() *extensions.Ingress {
|
|
|
|
defaultBackend := extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &extensions.Ingress{
|
2017-04-01 14:39:42 +00:00
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
2016-06-01 18:47:37 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: extensions.IngressSpec{
|
|
|
|
Backend: &extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
Rules: []extensions.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo.bar.com",
|
|
|
|
IngressRuleValue: extensions.IngressRuleValue{
|
|
|
|
HTTP: &extensions.HTTPIngressRuleValue{
|
|
|
|
Paths: []extensions.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/foo",
|
|
|
|
Backend: defaultBackend,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 22:14:27 +00:00
|
|
|
type mockCfg struct {
|
|
|
|
certs map[string]resolver.AuthSSLCert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg mockCfg) GetAuthCertificate(secret string) (*resolver.AuthSSLCert, error) {
|
|
|
|
if cert, ok := cfg.certs[secret]; ok {
|
|
|
|
return &cert, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("secret not found: %v", secret)
|
|
|
|
}
|
|
|
|
|
2016-06-01 18:47:37 +00:00
|
|
|
func TestAnnotations(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
data := map[string]string{}
|
|
|
|
data[secureUpstream] = "true"
|
2017-05-14 22:14:27 +00:00
|
|
|
data[secureVerifyCASecret] = "secure-verify-ca"
|
2016-06-01 18:47:37 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2017-05-14 22:14:27 +00:00
|
|
|
_, err := NewParser(mockCfg{
|
|
|
|
certs: map[string]resolver.AuthSSLCert{
|
2017-06-13 13:07:14 +00:00
|
|
|
"default/secure-verify-ca": {},
|
2017-05-14 22:14:27 +00:00
|
|
|
},
|
|
|
|
}).Parse(ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
if err != nil {
|
2017-05-14 22:14:27 +00:00
|
|
|
t.Errorf("Unexpected error on ingress: %v", err)
|
2016-06-01 18:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 22:14:27 +00:00
|
|
|
func TestSecretNotFound(t *testing.T) {
|
2016-06-01 18:47:37 +00:00
|
|
|
ing := buildIngress()
|
2017-05-14 22:14:27 +00:00
|
|
|
data := map[string]string{}
|
|
|
|
data[secureUpstream] = "true"
|
|
|
|
data[secureVerifyCASecret] = "secure-verify-ca"
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
_, err := NewParser(mockCfg{}).Parse(ing)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected secret not found error on ingress")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretOnNonSecure(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
data := map[string]string{}
|
|
|
|
data[secureUpstream] = "false"
|
|
|
|
data[secureVerifyCASecret] = "secure-verify-ca"
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
_, err := NewParser(mockCfg{
|
|
|
|
certs: map[string]resolver.AuthSSLCert{
|
2017-06-13 13:07:14 +00:00
|
|
|
"default/secure-verify-ca": {},
|
2017-05-14 22:14:27 +00:00
|
|
|
},
|
|
|
|
}).Parse(ing)
|
2016-06-01 18:47:37 +00:00
|
|
|
if err == nil {
|
2017-05-14 22:14:27 +00:00
|
|
|
t.Error("Expected CA secret on non secure backend error on ingress")
|
2016-06-01 18:47:37 +00:00
|
|
|
}
|
|
|
|
}
|