2018-04-02 00:02:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2018-04-27 12:29:08 +00:00
|
|
|
package settings
|
2018-04-02 00:02:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
2020-02-19 03:08:56 +00:00
|
|
|
"strings"
|
2018-04-02 00:02:34 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/onsi/ginkgo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-04-02 00:02:34 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2018-04-02 00:02:34 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.DescribeSetting("[Security] no-auth-locations", func() {
|
2018-04-02 00:02:34 +00:00
|
|
|
f := framework.NewDefaultFramework("no-auth-locations")
|
|
|
|
|
|
|
|
setting := "no-auth-locations"
|
|
|
|
username := "foo"
|
|
|
|
password := "bar"
|
|
|
|
secretName := "test-secret"
|
|
|
|
host := "no-auth-locations"
|
|
|
|
noAuthPath := "/noauth"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.BeforeEach(func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.NewEchoDeployment()
|
2018-04-02 00:02:34 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
s := f.EnsureSecret(buildSecret(username, password, secretName, f.Namespace))
|
2018-04-02 00:02:34 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.UpdateNginxConfigMapData(setting, noAuthPath)
|
2018-04-16 19:30:35 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
bi := buildBasicAuthIngressWithSecondPath(host, f.Namespace, s.Name, noAuthPath)
|
2018-10-29 21:39:04 +00:00
|
|
|
f.EnsureIngress(bi)
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status code 401 when accessing '/' unauthentication", func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-04-02 00:02:34 +00:00
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "test auth")
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusUnauthorized).
|
|
|
|
Body().Contains("401 Authorization Required")
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status code 200 when accessing '/' authentication", func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-04-02 00:02:34 +00:00
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "test auth")
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithBasicAuth(username, password).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status code 200 when accessing '/noauth' unauthenticated", func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-04-02 00:02:34 +00:00
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "test auth")
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/noauth").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithBasicAuth(username, password).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2018-04-02 00:02:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-12 23:12:12 +00:00
|
|
|
func buildBasicAuthIngressWithSecondPath(host, namespace, secretName, pathName string) *networking.Ingress {
|
2021-08-21 20:42:00 +00:00
|
|
|
pathtype := networking.PathTypePrefix
|
2019-12-12 23:12:12 +00:00
|
|
|
return &networking.Ingress{
|
2018-04-02 00:02:34 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: host,
|
|
|
|
Namespace: namespace,
|
|
|
|
Annotations: map[string]string{"nginx.ingress.kubernetes.io/auth-type": "basic",
|
|
|
|
"nginx.ingress.kubernetes.io/auth-secret": secretName,
|
|
|
|
"nginx.ingress.kubernetes.io/auth-realm": "test auth",
|
|
|
|
},
|
|
|
|
},
|
2019-12-12 23:12:12 +00:00
|
|
|
Spec: networking.IngressSpec{
|
2021-08-21 20:42:00 +00:00
|
|
|
IngressClassName: framework.GetIngressClassName(namespace),
|
2019-12-12 23:12:12 +00:00
|
|
|
Rules: []networking.IngressRule{
|
2018-04-02 00:02:34 +00:00
|
|
|
{
|
|
|
|
Host: host,
|
2019-12-12 23:12:12 +00:00
|
|
|
IngressRuleValue: networking.IngressRuleValue{
|
|
|
|
HTTP: &networking.HTTPIngressRuleValue{
|
|
|
|
Paths: []networking.HTTPIngressPath{
|
2018-04-02 00:02:34 +00:00
|
|
|
{
|
2021-08-21 20:42:00 +00:00
|
|
|
Path: "/",
|
|
|
|
PathType: &pathtype,
|
2019-12-12 23:12:12 +00:00
|
|
|
Backend: networking.IngressBackend{
|
2021-08-21 20:42:00 +00:00
|
|
|
Service: &networking.IngressServiceBackend{
|
|
|
|
Name: framework.EchoService,
|
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Number: int32(80),
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 00:02:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-08-21 20:42:00 +00:00
|
|
|
Path: pathName,
|
|
|
|
PathType: &pathtype,
|
2019-12-12 23:12:12 +00:00
|
|
|
Backend: networking.IngressBackend{
|
2021-08-21 20:42:00 +00:00
|
|
|
Service: &networking.IngressServiceBackend{
|
|
|
|
Name: framework.EchoService,
|
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Number: int32(80),
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 00:02:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildSecret(username, password, name, namespace string) *corev1.Secret {
|
|
|
|
out, err := exec.Command("openssl", "passwd", "-crypt", password).CombinedOutput()
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "creating password")
|
2018-10-29 21:39:04 +00:00
|
|
|
|
2018-04-02 00:02:34 +00:00
|
|
|
encpass := fmt.Sprintf("%v:%s\n", username, out)
|
|
|
|
|
|
|
|
return &corev1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Namespace: namespace,
|
|
|
|
DeletionGracePeriodSeconds: framework.NewInt64(1),
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
"auth": []byte(encpass),
|
|
|
|
},
|
|
|
|
Type: corev1.SecretTypeOpaque,
|
|
|
|
}
|
|
|
|
}
|